From fc0841fa9477f13f9b615db96b19891ad8999610 Mon Sep 17 00:00:00 2001 From: SunRain <41245110@qq.com> Date: Thu, 12 May 2016 22:05:12 +0800 Subject: [PATCH 1/3] add iconhelper plugin to support icons both in system-wide installation and qrc --- demo/demo.qrc | 4 +- demo/icons/icons.qrc | 20 ++++---- demo/main.qml | 11 +++++ src/core/Icon.qml | 2 +- src/core/iconhelper.cpp | 78 ++++++++++++++++++++++++++++++++ src/core/iconhelper.h | 43 ++++++++++++++++++ src/plugin.cpp | 2 + src/src.pro | 9 ++-- src/window/ApplicationWindow.qml | 20 ++++++++ 9 files changed, 173 insertions(+), 16 deletions(-) create mode 100644 src/core/iconhelper.cpp create mode 100644 src/core/iconhelper.h diff --git a/demo/demo.qrc b/demo/demo.qrc index 69946138..f10ae10b 100644 --- a/demo/demo.qrc +++ b/demo/demo.qrc @@ -21,7 +21,7 @@ TextFieldDemo.qml TimePickerDemo.qml TypographyDemo.qml - icons/action_account_circle.svg + images/balloon.jpg images/go-last.color.svg images/list-add.color.svg diff --git a/demo/icons/icons.qrc b/demo/icons/icons.qrc index 09383aa2..cc85d4ff 100644 --- a/demo/icons/icons.qrc +++ b/demo/icons/icons.qrc @@ -1,42 +1,42 @@ - + maps_place.svg - + communication_email.svg - + image_color_lens.svg image_edit.svg - + alert_warning.svg - + content_add.svg content_create.svg content_forward.svg - + device_access_alarm.svg - + file_file_download.svg - + social_share.svg - + action_account_circle.svg action_autorenew.svg action_delete.svg @@ -44,7 +44,7 @@ action_settings.svg - + navigation_arrow_drop_down.svg diff --git a/demo/main.qml b/demo/main.qml index f628cb8c..80e479f4 100644 --- a/demo/main.qml +++ b/demo/main.qml @@ -16,6 +16,17 @@ ApplicationWindow { tabHighlightColor: "white" } + iconHelper { + useQtResource: true + /************************** + if set useQtResource to true, alternativePath should be the prefix value in qrc + if set useQtResource to false + if using material system-wide icons, alternativePath should be setted to empty, Eg. "" + if using application alternative icons, alternativePath should be setted to full path of icon path + ***************************/ + alternativePath: "demoicons" + } + property var styles: [ "Custom Icons", "Color Palette", "Typography" ] diff --git a/src/core/Icon.qml b/src/core/Icon.qml index d73a6705..fa8bca63 100644 --- a/src/core/Icon.qml +++ b/src/core/Icon.qml @@ -64,7 +64,7 @@ Item { var name = icon.source.substring(7) if (name) - return "qrc:/icons/" + name + '.svg' + return iconHelper.parseIcon(name) + '.svg' //"qrc:/icons/" + name + '.svg' else return "" } else { diff --git a/src/core/iconhelper.cpp b/src/core/iconhelper.cpp new file mode 100644 index 00000000..01e012b5 --- /dev/null +++ b/src/core/iconhelper.cpp @@ -0,0 +1,78 @@ +#include "iconhelper.h" + +#include +#include +#include + +#include +#include + +IconHelper::IconHelper(QObject *parent) + : QObject(parent) + , m_useQtResource(false) + , m_alternativePath(QString()) + , m_applicationPath(qApp->applicationDirPath()) +{ +#ifdef MATERIAL_ICON_PATH + m_libraryPath = QString(MATERIAL_ICON_PATH); +#else + qWarning()< + +class QQmlEngine; +class QJSEngine; +class IconHelper : public QObject +{ + Q_OBJECT + + Q_PROPERTY(bool useQtResource READ useQtResource WRITE setUseQtResource) + Q_PROPERTY(QString alternativePath READ alternativePath WRITE setAlternativePath) + Q_PROPERTY(QString applicationPath READ applicationPath CONSTANT) + +public: + explicit IconHelper(QObject *parent = 0); + virtual ~IconHelper(); + + /// + /// \brief parseIcon + /// \param icon the iconname + /// \return + /// + Q_INVOKABLE QString parseIcon(const QString &icon); + + bool useQtResource() const; + QString alternativePath() const; + QString applicationPath() const; + +public slots: + void setUseQtResource(bool useQtResource); + void setAlternativePath(QString alternativePath); + +private: + bool m_useQtResource; + QString m_alternativePath; + QString m_applicationPath; + QString m_libraryPath; +}; + + +#endif // ICONHELPER_H diff --git a/src/plugin.cpp b/src/plugin.cpp index 76c5cb87..07102db7 100644 --- a/src/plugin.cpp +++ b/src/plugin.cpp @@ -14,12 +14,14 @@ #include "core/device.h" #include "core/units.h" +#include "core/iconhelper.h" class MaterialRegisterHelper { public: MaterialRegisterHelper(const char *uri) { qmlRegisterSingletonType(uri, 0, 1, "Device", Device::qmlSingleton); + qmlRegisterType(uri, 0, 1, "IconHelper"/*, IconHelper::qmlSingleton*/); qmlRegisterUncreatableType(uri, 0, 3, "Units", QStringLiteral("Units can only be used via the attached property.")); } }; diff --git a/src/src.pro b/src/src.pro index 6d482d04..0d9f9655 100644 --- a/src/src.pro +++ b/src/src.pro @@ -4,6 +4,7 @@ TARGET = material CONFIG += c++11 QT += qml quick +DEFINES += MATERIAL_ICON_PATH=\\\"$$[QT_INSTALL_QML]/Material/icons\\\" android { QT += androidextras svg xml @@ -11,13 +12,15 @@ android { HEADERS += plugin.h \ core/device.h \ - core/units.h + core/units.h \ + core/iconhelper.h SOURCES += plugin.cpp \ core/device.cpp \ - core/units.cpp + core/units.cpp \ + core/iconhelper.cpp -RESOURCES += ../icons/core_icons.qrc +#RESOURCES += ../icons/core_icons.qrc target.path = $$[QT_INSTALL_QML]/Material diff --git a/src/window/ApplicationWindow.qml b/src/window/ApplicationWindow.qml index e4a21712..a6f89622 100644 --- a/src/window/ApplicationWindow.qml +++ b/src/window/ApplicationWindow.qml @@ -77,6 +77,26 @@ Controls.ApplicationWindow { */ property alias theme: __theme + /*! + \qmlproperty IconHelper iconHelper + + IconHelper is a c++ plugin that allows the application to use material icons wether in qrc or + in local-filesystem. + */ + property alias iconHelper: __iconHelperPriv + + IconHelper { + id: __iconHelperPriv + /************************** + if set useQtResource to true, alternativePath should be the prefix value in qrc + if set useQtResource to false + if using material system-wide icons, alternativePath should be setted to empty, Eg. "" + if using application alternative icons, alternativePath should be setted to full path of icon path + ***************************/ + useQtResource: false + alternativePath: "" + } + AppTheme { id: __theme } From 3661f137eeeae424eb85eea8c7e97ca0cd7c6001 Mon Sep 17 00:00:00 2001 From: SunRain <41245110@qq.com> Date: Fri, 17 Jun 2016 18:01:21 +0800 Subject: [PATCH 2/3] fix iconhelper not included in material.pri --- material.pri | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/material.pri b/material.pri index 4538f4c6..9adebb18 100644 --- a/material.pri +++ b/material.pri @@ -7,11 +7,13 @@ android { HEADERS += $$PWD/src/plugin.h \ $$PWD/src/core/device.h \ - $$PWD/src/core/units.h + $$PWD/src/core/units.h \ + $$PWD/src/core/iconhelper.h SOURCES += $$PWD/src/plugin.cpp \ $$PWD/src/core/device.cpp \ - $$PWD/src/core/units.cpp + $$PWD/src/core/units.cpp \ + $$PWD/src/core/iconhelper.cpp RESOURCES += $$PWD/src/material.qrc \ $$PWD/src/components/components.qrc \ From f08e9d7407d0e79932a3b4be423f62c4a75ae5f5 Mon Sep 17 00:00:00 2001 From: SunRain <41245110@qq.com> Date: Fri, 17 Jun 2016 18:01:53 +0800 Subject: [PATCH 3/3] demo apps should use build-in method --- demo/demo.pro | 5 +++++ demo/main.cpp | 4 +++- demo/main.qml | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/demo/demo.pro b/demo/demo.pro index dae52407..fe980842 100644 --- a/demo/demo.pro +++ b/demo/demo.pro @@ -2,5 +2,10 @@ TEMPLATE = app QT += qml quick +DEFINES += QPM_INIT +OPTIONS += roboto + +include(../material.pri) + SOURCES += main.cpp RESOURCES += demo.qrc icons/icons.qrc diff --git a/demo/main.cpp b/demo/main.cpp index d1e6dc06..92e33deb 100644 --- a/demo/main.cpp +++ b/demo/main.cpp @@ -5,7 +5,9 @@ int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); - QQmlApplicationEngine engine(QUrl(QStringLiteral("qrc:/main.qml"))); + QQmlApplicationEngine engine; + engine.addImportPath("qrc:///"); + engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); return app.exec(); } diff --git a/demo/main.qml b/demo/main.qml index 80e479f4..644acd86 100644 --- a/demo/main.qml +++ b/demo/main.qml @@ -1,5 +1,5 @@ import QtQuick 2.4 -import Material 0.2 +import Material 0.3 import Material.ListItems 0.1 as ListItem ApplicationWindow {