Use a thread lock for Clipboard.qimage()

Prevents this: user paste image (image provider retrieves the clipboard
image to show it as a thumbnail),
and immediatly presses enter (retrieves the image to convert it and send
to Python); the two calls would run at the same time instead of one
waiting for the other to set the cache.
This commit is contained in:
miruka 2020-07-16 16:24:16 -04:00
parent 5cc1c55d95
commit 606fe3d022

View File

@ -13,6 +13,8 @@
#include <QIODevice> #include <QIODevice>
#include <QImage> #include <QImage>
#include <QMimeData> #include <QMimeData>
#include <QMutex>
#include <QMutexLocker>
#include <QObject> #include <QObject>
@ -47,6 +49,8 @@ public:
} }
QImage *qimage() { QImage *qimage() {
QMutexLocker locker(&(this->imageRetrievalLock));
if (this->cachedImage.isNull()) if (this->cachedImage.isNull())
this->cachedImage = this->clipboard->image(); this->cachedImage = this->clipboard->image();
@ -100,6 +104,7 @@ signals:
private: private:
QClipboard *clipboard = QGuiApplication::clipboard(); QClipboard *clipboard = QGuiApplication::clipboard();
QImage cachedImage = QImage(); QImage cachedImage = QImage();
QMutex imageRetrievalLock;
void mainClipboardChanged() { void mainClipboardChanged() {
this->contentChanged(); this->contentChanged();