MauiKit Image Tools

imagedocument.h
1/*
2 * SPDX-FileCopyrightText: (C) 2020 Carl Schwan <carl@carlschwan.eu>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6
7#pragma once
8
9#include <QImage>
10#include <QObject>
11#include <QStack>
12#include <QUrl>
13#include <qqmlregistration.h>
14
15#include "commands/command.h"
16
17/**
18 * @brief An ImageDocument is the base class of the ImageEditor.
19 *
20 * This class handles various image manipulation and contains an undo stack to allow
21 * reverting the last actions. This class does not display the image, use @c ImageItem
22 * for this task.
23 *
24 * @code{.qml}
25 * KQuickImageEditor.ImageDocument {
26 * id: imageDocument
27 * path: myModel.image
28 * }
29 *
30 * Kirigami.Actions {
31 * iconName: "object-rotate-left"
32 * onTriggered: imageDocument.rotate(-90);
33 * }
34 *
35 * KQuickImageEditor.ImageItem {
36 * image: imageDocument.image
37 * }
38 * @endcode
39 */
40class ImageDocument : public QObject
41{
43 QML_ELEMENT
44
45 Q_PROPERTY(QUrl path READ path WRITE setPath NOTIFY pathChanged)
46 Q_PROPERTY(QImage image READ image NOTIFY imageChanged)
47 Q_PROPERTY(bool edited READ edited NOTIFY editedChanged)
48 Q_PROPERTY(bool changesApplied READ changesApplied NOTIFY changesAppliedChanged)
49 Q_PROPERTY(int brightness READ brightness NOTIFY brightnessChanged FINAL)
50 Q_PROPERTY(int contrast READ contrast NOTIFY contrastChanged FINAL)
51 Q_PROPERTY(int saturation READ saturation NOTIFY saturationChanged FINAL)
52 Q_PROPERTY(int hue READ hue NOTIFY hueChanged FINAL)
53 Q_PROPERTY(int gamma READ gamma NOTIFY gammaChanged FINAL)
54 Q_PROPERTY(int sharpness READ sharpness NOTIFY sharpnessChanged FINAL)
55 Q_PROPERTY(int threshold READ threshold NOTIFY thresholdChanged FINAL)
56 Q_PROPERTY(int gaussianBlur READ gaussianBlur NOTIFY gaussianBlurChanged FINAL)
57 Q_PROPERTY(QRectF area READ area WRITE setArea NOTIFY areaChanged RESET resetArea)
58
59public:
60 ImageDocument(QObject *parent = nullptr);
61 // ~ImageDocument() override = default;
62
63 /**
64 * The image was is displayed. This propriety is updated when the path change
65 * or commands are applied.
66 *
67 * @see imageChanged
68 */
69 QImage image() const;
70
71 /**
72 * This propriety store if the document was changed or not.
73 *
74 * @see setEdited
75 * @see editedChanged
76 */
77 bool edited() const;
78
79 /**
80 * Change the edited value.
81 * @param value The new value.
82 */
83 void setEdited(bool value);
84
85 QUrl path() const;
86 void setPath(const QUrl &path);
87
88 /**
89 * Rotate the image.
90 * @param angle The angle of the rotation in degree.
91 */
92 Q_INVOKABLE void rotate(int angle);
93
94 /**
95 * Mirror the image.
96 * @param horizontal Mirror the image horizontally.
97 * @param vertical Mirror the image vertically.
98 */
99 Q_INVOKABLE void mirror(bool horizontal, bool vertical);
100
101 /**
102 * Crop the image.
103 * @param x The x coordinate of the new image in the old image.
104 * @param y The y coordinate of the new image in the old image.
105 * @param width The width of the new image.
106 * @param height The height of the new image.
107 */
108 Q_INVOKABLE void crop
109 (int x, int y, int width, int height);
110
111 /**
112 * Resize the image.
113 * @param width The width of the new image.
114 * @param height The height of the new image.
115 */
116 Q_INVOKABLE void resize(int width, int height);
117
118 /**
119 * Undo the last edit on the images.
120 */
121 Q_INVOKABLE void undo();
122
123 /**
124 * Cancel all the edit.
125 */
126 Q_INVOKABLE void cancel();
127
128 /**
129 * Save current edited image in place. This is a destructive operation and can't be reverted.
130 * @return true iff the file saving operation was successful.
131 */
132 Q_INVOKABLE bool save();
133
134 /**
135 * Save current edited image as a new image.
136 * @param location The location where to save the new image.
137 * @return true iff the file saving operattion was successful.
138 */
139 Q_INVOKABLE bool saveAs(const QUrl &location);
140
141 Q_INVOKABLE void adjustBrightness(int value);// between -255 and 255
142 Q_INVOKABLE void adjustContrast(int value); // between -255 and 255
143 Q_INVOKABLE void adjustSaturation(int value); //between -255 and 255
144 Q_INVOKABLE void adjustHue(int value); //between 0 and 180
145 Q_INVOKABLE void adjustGamma(int value); //between -100 and 100
146 Q_INVOKABLE void adjustSharpness(int value); //between 0 and 100
147 Q_INVOKABLE void adjustThreshold(int value); //between 0 and 180
148 Q_INVOKABLE void adjustGaussianBlur(int value);
149 Q_INVOKABLE void toGray();
150 Q_INVOKABLE void toSketch();
151 Q_INVOKABLE void addVignette();
152
153 Q_INVOKABLE void applyChanges();
154
155 int brightness() const;
156 int contrast() const;
157 int saturation() const;
158 int hue() const;
159 int gamma() const;
160 int sharpness() const;
161 int threshold() const;
162 int gaussianBlur() const;
163
164 QRectF area() const;
165 void setArea(const QRectF &newArea);
166 void resetArea();
167
168 bool changesApplied() const;
169
171 void pathChanged(const QUrl &url);
172 void imageChanged();
173 void editedChanged();
174 void brightnessChanged();
175 void contrastChanged();
176 void saturationChanged();
177 void areaChanged();
178 void hueChanged();
179 void sharpnessChanged();
180 void gammaChanged();
181 void thresholdChanged();
182 void changesAppliedChanged();
183 void gaussianBlurChanged();
184
185private:
186 QUrl m_path;
187 QStack<Command *> m_undos;
188 QImage m_image;
189 QImage m_originalImage;
190 bool m_edited;
191 int m_brightness = 0;
192 int m_contrast = 0;
193 int m_saturation = 0;
194 int m_hue = 0;
195 int m_gamma = 0;
196 int m_sharpness = 0;
197 int m_threshold = 0;
198 int m_gaussianBlur = 0;
199 QRectF m_area;
200
201 void resetValues();
202 bool m_changesApplied;
203};
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.
QObject(QObject *parent)
Q_INVOKABLEQ_INVOKABLE
Q_OBJECTQ_OBJECT
Q_PROPERTY(...)
Q_SIGNALSQ_SIGNALS
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Apr 18 2025 12:10:54 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.