2021-03-05 02:35:15 +03:00
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
2022-01-01 06:57:53 +03:00
|
|
|
// SPDX-FileCopyrightText: 2022 Nheko Contributors
|
2023-01-02 06:25:33 +03:00
|
|
|
// SPDX-FileCopyrightText: 2023 Nheko Contributors
|
2021-03-05 02:35:15 +03:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2019-09-07 23:22:07 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <QQuickAsyncImageProvider>
|
|
|
|
#include <QQuickImageResponse>
|
|
|
|
|
|
|
|
#include <QImage>
|
|
|
|
|
2021-03-17 05:27:28 +03:00
|
|
|
#include <functional>
|
2019-12-04 01:34:16 +03:00
|
|
|
|
2021-03-17 05:27:28 +03:00
|
|
|
#include <mtx/common.hpp>
|
2019-12-04 01:34:16 +03:00
|
|
|
|
2022-10-10 15:38:29 +03:00
|
|
|
class MxcImageRunnable final : public QObject
|
2019-09-07 23:22:07 +03:00
|
|
|
{
|
2021-12-08 01:58:17 +03:00
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void done(QImage image);
|
|
|
|
void error(QString error);
|
|
|
|
|
2019-09-07 23:22:07 +03:00
|
|
|
public:
|
2021-12-08 01:58:17 +03:00
|
|
|
MxcImageRunnable(const QString &id, bool crop, double radius, const QSize &requestedSize)
|
2021-09-18 01:22:33 +03:00
|
|
|
: m_id(id)
|
|
|
|
, m_requestedSize(requestedSize)
|
|
|
|
, m_crop(crop)
|
|
|
|
, m_radius(radius)
|
2022-09-25 21:05:08 +03:00
|
|
|
{
|
|
|
|
}
|
2021-12-08 01:58:17 +03:00
|
|
|
|
|
|
|
void run();
|
|
|
|
|
|
|
|
QString m_id;
|
|
|
|
QSize m_requestedSize;
|
|
|
|
bool m_crop;
|
|
|
|
double m_radius;
|
|
|
|
};
|
2022-10-10 15:38:29 +03:00
|
|
|
class MxcImageResponse final : public QQuickImageResponse
|
2021-12-08 01:58:17 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
MxcImageResponse(const QString &id, bool crop, double radius, const QSize &requestedSize)
|
|
|
|
|
|
|
|
{
|
|
|
|
auto runnable = new MxcImageRunnable(id, crop, radius, requestedSize);
|
|
|
|
connect(runnable, &MxcImageRunnable::done, this, &MxcImageResponse::handleDone);
|
|
|
|
connect(runnable, &MxcImageRunnable::error, this, &MxcImageResponse::handleError);
|
|
|
|
runnable->run();
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleDone(QImage image)
|
|
|
|
{
|
|
|
|
m_image = image;
|
|
|
|
emit finished();
|
|
|
|
}
|
|
|
|
void handleError(QString error)
|
|
|
|
{
|
|
|
|
m_error = error;
|
|
|
|
emit finished();
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2019-09-07 23:22:07 +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; }
|
2019-09-07 23:22:07 +03:00
|
|
|
|
2021-12-08 01:58:17 +03:00
|
|
|
QString m_error;
|
2021-09-18 01:22:33 +03:00
|
|
|
QImage m_image;
|
2019-09-07 23:22:07 +03:00
|
|
|
};
|
|
|
|
|
2019-12-04 01:34:16 +03:00
|
|
|
class MxcImageProvider
|
2021-12-28 22:09:08 +03:00
|
|
|
:
|
|
|
|
#if QT_VERSION < 0x60000
|
|
|
|
public QObject
|
|
|
|
,
|
|
|
|
#endif
|
|
|
|
public QQuickAsyncImageProvider
|
2019-09-07 23:22:07 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
Q_OBJECT
|
2022-12-13 08:02:07 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
MxcImageProvider(QObject *parent = nullptr);
|
|
|
|
|
2019-12-04 01:34:16 +03:00
|
|
|
public slots:
|
2021-11-22 02:32:49 +03:00
|
|
|
QQuickImageResponse *
|
|
|
|
requestImageResponse(const QString &id, const QSize &requestedSize) override;
|
2019-12-04 01:34:16 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
static void addEncryptionInfo(mtx::crypto::EncryptedFile info);
|
|
|
|
static void download(const QString &id,
|
|
|
|
const QSize &requestedSize,
|
|
|
|
std::function<void(QString, QSize, QImage, QString)> then,
|
|
|
|
bool crop = true,
|
|
|
|
double radius = 0);
|
2019-09-07 23:22:07 +03:00
|
|
|
};
|