matrixion/src/BlurhashProvider.h

83 lines
1.8 KiB
C
Raw Normal View History

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