Document clipboard

This commit is contained in:
James Graham
2023-04-23 08:24:09 +00:00
parent a8aec35884
commit ed874ed00a
2 changed files with 41 additions and 4 deletions

View File

@@ -62,10 +62,10 @@ QString Clipboard::saveImage(QString localPath) const
void Clipboard::saveText(QString message) void Clipboard::saveText(QString message)
{ {
static QRegularExpression re(QStringLiteral("<[^>]*>")); static QRegularExpression re(QStringLiteral("<[^>]*>"));
auto *mineData = new QMimeData; // ownership is transferred to clipboard auto *mimeData = new QMimeData; // ownership is transferred to clipboard
mineData->setHtml(message); mimeData->setHtml(message);
mineData->setText(message.replace(re, QString())); mimeData->setText(message.replace(re, QString()));
m_clipboard->setMimeData(mineData); m_clipboard->setMimeData(mimeData);
} }
void Clipboard::setImage(const QUrl &url) void Clipboard::setImage(const QUrl &url)

View File

@@ -9,23 +9,60 @@ class QClipboard;
class QImage; class QImage;
/** /**
* @class Clipboard
*
* Clipboard proxy * Clipboard proxy
*
* Used to set and retrieve content from the clipboard.
*/ */
class Clipboard : public QObject class Clipboard : public QObject
{ {
Q_OBJECT Q_OBJECT
/**
* @brief Whether the current clipboard content is an image.
*/
Q_PROPERTY(bool hasImage READ hasImage NOTIFY imageChanged) Q_PROPERTY(bool hasImage READ hasImage NOTIFY imageChanged)
/**
* @brief Return the current clipboard content image.
*
* Returns a null image if the clipboard does not contain an image or if it
* contains an image in an unsupported image format.
*/
Q_PROPERTY(QImage image READ image NOTIFY imageChanged) Q_PROPERTY(QImage image READ image NOTIFY imageChanged)
public: public:
explicit Clipboard(QObject *parent = nullptr); explicit Clipboard(QObject *parent = nullptr);
[[nodiscard]] bool hasImage() const; [[nodiscard]] bool hasImage() const;
[[nodiscard]] QImage image() const; [[nodiscard]] QImage image() const;
/**
* @brief Save the current clipboard image to file.
*
* If the clipboard does not contain an image or if it contains an image in an
* unsupported image format nothing happens.
*
* The given file path must be both valid and local or nothing happens.
*
* @param localPath the path to save the image. A default path for the app cache
* will be used if available and this is empty.
*
* @return A QString with the path that the image was saved to. The string will
* be empty if nothing was saved.
*/
Q_INVOKABLE QString saveImage(QString localPath = {}) const; Q_INVOKABLE QString saveImage(QString localPath = {}) const;
/**
* @brief Set the clipboard content to the input message.
*/
Q_INVOKABLE void saveText(QString message); Q_INVOKABLE void saveText(QString message);
/**
* @brief Set the clipboard content to the input image.
*/
Q_INVOKABLE void setImage(const QUrl &image); Q_INVOKABLE void setImage(const QUrl &image);
private: private: