Skip to content

Commit

Permalink
Extract custom color svg image finding into separate function
Browse files Browse the repository at this point in the history
Signed-off-by: Claudio Cambra <[email protected]>
  • Loading branch information
claucambra committed Jul 20, 2023
1 parent 0144d58 commit 258d09e
Showing 1 changed file with 52 additions and 37 deletions.
89 changes: 52 additions & 37 deletions src/gui/iconutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 258d09e

Please sign in to comment.