File indexing completed on 2024-10-06 07:36:00

0001 // SPDX-FileCopyrightText: 2019 Black Hat <bhat@encom.eu.org>
0002 // SPDX-License-Identifier: GPL-3.0-only
0003 
0004 #pragma once
0005 
0006 #include <QObject>
0007 #include <QQmlEngine>
0008 
0009 class QClipboard;
0010 class QImage;
0011 
0012 /**
0013  * @class Clipboard
0014  *
0015  * Clipboard proxy
0016  *
0017  * Used to set and retrieve content from the clipboard.
0018  */
0019 class Clipboard : public QObject
0020 {
0021     Q_OBJECT
0022     QML_ELEMENT
0023     QML_SINGLETON
0024 
0025     /**
0026      * @brief Whether the current clipboard content is an image.
0027      */
0028     Q_PROPERTY(bool hasImage READ hasImage NOTIFY imageChanged)
0029 
0030     /**
0031      * @brief Return the current clipboard content image.
0032      *
0033      * Returns a null image if the clipboard does not contain an image or if it
0034      * contains an image in an unsupported image format.
0035      */
0036     Q_PROPERTY(QImage image READ image NOTIFY imageChanged)
0037 
0038 public:
0039     explicit Clipboard(QObject *parent = nullptr);
0040 
0041     [[nodiscard]] bool hasImage() const;
0042 
0043     [[nodiscard]] QImage image() const;
0044 
0045     /**
0046      * @brief Save the current clipboard image to file.
0047      *
0048      * If the clipboard does not contain an image or if it contains an image in an
0049      * unsupported image format nothing happens.
0050      *
0051      * The given file path must be both valid and local or nothing happens.
0052      *
0053      * @param localPath the path to save the image. A default path for the app cache
0054      *                  will be used if available and this is empty.
0055      *
0056      * @return A QString with the path that the image was saved to. The string will
0057      *         be empty if nothing was saved.
0058      */
0059     Q_INVOKABLE QString saveImage(QString localPath = {}) const;
0060 
0061     /**
0062      * @brief Set the clipboard content to the input message.
0063      */
0064     Q_INVOKABLE void saveText(QString message);
0065 
0066     /**
0067      * @brief Set the clipboard content to the input image.
0068      */
0069     Q_INVOKABLE void setImage(const QUrl &image);
0070 
0071 private:
0072     QClipboard *m_clipboard;
0073 
0074 Q_SIGNALS:
0075     void imageChanged();
0076 };