aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2016-04-22 10:07:45 +0200
committerJ-P Nurmi <jpnurmi@qt.io>2016-05-26 10:00:21 +0000
commit9ca63fbd0b022bd48d112f4beb38712e41612fb5 (patch)
tree53520b7dbc4b70f9cda47dcbc9429fbb4b4c2479
parent44ac831c51d248b115e594ae930c3bb359df68a1 (diff)
Introduce a 'System' theme to the styles.
Setting the theme to System chooses either the light or dark theme based on the system theme colors. However, when reading the value of the theme property, the value is never System, but the actual theme. [ChangeLog][Controls][Material] Added Material.System theme enum value, that can be used to let the Material style choose either the light or dark theme based on the system theme colors. [ChangeLog][Controls][Universal] Added Universal.System theme enum value, that can be used to let the Universal style choose either the light or dark theme based on the system theme colors. Change-Id: Ibfc9f01953cb8322b64d59413cfbaef9d4bb28fd Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
-rw-r--r--examples/quickcontrols2/gallery/qtquickcontrols2.conf4
-rw-r--r--src/imports/controls/doc/src/qtquickcontrols2-material.qdoc5
-rw-r--r--src/imports/controls/doc/src/qtquickcontrols2-styles.qdoc6
-rw-r--r--src/imports/controls/doc/src/qtquickcontrols2-universal.qdoc5
-rw-r--r--src/imports/controls/material/qquickmaterialstyle.cpp14
-rw-r--r--src/imports/controls/material/qquickmaterialstyle_p.h3
-rw-r--r--src/imports/controls/universal/qquickuniversalstyle.cpp12
-rw-r--r--src/imports/controls/universal/qquickuniversalstyle_p.h2
-rw-r--r--src/quickcontrols2/qquickstyleattached.cpp21
9 files changed, 63 insertions, 9 deletions
diff --git a/examples/quickcontrols2/gallery/qtquickcontrols2.conf b/examples/quickcontrols2/gallery/qtquickcontrols2.conf
index 61375ac1..e219ac1a 100644
--- a/examples/quickcontrols2/gallery/qtquickcontrols2.conf
+++ b/examples/quickcontrols2/gallery/qtquickcontrols2.conf
@@ -1,8 +1,8 @@
[Material]
Primary=LightGreen
Accent=LightGreen
-Theme=Light
+Theme=System
[Universal]
Accent=Green
-Theme=Light
+Theme=System
diff --git a/src/imports/controls/doc/src/qtquickcontrols2-material.qdoc b/src/imports/controls/doc/src/qtquickcontrols2-material.qdoc
index e0b0126f..b96ffaa0 100644
--- a/src/imports/controls/doc/src/qtquickcontrols2-material.qdoc
+++ b/src/imports/controls/doc/src/qtquickcontrols2-material.qdoc
@@ -254,6 +254,11 @@
Available themes:
\value Material.Light Light theme (default)
\value Material.Dark Dark theme
+ \value Material.System System theme
+
+ Setting the theme to \c System chooses either the light or dark theme based
+ on the system theme colors. However, when reading the value of the theme
+ property, the value is never \c System, but the actual theme.
\endstyleproperty
diff --git a/src/imports/controls/doc/src/qtquickcontrols2-styles.qdoc b/src/imports/controls/doc/src/qtquickcontrols2-styles.qdoc
index 5ea56c49..d2c4c5f3 100644
--- a/src/imports/controls/doc/src/qtquickcontrols2-styles.qdoc
+++ b/src/imports/controls/doc/src/qtquickcontrols2-styles.qdoc
@@ -101,15 +101,15 @@
of the methods described earlier) and certain style-specific attributes. The following
example specifies that the preferred style is the Material style. Furthermore, when the
application is run with the Material style, its theme is light and the accent color is
- brown. However, if the application is run with the Universal style instead, the theme
- is dark and the accent color is red.
+ brown. However, if the application is run with the Universal style instead, the accent
+ color is red and the appropriate theme is chosen based on the system theme colors.
\code
[Controls]
Style=Material
[Universal]
- Theme=Dark
+ Theme=System
Accent=Red
[Material]
diff --git a/src/imports/controls/doc/src/qtquickcontrols2-universal.qdoc b/src/imports/controls/doc/src/qtquickcontrols2-universal.qdoc
index 1e12a9af..ac2c051d 100644
--- a/src/imports/controls/doc/src/qtquickcontrols2-universal.qdoc
+++ b/src/imports/controls/doc/src/qtquickcontrols2-universal.qdoc
@@ -217,6 +217,11 @@
Available themes:
\value Universal.Light Light theme (default)
\value Universal.Dark Dark theme
+ \value Universal.System System theme
+
+ Setting the theme to \c System chooses either the light or dark theme based
+ on the system theme colors. However, when reading the value of the theme
+ property, the value is never \c System, but the actual theme.
\endstyleproperty
diff --git a/src/imports/controls/material/qquickmaterialstyle.cpp b/src/imports/controls/material/qquickmaterialstyle.cpp
index ce72e04b..8ad1ba66 100644
--- a/src/imports/controls/material/qquickmaterialstyle.cpp
+++ b/src/imports/controls/material/qquickmaterialstyle.cpp
@@ -426,6 +426,15 @@ static QColor alphaBlend(const QColor &bg, const QColor &fg)
return result;
}
+extern bool qt_is_dark_system_theme();
+
+static QQuickMaterialStyle::Theme effectiveTheme(QQuickMaterialStyle::Theme theme)
+{
+ if (theme == QQuickMaterialStyle::System)
+ theme = qt_is_dark_system_theme() ? QQuickMaterialStyle::Dark : QQuickMaterialStyle::Light;
+ return theme;
+}
+
QQuickMaterialStyle::QQuickMaterialStyle(QObject *parent) : QQuickStyleAttached(parent),
m_explicitTheme(false),
m_explicitPrimary(false),
@@ -460,6 +469,9 @@ QQuickMaterialStyle::Theme QQuickMaterialStyle::theme() const
void QQuickMaterialStyle::setTheme(Theme theme)
{
+ if (theme == System)
+ theme = qt_is_dark_system_theme() ? Dark : Light;
+
m_explicitTheme = true;
if (m_theme == theme)
return;
@@ -1195,7 +1207,7 @@ void QQuickMaterialStyle::init()
QByteArray themeValue = resolveSetting("QT_QUICK_CONTROLS_MATERIAL_THEME", settings, QStringLiteral("Theme"));
Theme themeEnum = toEnumValue<Theme>(themeValue, &ok);
if (ok)
- defaultTheme = m_theme = themeEnum;
+ defaultTheme = m_theme = effectiveTheme(themeEnum);
else if (!themeValue.isEmpty())
qWarning().nospace().noquote() << "Material: unknown theme value: " << themeValue;
diff --git a/src/imports/controls/material/qquickmaterialstyle_p.h b/src/imports/controls/material/qquickmaterialstyle_p.h
index e7434d83..2686d14b 100644
--- a/src/imports/controls/material/qquickmaterialstyle_p.h
+++ b/src/imports/controls/material/qquickmaterialstyle_p.h
@@ -108,7 +108,8 @@ class QQuickMaterialStyle : public QQuickStyleAttached
public:
enum Theme {
Light,
- Dark
+ Dark,
+ System
};
enum Color {
diff --git a/src/imports/controls/universal/qquickuniversalstyle.cpp b/src/imports/controls/universal/qquickuniversalstyle.cpp
index f4ec1fe7..32c00de9 100644
--- a/src/imports/controls/universal/qquickuniversalstyle.cpp
+++ b/src/imports/controls/universal/qquickuniversalstyle.cpp
@@ -132,6 +132,15 @@ static QRgb qquickuniversal_accent_color(QQuickUniversalStyle::Color accent)
return colors[accent];
}
+extern bool qt_is_dark_system_theme();
+
+static QQuickUniversalStyle::Theme qquickuniversal_effective_theme(QQuickUniversalStyle::Theme theme)
+{
+ if (theme == QQuickUniversalStyle::System)
+ theme = qt_is_dark_system_theme() ? QQuickUniversalStyle::Dark : QQuickUniversalStyle::Light;
+ return theme;
+}
+
static QQuickUniversalStyle::Theme DefaultTheme = QQuickUniversalStyle::Light;
static QRgb DefaultAccent = qquickuniversal_accent_color(QQuickUniversalStyle::Cobalt);
static QRgb DefaultForeground = qquickuniversal_light_color(QQuickUniversalStyle::BaseHigh);
@@ -157,6 +166,7 @@ QQuickUniversalStyle::Theme QQuickUniversalStyle::theme() const
void QQuickUniversalStyle::setTheme(Theme theme)
{
+ theme = qquickuniversal_effective_theme(theme);
m_explicitTheme = true;
if (m_theme == theme)
return;
@@ -529,7 +539,7 @@ void QQuickUniversalStyle::init()
QByteArray themeValue = resolveSetting("QT_QUICK_CONTROLS_UNIVERSAL_THEME", settings, QStringLiteral("Theme"));
Theme themeEnum = toEnumValue<Theme>(themeValue, &ok);
if (ok)
- DefaultTheme = m_theme = themeEnum;
+ DefaultTheme = m_theme = qquickuniversal_effective_theme(themeEnum);
else if (!themeValue.isEmpty())
qWarning().nospace().noquote() << "Universal: unknown theme value: " << themeValue;
diff --git a/src/imports/controls/universal/qquickuniversalstyle_p.h b/src/imports/controls/universal/qquickuniversalstyle_p.h
index 8e27215d..19c346c8 100644
--- a/src/imports/controls/universal/qquickuniversalstyle_p.h
+++ b/src/imports/controls/universal/qquickuniversalstyle_p.h
@@ -93,7 +93,7 @@ public:
static QQuickUniversalStyle *qmlAttachedProperties(QObject *object);
- enum Theme { Light, Dark };
+ enum Theme { Light, Dark, System };
Q_ENUM(Theme)
Theme theme() const;
diff --git a/src/quickcontrols2/qquickstyleattached.cpp b/src/quickcontrols2/qquickstyleattached.cpp
index d0df79c5..6683b018 100644
--- a/src/quickcontrols2/qquickstyleattached.cpp
+++ b/src/quickcontrols2/qquickstyleattached.cpp
@@ -39,12 +39,33 @@
#include <QtCore/qfile.h>
#include <QtCore/qsettings.h>
#include <QtCore/qfileselector.h>
+#include <QtGui/qcolor.h>
+#include <QtGui/qpalette.h>
+#include <QtGui/private/qguiapplication_p.h>
+#include <QtGui/qpa/qplatformtheme.h>
#include <QtQuick/qquickwindow.h>
#include <QtQuick/private/qquickitem_p.h>
#include <QtQuickTemplates2/private/qquickpopup_p.h>
QT_BEGIN_NAMESPACE
+static bool isDarkSystemTheme()
+{
+ if (const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme()) {
+ if (const QPalette *systemPalette = theme->palette(QPlatformTheme::SystemPalette)) {
+ const QColor textColor = systemPalette->color(QPalette::WindowText);
+ return textColor.red() > 128 && textColor.blue() > 128 && textColor.green() > 128;
+ }
+ }
+ return false;
+}
+
+Q_QUICKCONTROLS2_PRIVATE_EXPORT bool qt_is_dark_system_theme()
+{
+ static bool dark = isDarkSystemTheme();
+ return dark;
+}
+
static QQuickStyleAttached *attachedStyle(const QMetaObject *type, QObject *object, bool create = false)
{
if (!object)