Skip to content
Snippets Groups Projects
Verified Commit d424145e authored by Nicolas Werner's avatar Nicolas Werner
Browse files
parent b9a4f369
No related branches found
No related tags found
No related merge requests found
Pipeline #2336 failed
......@@ -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));
}
......@@ -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:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment