diff options
author | Nicolas Werner <nicolas.werner@hotmail.de> | 2021-12-21 15:06:34 +0100 |
---|---|---|
committer | Nicolas Werner <nicolas.werner@hotmail.de> | 2021-12-21 15:06:34 +0100 |
commit | 8edc46dc16e2d3a5e5cbbd290a815d7e9d853003 (patch) | |
tree | 89d0074c350d31f9bd3c81212b99c63eb6abda9b | |
parent | 04cccb8283e8950cb1fad12ed15effd5389203c3 (diff) |
Cleanup jdenticon code in the same way as blurhashes
-rw-r--r-- | src/BlurhashProvider.h | 2 | ||||
-rw-r--r-- | src/JdenticonProvider.cpp | 95 | ||||
-rw-r--r-- | src/JdenticonProvider.h | 52 |
3 files changed, 91 insertions, 58 deletions
diff --git a/src/BlurhashProvider.h b/src/BlurhashProvider.h index 21e4ff16..73b95eee 100644 --- a/src/BlurhashProvider.h +++ b/src/BlurhashProvider.h @@ -50,7 +50,7 @@ public: void handleDone(QImage image) { - m_image = image; + m_image = std::move(image); emit finished(); } void handleError(QString error) diff --git a/src/JdenticonProvider.cpp b/src/JdenticonProvider.cpp index e421c932..f0360ad6 100644 --- a/src/JdenticonProvider.cpp +++ b/src/JdenticonProvider.cpp @@ -19,6 +19,35 @@ #include "Utils.h" #include "jdenticoninterface.h" +namespace Jdenticon { +JdenticonInterface * +getJdenticonInterface() +{ + static JdenticonInterface *interface = nullptr; + static bool interfaceExists{true}; + + if (interface == nullptr && interfaceExists) { + QDir pluginsDir(qApp->applicationDirPath()); + + QPluginLoader pluginLoader("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) { @@ -41,30 +70,43 @@ clipRadius(QPixmap img, double radius) JdenticonResponse::JdenticonResponse(const QString &key, bool crop, double radius, + const QSize &requestedSize, + QThreadPool *pool) +{ + auto runnable = new JdenticonRunnable(key, crop, radius, requestedSize); + connect(runnable, &JdenticonRunnable::done, this, &JdenticonResponse::handleDone); + pool->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)) - , m_pixmap{m_requestedSize} - , jdenticonInterface_{Jdenticon::getJdenticonInterface()} -{ - setAutoDelete(false); -} +{} void -JdenticonResponse::run() +JdenticonRunnable::run() { - m_pixmap.fill(Qt::transparent); + QPixmap pixmap(m_requestedSize); + pixmap.fill(Qt::transparent); + + auto jdenticon = Jdenticon::getJdenticonInterface(); + if (!jdenticon) { + emit done(pixmap.toImage()); + return; + } QPainter painter; - painter.begin(&m_pixmap); + painter.begin(&pixmap); painter.setRenderHint(QPainter::Antialiasing, true); painter.setRenderHint(QPainter::SmoothPixmapTransform, true); try { - QSvgRenderer renderer{ - jdenticonInterface_->generate(m_key, m_requestedSize.width()).toUtf8()}; + QSvgRenderer renderer{jdenticon->generate(m_key, m_requestedSize.width()).toUtf8()}; renderer.render(&painter); } catch (std::exception &e) { nhlog::ui()->error( @@ -73,36 +115,13 @@ JdenticonResponse::run() painter.end(); - m_pixmap = clipRadius(m_pixmap, m_radius); + pixmap = clipRadius(pixmap, m_radius); - emit finished(); + emit done(pixmap.toImage()); } -namespace Jdenticon { -JdenticonInterface * -getJdenticonInterface() +bool +JdenticonProvider::isAvailable() { - static JdenticonInterface *interface = nullptr; - static bool interfaceExists{true}; - - if (interface == nullptr && interfaceExists) { - QDir pluginsDir(qApp->applicationDirPath()); - - QPluginLoader pluginLoader("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; -} + return Jdenticon::getJdenticonInterface() != nullptr; } diff --git a/src/JdenticonProvider.h b/src/JdenticonProvider.h index 95f3db78..b387b3c1 100644 --- a/src/JdenticonProvider.h +++ b/src/JdenticonProvider.h @@ -13,31 +13,47 @@ #include "jdenticoninterface.h" -namespace Jdenticon { -JdenticonInterface * -getJdenticonInterface(); -} - -class JdenticonResponse - : public QQuickImageResponse +class JdenticonRunnable + : public QObject , public QRunnable { + Q_OBJECT public: - JdenticonResponse(const QString &key, bool crop, double radius, const QSize &requestedSize); - - QQuickTextureFactory *textureFactory() const override - { - return QQuickTextureFactory::textureFactoryForImage(m_pixmap.toImage()); - } + JdenticonRunnable(const QString &key, bool crop, double radius, const QSize &requestedSize); void run() override; +signals: + void done(QImage img); + +private: QString m_key; bool m_crop; double m_radius; QSize m_requestedSize; - QPixmap m_pixmap; - JdenticonInterface *jdenticonInterface_ = nullptr; +}; + +class JdenticonResponse : public QQuickImageResponse +{ +public: + JdenticonResponse(const QString &key, + bool crop, + double radius, + const QSize &requestedSize, + QThreadPool *pool); + + QQuickTextureFactory *textureFactory() const override + { + return QQuickTextureFactory::textureFactoryForImage(m_pixmap); + } + + void handleDone(QImage img) + { + m_pixmap = std::move(img); + emit finished(); + } + + QImage m_pixmap; }; class JdenticonProvider @@ -47,7 +63,7 @@ class JdenticonProvider Q_OBJECT public: - static bool isAvailable() { return Jdenticon::getJdenticonInterface() != nullptr; } + static bool isAvailable(); public slots: QQuickImageResponse * @@ -70,9 +86,7 @@ public slots: } } - JdenticonResponse *response = new JdenticonResponse(id_, crop, radius, requestedSize); - pool.start(response); - return response; + return new JdenticonResponse(id_, crop, radius, requestedSize, &pool); } private: |