60 lines
1.8 KiB
C++
60 lines
1.8 KiB
C++
#ifndef CLIPBOARDHELPER_H
|
|
#define CLIPBOARDHELPER_H
|
|
|
|
#include <QObject>
|
|
#include <QClipboard>
|
|
#include <QGuiApplication>
|
|
#include <QImage>
|
|
#include <QMimeData>
|
|
|
|
class ClipboardHelper : public QObject
|
|
{
|
|
Q_OBJECT
|
|
|
|
public:
|
|
explicit ClipboardHelper(QObject *parent = nullptr) : QObject(parent) {}
|
|
|
|
// Copy image from data URI (data:image/svg+xml;base64,...) to clipboard
|
|
Q_INVOKABLE void copyImage(const QString &dataUri) {
|
|
if (!dataUri.startsWith(QStringLiteral("data:image/")))
|
|
return;
|
|
|
|
// Parse data URI: data:image/TYPE;base64,DATA
|
|
int commaIdx = dataUri.indexOf(',');
|
|
if (commaIdx < 0) return;
|
|
|
|
QString header = dataUri.left(commaIdx);
|
|
QString base64Data = dataUri.mid(commaIdx + 1);
|
|
QByteArray imageData = QByteArray::fromBase64(base64Data.toLatin1());
|
|
|
|
QClipboard *clipboard = QGuiApplication::clipboard();
|
|
QMimeData *mimeData = new QMimeData();
|
|
|
|
if (header.contains(QStringLiteral("svg"))) {
|
|
// For SVG, set as both image/svg+xml and try to render as raster
|
|
mimeData->setData(QStringLiteral("image/svg+xml"), imageData);
|
|
|
|
// Also try to load as QImage for apps that don't support SVG
|
|
QImage img;
|
|
if (img.loadFromData(imageData, "SVG")) {
|
|
mimeData->setImageData(img);
|
|
}
|
|
} else {
|
|
// PNG, JPEG, etc.
|
|
QImage img;
|
|
if (img.loadFromData(imageData)) {
|
|
mimeData->setImageData(img);
|
|
}
|
|
}
|
|
|
|
clipboard->setMimeData(mimeData);
|
|
}
|
|
|
|
// Copy plain text to clipboard
|
|
Q_INVOKABLE void copyText(const QString &text) {
|
|
QGuiApplication::clipboard()->setText(text);
|
|
}
|
|
};
|
|
|
|
#endif // CLIPBOARDHELPER_H
|