nheko/src/BlurhashProvider.h

78 lines
1.7 KiB
C
Raw Normal View History

2021-03-05 00:35:15 +01:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
2020-03-04 01:56:58 +01:00
#pragma once
2020-03-04 01:56:58 +01:00
#include <QQuickAsyncImageProvider>
#include <QQuickImageResponse>
#include <QImage>
#include <QThreadPool>
class BlurhashRunnable
: public QObject
2020-03-04 01:56:58 +01:00
, public QRunnable
{
Q_OBJECT
public:
BlurhashRunnable(const QString &id, const QSize &requestedSize)
2021-09-18 00:22:33 +02:00
: m_id(id)
, m_requestedSize(requestedSize)
2021-12-21 12:34:52 +01:00
{}
void run() override;
signals:
void done(QImage);
void error(QString);
private:
QString m_id;
QSize m_requestedSize;
};
2021-12-21 12:34:52 +01:00
class BlurhashResponse : public QQuickImageResponse
{
public:
2021-12-21 15:57:46 +01:00
BlurhashResponse(const QString &id, const QSize &requestedSize)
{
auto runnable = new BlurhashRunnable(id, requestedSize);
connect(runnable, &BlurhashRunnable::done, this, &BlurhashResponse::handleDone);
connect(runnable, &BlurhashRunnable::error, this, &BlurhashResponse::handleError);
2021-12-21 15:57:46 +01:00
QThreadPool::globalInstance()->start(runnable);
2021-09-18 00:22:33 +02:00
}
2020-03-04 01:56:58 +01:00
2021-09-18 00:22:33 +02:00
QQuickTextureFactory *textureFactory() const override
{
return QQuickTextureFactory::textureFactoryForImage(m_image);
}
QString errorString() const override { return m_error; }
2020-03-04 01:56:58 +01:00
void handleDone(QImage image)
{
m_image = std::move(image);
emit finished();
}
void handleError(QString error)
{
m_error = error;
emit finished();
}
2020-03-04 01:56:58 +01:00
QString m_error;
2021-09-18 00:22:33 +02:00
QImage m_image;
2020-03-04 01:56:58 +01:00
};
class BlurhashProvider
: public QObject
, public QQuickAsyncImageProvider
{
2021-09-18 00:22:33 +02:00
Q_OBJECT
2020-03-04 01:56:58 +01:00
public slots:
QQuickImageResponse *
requestImageResponse(const QString &id, const QSize &requestedSize) override
2021-09-18 00:22:33 +02:00
{
2021-12-21 15:57:46 +01:00
return new BlurhashResponse(id, requestedSize);
2021-09-18 00:22:33 +02:00
}
};