Debian Codegraph @

Dekko/components/ImageHelper.h

raw symbols SCIP
1 /* Copyright (C) 2016 - 2017 Dan Chapman <[email protected]>
2
3 This file is part of Dekko email client for Ubuntu devices
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of
8 the License or (at your option) version 3
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
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 //This is where the magic happens in qml. When we
62 // do something like
63 // Image {
64 // height: 10
65 // width: 10
66 // ImageHelper on source {
67 // }
68 // this property will get set to the source property of the image
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 // IMAGEHELPER_H