matrixion/src/JdenticonProvider.cpp

125 lines
3.3 KiB
C++
Raw Normal View History

2021-08-13 03:49:25 +03:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
// SPDX-FileCopyrightText: 2022 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
#include "JdenticonProvider.h"
#include <QApplication>
#include <QPainter>
2021-08-14 03:05:51 +03:00
#include <QPainterPath>
2020-12-25 17:14:00 +03:00
#include <QPluginLoader>
#include <QSvgRenderer>
#include <mtxclient/crypto/client.hpp>
#include "Cache.h"
#include "Logging.h"
#include "MatrixClient.h"
#include "Utils.h"
#include "jdenticoninterface.h"
namespace Jdenticon {
JdenticonInterface *
getJdenticonInterface()
{
static JdenticonInterface *interface = nullptr;
static bool interfaceExists{true};
if (interface == nullptr && interfaceExists) {
QPluginLoader pluginLoader(QStringLiteral("qtjdenticon"));
QObject *plugin = pluginLoader.instance();
if (plugin) {
interface = qobject_cast<JdenticonInterface *>(plugin);
if (interface) {
nhlog::ui()->info("Loaded jdenticon plugin.");
}
}
if (!interface) {
nhlog::ui()->info("jdenticon plugin not found.");
interfaceExists = false;
}
}
return interface;
}
}
2021-08-31 03:19:17 +03:00
static QPixmap
clipRadius(QPixmap img, double radius)
{
2021-09-18 01:22:33 +03:00
QPixmap out(img.size());
out.fill(Qt::transparent);
2021-08-31 03:19:17 +03:00
2021-09-18 01:22:33 +03:00
QPainter painter(&out);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
2021-08-31 03:19:17 +03:00
2021-09-18 01:22:33 +03:00
QPainterPath ppath;
ppath.addRoundedRect(img.rect(), radius, radius, Qt::SizeMode::RelativeSize);
2021-08-31 03:19:17 +03:00
2021-09-18 01:22:33 +03:00
painter.setClipPath(ppath);
painter.drawPixmap(img.rect(), img);
2021-08-31 03:19:17 +03:00
2021-09-18 01:22:33 +03:00
return out;
2021-08-31 03:19:17 +03:00
}
2021-08-14 03:05:51 +03:00
JdenticonResponse::JdenticonResponse(const QString &key,
bool crop,
double radius,
2021-12-21 17:57:46 +03:00
const QSize &requestedSize)
{
auto runnable = new JdenticonRunnable(key, crop, radius, requestedSize);
connect(runnable, &JdenticonRunnable::done, this, &JdenticonResponse::handleDone);
2021-12-21 17:57:46 +03:00
QThreadPool::globalInstance()->start(runnable);
}
JdenticonRunnable::JdenticonRunnable(const QString &key,
2021-08-14 03:05:51 +03:00
bool crop,
double radius,
const QSize &requestedSize)
2020-12-25 17:14:00 +03:00
: m_key(key)
2021-08-14 03:05:51 +03:00
, m_crop{crop}
, m_radius{radius}
2020-12-25 17:14:00 +03:00
, m_requestedSize(requestedSize.isValid() ? requestedSize : QSize(100, 100))
2022-05-10 04:53:35 +03:00
{}
2020-12-25 17:14:00 +03:00
void
JdenticonRunnable::run()
2020-12-25 17:14:00 +03:00
{
QPixmap pixmap(m_requestedSize);
pixmap.fill(Qt::transparent);
auto jdenticon = Jdenticon::getJdenticonInterface();
if (!jdenticon) {
emit done(pixmap.toImage());
return;
}
2021-09-18 01:22:33 +03:00
QPainter painter;
painter.begin(&pixmap);
2021-09-18 01:22:33 +03:00
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
try {
QSvgRenderer renderer{jdenticon->generate(m_key, m_requestedSize.width()).toUtf8()};
2021-09-18 01:22:33 +03:00
renderer.render(&painter);
} catch (std::exception &e) {
nhlog::ui()->error(
"caught {} in jdenticonprovider, key '{}'", e.what(), m_key.toStdString());
}
2020-12-25 17:14:00 +03:00
2021-09-18 01:22:33 +03:00
painter.end();
2021-08-31 03:19:17 +03:00
pixmap = clipRadius(pixmap, m_radius);
2021-08-31 03:19:17 +03:00
emit done(pixmap.toImage());
2020-12-25 17:14:00 +03:00
}
bool
JdenticonProvider::isAvailable()
2020-12-25 17:14:00 +03:00
{
return Jdenticon::getJdenticonInterface() != nullptr;
2020-12-25 17:14:00 +03:00
}