From 4ac9a9b30542fc59e404613428014b9f7d20570f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Cie=C5=9Blak?= Date: Wed, 28 Aug 2024 10:37:52 +0200 Subject: [PATCH] copyImageToClipboardByUrl moved to ClipboardUtils --- storybook/pages/ProfileDialogViewPage.qml | 4 -- ui/StatusQ/include/StatusQ/clipboardutils.h | 17 ++---- ui/StatusQ/src/clipboardutils.cpp | 56 ++++++++++++++----- ui/app/mainui/AppMain.qml | 2 +- ui/imports/shared/popups/ImageContextMenu.qml | 2 +- ui/imports/utils/Utils.qml | 4 -- 6 files changed, 49 insertions(+), 36 deletions(-) diff --git a/storybook/pages/ProfileDialogViewPage.qml b/storybook/pages/ProfileDialogViewPage.qml index 19118846c4..1111c9ca93 100644 --- a/storybook/pages/ProfileDialogViewPage.qml +++ b/storybook/pages/ProfileDialogViewPage.qml @@ -45,10 +45,6 @@ SplitView { return url } - function copyImageToClipboardByUrl(data) { - logs.logEvent("Utils::copyImageToClipboardByUrl", ["data"], arguments) - } - function downloadImageByUrl(url, path) { logs.logEvent("Utils::downloadImageByUrl", ["url", "path"], arguments) } diff --git a/ui/StatusQ/include/StatusQ/clipboardutils.h b/ui/StatusQ/include/StatusQ/clipboardutils.h index 7ee3d6dd53..c6bccbc47b 100644 --- a/ui/StatusQ/include/StatusQ/clipboardutils.h +++ b/ui/StatusQ/include/StatusQ/clipboardutils.h @@ -3,7 +3,6 @@ #include #include -class QClipboard; class QJSEngine; class QQmlEngine; @@ -11,8 +10,6 @@ class ClipboardUtils : public QObject { Q_OBJECT - Q_DISABLE_COPY(ClipboardUtils) - Q_PROPERTY(bool hasText READ hasText NOTIFY contentChanged) Q_PROPERTY(QString text READ text NOTIFY contentChanged) @@ -41,20 +38,14 @@ class ClipboardUtils : public QObject bool hasUrls() const; QList urls() const; - QClipboard* m_clipboard{nullptr}; - public: - static QObject* qmlInstance(QQmlEngine* engine, QJSEngine* scriptEngine) - { - Q_UNUSED(engine); - Q_UNUSED(scriptEngine); - - return new ClipboardUtils; - } - Q_INVOKABLE void setText(const QString& text); + Q_INVOKABLE void setImageByUrl(const QUrl& url); + Q_INVOKABLE void clear(); + static QObject* qmlInstance(QQmlEngine* engine, QJSEngine* scriptEngine); + signals: void contentChanged(); }; diff --git a/ui/StatusQ/src/clipboardutils.cpp b/ui/StatusQ/src/clipboardutils.cpp index a263b6a934..12ebec5b8b 100644 --- a/ui/StatusQ/src/clipboardutils.cpp +++ b/ui/StatusQ/src/clipboardutils.cpp @@ -2,18 +2,20 @@ #include #include +#include #include #include #include +#include +#include #include -#include #include ClipboardUtils::ClipboardUtils() - : m_clipboard(QGuiApplication::clipboard()) { - connect(m_clipboard, &QClipboard::changed, this, [this](QClipboard::Mode mode) { + connect(QGuiApplication::clipboard(), &QClipboard::changed, this, + [this](QClipboard::Mode mode) { if (mode == QClipboard::Clipboard) emit contentChanged(); }); @@ -21,33 +23,33 @@ ClipboardUtils::ClipboardUtils() bool ClipboardUtils::hasText() const { - return m_clipboard->mimeData()->hasText(); + return QGuiApplication::clipboard()->mimeData()->hasText(); } QString ClipboardUtils::text() const { - return m_clipboard->text(); + return QGuiApplication::clipboard()->text(); } bool ClipboardUtils::hasHtml() const { - return m_clipboard->mimeData()->hasHtml(); + return QGuiApplication::clipboard()->mimeData()->hasHtml(); } QString ClipboardUtils::html() const { - auto mimeData = m_clipboard->mimeData(); + auto mimeData = QGuiApplication::clipboard()->mimeData(); return mimeData ? mimeData->html() : QString{}; } bool ClipboardUtils::hasImage() const { - return m_clipboard->mimeData()->hasImage(); + return QGuiApplication::clipboard()->mimeData()->hasImage(); } QImage ClipboardUtils::image() const { - return m_clipboard->image(); + return QGuiApplication::clipboard()->image(); } QString ClipboardUtils::imageBase64() const @@ -64,20 +66,48 @@ QString ClipboardUtils::imageBase64() const bool ClipboardUtils::hasUrls() const { - return m_clipboard->mimeData()->hasUrls(); + return QGuiApplication::clipboard()->mimeData()->hasUrls(); } QList ClipboardUtils::urls() const { - return m_clipboard->mimeData()->urls(); + return QGuiApplication::clipboard()->mimeData()->urls(); } void ClipboardUtils::setText(const QString &text) { - m_clipboard->setText(text); + QGuiApplication::clipboard()->setText(text); +} + +void ClipboardUtils::setImageByUrl(const QUrl &url) +{ + static thread_local QNetworkAccessManager manager; + manager.setAutoDeleteReplies(true); + + QNetworkReply *reply = manager.get(QNetworkRequest(url)); + + QObject::connect(reply, &QNetworkReply::finished, [reply]() { + if(reply->error() == QNetworkReply::NoError) { + QByteArray btArray = reply->readAll(); + QImage image; + image.loadFromData(btArray); + Q_ASSERT(!image.isNull()); + QGuiApplication::clipboard()->setImage(image); + } else { + qWarning() << "ClipboardUtils::setImageByUrl: Downloading image failed!"; + } + }); } void ClipboardUtils::clear() { - m_clipboard->clear(); + QGuiApplication::clipboard()->clear(); +} + +QObject* ClipboardUtils::qmlInstance(QQmlEngine* engine, QJSEngine* scriptEngine) +{ + Q_UNUSED(engine); + Q_UNUSED(scriptEngine); + + return new ClipboardUtils; } diff --git a/ui/app/mainui/AppMain.qml b/ui/app/mainui/AppMain.qml index 4c15b66644..ccde749f6a 100644 --- a/ui/app/mainui/AppMain.qml +++ b/ui/app/mainui/AppMain.qml @@ -615,7 +615,7 @@ Item { active: appMain.rootStore.mainModuleInst.sectionsLoaded sourceComponent: StatusEmojiPopup { width: 360 - height: 440 + height: 940 } } diff --git a/ui/imports/shared/popups/ImageContextMenu.qml b/ui/imports/shared/popups/ImageContextMenu.qml index a169c4611f..376451e6b5 100644 --- a/ui/imports/shared/popups/ImageContextMenu.qml +++ b/ui/imports/shared/popups/ImageContextMenu.qml @@ -24,7 +24,7 @@ StatusMenu { icon.name: "copy" enabled: !!root.imageSource && !root.isVideo onTriggered: { - Utils.copyImageToClipboardByUrl(root.imageSource) + ClipboardUtils.setImageByUrl(root.imageSource) } } diff --git a/ui/imports/utils/Utils.qml b/ui/imports/utils/Utils.qml index 8d0d162bb1..9f2927870d 100644 --- a/ui/imports/utils/Utils.qml +++ b/ui/imports/utils/Utils.qml @@ -714,10 +714,6 @@ QtObject { return text } - function copyImageToClipboardByUrl(content) { - globalUtilsInst.copyImageToClipboardByUrl(content) - } - function downloadImageByUrl(url, path) { globalUtilsInst.downloadImageByUrl(url, path) }