nheko/src/JdenticonProvider.cpp

109 lines
2.7 KiB
C++
Raw Normal View History

2021-08-13 02:49:25 +02:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
2020-12-25 15:14:00 +01:00
#include "JdenticonProvider.h"
#include <QApplication>
#include <QDir>
#include <QPainter>
2021-08-14 02:05:51 +02:00
#include <QPainterPath>
2020-12-25 15:14:00 +01: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"
2021-08-31 02:19:17 +02:00
static QPixmap
clipRadius(QPixmap img, double radius)
{
2021-09-18 00:22:33 +02:00
QPixmap out(img.size());
out.fill(Qt::transparent);
2021-08-31 02:19:17 +02:00
2021-09-18 00:22:33 +02:00
QPainter painter(&out);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
2021-08-31 02:19:17 +02:00
2021-09-18 00:22:33 +02:00
QPainterPath ppath;
ppath.addRoundedRect(img.rect(), radius, radius, Qt::SizeMode::RelativeSize);
2021-08-31 02:19:17 +02:00
2021-09-18 00:22:33 +02:00
painter.setClipPath(ppath);
painter.drawPixmap(img.rect(), img);
2021-08-31 02:19:17 +02:00
2021-09-18 00:22:33 +02:00
return out;
2021-08-31 02:19:17 +02:00
}
2021-08-14 02:05:51 +02:00
JdenticonResponse::JdenticonResponse(const QString &key,
bool crop,
double radius,
const QSize &requestedSize)
2020-12-25 15:14:00 +01:00
: m_key(key)
2021-08-14 02:05:51 +02:00
, m_crop{crop}
, m_radius{radius}
2020-12-25 15:14:00 +01:00
, m_requestedSize(requestedSize.isValid() ? requestedSize : QSize(100, 100))
, m_pixmap{m_requestedSize}
, jdenticonInterface_{Jdenticon::getJdenticonInterface()}
{
2021-09-18 00:22:33 +02:00
setAutoDelete(false);
2020-12-25 15:14:00 +01:00
}
void
JdenticonResponse::run()
{
2021-09-18 00:22:33 +02:00
m_pixmap.fill(Qt::transparent);
QPainter painter;
painter.begin(&m_pixmap);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
try {
QSvgRenderer renderer{
jdenticonInterface_->generate(m_key, m_requestedSize.width()).toUtf8()};
renderer.render(&painter);
} catch (std::exception &e) {
nhlog::ui()->error(
"caught {} in jdenticonprovider, key '{}'", e.what(), m_key.toStdString());
}
2020-12-25 15:14:00 +01:00
2021-09-18 00:22:33 +02:00
painter.end();
2021-08-31 02:19:17 +02:00
2021-09-18 00:22:33 +02:00
m_pixmap = clipRadius(m_pixmap, m_radius);
2021-08-31 02:19:17 +02:00
2021-09-18 00:22:33 +02:00
emit finished();
2020-12-25 15:14:00 +01:00
}
namespace Jdenticon {
JdenticonInterface *
getJdenticonInterface()
{
2021-09-18 00:22:33 +02:00
static JdenticonInterface *interface = nullptr;
static bool interfaceExists{true};
if (interface == nullptr && interfaceExists) {
QDir pluginsDir(qApp->applicationDirPath());
2021-11-15 01:23:15 +01:00
QPluginLoader pluginLoader("qtjdenticon");
QObject *plugin = pluginLoader.instance();
if (plugin) {
interface = qobject_cast<JdenticonInterface *>(plugin);
if (interface) {
nhlog::ui()->info("Loaded jdenticon plugin.");
2021-09-18 00:22:33 +02:00
}
2021-11-15 01:23:15 +01:00
}
if (!interface) {
2021-09-18 00:22:33 +02:00
nhlog::ui()->info("jdenticon plugin not found.");
interfaceExists = false;
2020-12-25 15:14:00 +01:00
}
2021-09-18 00:22:33 +02:00
}
2020-12-25 15:14:00 +01:00
2021-09-18 00:22:33 +02:00
return interface;
2020-12-25 15:14:00 +01:00
}
}