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#include "commands/undocommand.h"
14
15#include <QDebug>
16#include <QFile>
17#include <QImage>
18#include <QString>
19#include <QUrl>
20
21ImageDocument::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
43QImage ImageDocument::image() const
44{
45 return m_image;
46}
47
48bool 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
65void 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
74void 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
83void 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
92void 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
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
118bool ImageDocument::saveAs(const QUrl &location)
119{
120 return m_image.save(location.isLocalFile() ? location.toLocalFile() : location.toString());
121}
122
123QUrl ImageDocument::path() const
124{
125 return m_path;
126}
127
128void ImageDocument::setPath(const QUrl &path)
129{
130 m_path = path;
131 Q_EMIT pathChanged(path);
132}
133
134#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.
char * toString(const EngineQuery &query)
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 Tue Mar 26 2024 11:19:40 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.