Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SVG rendering error in SvgImageProvider #5666

Merged
merged 3 commits into from
Jul 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 58 additions & 41 deletions src/gui/iconutils.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*

Check notice on line 1 in src/gui/iconutils.cpp

View workflow job for this annotation

GitHub Actions / build

Run clang-format on src/gui/iconutils.cpp

File src/gui/iconutils.cpp (lines 47, 48, 49, 66, 67, 98, 99, 100, 163, 164, 165): Code does not conform to Custom style guidelines.
* Copyright (C) by Oleksandr Zolotov <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
Expand All @@ -23,15 +23,16 @@
#include <QSvgRenderer>

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;
Expand All @@ -42,77 +43,91 @@

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]() {
claucambra marked this conversation as resolved.
Show resolved Hide resolved
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());

const auto pixmapColor = backgroundColor.isValid() && !Theme::isDarkColor(backgroundColor)
? QColorConstants::Svg::black
: QColorConstants::Svg::white;
;

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 @@ -145,8 +160,10 @@
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
Loading