KQuickImageEditor

imagedocument.cpp
1/*
2 * SPDX-FileCopyrightText: (C) 2020 Carl Schwan <carl@carlschwan.eu>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6
7#include "imagedocument.h"
8
9#include "commands/cropcommand.h"
10#include "commands/mirrorcommand.h"
11#include "commands/resizecommand.h"
12#include "commands/rotatecommand.h"
13
14ImageDocument::ImageDocument(QObject *parent)
15 : QObject(parent)
16{
17 connect(this, &ImageDocument::pathChanged, this, [this](const QUrl &url) {
18 m_image = QImage(url.isLocalFile() ? url.toLocalFile() : url.toString());
19 m_edited = false;
20 Q_EMIT editedChanged();
21 Q_EMIT imageChanged();
22 });
23}
24
26{
27 while (!m_undos.empty()) {
28 const auto command = m_undos.pop();
29 m_image = command->undo(m_image);
30 delete command;
31 }
32 setEdited(false);
33 Q_EMIT imageChanged();
34}
35
36QImage ImageDocument::image() const
37{
38 return m_image;
39}
40
41bool ImageDocument::edited() const
42{
43 return m_edited;
44}
45
47{
48 Q_ASSERT(!m_undos.empty());
49 const auto command = m_undos.pop();
50 m_image = command->undo(m_image);
51 delete command;
52 Q_EMIT imageChanged();
53 if (m_undos.empty()) {
54 setEdited(false);
55 }
56}
57
58void ImageDocument::crop(int x, int y, int width, int height)
59{
60 const auto command = new CropCommand(QRect(x, y, width, height));
61 m_image = command->redo(m_image);
62 m_undos.append(command);
63 setEdited(true);
64 Q_EMIT imageChanged();
65}
66
67void ImageDocument::resize(int width, int height)
68{
69 const auto command = new ResizeCommand(QSize(width, height));
70 m_image = command->redo(m_image);
71 m_undos.append(command);
72 setEdited(true);
73 Q_EMIT imageChanged();
74}
75
76void ImageDocument::mirror(bool horizontal, bool vertical)
77{
78 const auto command = new MirrorCommand(horizontal, vertical);
79 m_image = command->redo(m_image);
80 m_undos.append(command);
81 setEdited(true);
82 Q_EMIT imageChanged();
83}
84
85void ImageDocument::rotate(int angle)
86{
87 QTransform transform;
88 transform.rotate(angle);
89 const auto command = new RotateCommand(transform);
90 m_image = command->redo(m_image);
91 m_undos.append(command);
92 setEdited(true);
93 Q_EMIT imageChanged();
94}
95
97{
98 if (m_edited == value) {
99 return;
100 }
101
102 m_edited = value;
103 Q_EMIT editedChanged();
104}
105
107{
108 return m_image.save(m_path.isLocalFile() ? m_path.toLocalFile() : m_path.toString());
109}
110
111bool ImageDocument::saveAs(const QUrl &location)
112{
113 return m_image.save(location.isLocalFile() ? location.toLocalFile() : location.toString());
114}
115
116QUrl ImageDocument::path() const
117{
118 return m_path;
119}
120
121void ImageDocument::setPath(const QUrl &path)
122{
123 m_path = path;
124 Q_EMIT pathChanged(path);
125}
126
127#include "moc_imagedocument.cpp"
CropCommand that crop the current image.
Definition cropcommand.h:18
Q_INVOKABLE void undo()
Undo the last edit on the images.
Q_INVOKABLE void rotate(int angle)
Rotate the image.
Q_INVOKABLE bool saveAs(const QUrl &location)
Save current edited image as a new image.
Q_INVOKABLE bool save()
Save current edited image in place.
Q_INVOKABLE void mirror(bool horizontal, bool vertical)
Mirror the image.
Q_INVOKABLE void crop(int x, int y, int width, int height)
Crop the image.
Q_INVOKABLE void cancel()
Cancel all the edit.
void setEdited(bool value)
Change the edited value.
Q_INVOKABLE void resize(int width, int height)
Resize the image.
MirrorCommand that mirror an image horizontally or vertically.
ResizeCommand that resizes the current image.
RotateCommand that rotates the current image.
QString path(const QString &relativePath)
bool save(QIODevice *device, const char *format, int quality) const const
Q_EMITQ_EMIT
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
bool isLocalFile() const const
QString toLocalFile() const const
QString toString(FormattingOptions options) const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:17:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.