matrixion/src/JdenticonProvider.h

84 lines
2.3 KiB
C
Raw Normal View History

2021-08-13 03:49:25 +03:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// 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>
#include "jdenticoninterface.h"
namespace Jdenticon {
JdenticonInterface *
getJdenticonInterface();
}
class JdenticonResponse
: public QQuickImageResponse
, public QRunnable
{
public:
2021-08-14 03:05:51 +03:00
JdenticonResponse(const QString &key, bool crop, double radius, const QSize &requestedSize);
2020-12-25 17:14:00 +03:00
QQuickTextureFactory *textureFactory() const override
{
return QQuickTextureFactory::textureFactoryForImage(m_pixmap.toImage());
}
void run() override;
QString m_key;
2021-08-14 03:05:51 +03:00
bool m_crop;
double m_radius;
2020-12-25 17:14:00 +03:00
QSize m_requestedSize;
QPixmap m_pixmap;
JdenticonInterface *jdenticonInterface_ = nullptr;
};
class JdenticonProvider
: public QObject
, public QQuickAsyncImageProvider
{
Q_OBJECT
public:
static bool isAvailable() { return Jdenticon::getJdenticonInterface() != nullptr; }
public slots:
2021-08-14 03:05:51 +03:00
QQuickImageResponse *requestImageResponse(const QString &id,
2020-12-25 17:14:00 +03:00
const QSize &requestedSize) override
{
2021-08-14 03:05:51 +03:00
auto id_ = id;
bool crop = true;
double radius = 0;
auto queryStart = id.lastIndexOf('?');
if (queryStart != -1) {
id_ = id.left(queryStart);
auto query = id.midRef(queryStart + 1);
auto queryBits = query.split('&');
for (auto b : queryBits) {
if (b == "scale") {
crop = false;
} else if (b.startsWith("radius=")) {
radius = b.mid(7).toDouble();
}
}
}
JdenticonResponse *response =
new JdenticonResponse(id_, crop, radius, requestedSize);
2020-12-25 17:14:00 +03:00
pool.start(response);
return response;
}
private:
QThreadPool pool;
};