Cache the clipboard QImage, share with provider
This commit is contained in:
parent
74c4d63c16
commit
6365beb455
|
@ -46,11 +46,20 @@ public:
|
||||||
this->clipboard->setText(text, QClipboard::Clipboard);
|
this->clipboard->setText(text, QClipboard::Clipboard);
|
||||||
}
|
}
|
||||||
|
|
||||||
QByteArray image() const {
|
QImage *qimage() {
|
||||||
|
if (this->cachedImage.isNull())
|
||||||
|
this->cachedImage = this->clipboard->image();
|
||||||
|
|
||||||
|
return &(this->cachedImage);
|
||||||
|
}
|
||||||
|
|
||||||
|
QByteArray image() {
|
||||||
QByteArray byteArray;
|
QByteArray byteArray;
|
||||||
QBuffer buffer(&byteArray);
|
QBuffer buffer(&byteArray);
|
||||||
buffer.open(QIODevice::WriteOnly);
|
buffer.open(QIODevice::WriteOnly);
|
||||||
this->clipboard->image(QClipboard::Clipboard).save(&buffer, "PNG");
|
QImage *image = this->qimage();
|
||||||
|
// minimum compression, fastest to not freeze the UI
|
||||||
|
image->save(&buffer, "PNG", 100);
|
||||||
buffer.close();
|
buffer.close();
|
||||||
return byteArray;
|
return byteArray;
|
||||||
}
|
}
|
||||||
|
@ -90,9 +99,11 @@ signals:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QClipboard *clipboard = QGuiApplication::clipboard();
|
QClipboard *clipboard = QGuiApplication::clipboard();
|
||||||
|
QImage cachedImage = QImage();
|
||||||
|
|
||||||
void mainClipboardChanged() {
|
void mainClipboardChanged() {
|
||||||
contentChanged();
|
this->contentChanged();
|
||||||
|
this->cachedImage = QImage();
|
||||||
this->hasImage() ? this->imageChanged() : this->textChanged();
|
this->hasImage() ? this->imageChanged() : this->textChanged();
|
||||||
this->hasImageChanged();
|
this->hasImageChanged();
|
||||||
};
|
};
|
||||||
|
|
|
@ -3,36 +3,39 @@
|
||||||
#ifndef CLIPBOARD_IMAGE_PROVIDER_H
|
#ifndef CLIPBOARD_IMAGE_PROVIDER_H
|
||||||
#define CLIPBOARD_IMAGE_PROVIDER_H
|
#define CLIPBOARD_IMAGE_PROVIDER_H
|
||||||
|
|
||||||
#include <QClipboard>
|
|
||||||
#include <QImage>
|
#include <QImage>
|
||||||
#include <QQuickImageProvider>
|
#include <QQuickImageProvider>
|
||||||
|
|
||||||
|
#include "clipboard.h"
|
||||||
|
|
||||||
|
|
||||||
class ClipboardImageProvider : public QQuickImageProvider {
|
class ClipboardImageProvider : public QQuickImageProvider {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit ClipboardImageProvider()
|
explicit ClipboardImageProvider(Clipboard *clipboard)
|
||||||
: QQuickImageProvider(QQuickImageProvider::Image) {}
|
: QQuickImageProvider(QQuickImageProvider::Image)
|
||||||
|
{
|
||||||
|
this->clipboard = clipboard;
|
||||||
|
}
|
||||||
|
|
||||||
QImage requestImage(
|
QImage requestImage(
|
||||||
const QString &id, QSize *size, const QSize &requestSize
|
const QString &id, QSize *size, const QSize &requestSize
|
||||||
) {
|
) {
|
||||||
Q_UNUSED(id);
|
Q_UNUSED(id);
|
||||||
|
|
||||||
QImage image = this->clipboard->image();
|
QImage *image = this->clipboard->qimage();
|
||||||
|
|
||||||
if (size) *size = image.size();
|
if (size) *size = image->size();
|
||||||
|
|
||||||
if (requestSize.width() > 0 && requestSize.height() > 0)
|
if (requestSize.width() > 0 && requestSize.height() > 0)
|
||||||
image = image.scaled(
|
return image->scaled(
|
||||||
requestSize.width(), requestSize.height(), Qt::KeepAspectRatio
|
requestSize.width(), requestSize.height(), Qt::KeepAspectRatio
|
||||||
);
|
);
|
||||||
|
|
||||||
return image;
|
return *image;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QClipboard *clipboard = QGuiApplication::clipboard();
|
Clipboard *clipboard;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -126,10 +126,12 @@ int main(int argc, char *argv[]) {
|
||||||
objectContext->setContextProperty("debugMode", false);
|
objectContext->setContextProperty("debugMode", false);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Clipboard *clipboard = new Clipboard();
|
||||||
|
|
||||||
// Register out custom image providers.
|
// Register out custom image providers.
|
||||||
// QML will be able to request an image from them by setting an
|
// QML will be able to request an image from them by setting an
|
||||||
// `Image`'s `source` to `image://<providerId>/<id>`
|
// `Image`'s `source` to `image://<providerId>/<id>`
|
||||||
engine.addImageProvider("clipboard", new ClipboardImageProvider);
|
engine.addImageProvider("clipboard",new ClipboardImageProvider(clipboard));
|
||||||
|
|
||||||
// Register our custom non-visual QObject singletons,
|
// Register our custom non-visual QObject singletons,
|
||||||
// that will be importable anywhere in QML. Example:
|
// that will be importable anywhere in QML. Example:
|
||||||
|
@ -138,10 +140,10 @@ int main(int argc, char *argv[]) {
|
||||||
// Component.onCompleted: print(Clipboard.text)
|
// Component.onCompleted: print(Clipboard.text)
|
||||||
qmlRegisterSingletonType<Clipboard>(
|
qmlRegisterSingletonType<Clipboard>(
|
||||||
"Clipboard", 0, 1, "Clipboard",
|
"Clipboard", 0, 1, "Clipboard",
|
||||||
[](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * {
|
[clipboard](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * {
|
||||||
Q_UNUSED(engine)
|
Q_UNUSED(engine)
|
||||||
Q_UNUSED(scriptEngine)
|
Q_UNUSED(scriptEngine)
|
||||||
return new Clipboard();
|
return clipboard;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user