[vlc-commits] [Git][videolan/vlc][master] 4 commits: qt: use move assignment to simplify construction in `ColorizedSvgIcon`
Felix Paul Kühne (@fkuehne)
gitlab at videolan.org
Thu May 22 12:29:31 UTC 2025
Felix Paul Kühne pushed to branch master at VideoLAN / VLC
Commits:
b822abc2 by Fatih Uzunoglu at 2025-05-22T12:11:20+00:00
qt: use move assignment to simplify construction in `ColorizedSvgIcon`
This also removes the overzealous assertion of a valid SVG icon engine
plugin being retrieved, which may not be the case if relevant package
(Qt Svg) is not installed.
Since we are using the assignment operator in both branches, I don't
think it should be necessary to call the `QIcon` constructor anymore.
Unlike the image colorization, where a purple image is provided if the
engine is not available, I decided to rely on the default behavior of
`QIcon` in this case, which I assume is having no icon.
- - - - -
096292d3 by Fatih Uzunoglu at 2025-05-22T12:11:20+00:00
qt: no need to use `std::optional` for `QColor`
This is because `QColor` can already be invalid, we can use that
state instead.
- - - - -
bcd587fe by Fatih Uzunoglu at 2025-05-22T12:11:20+00:00
qt: use const reference for filename in `ColorizedSvgIcon`
- - - - -
8fa60f98 by Fatih Uzunoglu at 2025-05-22T12:11:20+00:00
qt: move include directive from the header to source file in `ColorizedSvgIcon`
It is probably not really important to expose `COLOR1_KEY`, `COLOR2_KEY`, and
`COLOR_ACCENT_KEY` to the users of this class.
- - - - -
4 changed files:
- modules/gui/qt/util/color_svg_image_provider.cpp
- modules/gui/qt/util/color_svg_image_provider.hpp
- modules/gui/qt/util/colorizedsvgicon.cpp
- modules/gui/qt/util/colorizedsvgicon.hpp
Changes:
=====================================
modules/gui/qt/util/color_svg_image_provider.cpp
=====================================
@@ -36,7 +36,7 @@ static const QMap<QString, QString> predefinedSubst = {
{COLOR_ACCENT_KEY, "#FF8800"},
};
-QPair<QByteArray, std::optional<QColor>> colorizeSvg(const QString &filename, const QList<QPair<QString, QString> > &replacements)
+QPair<QByteArray, QColor> colorizeSvg(const QString &filename, const QList<QPair<QString, QString> > &replacements)
{
QFile file(filename);
if (!file.open(QIODevice::ReadOnly))
@@ -49,7 +49,7 @@ QPair<QByteArray, std::optional<QColor>> colorizeSvg(const QString &filename, co
//we pass by QString because we want to perform case incensite replacements
QString dataStr = QString::fromUtf8(data);
- std::optional<QColor> backgroundColor;
+ QColor backgroundColor;
for (const auto& replacePair: replacements)
{
@@ -147,8 +147,8 @@ public:
if (!scaledSize.isEmpty())
svgHandler->setOption(QImageIOHandler::ScaledSize, scaledSize);
- if (data.second.has_value())
- svgHandler->setOption(QImageIOHandler::BackgroundColor, *data.second);
+ if (data.second.isValid())
+ svgHandler->setOption(QImageIOHandler::BackgroundColor, data.second);
svgHandler->read(&image);
}
=====================================
modules/gui/qt/util/color_svg_image_provider.hpp
=====================================
@@ -30,7 +30,7 @@
#define COLOR2_KEY "_res_C2"
#define COLOR_ACCENT_KEY "_res_ACCENT"
-QPair<QByteArray, std::optional<QColor>> colorizeSvg(const QString& filename, const QList<QPair<QString, QString>>& replacements);
+QPair<QByteArray, QColor> colorizeSvg(const QString& filename, const QList<QPair<QString, QString>>& replacements);
class SVGColorImageImageProvider: public QQuickAsyncImageProvider
{
=====================================
modules/gui/qt/util/colorizedsvgicon.cpp
=====================================
@@ -25,28 +25,32 @@
#include <QWidget>
#include <QFileInfo>
-ColorizedSvgIcon::ColorizedSvgIcon(QString filename, std::optional<QColor> color1, std::optional<QColor> color2, std::optional<QColor> accentColor, const QList<QPair<QString, QString> > &otherReplacements)
- : QIcon(newEngine()) // QIcon takes the ownership of the icon engine
+#include "util/color_svg_image_provider.hpp"
+
+ColorizedSvgIcon::ColorizedSvgIcon(const QString& filename, const QColor color1, const QColor color2, const QColor accentColor, const QList<QPair<QString, QString> > &otherReplacements)
{
- captureEngine();
+ QIcon& qIconRef = *this;
- if (!m_engine)
+ const auto engine = svgIconEngine();
+ if (!engine)
{
qWarning() << "ColorizedSvgIcon: could not create svg icon engine, icon " << filename << " will not be colorized.";
- addFile(filename);
+ qIconRef = QIcon(filename);
return;
}
+ qIconRef = QIcon(engine); // QIcon takes the ownership of the engine
+
QList<QPair<QString, QString>> replacements;
{
- if (color1.has_value())
- replacements.push_back({QStringLiteral(COLOR1_KEY), color1->name(QColor::HexRgb)});
+ if (color1.isValid())
+ replacements.push_back({QStringLiteral(COLOR1_KEY), color1.name(QColor::HexRgb)});
- if (color2.has_value())
- replacements.push_back({QStringLiteral(COLOR2_KEY), color2->name(QColor::HexRgb)});
+ if (color2.isValid())
+ replacements.push_back({QStringLiteral(COLOR2_KEY), color2.name(QColor::HexRgb)});
- if (accentColor.has_value())
- replacements.push_back({QStringLiteral(COLOR_ACCENT_KEY), accentColor->name(QColor::HexRgb)});
+ if (accentColor.isValid())
+ replacements.push_back({QStringLiteral(COLOR_ACCENT_KEY), accentColor.name(QColor::HexRgb)});
replacements.append(otherReplacements.begin(), otherReplacements.end());
}
@@ -78,7 +82,7 @@ ColorizedSvgIcon::ColorizedSvgIcon(QString filename, std::optional<QColor> color
{
// Feed the engine with the colorized svg content:
QDataStream in(std::as_const(data)); // read-only
- if (!m_engine->read(in))
+ if (!engine->read(in))
{
qWarning() << "ColorizedSvgIcon: svg icon engine can not read contents, icon " << filename << " will not be colorized.";
addFile(filename);
=====================================
modules/gui/qt/util/colorizedsvgicon.hpp
=====================================
@@ -20,36 +20,8 @@
#include <QIcon>
-#include "util/color_svg_image_provider.hpp"
-
-#include <QMutex>
-
-#include <optional>
-
class ColorizedSvgIcon : public QIcon
{
- QIconEngine *m_engine = nullptr;
-
- inline static QMutex engineLock;
- inline static QIconEngine *lastEngine; // static variables are initialized by default
-
- static QIconEngine *newEngine()
- {
- engineLock.lock();
- assert(!lastEngine);
- lastEngine = svgIconEngine();
- return lastEngine;
- }
-
- void captureEngine()
- {
- assert(!m_engine);
- assert(lastEngine);
- m_engine = lastEngine;
- lastEngine = nullptr;
- engineLock.unlock();
- }
-
static int hashKey(QIcon::Mode mode, QIcon::State state)
{
// From QSvgIconEnginePrivate:
@@ -57,10 +29,10 @@ class ColorizedSvgIcon : public QIcon
}
public:
- explicit ColorizedSvgIcon(QString filename,
- std::optional<QColor> color1 = std::nullopt,
- std::optional<QColor> color2 = std::nullopt,
- std::optional<QColor> accentColor = std::nullopt,
+ explicit ColorizedSvgIcon(const QString& filename,
+ const QColor color1 = {},
+ const QColor color2 = {},
+ const QColor accentColor = {},
const QList<QPair<QString, QString>>& otherReplacements = {});
[[nodiscard]] static ColorizedSvgIcon colorizedIconForWidget(const QString& fileName, const QWidget *widget);
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/920f0addc5909e21ad9fd51c2b7ddfc02ab3f428...8fa60f98be8f0b7e3f15c4d1d30fe6528059ad70
--
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/920f0addc5909e21ad9fd51c2b7ddfc02ab3f428...8fa60f98be8f0b7e3f15c4d1d30fe6528059ad70
You're receiving this email because of your account on code.videolan.org.
VideoLAN code repository instance
More information about the vlc-commits
mailing list