Nicolas Werner 2021-12-21 12:17:12 +01:00
parent b9a4f369e1
commit d424145ee4
No known key found for this signature in database
GPG key ID: C8D75E610773F2D9
2 changed files with 45 additions and 20 deletions

View file

@ -11,17 +11,16 @@
#include "blurhash.hpp"
void
BlurhashResponse::run()
BlurhashRunnable::run()
{
if (m_requestedSize.width() < 0 || m_requestedSize.height() < 0) {
m_error = QStringLiteral("Blurhash needs size request");
emit finished();
emit error("Blurhash needs size request");
return;
}
if (m_requestedSize.width() == 0 || m_requestedSize.height() == 0) {
m_image = QImage(m_requestedSize, QImage::Format_RGB32);
m_image.fill(QColor(0, 0, 0));
emit finished();
auto image = QImage(m_requestedSize, QImage::Format_RGB32);
image.fill(QColor(0, 0, 0));
emit done(image);
return;
}
@ -29,8 +28,7 @@ BlurhashResponse::run()
m_requestedSize.width(),
m_requestedSize.height());
if (decoded.image.empty()) {
m_error = QStringLiteral("Failed decode!");
emit finished();
emit error(QStringLiteral("Failed decode!"));
return;
}
@ -40,6 +38,5 @@ BlurhashResponse::run()
(int)decoded.width * 3,
QImage::Format_RGB888);
m_image = image.copy();
emit finished();
emit done(std::move(image));
}

View file

@ -10,16 +10,38 @@
#include <QImage>
#include <QThreadPool>
class BlurhashResponse
: public QQuickImageResponse
class BlurhashRunnable
: public QObject
, public QRunnable
{
Q_OBJECT
public:
BlurhashResponse(const QString &id, const QSize &requestedSize)
BlurhashRunnable(const QString &id, const QSize &requestedSize)
: m_id(id)
, m_requestedSize(requestedSize)
{
setAutoDelete(false);
}
void run() override;
signals:
void done(QImage);
void error(QString);
private:
QString m_id;
QSize m_requestedSize;
};
class BlurhashResponse
: public QQuickImageResponse
{
public:
BlurhashResponse(const QString &id, const QSize &requestedSize, QThreadPool *pool)
{
auto runnable = new BlurhashRunnable(id, requestedSize);
connect(runnable, &BlurhashRunnable::done, this, &BlurhashResponse::handleDone);
connect(runnable, &BlurhashRunnable::error, this, &BlurhashResponse::handleError);
pool->start(runnable);
}
QQuickTextureFactory *textureFactory() const override
@ -28,10 +50,18 @@ public:
}
QString errorString() const override { return m_error; }
void run() override;
void handleDone(QImage image)
{
m_image = image;
emit finished();
}
void handleError(QString error)
{
m_error = error;
emit finished();
}
QString m_id, m_error;
QSize m_requestedSize;
QString m_error;
QImage m_image;
};
@ -44,9 +74,7 @@ public slots:
QQuickImageResponse *
requestImageResponse(const QString &id, const QSize &requestedSize) override
{
BlurhashResponse *response = new BlurhashResponse(id, requestedSize);
pool.start(response);
return response;
return new BlurhashResponse(id, requestedSize, &pool);
}
private: