diff --git a/src/kmessagewidget.cpp b/src/kmessagewidget.cpp --- a/src/kmessagewidget.cpp +++ b/src/kmessagewidget.cpp @@ -32,6 +32,9 @@ #include #include +#include +#include + //--------------------------------------------------------------------- // KMessageWidgetPrivate //--------------------------------------------------------------------- @@ -254,33 +257,78 @@ return d->messageType; } +static QColor qColorFromSettings(const QSettings *settings, const QString &key) +{ + QStringList rgb = settings->value(key).toStringList(); + return QColor(rgb.at(0).toInt(), rgb.at(1).toInt(), rgb.at(2).toInt()); +} + void KMessageWidget::setMessageType(KMessageWidget::MessageType type) { d->messageType = type; QColor bgBaseColor; + const QPalette palette = QGuiApplication::palette(); + qreal bgBaseColorAlpha = 0.2; + const QColor windowColor = palette.window().color(); - // 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 colours that interest us directly from the global settings store using QSettings. + // It that fails we use hardcoded colours, or the highlight colour (for Information). + // The hardcoded RGB color values come from the "default" scheme in kcolorscheme.cpp + const QString kdeGlobals = QStandardPaths::locate(QStandardPaths::ConfigLocation, QStringLiteral("kdeglobals")); + const QString themeGroup = QStringLiteral("Colors:Window"); + QSettings *settings = !kdeGlobals.isEmpty()? new QSettings(kdeGlobals, QSettings::IniFormat) : nullptr; + if (settings && settings->childGroups().contains(themeGroup)) { + settings->beginGroup(themeGroup); + } else { + delete settings; + settings = nullptr; + } switch (type) { case Positive: - bgBaseColor.setRgb(39, 174, 96); // Window: ForegroundPositive + if (settings && settings->contains(QStringLiteral("ForegroundPositive"))) { + bgBaseColor = qColorFromSettings(settings, QStringLiteral("ForegroundPositive")); + } else { + bgBaseColor.setRgb(39, 174, 96); // Window: ForegroundPositive + } break; - case Information: - bgBaseColor.setRgb(61, 174, 233); // Window: ForegroundActive + case Information: { + if (settings && settings->contains(QStringLiteral("ForegroundActive"))) { + bgBaseColor = qColorFromSettings(settings, QStringLiteral("ForegroundActive")); + } else { + bgBaseColor = palette.highlight().color(); + } + // the alpha component is based on the perceived brightness (CIE-XYZ 1931) of the background colour + // and its difference with receiving widget's background intensity: half the average of those values, + // constrained by the background intensity itself. + qreal bgIntensity = 0.299 * bgBaseColor.redF() + 0.586 * bgBaseColor.greenF() + 0.115 * bgBaseColor.blueF(); + qreal windowIntensity = 0.299 * windowColor.redF() + 0.586 * windowColor.greenF() + 0.115 * windowColor.blueF(); + bgBaseColorAlpha = 0.5 * qMax((bgIntensity + (1 - windowIntensity)), bgIntensity); break; + } case Warning: - bgBaseColor.setRgb(246, 116, 0); // Window: ForegroundNeutral + if (settings && settings->contains(QStringLiteral("ForegroundNeutral"))) { + bgBaseColor = qColorFromSettings(settings, QStringLiteral("ForegroundNeutral")); + } else { + bgBaseColor.setRgb(246, 116, 0); // Window: ForegroundNeutral + } break; case Error: - bgBaseColor.setRgb(218, 68, 83); // Window: ForegroundNegative + if (settings && settings->contains(QStringLiteral("ForegroundNegative"))) { + bgBaseColor = qColorFromSettings(settings, QStringLiteral("ForegroundNegative")); + } else { + bgBaseColor.setRgb(218, 68, 83); // Window: ForegroundNegative + } break; } - const qreal bgBaseColorAlpha = 0.2; + if (settings) { + settings->endGroup(); + delete settings; + } + bgBaseColor.setAlphaF(bgBaseColorAlpha); - const QPalette palette = QGuiApplication::palette(); - const QColor windowColor = palette.window().color(); const QColor textColor = palette.text().color(); const QColor border = bgBaseColor;