| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <QQuickAsyncImageProvider> |
| 8 |
#include <QQuickImageResponse> |
| 9 |
|
| 10 |
#include <QImage> |
| 11 |
#include <QThreadPool> |
| 12 |
|
| 13 |
class BlurhashRunnable final |
| 14 |
: public QObject |
| 15 |
, public QRunnable |
| 16 |
{ |
| 17 |
Q_OBJECT |
| 18 |
public: |
| 19 |
BlurhashRunnable(const QString &id, const QSize &requestedSize) |
| 20 |
: m_id(id) |
| 21 |
, m_requestedSize(requestedSize) |
| 22 |
{ |
| 23 |
} |
| 24 |
|
| 25 |
void run() override; |
| 26 |
signals: |
| 27 |
void done(QImage); |
| 28 |
void error(QString); |
| 29 |
|
| 30 |
private: |
| 31 |
QString m_id; |
| 32 |
QSize m_requestedSize; |
| 33 |
}; |
| 34 |
|
| 35 |
class BlurhashResponse final : public QQuickImageResponse |
| 36 |
{ |
| 37 |
public: |
| 38 |
BlurhashResponse(const QString &id, const QSize &requestedSize) |
| 39 |
{ |
| 40 |
auto runnable = new BlurhashRunnable(id, requestedSize); |
| 41 |
connect(runnable, &BlurhashRunnable::done, this, &BlurhashResponse::handleDone); |
| 42 |
connect(runnable, &BlurhashRunnable::error, this, &BlurhashResponse::handleError); |
| 43 |
QThreadPool::globalInstance()->start(runnable); |
| 44 |
} |
| 45 |
|
| 46 |
QQuickTextureFactory *textureFactory() const override |
| 47 |
{ |
| 48 |
return QQuickTextureFactory::textureFactoryForImage(m_image); |
| 49 |
} |
| 50 |
QString errorString() const override { return m_error; } |
| 51 |
|
| 52 |
void handleDone(QImage image) |
| 53 |
{ |
| 54 |
m_image = std::move(image); |
| 55 |
emit finished(); |
| 56 |
} |
| 57 |
void handleError(QString error) |
| 58 |
{ |
| 59 |
m_error = error; |
| 60 |
emit finished(); |
| 61 |
} |
| 62 |
|
| 63 |
QString m_error; |
| 64 |
QImage m_image; |
| 65 |
}; |
| 66 |
|
| 67 |
class BlurhashProvider |
| 68 |
: |
| 69 |
#if QT_VERSION < 0x60000 |
| 70 |
public QObject |
| 71 |
, |
| 72 |
#endif |
| 73 |
public QQuickAsyncImageProvider |
| 74 |
{ |
| 75 |
Q_OBJECT |
| 76 |
public slots: |
| 77 |
QQuickImageResponse * |
| 78 |
requestImageResponse(const QString &id, const QSize &requestedSize) override |
| 79 |
{ |
| 80 |
return new BlurhashResponse(id, requestedSize); |
| 81 |
} |
| 82 |
}; |