From abb569c99602febfec6d5822b26d36eb1af4a83f Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Tue, 19 Dec 2017 11:13:03 +0100 Subject: Set the imageprovider as part of the framework plugin --- applications/kube/main.cpp | 41 ---------------------------------- framework/src/frameworkplugin.cpp | 47 +++++++++++++++++++++++++++++++++++++++ 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 @@ #include #include -#include -#include #include #include @@ -133,44 +131,6 @@ void terminateHandler() std::abort(); } -class KubeImageProvider : public QQuickImageProvider -{ -public: - KubeImageProvider() - : QQuickImageProvider(QQuickImageProvider::Pixmap) - { - } - - QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) Q_DECL_OVERRIDE - { - //The platform theme plugin can overwrite our setting again once it gets loaded, - //so we check on every icon load request... - if (QIcon::themeName() != "kube") { - QIcon::setThemeName("kube"); - } - const auto icon = QIcon::fromTheme(id); - auto expectedSize = requestedSize; - //Get the largest size that is still smaller or equal than requested - //Except if we only have larger sizes, then just pick the closest one - bool first = true; - for (const auto s : icon.availableSizes()) { - if (first && s.width() > requestedSize.width()) { - expectedSize = s; - break; - } - first = false; - if (s.width() <= requestedSize.width()) { - expectedSize = s; - } - } - const auto pixmap = icon.pixmap(expectedSize); - if (size) { - *size = pixmap.size(); - } - return pixmap; - } -}; - int main(int argc, char *argv[]) { std::signal(SIGSEGV, crashHandler); @@ -211,7 +171,6 @@ int main(int argc, char *argv[]) QIcon::setThemeName("kube"); QQmlApplicationEngine engine; - engine.addImageProvider(QLatin1String("kube"), new KubeImageProvider); const auto file = "/org/kube/components/kube/main.qml"; const auto mainFile = [&] { 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 @@ #include "controller.h" #include +#include +#include + +class KubeImageProvider : public QQuickImageProvider +{ +public: + KubeImageProvider() + : QQuickImageProvider(QQuickImageProvider::Pixmap) + { + } + + QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) Q_DECL_OVERRIDE + { + //The platform theme plugin can overwrite our setting again once it gets loaded, + //so we check on every icon load request... + if (QIcon::themeName() != "kube") { + QIcon::setThemeName("kube"); + } + const auto icon = QIcon::fromTheme(id); + auto expectedSize = requestedSize; + //Get the largest size that is still smaller or equal than requested + //Except if we only have larger sizes, then just pick the closest one + bool first = true; + for (const auto s : icon.availableSizes()) { + if (first && s.width() > requestedSize.width()) { + expectedSize = s; + break; + } + first = false; + if (s.width() <= requestedSize.width()) { + expectedSize = s; + } + } + const auto pixmap = icon.pixmap(expectedSize); + if (size) { + *size = pixmap.size(); + } + return pixmap; + } +}; + static QObject *fabric_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine) { @@ -66,6 +107,12 @@ static QObject *keyring_singletontype_provider(QQmlEngine *engine, QJSEngine *sc return instance; } +void FrameworkPlugin::initializeEngine(QQmlEngine *engine, const char *uri) +{ + Q_UNUSED(uri); + engine->addImageProvider(QLatin1String("kube"), new KubeImageProvider); +} + void FrameworkPlugin::registerTypes (const char *uri) { qmlRegisterType(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 Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface") public: - virtual void registerTypes(const char *uri); + void registerTypes(const char *uri) Q_DECL_OVERRIDE; + void initializeEngine(QQmlEngine *engine, const char *uri) Q_DECL_OVERRIDE; }; -- cgit v1.2.3