KQuickImageEditor

imagedocument.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2020 Carl Schwan <[email protected]>
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 #include "commands/undocommand.h"
14 
15 #include <QDebug>
16 #include <QFile>
17 #include <QImage>
18 #include <QString>
19 #include <QUrl>
20 
21 ImageDocument::ImageDocument(QObject *parent)
22  : QObject(parent)
23 {
24  connect(this, &ImageDocument::pathChanged, this, [this](const QUrl &url) {
25  m_image = QImage(url.isLocalFile() ? url.toLocalFile() : url.toString());
26  m_edited = false;
27  Q_EMIT editedChanged();
28  Q_EMIT imageChanged();
29  });
30 }
31 
33 {
34  while (!m_undos.empty()) {
35  const auto command = m_undos.pop();
36  m_image = command->undo(m_image);
37  delete command;
38  }
39  setEdited(false);
40  Q_EMIT imageChanged();
41 }
42 
44 {
45  return m_image;
46 }
47 
48 bool ImageDocument::edited() const
49 {
50  return m_edited;
51 }
52 
54 {
55  Q_ASSERT(!m_undos.empty());
56  const auto command = m_undos.pop();
57  m_image = command->undo(m_image);
58  delete command;
59  Q_EMIT imageChanged();
60  if (m_undos.empty()) {
61  setEdited(false);
62  }
63 }
64 
65 void ImageDocument::crop(int x, int y, int width, int height)
66 {
67  const auto command = new CropCommand(QRect(x, y, width, height));
68  m_image = command->redo(m_image);
69  m_undos.append(command);
70  setEdited(true);
71  Q_EMIT imageChanged();
72 }
73 
74 void ImageDocument::resize(int width, int height)
75 {
76  const auto command = new ResizeCommand(QSize(width, height));
77  m_image = command->redo(m_image);
78  m_undos.append(command);
79  setEdited(true);
80  Q_EMIT imageChanged();
81 }
82 
83 void ImageDocument::mirror(bool horizontal, bool vertical)
84 {
85  const auto command = new MirrorCommand(horizontal, vertical);
86  m_image = command->redo(m_image);
87  m_undos.append(command);
88  setEdited(true);
89  Q_EMIT imageChanged();
90 }
91 
92 void ImageDocument::rotate(int angle)
93 {
94  QTransform transform;
95  transform.rotate(angle);
96  const auto command = new RotateCommand(transform);
97  m_image = command->redo(m_image);
98  m_undos.append(command);
99  setEdited(true);
100  Q_EMIT imageChanged();
101 }
102 
103 void ImageDocument::setEdited(bool value)
104 {
105  if (m_edited == value) {
106  return;
107  }
108 
109  m_edited = value;
110  Q_EMIT editedChanged();
111 }
112 
114 {
115  return m_image.save(m_path.isLocalFile() ? m_path.toLocalFile() : m_path.toString());
116 }
117 
118 bool ImageDocument::saveAs(const QUrl &location)
119 {
120  return m_image.save(location.isLocalFile() ? location.toLocalFile() : location.toString());
121 }
122 
123 QUrl ImageDocument::path() const
124 {
125  return m_path;
126 }
127 
128 void ImageDocument::setPath(const QUrl &path)
129 {
130  m_path = path;
131  Q_EMIT pathChanged(path);
132 }
133 
134 #include "moc_imagedocument.cpp"
CropCommand(const QRect &cropRect)
Contructor.
Definition: cropcommand.cpp:9
Q_INVOKABLE void cancel()
Cancel all the edit.
Q_INVOKABLE void undo()
Undo the last edit on the images.
bool save(const QString &fileName, const char *format, int quality) const const
RotateCommand that rotates the current image.
Definition: rotatecommand.h:17
QImage image() const
The image was is displayed.
void setEdited(bool value)
Change the edited value.
QString toString(QUrl::FormattingOptions options) const const
Q_INVOKABLE bool saveAs(const QUrl &location)
Save current edited image as a new image.
ResizeCommand that resizes the current image.
Definition: resizecommand.h:16
MirrorCommand that mirror an image horizontally or vertically.
Definition: mirrorcommand.h:17
QString toLocalFile() const const
QTransform & rotate(qreal angle, Qt::Axis axis)
bool edited() const
This propriety store if the document was changed or not.
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 bool save()
Save current edited image in place.
Q_INVOKABLE void resize(int width, int height)
Resize the image.
Q_INVOKABLE void rotate(int angle)
Rotate the image.
bool isLocalFile() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Oct 18 2021 23:08:19 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.