| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <QImage> |
| 8 |
#include <QQuickAsyncImageProvider> |
| 9 |
#include <QQuickImageResponse> |
| 10 |
#include <QThreadPool> |
| 11 |
|
| 12 |
class JdenticonRunnable final |
| 13 |
: public QObject |
| 14 |
, public QRunnable |
| 15 |
{ |
| 16 |
Q_OBJECT |
| 17 |
public: |
| 18 |
JdenticonRunnable(const QString &key, double radius, const QSize &requestedSize); |
| 19 |
|
| 20 |
void run() override; |
| 21 |
|
| 22 |
signals: |
| 23 |
void done(QImage img); |
| 24 |
|
| 25 |
private: |
| 26 |
QString m_key; |
| 27 |
double m_radius; |
| 28 |
QSize m_requestedSize; |
| 29 |
}; |
| 30 |
|
| 31 |
class JdenticonResponse final : public QQuickImageResponse |
| 32 |
{ |
| 33 |
public: |
| 34 |
JdenticonResponse(const QString &key, double radius, const QSize &requestedSize); |
| 35 |
|
| 36 |
QQuickTextureFactory *textureFactory() const override |
| 37 |
{ |
| 38 |
return QQuickTextureFactory::textureFactoryForImage(m_pixmap); |
| 39 |
} |
| 40 |
|
| 41 |
void handleDone(QImage img) |
| 42 |
{ |
| 43 |
m_pixmap = std::move(img); |
| 44 |
emit finished(); |
| 45 |
} |
| 46 |
|
| 47 |
QImage m_pixmap; |
| 48 |
}; |
| 49 |
|
| 50 |
class JdenticonProvider : public QQuickAsyncImageProvider |
| 51 |
{ |
| 52 |
Q_OBJECT |
| 53 |
|
| 54 |
public: |
| 55 |
static bool isAvailable(); |
| 56 |
|
| 57 |
public slots: |
| 58 |
QQuickImageResponse * |
| 59 |
requestImageResponse(const QString &id, const QSize &requestedSize) override |
| 60 |
{ |
| 61 |
auto id_ = id; |
| 62 |
double radius = 0; |
| 63 |
|
| 64 |
auto queryStart = id.lastIndexOf('?'); |
| 65 |
if (queryStart != -1) { |
| 66 |
id_ = id.left(queryStart); |
| 67 |
auto query = id.mid(queryStart + 1); |
| 68 |
auto queryBits = QStringView(query).split('&'); |
| 69 |
|
| 70 |
for (const auto &b : std::as_const(queryBits)) { |
| 71 |
if (b.startsWith(QStringView(u"radius="))) { |
| 72 |
radius = b.mid(7).toDouble(); |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
return new JdenticonResponse(id_, radius, requestedSize); |
| 78 |
} |
| 79 |
}; |