From d1f7e7091e53d8c7e275032e0e3eb97e2336d8fb Mon Sep 17 00:00:00 2001 From: Alessio Mattiazzi Date: Sun, 5 Mar 2023 23:19:02 +0000 Subject: [PATCH] Copy image to the clipboard Add a context menu option to save an image in the timeline to the clipboard BUG: 466478 --- src/clipboard.cpp | 12 ++++++++++++ src/clipboard.h | 1 + src/qml/Menu/Timeline/FileDelegateContextMenu.qml | 14 ++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/src/clipboard.cpp b/src/clipboard.cpp index b35222299..5424d3f3f 100644 --- a/src/clipboard.cpp +++ b/src/clipboard.cpp @@ -67,3 +67,15 @@ void Clipboard::saveText(QString message) mineData->setText(message.replace(re, "")); m_clipboard->setMimeData(mineData); } + +void Clipboard::setImage(const QUrl &url) +{ + if (url.isLocalFile()) { + QImage img(url.path()); + auto *mimeData = new QMimeData; + mimeData->setImageData(img); + if (!img.isNull()) { + m_clipboard->setMimeData(mimeData); + } + } +} diff --git a/src/clipboard.h b/src/clipboard.h index 32d884cf1..2d3a4dd7e 100644 --- a/src/clipboard.h +++ b/src/clipboard.h @@ -26,6 +26,7 @@ public: Q_INVOKABLE QString saveImage(QString localPath = {}) const; Q_INVOKABLE void saveText(QString message); + Q_INVOKABLE void setImage(const QUrl &image); private: QClipboard *m_clipboard; diff --git a/src/qml/Menu/Timeline/FileDelegateContextMenu.qml b/src/qml/Menu/Timeline/FileDelegateContextMenu.qml index b299eed9f..d59b0c5c1 100644 --- a/src/qml/Menu/Timeline/FileDelegateContextMenu.qml +++ b/src/qml/Menu/Timeline/FileDelegateContextMenu.qml @@ -55,6 +55,20 @@ MessageDelegateContextMenu { root.closeFullscreen() } }, + Kirigami.Action { + text: i18n("Copy") + icon.name: "edit-copy" + onTriggered: { + if(file.downloaded) { + Clipboard.setImage(progressInfo.localPath) + } else { + file.onDownloadedChanged.connect(function() { + Clipboard.setImage(progressInfo.localPath) + }); + currentRoom.downloadFile(eventId, StandardPaths.writableLocation(StandardPaths.CacheLocation) + "/" + eventId.replace(":", "_").replace("/", "_").replace("+", "_") + currentRoom.fileNameToDownload(eventId)) + } + } + }, Kirigami.Action { visible: author.id === currentRoom.localUser.id || currentRoom.canSendState("redact") text: i18n("Remove")