From 12ad4edee968dfb8ee5ffe258a24c017454c748c Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Tue, 9 May 2023 14:37:15 +0800 Subject: [PATCH 1/3] Simplify findSvgFilePath, define result value on definition Signed-off-by: Claudio Cambra --- src/gui/iconutils.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/gui/iconutils.cpp b/src/gui/iconutils.cpp index 01a1abbe570a..fa41f492a796 100644 --- a/src/gui/iconutils.cpp +++ b/src/gui/iconutils.cpp @@ -23,15 +23,16 @@ #include namespace { + QString findSvgFilePath(const QString &fileName, const QStringList &possibleColors) { - QString result; - result = QString{OCC::Theme::themePrefix} + fileName; + auto result = QString{OCC::Theme::themePrefix + fileName}; + if (QFile::exists(result)) { return result; } else { for (const auto &color : possibleColors) { - result = QString{OCC::Theme::themePrefix} + color + QStringLiteral("/") + fileName; + result = QString{OCC::Theme::themePrefix + color + QStringLiteral("/") + fileName}; if (QFile::exists(result)) { return result; @@ -42,6 +43,7 @@ QString findSvgFilePath(const QString &fileName, const QStringList &possibleColo return result; } + } namespace OCC { From 9d04c6f1400599dced03ee136d49fbbdb51cec5b Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Tue, 9 May 2023 14:43:54 +0800 Subject: [PATCH 2/3] Extract custom color svg image finding into separate function Signed-off-by: Claudio Cambra --- src/gui/iconutils.cpp | 89 +++++++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 37 deletions(-) diff --git a/src/gui/iconutils.cpp b/src/gui/iconutils.cpp index fa41f492a796..45a38e5ba916 100644 --- a/src/gui/iconutils.cpp +++ b/src/gui/iconutils.cpp @@ -44,12 +44,46 @@ QString findSvgFilePath(const QString &fileName, const QStringList &possibleColo return result; } +QImage findImageWithCustomColor(const QString &fileName, + const QColor &customColor, + const QStringList &iconBaseColors, + const QSize &requestedSize) +{ + // check if there is an existing image matching the custom color + const auto customColorName = [&customColor]() { + auto result = customColor.name(); + if (result.startsWith(QStringLiteral("#"))) { + if (result == QStringLiteral("#000000")) { + result = QStringLiteral("black"); + } + if (result == QStringLiteral("#ffffff")) { + result = QStringLiteral("white"); + } + } + return result; + }(); + + if (const auto possiblePath = QString(OCC::Theme::themePrefix + customColorName + QStringLiteral("/") + fileName); + iconBaseColors.contains(customColorName) && QFile::exists(possiblePath)) { + + if (requestedSize.width() > 0 && requestedSize.height() > 0) { + return QIcon(possiblePath).pixmap(requestedSize).toImage(); + } else { + return QImage{possiblePath}; + } + } + + return {}; +} + } namespace OCC { namespace Ui { namespace IconUtils { + Q_LOGGING_CATEGORY(lcIconUtils, "nextcloud.gui.iconutils", QtInfoMsg) + QPixmap pixmapForBackground(const QString &fileName, const QColor &backgroundColor) { Q_ASSERT(!fileName.isEmpty()); @@ -61,60 +95,39 @@ QPixmap pixmapForBackground(const QString &fileName, const QColor &backgroundCol return createSvgPixmapWithCustomColorCached(fileName, pixmapColor); } -QImage createSvgImageWithCustomColor(const QString &fileName, const QColor &customColor, QSize *originalSize, const QSize &requestedSize) +QImage createSvgImageWithCustomColor(const QString &fileName, + const QColor &customColor, + QSize *originalSize, + const QSize &requestedSize) { Q_ASSERT(!fileName.isEmpty()); Q_ASSERT(customColor.isValid()); - QImage result{}; - if (fileName.isEmpty() || !customColor.isValid()) { - qCWarning(lcIconUtils) << "invalid fileName or customColor"; - return result; + qWarning(lcIconUtils) << "invalid fileName or customColor"; + return {}; } // some icons are present in white or black only, so, we need to check both when needed const auto iconBaseColors = QStringList{QStringLiteral("black"), QStringLiteral("white")}; + const auto customColorImage = findImageWithCustomColor(fileName, customColor, iconBaseColors, requestedSize); - // check if there is an existing image matching the custom color - { - const auto customColorName = [&customColor]() { - auto result = customColor.name(); - if (result.startsWith(QStringLiteral("#"))) { - if (result == QStringLiteral("#000000")) { - result = QStringLiteral("black"); - } - if (result == QStringLiteral("#ffffff")) { - result = QStringLiteral("white"); - } - } - return result; - }(); - - if (iconBaseColors.contains(customColorName)) { - if (requestedSize.width() > 0 && requestedSize.height() > 0) { - result = QIcon(QString{OCC::Theme::themePrefix} + customColorName + QStringLiteral("/") + fileName).pixmap(requestedSize).toImage(); - } else { - result = QImage{QString{OCC::Theme::themePrefix} + customColorName + QStringLiteral("/") + fileName}; - } - if (!result.isNull()) { - return result; - } - } + if (!customColorImage.isNull()) { + return customColorImage; } // find the first matching svg file const auto sourceSvg = findSvgFilePath(fileName, iconBaseColors); - Q_ASSERT(!sourceSvg.isEmpty()); + if (sourceSvg.isEmpty()) { - qCWarning(lcIconUtils) << "Failed to find base SVG file for" << fileName; - return result; + qWarning(lcIconUtils) << "Failed to find base SVG file for" << fileName; + return {}; } - result = drawSvgWithCustomFillColor(sourceSvg, customColor, originalSize, requestedSize); - + const auto result = drawSvgWithCustomFillColor(sourceSvg, customColor, originalSize, requestedSize); Q_ASSERT(!result.isNull()); + if (result.isNull()) { qCWarning(lcIconUtils) << "Failed to load pixmap for" << fileName; } @@ -147,8 +160,10 @@ QPixmap createSvgPixmapWithCustomColorCached(const QString &fileName, const QCol return cachedPixmap; } -QImage drawSvgWithCustomFillColor( - const QString &sourceSvgPath, const QColor &fillColor, QSize *originalSize, const QSize &requestedSize) +QImage drawSvgWithCustomFillColor(const QString &sourceSvgPath, + const QColor &fillColor, + QSize *originalSize, + const QSize &requestedSize) { QSvgRenderer svgRenderer; From 733bb7ced09191f2a71f4a5bf31ee07c6be2db4a Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Thu, 20 Jul 2023 19:01:33 +0800 Subject: [PATCH 3/3] Remove empty statement, standalone semicolon Signed-off-by: Claudio Cambra --- src/gui/iconutils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gui/iconutils.cpp b/src/gui/iconutils.cpp index 45a38e5ba916..c25adbc76d00 100644 --- a/src/gui/iconutils.cpp +++ b/src/gui/iconutils.cpp @@ -91,7 +91,7 @@ QPixmap pixmapForBackground(const QString &fileName, const QColor &backgroundCol const auto pixmapColor = backgroundColor.isValid() && !Theme::isDarkColor(backgroundColor) ? QColorConstants::Svg::black : QColorConstants::Svg::white; - ; + return createSvgPixmapWithCustomColorCached(fileName, pixmapColor); }