| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#ifndef IMAGEHELPER_H |
| 19 |
#define IMAGEHELPER_H |
| 20 |
|
| 21 |
#include <QObject> |
| 22 |
#include <QtSvg/QSvgRenderer> |
| 23 |
#include <QQmlProperty> |
| 24 |
#include <QQmlPropertyValueSource> |
| 25 |
#include <QQmlParserStatus> |
| 26 |
#include <QColor> |
| 27 |
#include <QUrl> |
| 28 |
#include <QNetworkAccessManager> |
| 29 |
#include <QNetworkRequest> |
| 30 |
#include <QNetworkReply> |
| 31 |
#include <QFuture> |
| 32 |
#include <QFutureWatcher> |
| 33 |
#include <Helpers.h> |
| 34 |
#include <Paths.h> |
| 35 |
|
| 36 |
|
| 37 |
class SvgToPngWorker; |
| 38 |
|
| 39 |
class ImageHelper : public QObject, public QQmlPropertyValueSource, public QQmlParserStatus |
| 40 |
{ |
| 41 |
Q_OBJECT |
| 42 |
Q_INTERFACES(QQmlParserStatus) |
| 43 |
Q_INTERFACES(QQmlPropertyValueSource) |
| 44 |
QML_WRITABLE_PROPERTY_WITH_SIGNAL(int, icon, refresh) |
| 45 |
QML_WRITABLE_PROPERTY_WITH_SIGNAL(int, size, refresh) |
| 46 |
QML_WRITABLE_PROPERTY_WITH_SIGNAL(qreal, verticalRatio, refresh) |
| 47 |
QML_WRITABLE_PROPERTY_WITH_SIGNAL(qreal, horizontalRatio, refresh) |
| 48 |
QML_WRITABLE_PROPERTY_WITH_SIGNAL(QColor, color, refresh) |
| 49 |
QML_WRITABLE_PROPERTY_WITH_SIGNAL(QString, text, refresh) |
| 50 |
QML_WRITABLE_PROPERTY(QString, gravatarEmail) |
| 51 |
Q_PROPERTY(QUrl gravatarUrl READ gravatarUrl WRITE setGravatarUrl NOTIFY gravatarUrlChanged) |
| 52 |
|
| 53 |
public: |
| 54 |
explicit ImageHelper(QObject *parent = 0); |
| 55 |
~ImageHelper(); |
| 56 |
|
| 57 |
static void setBasePath(const QString &basePath); |
| 58 |
static void setCachePath(const QString &cachePath); |
| 59 |
virtual void classBegin(); |
| 60 |
virtual void componentComplete(); |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
virtual void setTarget(const QQmlProperty &target); |
| 70 |
|
| 71 |
QUrl gravatarUrl() const; |
| 72 |
void setGravatarUrl(const QUrl &gravurl); |
| 73 |
signals: |
| 74 |
void refresh(); |
| 75 |
void gravatarUrlChanged(); |
| 76 |
void gravatarImagePath(QUrl path); |
| 77 |
protected slots: |
| 78 |
void handleRefresh(); |
| 79 |
void finished(QNetworkReply *reply); |
| 80 |
void writeUrlToQmlProperty(); |
| 81 |
|
| 82 |
private: |
| 83 |
QNetworkAccessManager *m_nam; |
| 84 |
QQmlProperty m_property; |
| 85 |
static QString s_basePath; |
| 86 |
static QString s_cachePath; |
| 87 |
static QSvgRenderer s_renderer; |
| 88 |
bool m_ready; |
| 89 |
bool m_requestSent; |
| 90 |
bool m_alreadySeen; |
| 91 |
QUrl m_gravatarUrl; |
| 92 |
QFuture<QUrl> m_future; |
| 93 |
QFutureWatcher<QUrl> m_watcher; |
| 94 |
|
| 95 |
}; |
| 96 |
|
| 97 |
class SvgToPngWorker : public QObject |
| 98 |
{ |
| 99 |
Q_OBJECT |
| 100 |
public: |
| 101 |
SvgToPngWorker(QObject *parent, QString &basePath, QString &cachePath); |
| 102 |
|
| 103 |
QUrl convertSvgToPng(const int &size, const qreal &h, const qreal &v, const QColor &color, const QString &icon); |
| 104 |
|
| 105 |
signals: |
| 106 |
void complete(); |
| 107 |
void failed(); |
| 108 |
private: |
| 109 |
QString m_basePath; |
| 110 |
QString m_cachePath; |
| 111 |
}; |
| 112 |
|
| 113 |
class GravatarWorker : public QObject |
| 114 |
{ |
| 115 |
Q_OBJECT |
| 116 |
public: |
| 117 |
explicit GravatarWorker(QObject *parent = 0); |
| 118 |
|
| 119 |
QUrl saveGravatar(const QByteArray &reply, const QString &path); |
| 120 |
}; |
| 121 |
|
| 122 |
#endif |