copyImageToClipboardByUrl moved to ClipboardUtils

This commit is contained in:
Michał Cieślak 2024-08-28 10:37:52 +02:00 committed by Michał
parent 9c3b8fc34b
commit 4ac9a9b305
6 changed files with 49 additions and 36 deletions

View File

@ -45,10 +45,6 @@ SplitView {
return url return url
} }
function copyImageToClipboardByUrl(data) {
logs.logEvent("Utils::copyImageToClipboardByUrl", ["data"], arguments)
}
function downloadImageByUrl(url, path) { function downloadImageByUrl(url, path) {
logs.logEvent("Utils::downloadImageByUrl", ["url", "path"], arguments) logs.logEvent("Utils::downloadImageByUrl", ["url", "path"], arguments)
} }

View File

@ -3,7 +3,6 @@
#include <QObject> #include <QObject>
#include <QImage> #include <QImage>
class QClipboard;
class QJSEngine; class QJSEngine;
class QQmlEngine; class QQmlEngine;
@ -11,8 +10,6 @@ class ClipboardUtils : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_DISABLE_COPY(ClipboardUtils)
Q_PROPERTY(bool hasText READ hasText NOTIFY contentChanged) Q_PROPERTY(bool hasText READ hasText NOTIFY contentChanged)
Q_PROPERTY(QString text READ text NOTIFY contentChanged) Q_PROPERTY(QString text READ text NOTIFY contentChanged)
@ -41,20 +38,14 @@ class ClipboardUtils : public QObject
bool hasUrls() const; bool hasUrls() const;
QList<QUrl> urls() const; QList<QUrl> urls() const;
QClipboard* m_clipboard{nullptr};
public: 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 setText(const QString& text);
Q_INVOKABLE void setImageByUrl(const QUrl& url);
Q_INVOKABLE void clear(); Q_INVOKABLE void clear();
static QObject* qmlInstance(QQmlEngine* engine, QJSEngine* scriptEngine);
signals: signals:
void contentChanged(); void contentChanged();
}; };

View File

@ -2,18 +2,20 @@
#include <QBuffer> #include <QBuffer>
#include <QClipboard> #include <QClipboard>
#include <QFile>
#include <QGuiApplication> #include <QGuiApplication>
#include <QImage> #include <QImage>
#include <QMimeData> #include <QMimeData>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QUrl> #include <QUrl>
#include <QFile>
#include <algorithm> #include <algorithm>
ClipboardUtils::ClipboardUtils() 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) if (mode == QClipboard::Clipboard)
emit contentChanged(); emit contentChanged();
}); });
@ -21,33 +23,33 @@ ClipboardUtils::ClipboardUtils()
bool ClipboardUtils::hasText() const bool ClipboardUtils::hasText() const
{ {
return m_clipboard->mimeData()->hasText(); return QGuiApplication::clipboard()->mimeData()->hasText();
} }
QString ClipboardUtils::text() const QString ClipboardUtils::text() const
{ {
return m_clipboard->text(); return QGuiApplication::clipboard()->text();
} }
bool ClipboardUtils::hasHtml() const bool ClipboardUtils::hasHtml() const
{ {
return m_clipboard->mimeData()->hasHtml(); return QGuiApplication::clipboard()->mimeData()->hasHtml();
} }
QString ClipboardUtils::html() const QString ClipboardUtils::html() const
{ {
auto mimeData = m_clipboard->mimeData(); auto mimeData = QGuiApplication::clipboard()->mimeData();
return mimeData ? mimeData->html() : QString{}; return mimeData ? mimeData->html() : QString{};
} }
bool ClipboardUtils::hasImage() const bool ClipboardUtils::hasImage() const
{ {
return m_clipboard->mimeData()->hasImage(); return QGuiApplication::clipboard()->mimeData()->hasImage();
} }
QImage ClipboardUtils::image() const QImage ClipboardUtils::image() const
{ {
return m_clipboard->image(); return QGuiApplication::clipboard()->image();
} }
QString ClipboardUtils::imageBase64() const QString ClipboardUtils::imageBase64() const
@ -64,20 +66,48 @@ QString ClipboardUtils::imageBase64() const
bool ClipboardUtils::hasUrls() const bool ClipboardUtils::hasUrls() const
{ {
return m_clipboard->mimeData()->hasUrls(); return QGuiApplication::clipboard()->mimeData()->hasUrls();
} }
QList<QUrl> ClipboardUtils::urls() const QList<QUrl> ClipboardUtils::urls() const
{ {
return m_clipboard->mimeData()->urls(); return QGuiApplication::clipboard()->mimeData()->urls();
} }
void ClipboardUtils::setText(const QString &text) 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() 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;
} }

View File

@ -615,7 +615,7 @@ Item {
active: appMain.rootStore.mainModuleInst.sectionsLoaded active: appMain.rootStore.mainModuleInst.sectionsLoaded
sourceComponent: StatusEmojiPopup { sourceComponent: StatusEmojiPopup {
width: 360 width: 360
height: 440 height: 940
} }
} }

View File

@ -24,7 +24,7 @@ StatusMenu {
icon.name: "copy" icon.name: "copy"
enabled: !!root.imageSource && !root.isVideo enabled: !!root.imageSource && !root.isVideo
onTriggered: { onTriggered: {
Utils.copyImageToClipboardByUrl(root.imageSource) ClipboardUtils.setImageByUrl(root.imageSource)
} }
} }

View File

@ -714,10 +714,6 @@ QtObject {
return text return text
} }
function copyImageToClipboardByUrl(content) {
globalUtilsInst.copyImageToClipboardByUrl(content)
}
function downloadImageByUrl(url, path) { function downloadImageByUrl(url, path) {
globalUtilsInst.downloadImageByUrl(url, path) globalUtilsInst.downloadImageByUrl(url, path)
} }