1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
// SPDX-FileCopyrightText: Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "JdenticonProvider.h"
#include <QApplication>
#include <QPainter>
#include <QPainterPath>
#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;
}
}
static QPixmap
clipRadius(QPixmap img, double radius)
{
QPixmap out(img.size());
out.fill(Qt::transparent);
QPainter painter(&out);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
QPainterPath ppath;
ppath.addRoundedRect(img.rect(), radius, radius, Qt::SizeMode::RelativeSize);
painter.setClipPath(ppath);
painter.drawPixmap(img.rect(), img);
return out;
}
JdenticonResponse::JdenticonResponse(const QString &key,
bool crop,
double radius,
const QSize &requestedSize)
{
auto runnable = new JdenticonRunnable(key, crop, radius, requestedSize);
connect(runnable, &JdenticonRunnable::done, this, &JdenticonResponse::handleDone);
QThreadPool::globalInstance()->start(runnable);
}
JdenticonRunnable::JdenticonRunnable(const QString &key,
bool crop,
double radius,
const QSize &requestedSize)
: m_key(key)
, m_crop{crop}
, m_radius{radius}
, m_requestedSize(requestedSize.isValid() ? requestedSize : QSize(100, 100))
{
}
void
JdenticonRunnable::run()
{
QPixmap pixmap(m_requestedSize);
pixmap.fill(Qt::transparent);
auto jdenticon = Jdenticon::getJdenticonInterface();
if (!jdenticon) {
emit done(pixmap.toImage());
return;
}
QPainter painter;
painter.begin(&pixmap);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
try {
QSvgRenderer renderer{
jdenticon->generate(m_key, static_cast<std::uint16_t>(m_requestedSize.width())).toUtf8()};
renderer.render(&painter);
} catch (std::exception &e) {
nhlog::ui()->error(
"caught {} in jdenticonprovider, key '{}'", e.what(), m_key.toStdString());
}
painter.end();
pixmap = clipRadius(pixmap, m_radius);
emit done(pixmap.toImage());
}
bool
JdenticonProvider::isAvailable()
{
return Jdenticon::getJdenticonInterface() != nullptr;
}
|