diff --git a/Modules/QtWidgets/include/QmitkStyleManager.h b/Modules/QtWidgets/include/QmitkStyleManager.h index c2acc0d6e6..ea81d89dab 100644 --- a/Modules/QtWidgets/include/QmitkStyleManager.h +++ b/Modules/QtWidgets/include/QmitkStyleManager.h @@ -1,32 +1,35 @@ /*============================================================================ The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center (DKFZ) All rights reserved. Use of this source code is governed by a 3-clause BSD license that can be found in the LICENSE file. ============================================================================*/ #ifndef QmitkStyleManager_h #define QmitkStyleManager_h #include #include #include #include class MITKQTWIDGETS_EXPORT QmitkStyleManager { public: static QIcon ThemeIcon(const QByteArray &originalSVG); static QIcon ThemeIcon(const QString &resourcePath); + static QString GetIconColor(); + static QString GetIconAccentColor(); + QmitkStyleManager() = delete; ~QmitkStyleManager() = delete; }; #endif diff --git a/Modules/QtWidgets/src/QmitkStyleManager.cpp b/Modules/QtWidgets/src/QmitkStyleManager.cpp index 3f1270a8ed..cdb10e0262 100644 --- a/Modules/QtWidgets/src/QmitkStyleManager.cpp +++ b/Modules/QtWidgets/src/QmitkStyleManager.cpp @@ -1,69 +1,86 @@ /*============================================================================ The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center (DKFZ) All rights reserved. Use of this source code is governed by a 3-clause BSD license that can be found in the LICENSE file. ============================================================================*/ #include #include #include #include #include #include #include namespace { - QString ParseColor(const QString &subject, const QString &pattern, const QString &fallback) + QString ParseColor(const QString &subject, const QString &colorName, const QString &fallback) { + const QString pattern = QString("%1\\s*[=:]\\s*(#[0-9a-f]{6})").arg(colorName); + QRegularExpression re(pattern, QRegularExpression::CaseInsensitiveOption); auto match = re.match(subject); return match.hasMatch() ? match.captured(1) : fallback; } } QIcon QmitkStyleManager::ThemeIcon(const QByteArray &originalSVG) { auto styleSheet = qApp->styleSheet(); if (styleSheet.isEmpty()) return QPixmap::fromImage(QImage::fromData(originalSVG)); - auto iconColor = ParseColor(styleSheet, - QStringLiteral("iconColor\\s*[=:]\\s*(#[0-9a-f]{6})"), - QStringLiteral("#000000")); - - auto iconAccentColor = ParseColor(styleSheet, - QStringLiteral("iconAccentColor\\s*[=:]\\s*(#[0-9a-f]{6})"), - QStringLiteral("#ffffff")); + auto iconColor = GetIconColor(); + auto iconAccentColor = GetIconAccentColor(); auto themedSVG = QString(originalSVG).replace(QStringLiteral("#00ff00"), iconColor, Qt::CaseInsensitive); themedSVG = themedSVG.replace(QStringLiteral("#ff00ff"), iconAccentColor, Qt::CaseInsensitive); return QPixmap::fromImage(QImage::fromData(themedSVG.toLatin1())); } QIcon QmitkStyleManager::ThemeIcon(const QString &resourcePath) { QFile resourceFile(resourcePath); if (resourceFile.open(QIODevice::ReadOnly)) { auto originalSVG = resourceFile.readAll(); return ThemeIcon(originalSVG); } MITK_WARN << "Could not read " << resourcePath.toStdString(); return QIcon(); } + +QString QmitkStyleManager::GetIconColor() +{ + const auto styleSheet = qApp->styleSheet(); + const auto fallback = QStringLiteral("#000000"); + + return !styleSheet.isEmpty() + ? ParseColor(styleSheet, QStringLiteral("iconColor"), fallback) + : fallback; +} + +QString QmitkStyleManager::GetIconAccentColor() +{ + const auto styleSheet = qApp->styleSheet(); + const auto fallback = QStringLiteral("#ffffff"); + + return !styleSheet.isEmpty() + ? ParseColor(styleSheet, QStringLiteral("iconAccentColor"), fallback) + : fallback; +}