mirror of
https://github.com/Nheko-Reborn/nheko.git
synced 2024-11-26 04:58:49 +03:00
Modernize blurhashprovider implementation
Might fix #844 See https://code.qt.io/cgit/qt/qtdeclarative.git/commit/examples/quick/imageresponseprovider/imageresponseprovider.cpp?h=5.15&id=b1f238568214e6587b829d6695677e55a99b1d40 for context.
This commit is contained in:
parent
b9a4f369e1
commit
d424145ee4
2 changed files with 45 additions and 20 deletions
|
@ -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:
|
||||
|
|
Loading…
Reference in a new issue