File indexing completed on 2024-10-06 12:53:59

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