From a19f23435fc140224dd62e964e975130ff0e4f9d Mon Sep 17 00:00:00 2001 From: Anmol Gautam Date: Sat, 16 Jun 2018 11:37:46 +0530 Subject: [PATCH] enable loading icons from resource in qml --- .../qml/api/browseraction/qmlbrowseraction.cpp | 5 +++-- .../qml/api/notifications/qmlnotifications.cpp | 7 ++++--- src/lib/plugins/qml/api/sidebar/qmlsidebar.cpp | 4 +++- src/lib/tools/qztools.cpp | 13 +++++++++++++ src/lib/tools/qztools.h | 1 + 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/lib/plugins/qml/api/browseraction/qmlbrowseraction.cpp b/src/lib/plugins/qml/api/browseraction/qmlbrowseraction.cpp index afcf58423..38d267120 100644 --- a/src/lib/plugins/qml/api/browseraction/qmlbrowseraction.cpp +++ b/src/lib/plugins/qml/api/browseraction/qmlbrowseraction.cpp @@ -17,6 +17,7 @@ * ============================================================ */ #include "qmlbrowseraction.h" #include "mainapplication.h" +#include "qztools.h" #include QmlBrowserAction::QmlBrowserAction(QObject *parent) @@ -54,8 +55,8 @@ QString QmlBrowserAction::iconUrl() const void QmlBrowserAction::setIconUrl(const QString &iconUrl) { m_iconUrl = iconUrl; - QString fileName = QUrl(m_iconUrl.toUtf8()).toLocalFile(); - setIcon(QIcon(fileName)); + QString iconPath = QzTools::getPathFromUrl(QUrl(m_iconUrl)); + setIcon(QIcon(iconPath)); } QQmlComponent* QmlBrowserAction::popup() const diff --git a/src/lib/plugins/qml/api/notifications/qmlnotifications.cpp b/src/lib/plugins/qml/api/notifications/qmlnotifications.cpp index af74a90cf..0de5fdece 100644 --- a/src/lib/plugins/qml/api/notifications/qmlnotifications.cpp +++ b/src/lib/plugins/qml/api/notifications/qmlnotifications.cpp @@ -18,6 +18,7 @@ #include "qmlnotifications.h" #include "mainapplication.h" #include "desktopnotificationsfactory.h" +#include "qztools.h" QmlNotifications::QmlNotifications(QObject *parent) : QObject(parent) @@ -36,9 +37,9 @@ QmlNotifications::QmlNotifications(QObject *parent) */ void QmlNotifications::create(const QVariantMap &map) { - const QString fileUrl = map.value(QSL("icon")).toString(); - const QString fileName = QUrl::fromEncoded(fileUrl.toUtf8()).toLocalFile(); - const QPixmap icon(fileName); + const QString iconUrl = map.value(QSL("icon")).toString(); + const QString iconPath = QzTools::getPathFromUrl(QUrl(iconUrl)); + const QPixmap icon(iconPath); const QString heading = map.value(QSL("heading")).toString(); const QString message = map.value(QSL("message")).toString(); mApp->desktopNotifications()->showNotification(icon, heading, message); diff --git a/src/lib/plugins/qml/api/sidebar/qmlsidebar.cpp b/src/lib/plugins/qml/api/sidebar/qmlsidebar.cpp index 6e2f180ec..e1b082470 100644 --- a/src/lib/plugins/qml/api/sidebar/qmlsidebar.cpp +++ b/src/lib/plugins/qml/api/sidebar/qmlsidebar.cpp @@ -17,6 +17,7 @@ * ============================================================ */ #include "qmlsidebar.h" #include "mainapplication.h" +#include "qztools.h" #include #include @@ -92,7 +93,8 @@ QAction *QmlSideBar::createMenuAction() action->setText(m_title); action->setCheckable(m_checkable); action->setShortcut(QKeySequence(m_shortcut)); - action->setIcon(QIcon(QUrl(m_iconUrl).toLocalFile())); + QString iconPath = QzTools::getPathFromUrl(QUrl(m_iconUrl)); + action->setIcon(QIcon(iconPath)); return action; } diff --git a/src/lib/tools/qztools.cpp b/src/lib/tools/qztools.cpp index 5d17e3e4b..dd5d2c79a 100644 --- a/src/lib/tools/qztools.cpp +++ b/src/lib/tools/qztools.cpp @@ -1018,3 +1018,16 @@ void QzTools::paintDropIndicator(QWidget *widget, const QRect &r) p.drawLine(x + i, r.top(), x + i, r.bottom()); } } + +QString QzTools::getPathFromUrl(const QUrl &url) +{ + if (url.isLocalFile()) { + return url.toLocalFile(); + } else if (url.scheme() == QSL("qrc")) { + const QString path = url.toString(QUrl::RemoveScheme); + return QSL(":") + path; + } else { + const QString path = url.toString(QUrl::RemoveScheme); + return path; + } +} diff --git a/src/lib/tools/qztools.h b/src/lib/tools/qztools.h index 1c4872a0c..f2a437950 100644 --- a/src/lib/tools/qztools.h +++ b/src/lib/tools/qztools.h @@ -98,6 +98,7 @@ public: } static void paintDropIndicator(QWidget *widget, const QRect &r); + static QString getPathFromUrl(const QUrl &url); }; #endif // QZTOOLS_H