matrixion/src/BlurhashProvider.h

56 lines
1.2 KiB
C
Raw Normal View History

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-04 03:56:58 +03:00
#include <QQuickAsyncImageProvider>
#include <QQuickImageResponse>
#include <QImage>
#include <QThreadPool>
class BlurhashResponse
: public QQuickImageResponse
, public QRunnable
{
public:
2021-09-18 01:22:33 +03:00
BlurhashResponse(const QString &id, const QSize &requestedSize)
2020-03-04 03:56:58 +03:00
2021-09-18 01:22:33 +03:00
: m_id(id)
, m_requestedSize(requestedSize)
{
setAutoDelete(false);
}
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-09-18 01:22:33 +03:00
void run() override;
2020-03-04 03:56:58 +03:00
2021-09-18 01:22:33 +03:00
QString m_id, m_error;
QSize m_requestedSize;
QImage m_image;
2020-03-04 03:56:58 +03:00
};
class BlurhashProvider
: public QObject
, public QQuickAsyncImageProvider
{
2021-09-18 01:22:33 +03:00
Q_OBJECT
2020-03-04 03:56:58 +03:00
public slots:
2021-09-18 01:22:33 +03:00
QQuickImageResponse *requestImageResponse(const QString &id,
const QSize &requestedSize) override
{
BlurhashResponse *response = new BlurhashResponse(id, requestedSize);
pool.start(response);
return response;
}
2020-03-04 03:56:58 +03:00
private:
2021-09-18 01:22:33 +03:00
QThreadPool pool;
};