Add some fancy effects to jdenticon

This commit is contained in:
Loren Burkholder 2021-08-13 20:05:51 -04:00
parent 1fdecdcc21
commit 350d1977af
3 changed files with 53 additions and 7 deletions

View file

@ -43,7 +43,19 @@ Rectangle {
id: identicon
anchors.fill: parent
visible: img.status != Image.Ready && Settings.useIdenticon
source: Settings.useIdenticon ? "image://jdenticon/" + userid : ""
source: Settings.useIdenticon ? "image://jdenticon/" + userid + "?radius=" + radius : ""
layer.enabled: true
MouseArea {
anchors.fill: parent
Ripple {
rippleTarget: parent
color: Qt.rgba(Nheko.colors.alternateBase.r, Nheko.colors.alternateBase.g, Nheko.colors.alternateBase.b, 0.5)
}
}
}
Image {

View file

@ -7,6 +7,7 @@
#include <QApplication>
#include <QDir>
#include <QPainter>
#include <QPainterPath>
#include <QPluginLoader>
#include <QSvgRenderer>
@ -18,8 +19,13 @@
#include "Utils.h"
#include "jdenticoninterface.h"
JdenticonResponse::JdenticonResponse(const QString &key, const QSize &requestedSize)
JdenticonResponse::JdenticonResponse(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))
, m_pixmap{m_requestedSize}
, jdenticonInterface_{Jdenticon::getJdenticonInterface()}
@ -32,10 +38,16 @@ JdenticonResponse::run()
{
m_pixmap.fill(Qt::transparent);
QPainter painter{&m_pixmap};
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
QPainterPath ppath;
ppath.addRoundedRect(m_pixmap.rect(), m_radius, m_radius);
painter.setClipPath(ppath);
QSvgRenderer renderer{
jdenticonInterface_->generate(m_key, m_requestedSize.width()).toUtf8()};
// m_image = QImage::fromData(jdenticonInterface_->generate(m_key,
// size->width()).toUtf8());
renderer.render(&painter);
emit finished();

View file

@ -23,7 +23,7 @@ class JdenticonResponse
, public QRunnable
{
public:
JdenticonResponse(const QString &key, const QSize &requestedSize);
JdenticonResponse(const QString &key, bool crop, double radius, const QSize &requestedSize);
QQuickTextureFactory *textureFactory() const override
{
@ -33,6 +33,8 @@ public:
void run() override;
QString m_key;
bool m_crop;
double m_radius;
QSize m_requestedSize;
QPixmap m_pixmap;
JdenticonInterface *jdenticonInterface_ = nullptr;
@ -48,10 +50,30 @@ public:
static bool isAvailable() { return Jdenticon::getJdenticonInterface() != nullptr; }
public slots:
QQuickImageResponse *requestImageResponse(const QString &key,
QQuickImageResponse *requestImageResponse(const QString &id,
const QSize &requestedSize) override
{
JdenticonResponse *response = new JdenticonResponse(key, requestedSize);
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);
pool.start(response);
return response;
}