diff options
author | J-P Nurmi <jpnurmi@theqtcompany.com> | 2016-03-22 21:46:26 +0100 |
---|---|---|
committer | J-P Nurmi <jpnurmi@theqtcompany.com> | 2016-03-24 10:01:53 +0000 |
commit | 5f9cbe0253e27c3e559ab3dc0310775aba8a0621 (patch) | |
tree | 4b3a37486ddbd7203b2a2c40043e53db8622f9c6 /src | |
parent | ddd88c154ec9f58e28014cb80f792ba0d2dcb619 (diff) |
Add support for specifying theme, accent & primary via env
Environment values take presedence over the default values requested in
qtquickcontrols.conf, the same way than style name.
Change-Id: I4641317bdce320c33a89f3614f3c782883c9c843
Reviewed-by: Mitch Curtis <mitch.curtis@theqtcompany.com>
Diffstat (limited to 'src')
5 files changed, 117 insertions, 54 deletions
diff --git a/src/imports/controls/doc/src/qtquickcontrols2-material.qdoc b/src/imports/controls/doc/src/qtquickcontrols2-material.qdoc index fb1ad081..ad807262 100644 --- a/src/imports/controls/doc/src/qtquickcontrols2-material.qdoc +++ b/src/imports/controls/doc/src/qtquickcontrols2-material.qdoc @@ -116,6 +116,32 @@ \image qtquickcontrols-material-dark.png \endtable + In addition to specifying the attributes in QML, it is also possible to + specify them via environment variables or in a \l {qtlabscontrols-conf} + {configuration file}. + + \section3 Environment Variables + + \table + \header + \li Variable + \li Description + \row + \li \c QT_LABS_CONTROLS_MATERIAL_THEME + \li The value can be one of the available \l {theme-attached-prop}{themes}, + for example \c "Dark". + \row + \li \c QT_LABS_CONTROLS_MATERIAL_ACCENT + \li The value can be any \l {colorbasictypedocs}{color}, but it is recommended + to use one of the \l {Pre-defined Colors}{pre-defined colors}, for example + for example \c "Teal". + \row + \li \c QT_LABS_CONTROLS_MATERIAL_PRIMARY + \li The value can be any \l {colorbasictypedocs}{color}, but it is recommended + to use one of the \l {Pre-defined Colors}{pre-defined colors}, for example + for example \c "BlueGrey". + \endtable + \section2 Dependency The Material style must be separately imported to gain access to the diff --git a/src/imports/controls/doc/src/qtquickcontrols2-styles.qdoc b/src/imports/controls/doc/src/qtquickcontrols2-styles.qdoc index 166edd45..a4f036e6 100644 --- a/src/imports/controls/doc/src/qtquickcontrols2-styles.qdoc +++ b/src/imports/controls/doc/src/qtquickcontrols2-styles.qdoc @@ -92,6 +92,7 @@ \endcode \section2 Configuration file + \target qtlabscontrols-conf Qt Labs Controls support a special configuration file, \c :/qtquickcontrols.conf, that is built into an application's resources. diff --git a/src/imports/controls/doc/src/qtquickcontrols2-universal.qdoc b/src/imports/controls/doc/src/qtquickcontrols2-universal.qdoc index 3943440d..78065ebd 100644 --- a/src/imports/controls/doc/src/qtquickcontrols2-universal.qdoc +++ b/src/imports/controls/doc/src/qtquickcontrols2-universal.qdoc @@ -116,6 +116,27 @@ \image qtquickcontrols-universal-dark.png \endtable + In addition to specifying the attributes in QML, it is also possible to + specify them via environment variables or in a \l {qtlabscontrols-conf} + {configuration file}. + + \section3 Environment Variables + + \table + \header + \li Variable + \li Description + \row + \li \c QT_LABS_CONTROLS_UNIVERSAL_THEME + \li The value can be one of the available \l {theme-attached-prop}{themes}, + for example \c "Dark". + \row + \li \c QT_LABS_CONTROLS_UNIVERSAL_ACCENT + \li The value can be any \l {colorbasictypedocs}{color}, but it is recommended + to use one of the pre-defined \l {accent-attached-prop} {accents}, for + example \c "Violet". + \endtable + \section2 Dependency The Universal style must be separately imported to gain access to the diff --git a/src/imports/controls/material/qquickmaterialstyle.cpp b/src/imports/controls/material/qquickmaterialstyle.cpp index 9757db9f..27ffca4d 100644 --- a/src/imports/controls/material/qquickmaterialstyle.cpp +++ b/src/imports/controls/material/qquickmaterialstyle.cpp @@ -962,48 +962,55 @@ static Enum toEnumValue(const QByteArray &value, bool *ok) return static_cast<Enum>(enumeration.keyToValue(value, ok)); } +static QByteArray resolveSetting(const QByteArray &env, const QSharedPointer<QSettings> &settings, const QString &name) +{ + QByteArray value = qgetenv(env); + if (value.isNull() && !settings.isNull()) + value = settings->value(name).toByteArray(); + return value; +} + void QQuickMaterialStyle::init() { static bool defaultsInitialized = false; if (!defaultsInitialized) { QSharedPointer<QSettings> settings = QQuickStyleAttached::settings(QStringLiteral("Material")); - if (!settings.isNull()) { - bool ok = false; - QByteArray value = settings->value(QStringLiteral("Theme")).toByteArray(); - Theme theme = toEnumValue<Theme>(value, &ok); - if (ok) - defaultTheme = m_theme = theme; - else if (!value.isEmpty()) - qWarning().nospace().noquote() << settings->fileName() << ": unknown Material theme value: " << value; - - value = settings->value(QStringLiteral("Primary")).toByteArray(); - Color primary = toEnumValue<Color>(value, &ok); - if (ok) { - defaultPrimaryCustom = m_customPrimary = false; - defaultPrimary = m_primary = primary; - } else { - QColor color(value.constData()); - if (color.isValid()) { - defaultPrimaryCustom = m_customPrimary = true; - defaultPrimary = m_primary = color.rgba(); - } else if (!value.isEmpty()) { - qWarning().nospace().noquote() << settings->fileName() << ": unknown Material primary value: " << value; - } + + bool ok = false; + QByteArray themeValue = resolveSetting("QT_LABS_CONTROLS_MATERIAL_THEME", settings, QStringLiteral("Theme")); + Theme themeEnum = toEnumValue<Theme>(themeValue, &ok); + if (ok) + defaultTheme = m_theme = themeEnum; + else if (!themeValue.isEmpty()) + qWarning().nospace().noquote() << "Material: unknown theme value: " << themeValue; + + QByteArray primaryValue = resolveSetting("QT_LABS_CONTROLS_MATERIAL_PRIMARY", settings, QStringLiteral("Primary")); + Color primaryEnum = toEnumValue<Color>(primaryValue, &ok); + if (ok) { + defaultPrimaryCustom = m_customPrimary = false; + defaultPrimary = m_primary = primaryEnum; + } else { + QColor color(primaryValue.constData()); + if (color.isValid()) { + defaultPrimaryCustom = m_customPrimary = true; + defaultPrimary = m_primary = color.rgba(); + } else if (!primaryValue.isEmpty()) { + qWarning().nospace().noquote() << "Material: unknown primary value: " << primaryValue; } + } - value = settings->value(QStringLiteral("Accent")).toByteArray(); - Color accent = toEnumValue<Color>(value, &ok); - if (ok) { - defaultAccentCustom = m_customAccent = false; - defaultAccent = m_accent = accent; + QByteArray accentValue = resolveSetting("QT_LABS_CONTROLS_MATERIAL_ACCENT", settings, QStringLiteral("Accent")); + Color accentEnum = toEnumValue<Color>(accentValue, &ok); + if (ok) { + defaultAccentCustom = m_customAccent = false; + defaultAccent = m_accent = accentEnum; + } else if (!accentValue.isEmpty()) { + QColor color(accentValue.constData()); + if (color.isValid()) { + defaultAccentCustom = m_customAccent = true; + defaultAccent = m_accent = color.rgba(); } else { - QColor color(value.constData()); - if (color.isValid()) { - defaultAccentCustom = m_customAccent = true; - defaultAccent = m_accent = color.rgba(); - } else if (!value.isEmpty()) { - qWarning().nospace().noquote() << settings->fileName() << ": unknown Material accent value: " << value; - } + qWarning().nospace().noquote() << "Material: unknown accent value: " << accentValue; } } defaultsInitialized = true; diff --git a/src/imports/controls/universal/qquickuniversalstyle.cpp b/src/imports/controls/universal/qquickuniversalstyle.cpp index fb948dec..ccbdfb29 100644 --- a/src/imports/controls/universal/qquickuniversalstyle.cpp +++ b/src/imports/controls/universal/qquickuniversalstyle.cpp @@ -404,32 +404,40 @@ static Enum toEnumValue(const QByteArray &value, bool *ok) return static_cast<Enum>(enumeration.keyToValue(value, ok)); } +static QByteArray resolveSetting(const QByteArray &env, const QSharedPointer<QSettings> &settings, const QString &name) +{ + QByteArray value = qgetenv(env); + if (value.isNull() && !settings.isNull()) + value = settings->value(name).toByteArray(); + return value; +} + void QQuickUniversalStyle::init() { static bool defaultsInitialized = false; if (!defaultsInitialized) { QSharedPointer<QSettings> settings = QQuickStyleAttached::settings(QStringLiteral("Universal")); - if (!settings.isNull()) { - bool ok = false; - QByteArray value = settings->value(QStringLiteral("Theme")).toByteArray(); - Theme theme = toEnumValue<Theme>(value, &ok); - if (ok) - DefaultTheme = m_theme = theme; - else if (!value.isEmpty()) - qWarning().nospace().noquote() << settings->fileName() << ": unknown Universal theme value: " << value; - - value = settings->value(QStringLiteral("Accent")).toByteArray(); - Accent accent = toEnumValue<Accent>(value, &ok); - if (ok) { - DefaultAccent = m_accent = qquickuniversal_accent_color(accent); - } else { - QColor color(value.constData()); - if (color.isValid()) - DefaultAccent = m_accent = color.rgba(); - else if (!value.isEmpty()) - qWarning().nospace().noquote() << settings->fileName() << ": unknown Universal accent value: " << value; - } + + bool ok = false; + QByteArray themeValue = resolveSetting("QT_LABS_CONTROLS_UNIVERSAL_THEME", settings, QStringLiteral("Theme")); + Theme themeEnum = toEnumValue<Theme>(themeValue, &ok); + if (ok) + DefaultTheme = m_theme = themeEnum; + else if (!themeValue.isEmpty()) + qWarning().nospace().noquote() << "Universal: unknown theme value: " << themeValue; + + QByteArray accentValue = resolveSetting("QT_LABS_CONTROLS_UNIVERSAL_ACCENT", settings, QStringLiteral("Accent")); + Accent accentEnum = toEnumValue<Accent>(accentValue, &ok); + if (ok) { + DefaultAccent = m_accent = qquickuniversal_accent_color(accentEnum); + } else if (!accentValue.isEmpty()) { + QColor color(accentValue.constData()); + if (color.isValid()) + DefaultAccent = m_accent = color.rgba(); + else + qWarning().nospace().noquote() << "Universal: unknown accent value: " << accentValue; } + defaultsInitialized = true; } |