Browse Source

Extract custom color svg image finding into separate function

Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
Claudio Cambra 2 years ago
parent
commit
9d04c6f140
1 changed files with 52 additions and 37 deletions
  1. 52 37
      src/gui/iconutils.cpp

+ 52 - 37
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;