From 3b303d73631746e83f18a3298309665bae64b377 Mon Sep 17 00:00:00 2001 From: Aleix Pol Date: Mon, 29 May 2017 17:18:42 +0200 Subject: [PATCH] Small improvements in IconItem Summary: Don't construct a QUrl for every source strings. Check it's a file url first (which is the only kind of url we support at the moment) When extracting the path, use toLocalFile, so it works on platforms where path!=toLocalPath (e.g. windows) Don't split a string and get a chunk, but just extract the chunk we need with section. Reduces allocations that are automatically discarded. Narrow iconPath scope Test Plan: Tests still pass, plasmashell still works Reviewers: #frameworks, #plasma, mart Reviewed By: #plasma, mart Subscribers: plasma-devel Tags: #plasma, #frameworks Differential Revision: https://phabricator.kde.org/D6014 --- src/declarativeimports/core/iconitem.cpp | 23 +++++++++++------------ src/plasma/svg.cpp | 8 ++++---- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/declarativeimports/core/iconitem.cpp b/src/declarativeimports/core/iconitem.cpp index da3bf4720..445f28ca7 100644 --- a/src/declarativeimports/core/iconitem.cpp +++ b/src/declarativeimports/core/iconitem.cpp @@ -26,7 +26,6 @@ #include #include #include -#include #include #include @@ -156,10 +155,10 @@ void IconItem::setSource(const QVariant &source) if (!sourceString.isEmpty()) { //If a url in the form file:// is passed, take the image pointed by that from disk - QUrl url(sourceString); - if (url.isLocalFile()) { + if (sourceString.startsWith(QLatin1String("file:"))) { + const QUrl url(sourceString); m_icon = QIcon(); - m_imageIcon = QImage(url.path()); + m_imageIcon = QImage(url.toLocalFile()); m_svgIconName.clear(); delete m_svgIcon; m_svgIcon = 0; @@ -174,7 +173,7 @@ void IconItem::setSource(const QVariant &source) if (m_usesPlasmaTheme) { //try as a svg icon from plasma theme - m_svgIcon->setImagePath(QLatin1String("icons/") + sourceString.split('-').first()); + m_svgIcon->setImagePath(QLatin1String("icons/") + sourceString.section('-', 0, 0)); m_svgIcon->setContainsMultipleImages(true); //invalidate the image path to recalculate it later } else { @@ -562,23 +561,23 @@ void IconItem::loadPixmap() return; } else if (m_svgIcon) { m_svgIcon->resize(size, size); - if (m_svgIcon->hasElement(m_svgIconName)) { + if (!m_svgIconName.isEmpty() && m_svgIcon->hasElement(m_svgIconName)) { result = m_svgIcon->pixmap(m_svgIconName); } else if (!m_svgIconName.isEmpty()) { const auto *iconTheme = KIconLoader::global()->theme(); - QString iconPath; if (iconTheme) { - iconPath = iconTheme->iconPath(m_svgIconName + QLatin1String(".svg"), qMin(width(), height()), KIconLoader::MatchBest); + QString iconPath = iconTheme->iconPath(m_svgIconName + QLatin1String(".svg"), size, KIconLoader::MatchBest); if (iconPath.isEmpty()) { - iconPath = iconTheme->iconPath(m_svgIconName + QLatin1String(".svgz"), qMin(width(), height()), KIconLoader::MatchBest); + iconPath = iconTheme->iconPath(m_svgIconName + QLatin1String(".svgz"), size, KIconLoader::MatchBest); + } + + if (!iconPath.isEmpty()) { + m_svgIcon->setImagePath(iconPath); } } else { qWarning() << "KIconLoader has no theme set"; } - if (!iconPath.isEmpty()) { - m_svgIcon->setImagePath(iconPath); - } result = m_svgIcon->pixmap(); } } else if (!m_icon.isNull()) { diff --git a/src/plasma/svg.cpp b/src/plasma/svg.cpp index 6ededa093..f7a8d8d0d 100644 --- a/src/plasma/svg.cpp +++ b/src/plasma/svg.cpp @@ -528,10 +528,10 @@ QRectF SvgPrivate::elementRect(const QString &elementId) return QRectF(); } - QString id = cacheId(elementId); - - if (localRectCache.contains(id)) { - return localRectCache.value(id); + const QString id = cacheId(elementId); + const auto it = localRectCache.constFind(id); + if (it != localRectCache.constEnd()) { + return *it; } QRectF rect;