File indexing completed on 2024-07-21 04:18:48

0001 /*
0002  * SPDX-FileCopyrightText: (C) 2020 Carl Schwan <carl@carlschwan.eu>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-or-later
0005  */
0006 
0007 #pragma once
0008 
0009 #include <QImage>
0010 #include <QObject>
0011 #include <QStack>
0012 #include <QUrl>
0013 
0014 #include "commands/undocommand.h"
0015 
0016 /**
0017  * @brief An ImageDocument is the base class of the ImageEditor.
0018  *
0019  * This class handles various image manipulation and contains an undo stack to allow
0020  * reverting the last actions. This class does not display the image, use @c ImageItem
0021  * for this task.
0022  *
0023  * @code{.qml}
0024  * KQuickImageEditor.ImageDocument {
0025  *    id: imageDocument
0026  *    path: myModel.image
0027  * }
0028  *
0029  * Kirigami.Actions {
0030  *    iconName: "object-rotate-left"
0031  *    onTriggered: imageDocument.rotate(-90);
0032  * }
0033  *
0034  * KQuickImageEditor.ImageItem {
0035  *     image: imageDocument.image
0036  * }
0037  * @endcode
0038  */
0039 class ImageDocument : public QObject
0040 {
0041     Q_OBJECT
0042 
0043     Q_PROPERTY(QUrl path READ path WRITE setPath NOTIFY pathChanged)
0044     Q_PROPERTY(QImage image READ image NOTIFY imageChanged)
0045     Q_PROPERTY(bool edited READ edited WRITE setEdited NOTIFY editedChanged)
0046 
0047 public:
0048     ImageDocument(QObject *parent = nullptr);
0049     ~ImageDocument() override = default;
0050 
0051     /**
0052      * The image was is displayed. This propriety is updated when the path change
0053      * or commands are applied.
0054      *
0055      * @see imageChanged
0056      */
0057     QImage image() const;
0058 
0059     /**
0060      * This propriety store if the document was changed or not.
0061      *
0062      * @see setEdited
0063      * @see editedChanged
0064      */
0065     bool edited() const;
0066 
0067     /**
0068      * Change the edited value.
0069      * @param value The new value.
0070      */
0071     void setEdited(bool value);
0072 
0073     QUrl path() const;
0074     void setPath(const QUrl &path);
0075 
0076     /**
0077      * Rotate the image.
0078      * @param angle The angle of the rotation in degree.
0079      */
0080     Q_INVOKABLE void rotate(int angle);
0081 
0082     /**
0083      * Mirror the image.
0084      * @param horizontal Mirror the image horizontally.
0085      * @param vertical Mirror the image vertically.
0086      */
0087     Q_INVOKABLE void mirror(bool horizontal, bool vertical);
0088 
0089     /**
0090      * Crop the image.
0091      * @param x The x coordinate of the new image in the old image.
0092      * @param y The y coordinate of the new image in the old image.
0093      * @param width The width of the new image.
0094      * @param height The height of the new image.
0095      */
0096     Q_INVOKABLE void crop(int x, int y, int width, int height);
0097 
0098     /**
0099      * Resize the image.
0100      * @param width The width of the new image.
0101      * @param height The height of the new image.
0102      */
0103     Q_INVOKABLE void resize(int width, int height);
0104 
0105     /**
0106      * Undo the last edit on the images.
0107      */
0108     Q_INVOKABLE void undo();
0109 
0110     /**
0111      * Cancel all the edit.
0112      */
0113     Q_INVOKABLE void cancel();
0114 
0115     /**
0116      * Save current edited image in place. This is a destructive operation and can't be reverted.
0117      * @return true iff the file saving operation was successful.
0118      */
0119     Q_INVOKABLE bool save();
0120 
0121     /**
0122      * Save current edited image as a new image.
0123      * @param location The location where to save the new image.
0124      * @return true iff the file saving operattion was successful.
0125      */
0126     Q_INVOKABLE bool saveAs(const QUrl &location);
0127 
0128 Q_SIGNALS:
0129     void pathChanged(const QUrl &url);
0130     void imageChanged();
0131     void editedChanged();
0132 
0133 private:
0134     QUrl m_path;
0135     QStack<UndoCommand *> m_undos;
0136     QImage m_image;
0137     bool m_edited;
0138 };