diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-12-19 11:13:03 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-12-19 12:39:51 +0100 |
commit | abb569c99602febfec6d5822b26d36eb1af4a83f (patch) | |
tree | 811fc73d727f628e7ff16521ffc3766af3b0de4d | |
parent | 1fc4fd0a0c067cb9900c8ebda273d65e0d140fd3 (diff) | |
download | kube-abb569c99602febfec6d5822b26d36eb1af4a83f.tar.gz kube-abb569c99602febfec6d5822b26d36eb1af4a83f.zip |
Set the imageprovider as part of the framework plugin
-rw-r--r-- | applications/kube/main.cpp | 41 | ||||
-rw-r--r-- | framework/src/frameworkplugin.cpp | 47 | ||||
-rw-r--r-- | framework/src/frameworkplugin.h | 3 |
3 files changed, 49 insertions, 42 deletions
diff --git a/applications/kube/main.cpp b/applications/kube/main.cpp index 345d35bf..a7a3354e 100644 --- a/applications/kube/main.cpp +++ b/applications/kube/main.cpp | |||
@@ -36,8 +36,6 @@ | |||
36 | #include <QJsonDocument> | 36 | #include <QJsonDocument> |
37 | #include <QFileInfo> | 37 | #include <QFileInfo> |
38 | 38 | ||
39 | #include <QStandardPaths> | ||
40 | #include <QQuickImageProvider> | ||
41 | #include <QIcon> | 39 | #include <QIcon> |
42 | #include <QtWebEngine> | 40 | #include <QtWebEngine> |
43 | 41 | ||
@@ -133,44 +131,6 @@ void terminateHandler() | |||
133 | std::abort(); | 131 | std::abort(); |
134 | } | 132 | } |
135 | 133 | ||
136 | class KubeImageProvider : public QQuickImageProvider | ||
137 | { | ||
138 | public: | ||
139 | KubeImageProvider() | ||
140 | : QQuickImageProvider(QQuickImageProvider::Pixmap) | ||
141 | { | ||
142 | } | ||
143 | |||
144 | QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) Q_DECL_OVERRIDE | ||
145 | { | ||
146 | //The platform theme plugin can overwrite our setting again once it gets loaded, | ||
147 | //so we check on every icon load request... | ||
148 | if (QIcon::themeName() != "kube") { | ||
149 | QIcon::setThemeName("kube"); | ||
150 | } | ||
151 | const auto icon = QIcon::fromTheme(id); | ||
152 | auto expectedSize = requestedSize; | ||
153 | //Get the largest size that is still smaller or equal than requested | ||
154 | //Except if we only have larger sizes, then just pick the closest one | ||
155 | bool first = true; | ||
156 | for (const auto s : icon.availableSizes()) { | ||
157 | if (first && s.width() > requestedSize.width()) { | ||
158 | expectedSize = s; | ||
159 | break; | ||
160 | } | ||
161 | first = false; | ||
162 | if (s.width() <= requestedSize.width()) { | ||
163 | expectedSize = s; | ||
164 | } | ||
165 | } | ||
166 | const auto pixmap = icon.pixmap(expectedSize); | ||
167 | if (size) { | ||
168 | *size = pixmap.size(); | ||
169 | } | ||
170 | return pixmap; | ||
171 | } | ||
172 | }; | ||
173 | |||
174 | int main(int argc, char *argv[]) | 134 | int main(int argc, char *argv[]) |
175 | { | 135 | { |
176 | std::signal(SIGSEGV, crashHandler); | 136 | std::signal(SIGSEGV, crashHandler); |
@@ -211,7 +171,6 @@ int main(int argc, char *argv[]) | |||
211 | QIcon::setThemeName("kube"); | 171 | QIcon::setThemeName("kube"); |
212 | 172 | ||
213 | QQmlApplicationEngine engine; | 173 | QQmlApplicationEngine engine; |
214 | engine.addImageProvider(QLatin1String("kube"), new KubeImageProvider); | ||
215 | const auto file = "/org/kube/components/kube/main.qml"; | 174 | const auto file = "/org/kube/components/kube/main.qml"; |
216 | const auto mainFile = [&] { | 175 | const auto mainFile = [&] { |
217 | for (const auto &path : engine.importPathList()) { | 176 | for (const auto &path : engine.importPathList()) { |
diff --git a/framework/src/frameworkplugin.cpp b/framework/src/frameworkplugin.cpp index 4aff5708..1de776a5 100644 --- a/framework/src/frameworkplugin.cpp +++ b/framework/src/frameworkplugin.cpp | |||
@@ -42,6 +42,47 @@ | |||
42 | #include "controller.h" | 42 | #include "controller.h" |
43 | 43 | ||
44 | #include <QtQml> | 44 | #include <QtQml> |
45 | #include <QQuickImageProvider> | ||
46 | #include <QIcon> | ||
47 | |||
48 | class KubeImageProvider : public QQuickImageProvider | ||
49 | { | ||
50 | public: | ||
51 | KubeImageProvider() | ||
52 | : QQuickImageProvider(QQuickImageProvider::Pixmap) | ||
53 | { | ||
54 | } | ||
55 | |||
56 | QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) Q_DECL_OVERRIDE | ||
57 | { | ||
58 | //The platform theme plugin can overwrite our setting again once it gets loaded, | ||
59 | //so we check on every icon load request... | ||
60 | if (QIcon::themeName() != "kube") { | ||
61 | QIcon::setThemeName("kube"); | ||
62 | } | ||
63 | const auto icon = QIcon::fromTheme(id); | ||
64 | auto expectedSize = requestedSize; | ||
65 | //Get the largest size that is still smaller or equal than requested | ||
66 | //Except if we only have larger sizes, then just pick the closest one | ||
67 | bool first = true; | ||
68 | for (const auto s : icon.availableSizes()) { | ||
69 | if (first && s.width() > requestedSize.width()) { | ||
70 | expectedSize = s; | ||
71 | break; | ||
72 | } | ||
73 | first = false; | ||
74 | if (s.width() <= requestedSize.width()) { | ||
75 | expectedSize = s; | ||
76 | } | ||
77 | } | ||
78 | const auto pixmap = icon.pixmap(expectedSize); | ||
79 | if (size) { | ||
80 | *size = pixmap.size(); | ||
81 | } | ||
82 | return pixmap; | ||
83 | } | ||
84 | }; | ||
85 | |||
45 | 86 | ||
46 | static QObject *fabric_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine) | 87 | static QObject *fabric_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine) |
47 | { | 88 | { |
@@ -66,6 +107,12 @@ static QObject *keyring_singletontype_provider(QQmlEngine *engine, QJSEngine *sc | |||
66 | return instance; | 107 | return instance; |
67 | } | 108 | } |
68 | 109 | ||
110 | void FrameworkPlugin::initializeEngine(QQmlEngine *engine, const char *uri) | ||
111 | { | ||
112 | Q_UNUSED(uri); | ||
113 | engine->addImageProvider(QLatin1String("kube"), new KubeImageProvider); | ||
114 | } | ||
115 | |||
69 | void FrameworkPlugin::registerTypes (const char *uri) | 116 | void FrameworkPlugin::registerTypes (const char *uri) |
70 | { | 117 | { |
71 | qmlRegisterType<FolderListModel>(uri, 1, 0, "FolderListModel"); | 118 | qmlRegisterType<FolderListModel>(uri, 1, 0, "FolderListModel"); |
diff --git a/framework/src/frameworkplugin.h b/framework/src/frameworkplugin.h index 519e0ba9..d5fd918f 100644 --- a/framework/src/frameworkplugin.h +++ b/framework/src/frameworkplugin.h | |||
@@ -29,5 +29,6 @@ class FrameworkPlugin : public QQmlExtensionPlugin | |||
29 | Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface") | 29 | Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface") |
30 | 30 | ||
31 | public: | 31 | public: |
32 | virtual void registerTypes(const char *uri); | 32 | void registerTypes(const char *uri) Q_DECL_OVERRIDE; |
33 | void initializeEngine(QQmlEngine *engine, const char *uri) Q_DECL_OVERRIDE; | ||
33 | }; | 34 | }; |