Index: src/CMakeLists.txt =================================================================== --- src/CMakeLists.txt +++ src/CMakeLists.txt @@ -77,6 +77,7 @@ ksplittercollapserbutton.cpp kstyleextensions.cpp ktooltipwidget.cpp + kthemesettings.cpp ${kwidgetsaddons_QM_LOADER} ${kwidgetsaddons_ICON_SRCS} ) Index: src/kmessagewidget.cpp =================================================================== --- src/kmessagewidget.cpp +++ src/kmessagewidget.cpp @@ -19,6 +19,7 @@ * 02110-1301 USA */ #include "kmessagewidget.h" +#include "kthemesettings_p.h" #include #include @@ -168,30 +169,35 @@ void KMessageWidgetPrivate::applyStyleSheet() { QColor bgBaseColor; + const QPalette palette = QGuiApplication::palette(); - // We have to hardcode colors here because KWidgetsAddons is a tier 1 framework - // and therefore can't depend on any other KDE Frameworks - // The following RGB color values come from the "default" scheme in kcolorscheme.cpp + // We cannot use KColorScheme here because KWidgetsAddons is a tier 1 framework + // and therefore can't depend on any other KDE Frameworks. We thus try to get + // the RGB values that interest us directly from the theme definition for or from the + // global settings store, using KThemeSettings. + // If that fails we use hardcoded values (from kcolorscheme.cpp), or palette.highlight + // (for Information). + KThemeSettings settings(QStringLiteral("Colors:Window")); switch (messageType) { case KMessageWidget::Positive: - bgBaseColor.setRgb(39, 174, 96); // Window: ForegroundPositive + bgBaseColor = settings.readRgb(QStringLiteral("ForegroundPositive"), QColor(39, 174, 96)); break; case KMessageWidget::Information: - bgBaseColor.setRgb(61, 174, 233); // Window: ForegroundActive + bgBaseColor = settings.readRgb(QStringLiteral("ForegroundActive"), palette.highlight().color()); break; case KMessageWidget::Warning: - bgBaseColor.setRgb(246, 116, 0); // Window: ForegroundNeutral + bgBaseColor = settings.readRgb(QStringLiteral("ForegroundNeutral"), QColor(246, 116, 0)); break; case KMessageWidget::Error: - bgBaseColor.setRgb(218, 68, 83); // Window: ForegroundNegative + bgBaseColor = settings.readRgb(QStringLiteral("ForegroundNegative"), QColor(218, 68, 83)); break; } const qreal bgBaseColorAlpha = 0.2; - bgBaseColor.setAlphaF(bgBaseColorAlpha); - const QPalette palette = QGuiApplication::palette(); - const QColor windowColor = palette.window().color(); - const QColor textColor = palette.text().color(); + // also get the other values from the theme first for consistency (and to avoid possible artefacts + // just after a runtime theme change with KMessageWidget instances visible). + const QColor windowColor = settings.readRgb(QStringLiteral("BackgroundNormal"), palette.window().color()); + QColor textColor = settings.readRgb(QStringLiteral("ForegroundNormal"), palette.text().color()); const QColor border = bgBaseColor; // Generate a final background color from overlaying bgBaseColor over windowColor Index: src/kthemesettings.cpp =================================================================== --- /dev/null +++ src/kthemesettings.cpp @@ -0,0 +1,88 @@ +/* This file is part of the KDE libraries + * + * Copyright (c) 2018 René J.V. Bertin + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#include "kthemesettings_p.h" + +#include +#include +#include + +KThemeSettings::KThemeSettings(const QString &initialGroup) +{ + // Check if the KColorSchemeManager was used to activate a custom theme + QString themePath = qApp->property("KDE_COLOR_SCHEME_PATH").toString(); + if (themePath.isEmpty()) { + themePath = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + QLatin1String("/kdeglobals"); + } + if (themePath.isEmpty()) { + return; + } + m_settings.reset(new QSettings(themePath, QSettings::IniFormat)); + if (m_settings && !initialGroup.isEmpty()) { + if (m_settings->childGroups().contains(initialGroup)) { + m_settings->beginGroup(initialGroup); + } else { + m_settings.reset(); + } + } +} + +bool KThemeSettings::isValid() const +{ + return !m_settings.isNull(); +} + +bool KThemeSettings::contains(const QString &key) const +{ + return m_settings ? m_settings->contains(key) : false; +} + +const QStringList KThemeSettings::childGroups() const +{ + return m_settings ? m_settings->childGroups() : QStringList(); +} + +void KThemeSettings::beginGroup(const QString &group) +{ + if (m_settings) { + m_settings->beginGroup(group); + } +} + +void KThemeSettings::endGroup() +{ + if (m_settings) { + m_settings->endGroup(); + } +} + +const QColor KThemeSettings::readRgb(const QString &key, QColor defaultValue) const +{ + if (m_settings) { + QStringList rgb = m_settings->value(key).toStringList(); + if (rgb.size() >= 3) { + QColor newValue(rgb.at(0).toInt(), rgb.at(1).toInt(), rgb.at(2).toInt()); + if (newValue.isValid()) { + defaultValue = newValue; + } + } + } + return defaultValue; +} Index: src/kthemesettings_p.h =================================================================== --- /dev/null +++ src/kthemesettings_p.h @@ -0,0 +1,97 @@ +/* This file is part of the KDE libraries + * + * Copyright (c) 2018 René J.V. Bertin + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#ifndef KTHEMESETTINGS_P_H +#define KTHEMESETTINGS_P_H + +#include +#include +#include + +class QStringList; + +/** + * @class KThemeSettings + * + * @short A basic interface to read the custom or global theme settings + * + * KThemeSettings is wrapper around QSettings that provides an elementary + * alternative for KColorScheme because that class is not accessible in + * other Tier 1 frameworks. + * + * It allows to obtain RGB specifications from current theme's, either a + * custom theme if the application set one, or from the user-defined default + * theme (as set in the kdeglobals settings store). + * + * This class is not exported because any external code desiring to use it + * can and should use KConfig, KColorScheme and KColorSchemeManager instead. + * + */ +class KThemeSettings +{ +public: + /** + * Construct a KThemeSettings object + * + * @p initialGroup : the initial group to activate. This is a convenience + * option for quick lookups: setting this to a non-empty string will cause + * subsequent lookups to fail (return the fallback value) if the settings + * store does not contain the group. + * + */ + KThemeSettings(const QString &initialGroup = QString()); + + /** + * returns true if this instance refers to a valid QSettings store + * Note that this will return false for instances created with + * an inexistent initial group. + */ + bool isValid() const; + + /** + * exposes QSettings::contains(@p key) + */ + bool contains(const QString &key) const; + + /** + * exposes QSettings::childGroups() + */ + const QStringList childGroups() const; + + /** + * exposes QSettings::beginGroup(@p group) + */ + void beginGroup(const QString &group); + + /** + * exposes QSettings::endGroup() + */ + void endGroup(); + + /** + * read and return the RGB definition for @p key, or @p defaultValue on failure. + */ + const QColor readRgb(const QString &key, QColor defaultValue = QColor()) const; + +private: + QScopedPointer m_settings; +}; + +#endif