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
54 Q_PROPERTY(QRectF area READ area WRITE setArea NOTIFY areaChanged RESET resetArea)
55
56public:
57 ImageDocument(QObject *parent = nullptr);
58 // ~ImageDocument() override = default;
59
60 /**
61 * The image was is displayed. This propriety is updated when the path change
62 * or commands are applied.
63 *
64 * @see imageChanged
65 */
66 QImage image() const;
67
68 /**
69 * This propriety store if the document was changed or not.
70 *
71 * @see setEdited
72 * @see editedChanged
73 */
74 bool edited() const;
75
76 /**
77 * Change the edited value.
78 * @param value The new value.
79 */
80 void setEdited(bool value);
81
82 QUrl path() const;
83 void setPath(const QUrl &path);
84
85 /**
86 * Rotate the image.
87 * @param angle The angle of the rotation in degree.
88 */
89 Q_INVOKABLE void rotate(int angle);
90
91 /**
92 * Mirror the image.
93 * @param horizontal Mirror the image horizontally.
94 * @param vertical Mirror the image vertically.
95 */
96 Q_INVOKABLE void mirror(bool horizontal, bool vertical);
97
98 /**
99 * Crop the image.
100 * @param x The x coordinate of the new image in the old image.
101 * @param y The y coordinate of the new image in the old image.
102 * @param width The width of the new image.
103 * @param height The height of the new image.
104 */
105 Q_INVOKABLE void crop
106 (int x, int y, int width, int height);
107
108 /**
109 * Resize the image.
110 * @param width The width of the new image.
111 * @param height The height of the new image.
112 */
113 Q_INVOKABLE void resize(int width, int height);
114
115 /**
116 * Undo the last edit on the images.
117 */
118 Q_INVOKABLE void undo();
119
120 /**
121 * Cancel all the edit.
122 */
123 Q_INVOKABLE void cancel();
124
125 /**
126 * Save current edited image in place. This is a destructive operation and can't be reverted.
127 * @return true iff the file saving operation was successful.
128 */
129 Q_INVOKABLE bool save();
130
131 /**
132 * Save current edited image as a new image.
133 * @param location The location where to save the new image.
134 * @return true iff the file saving operattion was successful.
135 */
136 Q_INVOKABLE bool saveAs(const QUrl &location);
137
138 Q_INVOKABLE void adjustBrightness(int value);// between -255 and 255
139 Q_INVOKABLE void adjustContrast(int value); // between -255 and 255
140 Q_INVOKABLE void adjustSaturation(int value); //between -255 and 255
141 Q_INVOKABLE void adjustHue(int value); //between 0 and 180
142
143 Q_INVOKABLE void applyChanges();
144
145 int brightness() const;
146 int contrast() const;
147 int saturation() const;
148 int hue() const;
149
150 QRectF area() const;
151 void setArea(const QRectF &newArea);
152 void resetArea();
153
154 bool changesApplied() const;
155
157 void pathChanged(const QUrl &url);
158 void imageChanged();
159 void editedChanged();
160 void brightnessChanged();
161 void contrastChanged();
162 void saturationChanged();
163 void areaChanged();
164 void hueChanged();
165 void changesAppliedChanged();
166
167private:
168 QUrl m_path;
169 QStack<Command *> m_undos;
170 QImage m_image;
171 QImage m_originalImage;
172 bool m_edited;
173 int m_brightness = 0;
174 int m_contrast = 0;
175 int m_saturation = 0;
176 int m_hue = 0;
177 QRectF m_area;
178
179 void resetValues();
180 bool m_changesApplied;
181};
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 11 2025 11:57:09 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.