Add C++ clipboard image provider
Will be used to show a preview before uploading image from clipboard. Transforming the clipboard image to PNG (or any other real format) is slow and freezes the GUI, with the provider we can display it directly.
This commit is contained in:
@@ -82,6 +82,7 @@ public:
|
||||
}
|
||||
|
||||
signals:
|
||||
void contentChanged();
|
||||
void textChanged();
|
||||
void imageChanged();
|
||||
void hasImageChanged();
|
||||
@@ -91,6 +92,7 @@ private:
|
||||
QClipboard *clipboard = QGuiApplication::clipboard();
|
||||
|
||||
void mainClipboardChanged() {
|
||||
contentChanged();
|
||||
this->hasImage() ? this->imageChanged() : this->textChanged();
|
||||
this->hasImageChanged();
|
||||
};
|
||||
|
38
src/clipboard_image_provider.h
Normal file
38
src/clipboard_image_provider.h
Normal file
@@ -0,0 +1,38 @@
|
||||
// SPDX-License-Identifier: LGPL-3.0-or-later
|
||||
|
||||
#ifndef CLIPBOARD_IMAGE_PROVIDER_H
|
||||
#define CLIPBOARD_IMAGE_PROVIDER_H
|
||||
|
||||
#include <QClipboard>
|
||||
#include <QImage>
|
||||
#include <QQuickImageProvider>
|
||||
|
||||
|
||||
class ClipboardImageProvider : public QQuickImageProvider {
|
||||
|
||||
public:
|
||||
explicit ClipboardImageProvider()
|
||||
: QQuickImageProvider(QQuickImageProvider::Image) {}
|
||||
|
||||
QImage requestImage(
|
||||
const QString &id, QSize *size, const QSize &requestSize
|
||||
) {
|
||||
Q_UNUSED(id);
|
||||
|
||||
QImage image = this->clipboard->image();
|
||||
|
||||
if (size) *size = image.size();
|
||||
|
||||
if (requestSize.width() > 0 && requestSize.height() > 0)
|
||||
image = image.scaled(
|
||||
requestSize.width(), requestSize.height(), Qt::KeepAspectRatio
|
||||
);
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
private:
|
||||
QClipboard *clipboard = QGuiApplication::clipboard();
|
||||
};
|
||||
|
||||
#endif
|
@@ -19,6 +19,7 @@
|
||||
|
||||
#include "utils.h"
|
||||
#include "clipboard.h"
|
||||
#include "clipboard_image_provider.h"
|
||||
|
||||
|
||||
void loggingHandler(
|
||||
@@ -125,6 +126,11 @@ int main(int argc, char *argv[]) {
|
||||
objectContext->setContextProperty("debugMode", false);
|
||||
#endif
|
||||
|
||||
// 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);
|
||||
|
||||
// Register our custom non-visual QObject singletons,
|
||||
// that will be importable anywhere in QML. Example:
|
||||
// import Clipboard 0.1
|
||||
|
Reference in New Issue
Block a user