2021-03-05 02:35:15 +03:00
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-03-04 03:56:58 +03:00
|
|
|
#pragma once
|
2020-03-01 21:55:43 +03:00
|
|
|
|
2020-03-04 03:56:58 +03:00
|
|
|
#include <QQuickAsyncImageProvider>
|
|
|
|
#include <QQuickImageResponse>
|
|
|
|
|
|
|
|
#include <QImage>
|
|
|
|
#include <QThreadPool>
|
|
|
|
|
2021-12-21 14:17:12 +03:00
|
|
|
class BlurhashRunnable
|
|
|
|
: public QObject
|
2020-03-04 03:56:58 +03:00
|
|
|
, public QRunnable
|
2020-03-01 21:55:43 +03:00
|
|
|
{
|
2021-12-21 14:17:12 +03:00
|
|
|
Q_OBJECT
|
2020-03-01 21:55:43 +03:00
|
|
|
public:
|
2021-12-21 14:17:12 +03:00
|
|
|
BlurhashRunnable(const QString &id, const QSize &requestedSize)
|
2021-09-18 01:22:33 +03:00
|
|
|
: m_id(id)
|
|
|
|
, m_requestedSize(requestedSize)
|
2021-12-21 14:34:52 +03:00
|
|
|
{}
|
2021-12-21 14:17:12 +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
|
2021-12-21 14:17:12 +03:00
|
|
|
{
|
|
|
|
public:
|
2021-12-21 17:57:46 +03:00
|
|
|
BlurhashResponse(const QString &id, const QSize &requestedSize)
|
2021-12-21 14:17:12 +03:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2021-12-21 14:17:12 +03:00
|
|
|
void handleDone(QImage image)
|
|
|
|
{
|
2021-12-21 17:06:34 +03:00
|
|
|
m_image = std::move(image);
|
2021-12-21 14:17:12 +03:00
|
|
|
emit finished();
|
|
|
|
}
|
|
|
|
void handleError(QString error)
|
|
|
|
{
|
|
|
|
m_error = error;
|
|
|
|
emit finished();
|
|
|
|
}
|
2020-03-04 03:56:58 +03:00
|
|
|
|
2021-12-21 14:17:12 +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:
|
2021-11-22 02:32:49 +03:00
|
|
|
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
|
|
|
}
|
2020-03-01 21:55:43 +03:00
|
|
|
};
|