| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <QQuickAsyncImageProvider> |
| 8 |
#include <QQuickImageResponse> |
| 9 |
|
| 10 |
#include <QImage> |
| 11 |
|
| 12 |
#include <functional> |
| 13 |
|
| 14 |
namespace mtx::crypto { |
| 15 |
struct EncryptedFile; |
| 16 |
} |
| 17 |
|
| 18 |
class MxcImageRunnable final : public QObject |
| 19 |
{ |
| 20 |
Q_OBJECT |
| 21 |
|
| 22 |
signals: |
| 23 |
void done(QImage image); |
| 24 |
void error(QString error); |
| 25 |
|
| 26 |
public: |
| 27 |
MxcImageRunnable(const QString &id, bool crop, double radius, const QSize &requestedSize) |
| 28 |
: m_id(id) |
| 29 |
, m_requestedSize(requestedSize) |
| 30 |
, m_crop(crop) |
| 31 |
, m_radius(radius) |
| 32 |
{ |
| 33 |
} |
| 34 |
|
| 35 |
void run(); |
| 36 |
|
| 37 |
QString m_id; |
| 38 |
QSize m_requestedSize; |
| 39 |
bool m_crop; |
| 40 |
double m_radius; |
| 41 |
}; |
| 42 |
class MxcImageResponse final : public QQuickImageResponse |
| 43 |
{ |
| 44 |
public: |
| 45 |
MxcImageResponse(const QString &id, bool crop, double radius, const QSize &requestedSize) |
| 46 |
|
| 47 |
{ |
| 48 |
auto runnable = new MxcImageRunnable(id, crop, radius, requestedSize); |
| 49 |
connect(runnable, &MxcImageRunnable::done, this, &MxcImageResponse::handleDone); |
| 50 |
connect(runnable, &MxcImageRunnable::error, this, &MxcImageResponse::handleError); |
| 51 |
runnable->run(); |
| 52 |
} |
| 53 |
|
| 54 |
void handleDone(QImage image) |
| 55 |
{ |
| 56 |
m_image = image; |
| 57 |
emit finished(); |
| 58 |
} |
| 59 |
void handleError(QString error) |
| 60 |
{ |
| 61 |
m_error = error; |
| 62 |
emit finished(); |
| 63 |
} |
| 64 |
|
| 65 |
QQuickTextureFactory *textureFactory() const override |
| 66 |
{ |
| 67 |
return QQuickTextureFactory::textureFactoryForImage(m_image); |
| 68 |
} |
| 69 |
QString errorString() const override { return m_error; } |
| 70 |
|
| 71 |
QString m_error; |
| 72 |
QImage m_image; |
| 73 |
}; |
| 74 |
|
| 75 |
class MxcImageProvider : public QQuickAsyncImageProvider |
| 76 |
{ |
| 77 |
Q_OBJECT |
| 78 |
|
| 79 |
public: |
| 80 |
MxcImageProvider(); |
| 81 |
|
| 82 |
public slots: |
| 83 |
QQuickImageResponse * |
| 84 |
requestImageResponse(const QString &id, const QSize &requestedSize) override; |
| 85 |
|
| 86 |
static void addEncryptionInfo(const mtx::crypto::EncryptedFile &info); |
| 87 |
static void download(const QString &id, |
| 88 |
const QSize &requestedSize, |
| 89 |
std::function<void(QString, QSize, QImage, QString)> then, |
| 90 |
bool crop = true, |
| 91 |
double radius = 0); |
| 92 |
}; |