diff options
author | Thomas Hartmann <Thomas.Hartmann@theqtcompany.com> | 2016-07-20 14:31:43 +0200 |
---|---|---|
committer | Alessandro Portale <alessandro.portale@qt.io> | 2016-07-22 11:09:41 +0000 |
commit | a30b9b6c6259ec920085080a21869faad9b9dd1d (patch) | |
tree | a0650a37944f820f168e6b03d823477250371ced /src/libs/utils/theme | |
parent | 116baad6417ae247de44c0019000598529bf7b36 (diff) |
Theming: Do not hardcode the mapping from theming colors to QPalette
There is no reason to hardcode the mapping from theming color roles
to QPalette roles, since such a mapping can be easily defined in the
theme itself.
I added optional color roles for each QPalette role.
Themes that do not change the palette do not have to take care at
all and themes that change the palette define only the QPalette roles
they require.
Change-Id: Id195052c96117b7a1a8d7d7d6acacd44e66e22f3
Reviewed-by: Thomas Hartmann <Thomas.Hartmann@theqtcompany.com>
Diffstat (limited to 'src/libs/utils/theme')
-rw-r--r-- | src/libs/utils/theme/theme.cpp | 82 | ||||
-rw-r--r-- | src/libs/utils/theme/theme.h | 46 |
2 files changed, 106 insertions, 22 deletions
diff --git a/src/libs/utils/theme/theme.cpp b/src/libs/utils/theme/theme.cpp index 7195cab29d3..90037dee7c8 100644 --- a/src/libs/utils/theme/theme.cpp +++ b/src/libs/utils/theme/theme.cpp @@ -192,8 +192,9 @@ void Theme::readSettings(QSettings &settings) for (int i = 0, total = e.keyCount(); i < total; ++i) { const QString key = QLatin1String(e.key(i)); if (!settings.contains(key)) { - qWarning("Theme \"%s\" misses color setting for key \"%s\".", - qPrintable(d->fileName), qPrintable(key)); + if (i < PaletteWindow || i > PaletteShadowDisabled) + qWarning("Theme \"%s\" misses color setting for key \"%s\".", + qPrintable(d->fileName), qPrintable(key)); continue; } d->colors[i] = readNamedColor(settings.value(key).toString()); @@ -253,27 +254,64 @@ QPalette Theme::palette() const if (!flag(DerivePaletteFromTheme)) return pal; - // FIXME: introduce some more color roles for this + const static struct { + Color themeColor; + QPalette::ColorRole paletteColorRole; + QPalette::ColorGroup paletteColorGroup; + bool setColorRoleAsBrush; + } mapping[] = { + { PaletteWindow, QPalette::Window, QPalette::All, false}, + { PaletteWindowDisabled, QPalette::Window, QPalette::Disabled, false}, + { PaletteWindowText, QPalette::WindowText, QPalette::All, true}, + { PaletteWindowTextDisabled, QPalette::WindowText, QPalette::Disabled, true}, + { PaletteBase, QPalette::Base, QPalette::All, false}, + { PaletteBaseDisabled, QPalette::Base, QPalette::Disabled, false}, + { PaletteAlternateBase, QPalette::AlternateBase, QPalette::All, false}, + { PaletteAlternateBaseDisabled, QPalette::AlternateBase, QPalette::Disabled, false}, + { PaletteToolTipBase, QPalette::ToolTipBase, QPalette::All, true}, + { PaletteToolTipBaseDisabled, QPalette::ToolTipBase, QPalette::Disabled, true}, + { PaletteToolTipText, QPalette::ToolTipText, QPalette::All, false}, + { PaletteToolTipTextDisabled, QPalette::ToolTipText, QPalette::Disabled, false}, + { PaletteText, QPalette::Text, QPalette::All, true}, + { PaletteTextDisabled, QPalette::Text, QPalette::Disabled, true}, + { PaletteButton, QPalette::Button, QPalette::All, false}, + { PaletteButtonDisabled, QPalette::Button, QPalette::Disabled, false}, + { PaletteButtonText, QPalette::ButtonText, QPalette::All, true}, + { PaletteButtonTextDisabled, QPalette::ButtonText, QPalette::Disabled, true}, + { PaletteBrightText, QPalette::BrightText, QPalette::All, false}, + { PaletteBrightTextDisabled, QPalette::BrightText, QPalette::Disabled, false}, + { PaletteHighlight, QPalette::Highlight, QPalette::All, true}, + { PaletteHighlightDisabled, QPalette::Highlight, QPalette::Disabled, true}, + { PaletteHighlightedText, QPalette::HighlightedText, QPalette::All, true}, + { PaletteHighlightedTextDisabled, QPalette::HighlightedText, QPalette::Disabled, true}, + { PaletteLink, QPalette::Link, QPalette::All, false}, + { PaletteLinkDisabled, QPalette::Link, QPalette::Disabled, false}, + { PaletteLinkVisited, QPalette::LinkVisited, QPalette::All, false}, + { PaletteLinkVisitedDisabled, QPalette::LinkVisited, QPalette::Disabled, false}, + { PaletteLight, QPalette::Light, QPalette::All, false}, + { PaletteLightDisabled, QPalette::Light, QPalette::Disabled, false}, + { PaletteMidlight, QPalette::Midlight, QPalette::All, false}, + { PaletteMidlightDisabled, QPalette::Midlight, QPalette::Disabled, false}, + { PaletteDark, QPalette::Dark, QPalette::All, false}, + { PaletteDarkDisabled, QPalette::Dark, QPalette::Disabled, false}, + { PaletteMid, QPalette::Mid, QPalette::All, false}, + { PaletteMidDisabled, QPalette::Mid, QPalette::Disabled, false}, + { PaletteShadow, QPalette::Shadow, QPalette::All, false}, + { PaletteShadowDisabled, QPalette::Shadow, QPalette::Disabled, false} + }; + + for (auto entry: mapping) { + const QColor themeColor = color(entry.themeColor); + // Use original color if color is not defined in theme. + if (themeColor.isValid()) { + if (entry.setColorRoleAsBrush) + // TODO: Find out why sometimes setBrush is used + pal.setBrush(entry.paletteColorGroup, entry.paletteColorRole, themeColor); + else + pal.setColor(entry.paletteColorGroup, entry.paletteColorRole, themeColor); + } + } - pal.setColor(QPalette::Window, color(Theme::BackgroundColorNormal)); - pal.setBrush(QPalette::WindowText, color(Theme::TextColorNormal)); - pal.setColor(QPalette::Base, color(Theme::BackgroundColorNormal)); - pal.setColor(QPalette::AlternateBase, color(Theme::BackgroundColorAlternate)); - pal.setColor(QPalette::Button, color(Theme::BackgroundColorDark)); - pal.setColor(QPalette::BrightText, Qt::red); - pal.setBrush(QPalette::Text, color(Theme::TextColorNormal)); - pal.setBrush(QPalette::ButtonText, color(Theme::TextColorNormal)); - pal.setBrush(QPalette::ToolTipBase, color(Theme::BackgroundColorSelected)); - pal.setColor(QPalette::Highlight, color(Theme::BackgroundColorSelected)); - pal.setColor(QPalette::Dark, color(Theme::BackgroundColorDark)); - pal.setColor(QPalette::HighlightedText, Qt::white); - pal.setColor(QPalette::ToolTipText, color(Theme::TextColorNormal)); - pal.setColor(QPalette::Link, color(Theme::TextColorLink)); - pal.setColor(QPalette::LinkVisited, color(Theme::TextColorLinkVisited)); - pal.setColor(QPalette::Disabled, QPalette::Window, color(Theme::BackgroundColorDisabled)); - pal.setBrush(QPalette::Disabled, QPalette::WindowText, color(Theme::TextColorDisabled)); - pal.setColor(QPalette::Disabled, QPalette::Base, color(Theme::BackgroundColorDisabled)); - pal.setBrush(QPalette::Disabled, QPalette::Text, color(Theme::TextColorDisabled)); return pal; } diff --git a/src/libs/utils/theme/theme.h b/src/libs/utils/theme/theme.h index 2c608b2483d..9358f11d282 100644 --- a/src/libs/utils/theme/theme.h +++ b/src/libs/utils/theme/theme.h @@ -125,6 +125,52 @@ public: TreeViewArrowColorNormal, TreeViewArrowColorSelected, + /* Palette for QPalette */ + + PaletteWindow, + PaletteWindowText, + PaletteBase, + PaletteAlternateBase, + PaletteToolTipBase, + PaletteToolTipText, + PaletteText, + PaletteButton, + PaletteButtonText, + PaletteBrightText, + PaletteHighlight, + PaletteHighlightedText, + PaletteLink, + PaletteLinkVisited, + + PaletteLight, + PaletteMidlight, + PaletteDark, + PaletteMid, + PaletteShadow, + + PaletteWindowDisabled, + PaletteBackgroundDisabled, + PaletteWindowTextDisabled, + PaletteForegroundDisabled, + PaletteBaseDisabled, + PaletteAlternateBaseDisabled, + PaletteToolTipBaseDisabled, + PaletteToolTipTextDisabled, + PaletteTextDisabled, + PaletteButtonDisabled, + PaletteButtonTextDisabled, + PaletteBrightTextDisabled, + PaletteHighlightDisabled, + PaletteHighlightedTextDisabled, + PaletteLinkDisabled, + PaletteLinkVisitedDisabled, + + PaletteLightDisabled, + PaletteMidlightDisabled, + PaletteDarkDisabled, + PaletteMidDisabled, + PaletteShadowDisabled, + /* Icons */ IconsBaseColor, |