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);
|
||||
}
|
||||
|
||||
QByteArray image() const {
|
||||
QImage *qimage() {
|
||||
if (this->cachedImage.isNull())
|
||||
this->cachedImage = this->clipboard->image();
|
||||
|
||||
return &(this->cachedImage);
|
||||
}
|
||||
|
||||
QByteArray image() {
|
||||
QByteArray byteArray;
|
||||
QBuffer buffer(&byteArray);
|
||||
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();
|
||||
return byteArray;
|
||||
}
|
||||
|
@ -90,9 +99,11 @@ signals:
|
|||
|
||||
private:
|
||||
QClipboard *clipboard = QGuiApplication::clipboard();
|
||||
QImage cachedImage = QImage();
|
||||
|
||||
void mainClipboardChanged() {
|
||||
contentChanged();
|
||||
this->contentChanged();
|
||||
this->cachedImage = QImage();
|
||||
this->hasImage() ? this->imageChanged() : this->textChanged();
|
||||
this->hasImageChanged();
|
||||
};
|
||||
|
|
|
@ -3,36 +3,39 @@
|
|||
#ifndef CLIPBOARD_IMAGE_PROVIDER_H
|
||||
#define CLIPBOARD_IMAGE_PROVIDER_H
|
||||
|
||||
#include <QClipboard>
|
||||
#include <QImage>
|
||||
#include <QQuickImageProvider>
|
||||
|
||||
#include "clipboard.h"
|
||||
|
||||
|
||||
class ClipboardImageProvider : public QQuickImageProvider {
|
||||
|
||||
public:
|
||||
explicit ClipboardImageProvider()
|
||||
: QQuickImageProvider(QQuickImageProvider::Image) {}
|
||||
explicit ClipboardImageProvider(Clipboard *clipboard)
|
||||
: QQuickImageProvider(QQuickImageProvider::Image)
|
||||
{
|
||||
this->clipboard = clipboard;
|
||||
}
|
||||
|
||||
QImage requestImage(
|
||||
const QString &id, QSize *size, const QSize &requestSize
|
||||
) {
|
||||
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)
|
||||
image = image.scaled(
|
||||
return image->scaled(
|
||||
requestSize.width(), requestSize.height(), Qt::KeepAspectRatio
|
||||
);
|
||||
|
||||
return image;
|
||||
return *image;
|
||||
}
|
||||
|
||||
private:
|
||||
QClipboard *clipboard = QGuiApplication::clipboard();
|
||||
Clipboard *clipboard;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -126,10 +126,12 @@ int main(int argc, char *argv[]) {
|
|||
objectContext->setContextProperty("debugMode", false);
|
||||
#endif
|
||||
|
||||
Clipboard *clipboard = new Clipboard();
|
||||
|
||||
// Register out custom image providers.
|
||||
// QML will be able to request an image from them by setting an
|
||||
// `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,
|
||||
// that will be importable anywhere in QML. Example:
|
||||
|
@ -138,10 +140,10 @@ int main(int argc, char *argv[]) {
|
|||
// Component.onCompleted: print(Clipboard.text)
|
||||
qmlRegisterSingletonType<Clipboard>(
|
||||
"Clipboard", 0, 1, "Clipboard",
|
||||
[](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * {
|
||||
[clipboard](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * {
|
||||
Q_UNUSED(engine)
|
||||
Q_UNUSED(scriptEngine)
|
||||
return new Clipboard();
|
||||
return clipboard;
|
||||
}
|
||||
);
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user