Commit 34ba0c27 authored by Alessandro Portale's avatar Alessandro Portale Committed by hjk

Theming: Use QQmlPropertyHash for passing theme to QML

This makes the mapping of theme values to QML automatic.

Change-Id: I4442ef454dc3a7dde1bd6b068b03ff890db3e49f
Reviewed-by: default avatarhjk <hjk121@nokiamail.com>
parent b8ed1f61
......@@ -61,60 +61,60 @@ Button {
Rectangle {
anchors.fill: parent
antialiasing: true
radius: (creatorTheme.widgetStyle === 'flat') ? 0 : 3
radius: (creatorTheme.WidgetStyle === 'StyleFlat') ? 0 : 3
visible: !(button.pressed || button.checked)
gradient: Gradient {
GradientStop {
position: 0
color: (theme==='dark') ? "#232323" : "#f9f9f9"
color: (creatorTheme.WidgetStyle === 'StyleFlat') ? "#232323" : "#f9f9f9"
}
GradientStop {
position: 0.49
color: (theme === 'dark') ? "#232323" : "#f9f9f9"
color: (creatorTheme.WidgetStyle === 'StyleFlat') ? "#232323" : "#f9f9f9"
}
GradientStop {
position: 0.5
color: (theme === 'dark') ? "#232323" : "#eeeeee"
color: (creatorTheme.WidgetStyle === 'StyleFlat') ? "#232323" : "#eeeeee"
}
GradientStop {
position: 1
color: (theme === 'dark') ? "#232323" : "#eeeeee"
color: (creatorTheme.WidgetStyle === 'StyleFlat') ? "#232323" : "#eeeeee"
}
}
border.color: creatorTheme.button_BorderColor
border.color: creatorTheme.Welcome_Button_BorderColor
}
Rectangle {
anchors.fill: parent
antialiasing: true
radius: (creatorTheme.widgetStyle === 'flat') ? 0 : 3
radius: (creatorTheme.WidgetStyle === 'StyleFlat') ? 0 : 3
visible: button.pressed || button.checked
gradient: Gradient {
GradientStop {
position: 0.00;
color: (theme === "dark") ? "#151515" : "#4c4c4c"
color: (creatorTheme.WidgetStyle === 'StyleFlat') ? "#151515" : "#4c4c4c"
}
GradientStop {
position: 0.49;
color: (theme === "dark") ? "#151515" : "#4c4c4c"
color: (creatorTheme.WidgetStyle === 'StyleFlat') ? "#151515" : "#4c4c4c"
}
GradientStop {
position: 0.50;
color: (theme === "dark") ? "#151515" : "#424242"
color: (creatorTheme.WidgetStyle === 'StyleFlat') ? "#151515" : "#424242"
}
GradientStop {
position: 1.00;
color: (theme === "dark") ? "#151515" : "#424242"
color: (creatorTheme.WidgetStyle === 'StyleFlat') ? "#151515" : "#424242"
}
}
border.color: creatorTheme.button_BorderColor
border.color: creatorTheme.Welcome_Button_BorderColor
}
}
......@@ -124,8 +124,8 @@ Button {
verticalAlignment: Text.AlignVCenter
text: button.text
color: button.pressed || button.checked
? creatorTheme.button_TextColorPressed
: creatorTheme.button_TextColorNormal
? creatorTheme.Welcome_Button_TextColorPressed
: creatorTheme.Welcome_Button_TextColorNormal
font.pixelSize: 15
font.bold: false
smooth: true
......
......@@ -34,7 +34,7 @@ Rectangle {
id: delegate
height: 240
width: 216
color: creatorTheme.backgroundColorNormal
color: creatorTheme.Welcome_BackgroundColorNormal
property alias caption: captionItem.text
property alias imageSource: imageItem.source
......@@ -107,7 +107,7 @@ Rectangle {
y: 161
width: 200
height: 69
color: creatorTheme.backgroundColorNormal
color: creatorTheme.Welcome_BackgroundColorNormal
anchors.bottom: parent.bottom
anchors.right: parent.right
anchors.left: parent.left
......@@ -117,7 +117,7 @@ Rectangle {
id: captionItem
x: 16
y: 170
color: creatorTheme.caption_TextColorNormal
color: creatorTheme.Welcome_Caption_TextColorNormal
text: qsTr("2D PAINTING EXAMPLE long description")
elide: Text.ElideRight
anchors.right: parent.right
......@@ -164,7 +164,7 @@ Rectangle {
x: 16
y: 198
text: qsTr("Tags:")
color: creatorTheme.textColorNormal
color: creatorTheme.Welcome_TextColorNormal
smooth: true
font.italic: false
font.pixelSize: 11
......
......@@ -44,7 +44,7 @@ Row {
LinkedText {
text: title
font.pixelSize: 11
color: creatorTheme.textColorNormal // 'Qt Account' .. 'User Guide' on lower left
color: creatorTheme.Welcome_TextColorNormal // 'Qt Account' .. 'User Guide' on lower left
onClicked: {
if (openUrl)
gettingStarted.openUrl(openUrl);
......
......@@ -33,8 +33,8 @@ import QtQuick 2.1
NativeText {
id: root
height: 16
color: active ? creatorTheme.link_TextColorActive
: creatorTheme.link_TextColorNormal
color: active ? creatorTheme.Welcome_Link_TextColorActive
: creatorTheme.Welcome_Link_TextColorNormal
verticalAlignment: Text.AlignVCenter
font: fonts.linkFont
......
......@@ -37,7 +37,7 @@ Item {
Rectangle { // background shown on hover over project item
anchors.fill: parent
color: creatorTheme.projectItem_BackgroundColorHover
color: creatorTheme.Welcome_ProjectItem_BackgroundColorHover
visible: mouseArea.containsMouse
}
......@@ -67,7 +67,7 @@ Item {
NativeText {
id: pathText
height: 20
color: creatorTheme.projectItem_TextColorFilepath
color: creatorTheme.Welcome_ProjectItem_TextColorFilepath
font: fonts.smallPath
}
}
......
......@@ -34,7 +34,7 @@ import QtQuick.Controls 1.0
Rectangle {
id: projectList
height: column.height + 200
color: creatorTheme.backgroundColorNormal
color: creatorTheme.Welcome_BackgroundColorNormal
property alias model: repeater.model
// Behavior on verticalScrollBar.opacity {
......
......@@ -37,7 +37,7 @@ Rectangle {
width: 930
height: 27
color: creatorTheme.backgroundColorNormal
color: creatorTheme.Welcome_BackgroundColorNormal
radius: 6
border.color: "#cccccc" // FIXME: make themable
......@@ -56,8 +56,8 @@ Rectangle {
font.pixelSize: 14
placeholderText: qsTr("Search...")
style: TextFieldStyle {
placeholderTextColor: creatorTheme.textColorNormal
textColor: creatorTheme.textColorNormal
placeholderTextColor: creatorTheme.Welcome_TextColorNormal
textColor: creatorTheme.Welcome_TextColorNormal
background: Item {
}
}
......
......@@ -68,7 +68,7 @@ Item {
Rectangle {
z: -4
// background of session item
color: creatorTheme.sessionItem_BackgroundColorHover
color: creatorTheme.Welcome_SessionItem_BackgroundColorHover
anchors.fill: parent
visible: iArea.containsMouse || text.hovered
anchors.topMargin: 1
......@@ -116,7 +116,7 @@ Item {
NativeText {
text: projectsName[index]
font: fonts.boldDescription
color: creatorTheme.textColorNormal
color: creatorTheme.Welcome_TextColorNormal
}
NativeText {
x: 4
......@@ -141,7 +141,7 @@ Item {
maximumLineCount: 2
elide: Text.ElideRight
height: lineCount == 2 ? font.pixelSize * 2 + 4 : font.pixelSize + 2
color: creatorTheme.projectItem_TextColorFilepath
color: creatorTheme.Welcome_ProjectItem_TextColorFilepath
width: delegate.ListView.view.width - 48
MouseArea {
anchors.fill: parent
......@@ -224,8 +224,8 @@ Item {
id: collapseButton
visible: text.hovered || iArea.containsMouse || delegate.expanded
property color color: iArea.containsMouse ? creatorTheme.sessionItemExpanded_BackgroundColorHover
: creatorTheme.sessionItemExpanded_BackgroundColorNormal
property color color: iArea.containsMouse ? creatorTheme.Welcome_SessionItemExpanded_BackgroundColorHover
: creatorTheme.Welcome_SessionItemExpanded_BackgroundColorNormal
anchors.fill: parent
Image {
......@@ -241,7 +241,7 @@ Item {
Rectangle {
color: collapseButton.color
z: -1
radius: creatorTheme.widgetStyle === 'flat' ? 0 : 6
radius: creatorTheme.WidgetStyle === 'StyleFlat' ? 0 : 6
anchors.fill: parent
anchors.topMargin: 1
anchors.bottomMargin: 1
......
......@@ -59,13 +59,13 @@ ColumnLayout {
Component {
id: flatBackground
Rectangle {
color: creatorTheme.sideBar_BackgroundColor
color: creatorTheme.Welcome_SideBar_BackgroundColor
}
}
Loader {
id: topLeftLoader
anchors.fill: parent
sourceComponent: creatorTheme.widgetStyle === 'flat' ? flatBackground : imageBackground;
sourceComponent: creatorTheme.WidgetStyle === 'StyleFlat' ? flatBackground : imageBackground;
}
Tabs {
......@@ -78,7 +78,8 @@ ColumnLayout {
}
Rectangle {
color: creatorTheme.widgetStyle === 'flat' ? creatorTheme.sideBar_BackgroundColor : creatorTheme.dividerColor
color: creatorTheme.WidgetStyle === 'StyleFlat' ?
creatorTheme.Welcome_SideBar_BackgroundColor : creatorTheme.Welcome_DividerColor
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: parent.bottom
......@@ -87,7 +88,8 @@ ColumnLayout {
}
Rectangle {
color: creatorTheme.widgetStyle === 'flat' ? creatorTheme.sideBar_BackgroundColor : creatorTheme.dividerColor
color: creatorTheme.WidgetStyle === 'StyleFlat' ?
creatorTheme.Welcome_SideBar_BackgroundColor : creatorTheme.Welcome_DividerColor
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: parent.bottom
......@@ -97,7 +99,8 @@ ColumnLayout {
}
Rectangle {
color: creatorTheme.widgetStyle === 'flat' ? creatorTheme.sideBar_BackgroundColor : creatorTheme.dividerColor
color: creatorTheme.WidgetStyle === 'StyleFlat' ?
creatorTheme.Welcome_SideBar_BackgroundColor : creatorTheme.Welcome_DividerColor
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: parent.bottom
......@@ -108,7 +111,7 @@ ColumnLayout {
}
Rectangle {
color: creatorTheme.sideBar_BackgroundColor
color: creatorTheme.Welcome_SideBar_BackgroundColor
Layout.fillWidth: true
Layout.preferredWidth: innerColumn.width + 20
......@@ -129,7 +132,7 @@ ColumnLayout {
NativeText {
text: qsTr("New to Qt?")
color: creatorTheme.textColorNormal
color: creatorTheme.Welcome_TextColorNormal
font.pixelSize: 18
}
......@@ -139,7 +142,7 @@ ColumnLayout {
Layout.preferredWidth: innerColumn.width
text: qsTr("Learn how to develop your own applications and explore Qt Creator.")
color: creatorTheme.textColorNormal
color: creatorTheme.Welcome_TextColorNormal
font.pixelSize: 12
wrapMode: Text.WrapAtWordBoundaryOrAnywhere
}
......
......@@ -123,6 +123,32 @@ void Theme::setName(const QString &name)
d->name = name;
}
QVariantHash Theme::values() const
{
QVariantHash result;
const QMetaObject &m = *metaObject();
{
const QMetaEnum e = m.enumerator(m.indexOfEnumerator("ColorRole"));
for (int i = 0, total = e.keyCount(); i < total; ++i) {
const QString key = QLatin1String(e.key(i));
const QPair<QColor, QString> &var = d->colors.at(i);
result.insert(key, var.first);
}
}
{
const QMetaEnum e = m.enumerator(m.indexOfEnumerator("Flag"));
for (int i = 0, total = e.keyCount(); i < total; ++i) {
const QString key = QLatin1String(e.key(i));
result.insert(key, flag(static_cast<Theme::Flag>(i)));
}
}
{
const QMetaEnum e = m.enumerator(m.indexOfEnumerator("WidgetStyle"));
result.insert(QLatin1String("WidgetStyle"), QLatin1String(e.valueToKey(widgetStyle())));
}
return result;
}
static QColor readColor(const QString &color)
{
bool ok = true;
......
......@@ -201,6 +201,8 @@ public:
QString fileName() const;
void setName(const QString &name);
QVariantHash values() const;
void writeSettings(const QString &filename) const;
void readSettings(QSettings &settings);
ThemePrivate *d;
......
/****************************************************************************
**
** Copyright (C) 2014 Thorben Kroeger <thorbenkroeger@gmail.com>.
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://www.qt.io/licensing. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "welcometheme.h"
#include "theme.h"
using namespace Utils;
#define IMPL_COLOR_PROPERTY(propName, enumName) \
QColor WelcomeTheme::propName () const { \
return creatorTheme()->color(Theme::enumName); \
}
#define GRADIENT(x) \
QGradient grad; \
grad.setStops(creatorTheme()->gradient(Theme::x)); \
return grad;
WelcomeTheme::WelcomeTheme(QObject *parent)
: QObject(parent)
{
}
QString WelcomeTheme::widgetStyle() const
{
switch (creatorTheme()->widgetStyle()) {
case Theme::StyleDefault:
return QLatin1String("default");
case Theme::StyleFlat:
return QLatin1String("flat");
}
return QString::null;
}
void WelcomeTheme::notifyThemeChanged()
{
emit themeChanged();
}
IMPL_COLOR_PROPERTY(textColorNormal , Welcome_TextColorNormal )
IMPL_COLOR_PROPERTY(textColorHeading , Welcome_TextColorHeading )
IMPL_COLOR_PROPERTY(backgroundColorNormal , Welcome_BackgroundColorNormal )
IMPL_COLOR_PROPERTY(dividerColor , Welcome_DividerColor )
IMPL_COLOR_PROPERTY(button_BorderColor , Welcome_Button_BorderColor )
IMPL_COLOR_PROPERTY(button_TextColorNormal , Welcome_Button_TextColorNormal )
IMPL_COLOR_PROPERTY(button_TextColorPressed, Welcome_Button_TextColorPressed)
IMPL_COLOR_PROPERTY(link_TextColorNormal , Welcome_Link_TextColorNormal )
IMPL_COLOR_PROPERTY(link_TextColorActive , Welcome_Link_TextColorActive )
IMPL_COLOR_PROPERTY(link_BackgroundColor , Welcome_Link_BackgroundColor )
IMPL_COLOR_PROPERTY(caption_TextColorNormal, Welcome_Caption_TextColorNormal)
IMPL_COLOR_PROPERTY(sideBar_BackgroundColor, Welcome_SideBar_BackgroundColor)
IMPL_COLOR_PROPERTY(projectItem_TextColorFilepath, Welcome_ProjectItem_TextColorFilepath)
IMPL_COLOR_PROPERTY(projectItem_BackgroundColorHover, Welcome_ProjectItem_BackgroundColorHover)
IMPL_COLOR_PROPERTY(sessionItem_BackgroundColorNormal, Welcome_SessionItem_BackgroundColorNormal)
IMPL_COLOR_PROPERTY(sessionItemExpanded_BackgroundColorHover, Welcome_SessionItemExpanded_BackgroundColorHover)
IMPL_COLOR_PROPERTY(sessionItemExpanded_BackgroundColorNormal, Welcome_SessionItemExpanded_BackgroundColorNormal)
IMPL_COLOR_PROPERTY(sessionItem_BackgroundColorHover, Welcome_SessionItem_BackgroundColorHover)
QGradient WelcomeTheme::button_GradientNormal () const { GRADIENT(Welcome_Button_GradientNormal); }
QGradient WelcomeTheme::button_GradientPressed () const { GRADIENT(Welcome_Button_GradientPressed); }
/****************************************************************************
**
** Copyright (C) 2014 Thorben Kroeger <thorbenkroeger@gmail.com>.
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://www.qt.io/licensing. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef WELCOMETHEME_H
#define WELCOMETHEME_H
#include "../utils_global.h"
#include <QStyle>
#define DECLARE_COLOR_PROPERTY(propName) \
Q_PROPERTY(QColor propName READ propName NOTIFY themeChanged) \
QColor propName () const;
/*!
* Expose theme properties to QML (for welcomeplugin)
*/
class QTCREATOR_UTILS_EXPORT WelcomeTheme : public QObject
{
Q_OBJECT
public:
WelcomeTheme(QObject *parent);
DECLARE_COLOR_PROPERTY(textColorNormal)
DECLARE_COLOR_PROPERTY(textColorHeading)
DECLARE_COLOR_PROPERTY(backgroundColorNormal)
DECLARE_COLOR_PROPERTY(dividerColor)
DECLARE_COLOR_PROPERTY(button_BorderColor)
DECLARE_COLOR_PROPERTY(button_TextColorNormal)
DECLARE_COLOR_PROPERTY(button_TextColorPressed)
DECLARE_COLOR_PROPERTY(link_TextColorNormal)
DECLARE_COLOR_PROPERTY(link_TextColorActive)
DECLARE_COLOR_PROPERTY(link_BackgroundColor)
DECLARE_COLOR_PROPERTY(sideBar_BackgroundColor)
DECLARE_COLOR_PROPERTY(caption_TextColorNormal)
DECLARE_COLOR_PROPERTY(projectItem_TextColorFilepath)
DECLARE_COLOR_PROPERTY(projectItem_BackgroundColorHover)
DECLARE_COLOR_PROPERTY(sessionItem_BackgroundColorNormal)
DECLARE_COLOR_PROPERTY(sessionItemExpanded_BackgroundColorNormal)
DECLARE_COLOR_PROPERTY(sessionItemExpanded_BackgroundColorHover)
DECLARE_COLOR_PROPERTY(sessionItem_BackgroundColorHover)
Q_PROPERTY(QGradient button_GradientNormal READ button_GradientNormal NOTIFY themeChanged)
Q_PROPERTY(QGradient button_GradientPressed READ button_GradientPressed NOTIFY themeChanged)
Q_PROPERTY(QString widgetStyle READ widgetStyle CONSTANT)
QString widgetStyle() const;
QGradient button_GradientNormal () const;
QGradient button_GradientPressed () const;
public slots:
void notifyThemeChanged();
signals:
void themeChanged();
};
#endif // WELCOMETHEME_H
......@@ -93,8 +93,7 @@ SOURCES += $$PWD/environment.cpp \
$$PWD/treeviewcombobox.cpp \
$$PWD/proxycredentialsdialog.cpp \
$$PWD/macroexpander.cpp \
$$PWD/theme/theme.cpp \
$$PWD/theme/welcometheme.cpp
$$PWD/theme/theme.cpp
win32:SOURCES += $$PWD/consoleprocess_win.cpp
else:SOURCES += $$PWD/consoleprocess_unix.cpp
......@@ -193,8 +192,7 @@ HEADERS += \
$$PWD/proxycredentialsdialog.h \
$$PWD/macroexpander.h \
$$PWD/theme/theme.h \
$$PWD/theme/theme_p.h \
$$PWD/theme/welcometheme.h
$$PWD/theme/theme_p.h
FORMS += $$PWD/filewizardpage.ui \
$$PWD/projectintropage.ui \
......
......@@ -211,8 +211,6 @@ QtcLibrary {
"theme.cpp",
"theme.h",
"theme_p.h",
"welcometheme.cpp",
"welcometheme.h",
]
}
......
......@@ -45,12 +45,12 @@
#include <utils/iwelcomepage.h>
#include <utils/theme/theme.h>
#include <utils/theme/welcometheme.h>
#include <QVBoxLayout>
#include <QMessageBox>
#include <QDir>
#include <QQmlPropertyMap>
#ifdef USE_QUICK_WIDGET
#include <QtQuickWidgets/QQuickWidget>
......@@ -111,13 +111,12 @@ private:
QuickContainer *m_welcomePage;
QList<QObject*> m_pluginList;
int m_activePlugin;
WelcomeTheme *m_welcomeTheme;
QQmlPropertyMap m_themeProperties;
};
// --- WelcomeMode
WelcomeMode::WelcomeMode() :
m_activePlugin(0),
m_welcomeTheme(new WelcomeTheme(this))
WelcomeMode::WelcomeMode()
: m_activePlugin(0)
{
setDisplayName(tr("Welcome"));
setIcon(QIcon(QLatin1String(":/welcome/images/mode_welcome.png")));
......@@ -163,7 +162,10 @@ WelcomeMode::WelcomeMode() :
void WelcomeMode::onThemeChanged()
{
m_welcomePage->setColor(creatorTheme()->color(Theme::BackgroundColorNormal));
m_welcomeTheme->notifyThemeChanged();
const QVariantHash creatorTheme = Utils::creatorTheme()->values();
QVariantHash::const_iterator it;
for (it = creatorTheme.constBegin(); it != creatorTheme.constEnd(); ++it)
m_themeProperties.insert(it.key(), it.value());
}
WelcomeMode::~WelcomeMode()
......@@ -255,14 +257,9 @@ void WelcomeMode::initPlugins()
ctx->setContextProperty(QLatin1String("pagesModel"), QVariant::fromValue(m_pluginList));
onThemeChanged();
connect(creatorTheme(), &Theme::changed, this, &WelcomeMode::onThemeChanged);
ctx->setContextProperty(QLatin1String("creatorTheme"), m_welcomeTheme);
// FIXME: pass theme class to QML somehow
if (creatorTheme()->widgetStyle() == Theme::StyleFlat)
ctx->setContextProperty(QLatin1String("theme"), QLatin1String("dark"));
else
ctx->setContextProperty(QLatin1String("theme"), QLatin1String("default"));
ctx->setContextProperty(QLatin1String("creatorTheme"), &m_themeProperties);
QString path = resourcePath() + QLatin1String("/welcomescreen/welcomescreen.qml");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment