7#include "imagedocument.h"
9#include "commands/cropcommand.h"
10#include "commands/mirrorcommand.h"
11#include "commands/resizecommand.h"
12#include "commands/rotatecommand.h"
13#include "commands/transformcommand.h"
17ImageDocument::ImageDocument(
QObject *parent)
20 m_changesApplied =
true;
22 connect(
this, &ImageDocument::pathChanged,
this, [
this](
const QUrl &url) {
24 m_originalImage = m_image;
26 Q_EMIT editedChanged();
27 Q_EMIT imageChanged();
33 while (!m_undos.empty()) {
34 const auto command = m_undos.pop();
36 m_image = command->undo(m_image);
50bool ImageDocument::edited()
const
59 qDebug() <<
"No more commands to undo";
63 const auto command = m_undos.pop();
64 m_image = command->undo();
65 m_originalImage = m_image;
68 if (m_undos.empty()) {
76 m_image = command->redo(m_image);
77 m_undos.append(command);
85 m_image = command->redo(m_image);
86 m_undos.append(command);
94 m_image = command->redo(m_image);
96 m_undos.append(command);
104 transform.rotate(angle);
106 m_image = command->redo(m_image);
107 m_undos.append(command);
114 m_changesApplied = !value;
115 Q_EMIT changesAppliedChanged();
117 if (m_edited == value) {
127 return m_originalImage.save(m_path.isLocalFile() ? m_path.toLocalFile() : m_path.toString());
132 return m_originalImage.save(location.isLocalFile() ? location.toLocalFile() : location.
toString());
135void ImageDocument::adjustBrightness(
int value)
137 if(value == m_brightness)
140 auto oldValue = m_brightness;
141 m_brightness = value;
143 auto transformation = [val = m_brightness](
QImage &ref) ->
QImage
145 return Trans::adjustBrightness(ref, val);
148 auto undoCallback = [
this, oldValue]()
150 this->m_brightness = oldValue;
151 Q_EMIT brightnessChanged();
154 const auto command =
new TransformCommand(m_image, transformation, undoCallback);
156 m_image = command->redo(m_originalImage);
157 m_undos.append(command);
158 Q_EMIT brightnessChanged();
163void ImageDocument::adjustContrast(
int value)
165 if(value == m_contrast)
168 auto oldValue = m_contrast;
171 auto transformation = [val = m_contrast](QImage &ref) -> QImage
173 return Trans::adjustContrast(ref, val);
176 auto undoCallback = [
this, oldValue]()
178 this->m_contrast = oldValue;
182 const auto command =
new TransformCommand(m_image, transformation, undoCallback);
184 m_image = command->redo(m_originalImage);
185 m_undos.append(command);
191void ImageDocument::adjustSaturation(
int value)
193 if(m_image.isGrayscale())
196 if(value == m_saturation)
199 auto oldValue = m_saturation;
200 m_saturation = value;
202 auto transformation = [val = m_saturation](QImage &ref) -> QImage
204 return Trans::adjustSaturation(ref, val);
207 auto undoCallback = [
this, oldValue]()
209 this->m_saturation = oldValue;
210 Q_EMIT saturationChanged();
213 const auto command =
new TransformCommand(m_image, transformation, undoCallback);
215 m_image = command->redo(m_originalImage);
216 m_undos.append(command);
219 Q_EMIT saturationChanged();
222void ImageDocument::adjustHue(
int value)
224 qDebug() <<
"adjust HUE DOCUMENT" << value;
228 if(m_image.isGrayscale())
231 auto oldValue = m_hue;
234 auto transformation = [val = m_hue](QImage &ref) -> QImage
236 return Trans::adjustHue(ref, val);
239 auto undoCallback = [
this, oldValue]()
241 this->m_hue = oldValue;
245 const auto command =
new TransformCommand(m_image, transformation, undoCallback);
247 m_image = command->redo(m_originalImage);
248 m_undos.append(command);
254void ImageDocument::adjustGamma(
int value)
256 qDebug() <<
"adjust GAMMA DOCUMENT" << value;
263 auto oldValue = m_gamma;
266 auto transformation = [val = m_gamma](QImage &ref) -> QImage
268 return Trans::adjustGamma(ref, val);
271 auto undoCallback = [
this, oldValue]()
273 this->m_gamma = oldValue;
277 const auto command =
new TransformCommand(m_image, transformation, undoCallback);
279 m_image = command->redo(m_originalImage);
280 m_undos.append(command);
286void ImageDocument::adjustSharpness(
int value)
288 qDebug() <<
"adjust SHARPNESS DOCUMENT" << value;
292 if(value == m_sharpness)
295 auto oldValue = m_sharpness;
298 auto transformation = [val = m_sharpness](QImage &ref) -> QImage
300 return Trans::adjustSharpness(ref, val);
303 auto undoCallback = [
this, oldValue]()
305 this->m_sharpness = oldValue;
306 Q_EMIT sharpnessChanged();
309 const auto command =
new TransformCommand(m_image, transformation, undoCallback);
311 m_image = command->redo(m_originalImage);
312 m_undos.append(command);
315 Q_EMIT sharpnessChanged();
318void ImageDocument::adjustThreshold(
int value)
320 qDebug() <<
"adjust threshold DOCUMENT" << value;
324 if(value == m_threshold)
327 auto oldValue = m_threshold;
330 auto transformation = [val = m_threshold](QImage &ref) -> QImage
332 return Trans::adjustThreshold(ref, val);
335 auto undoCallback = [
this, oldValue]()
337 this->m_threshold = oldValue;
338 Q_EMIT thresholdChanged();
341 const auto command =
new TransformCommand(m_image, transformation, undoCallback);
343 m_image = command->redo(m_originalImage);
344 m_undos.append(command);
347 Q_EMIT thresholdChanged();
350void ImageDocument::adjustGaussianBlur(
int value)
352 if(value == m_gaussianBlur)
355 auto oldValue = m_gaussianBlur;
356 m_gaussianBlur = value;
357 auto transformation = [val = m_gaussianBlur](QImage &ref) -> QImage
359 qDebug() <<
"SXetting gaussian blur" << val;
360 return Trans::adjustGaussianBlur(ref, val);
363 auto undoCallback = [
this, oldValue]()
365 this->m_gaussianBlur = oldValue;
366 Q_EMIT gaussianBlurChanged();
369 const auto command =
new TransformCommand(m_image, transformation, undoCallback);
370 m_image = command->redo(m_originalImage);
371 m_undos.append(command);
374 Q_EMIT gaussianBlurChanged();
377void ImageDocument::toGray()
379 const auto command =
new TransformCommand(m_image, &Trans::toGray,
nullptr);
381 m_image = command->redo(m_originalImage);
382 m_undos.append(command);
387void ImageDocument::toSketch()
389 const auto command =
new TransformCommand(m_image, &Trans::sketch,
nullptr);
391 m_image = command->redo(m_originalImage);
392 m_undos.append(command);
397void ImageDocument::addVignette()
399 const auto command =
new TransformCommand(m_image, &Trans::vignette,
nullptr);
401 m_image = command->redo(m_originalImage);
402 m_undos.append(command);
407void ImageDocument::applyChanges()
411 m_originalImage = m_image;
412 m_changesApplied =
true;
413 Q_EMIT changesAppliedChanged();
416int ImageDocument::brightness()
const
421int ImageDocument::contrast()
const
426int ImageDocument::saturation()
const
431int ImageDocument::hue()
const
436int ImageDocument::gamma()
const
441int ImageDocument::sharpness()
const
446int ImageDocument::threshold()
const
451int ImageDocument::gaussianBlur()
const
453 return m_gaussianBlur;
456QUrl ImageDocument::path()
const
461void ImageDocument::setPath(
const QUrl &path)
467QRectF ImageDocument::area()
const
472void ImageDocument::setArea(
const QRectF &newArea)
474 if (m_area == newArea)
480void ImageDocument::resetArea()
485void ImageDocument::resetValues()
495 Q_EMIT saturationChanged();
496 Q_EMIT brightnessChanged();
499 Q_EMIT thresholdChanged();
500 Q_EMIT sharpnessChanged();
503bool ImageDocument::changesApplied()
const
505 return m_changesApplied;
CropCommand that crop the current image.
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.
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