You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
nheko/src/BlurhashProvider.h

80 lines
1.7 KiB

// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <QQuickAsyncImageProvider>
#include <QQuickImageResponse>
#include <QImage>
#include <QThreadPool>
class BlurhashRunnable
: public QObject
, public QRunnable
{
Q_OBJECT
public:
BlurhashRunnable(const QString &id, const QSize &requestedSize)
: m_id(id)
, m_requestedSize(requestedSize)
{}
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
{
return QQuickTextureFactory::textureFactoryForImage(m_image);
}
QString errorString() const override { return m_error; }
void handleDone(QImage image)
{
m_image = image;
emit finished();
}
void handleError(QString error)
{
m_error = error;
emit finished();
}
QString m_error;
QImage m_image;
};
class BlurhashProvider
: public QObject
, public QQuickAsyncImageProvider
{
Q_OBJECT
public slots:
QQuickImageResponse *
requestImageResponse(const QString &id, const QSize &requestedSize) override
{
return new BlurhashResponse(id, requestedSize, &pool);
}
private:
QThreadPool pool;
};