2023-02-22 01:48:49 +03:00
|
|
|
// SPDX-FileCopyrightText: Nheko Contributors
|
2021-08-13 03:49:25 +03:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-12-25 17:14:00 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <QImage>
|
|
|
|
#include <QQuickAsyncImageProvider>
|
|
|
|
#include <QQuickImageResponse>
|
|
|
|
#include <QThreadPool>
|
|
|
|
|
|
|
|
#include <mtx/common.hpp>
|
|
|
|
|
2022-10-10 15:38:29 +03:00
|
|
|
class JdenticonRunnable final
|
2021-12-21 17:06:34 +03:00
|
|
|
: public QObject
|
2020-12-25 17:14:00 +03:00
|
|
|
, public QRunnable
|
|
|
|
{
|
2021-12-21 17:06:34 +03:00
|
|
|
Q_OBJECT
|
2020-12-25 17:14:00 +03:00
|
|
|
public:
|
2021-12-21 17:06:34 +03:00
|
|
|
JdenticonRunnable(const QString &key, bool crop, double radius, const QSize &requestedSize);
|
2020-12-25 17:14:00 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
void run() override;
|
2020-12-25 17:14:00 +03:00
|
|
|
|
2021-12-21 17:06:34 +03:00
|
|
|
signals:
|
|
|
|
void done(QImage img);
|
|
|
|
|
|
|
|
private:
|
2021-09-18 01:22:33 +03:00
|
|
|
QString m_key;
|
|
|
|
bool m_crop;
|
|
|
|
double m_radius;
|
|
|
|
QSize m_requestedSize;
|
2021-12-21 17:06:34 +03:00
|
|
|
};
|
|
|
|
|
2022-10-10 15:38:29 +03:00
|
|
|
class JdenticonResponse final : public QQuickImageResponse
|
2021-12-21 17:06:34 +03:00
|
|
|
{
|
|
|
|
public:
|
2021-12-21 17:57:46 +03:00
|
|
|
JdenticonResponse(const QString &key, bool crop, double radius, const QSize &requestedSize);
|
2021-12-21 17:06:34 +03:00
|
|
|
|
|
|
|
QQuickTextureFactory *textureFactory() const override
|
|
|
|
{
|
|
|
|
return QQuickTextureFactory::textureFactoryForImage(m_pixmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleDone(QImage img)
|
|
|
|
{
|
|
|
|
m_pixmap = std::move(img);
|
|
|
|
emit finished();
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage m_pixmap;
|
2020-12-25 17:14:00 +03:00
|
|
|
};
|
|
|
|
|
2023-06-02 02:37:53 +03:00
|
|
|
class JdenticonProvider : public QQuickAsyncImageProvider
|
2020-12-25 17:14:00 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
Q_OBJECT
|
2020-12-25 17:14:00 +03:00
|
|
|
|
|
|
|
public:
|
2021-12-21 17:06:34 +03:00
|
|
|
static bool isAvailable();
|
2020-12-25 17:14:00 +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
|
|
|
{
|
|
|
|
auto id_ = id;
|
|
|
|
bool crop = true;
|
|
|
|
double radius = 0;
|
|
|
|
|
|
|
|
auto queryStart = id.lastIndexOf('?');
|
|
|
|
if (queryStart != -1) {
|
|
|
|
id_ = id.left(queryStart);
|
2021-12-28 22:09:08 +03:00
|
|
|
auto query = id.mid(queryStart + 1);
|
2023-06-02 01:24:26 +03:00
|
|
|
auto queryBits = QStringView(query).split('&');
|
2021-09-18 01:22:33 +03:00
|
|
|
|
2021-12-29 00:30:12 +03:00
|
|
|
for (const auto &b : queryBits) {
|
2021-12-28 22:09:08 +03:00
|
|
|
if (b.startsWith(QStringView(u"radius="))) {
|
2021-09-18 01:22:33 +03:00
|
|
|
radius = b.mid(7).toDouble();
|
2021-08-14 03:05:51 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2020-12-25 17:14:00 +03:00
|
|
|
}
|
|
|
|
|
2021-12-21 17:57:46 +03:00
|
|
|
return new JdenticonResponse(id_, crop, radius, requestedSize);
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2020-12-25 17:14:00 +03:00
|
|
|
};
|