diff options
author | J-P Nurmi <jpnurmi@theqtcompany.com> | 2015-11-28 11:13:15 +0100 |
---|---|---|
committer | J-P Nurmi <jpnurmi@theqtcompany.com> | 2015-12-02 16:16:38 +0000 |
commit | 4d05ededdd5730af746b6c5dd5f7625ff80533d4 (patch) | |
tree | f528d4ef91b68eb6889ae2bb81299d8e77541bc7 /src/imports/controls/universal | |
parent | b185fc1ac02d4887d2b187a4043b1fdedb95305e (diff) |
Add QQuickStyle
Now that we have a good place for it, we can share a common base class
for QQuickMaterialStyle and QQuickUniversalStyle. QQuickStyle implements
the inheritance pattern for attached styles, in one place.
Change-Id: I459d98f96ce7c6de1ce7ef716e859f459278d8ad
Reviewed-by: Mitch Curtis <mitch.curtis@theqtcompany.com>
Diffstat (limited to 'src/imports/controls/universal')
-rw-r--r-- | src/imports/controls/universal/qquickuniversalstyle.cpp | 94 | ||||
-rw-r--r-- | src/imports/controls/universal/qquickuniversalstyle_p.h | 17 | ||||
-rw-r--r-- | src/imports/controls/universal/universal.pro | 1 |
3 files changed, 42 insertions, 70 deletions
diff --git a/src/imports/controls/universal/qquickuniversalstyle.cpp b/src/imports/controls/universal/qquickuniversalstyle.cpp index 10c4e812..f2bfd70d 100644 --- a/src/imports/controls/universal/qquickuniversalstyle.cpp +++ b/src/imports/controls/universal/qquickuniversalstyle.cpp @@ -35,10 +35,9 @@ ****************************************************************************/ #include "qquickuniversalstyle_p.h" -#include "qquickstyle_p.h" -#include <QtGui/qguiapplication.h> -#include <QtQuick/private/qquickitem_p.h> +#include <QtCore/qdebug.h> +#include <QtLabsControls/private/qquickstyle_p.h> QT_BEGIN_NAMESPACE @@ -134,34 +133,15 @@ static QColor qquickuniversal_accent_color(QQuickUniversalStyle::Accent accent) return colors[accent]; } -QQuickUniversalStyle::QQuickUniversalStyle(QObject *parent) : QObject(parent), +QQuickUniversalStyle::QQuickUniversalStyle(QObject *parent) : QQuickStyle(parent), m_hasTheme(false), m_hasAccent(false), m_theme(DefaultTheme), m_accent(DefaultAccent) { - QQuickItem *item = qobject_cast<QQuickItem *>(parent); - if (item) - QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Parent); -} - -QQuickUniversalStyle::~QQuickUniversalStyle() -{ - QQuickItem *item = qobject_cast<QQuickItem *>(parent()); - if (item) - QQuickItemPrivate::get(item)->removeItemChangeListener(this, QQuickItemPrivate::Parent); - - reparent(Q_NULLPTR); + init(); // TODO: lazy init? } QQuickUniversalStyle *QQuickUniversalStyle::qmlAttachedProperties(QObject *object) { - QQuickUniversalStyle *style = new QQuickUniversalStyle(object); - QQuickUniversalStyle *parentStyle = QQuickStyle::findParent<QQuickUniversalStyle>(object); - if (parentStyle) - style->reparent(parentStyle); - - QList<QQuickUniversalStyle *> children = QQuickStyle::findChildren<QQuickUniversalStyle>(object); - foreach (QQuickUniversalStyle *child, children) - child->reparent(style); - return style; + return new QQuickUniversalStyle(object); } QQuickUniversalStyle::Theme QQuickUniversalStyle::theme() const @@ -174,8 +154,7 @@ void QQuickUniversalStyle::setTheme(Theme theme) m_hasTheme = true; if (m_theme != theme) { m_theme = theme; - foreach (QQuickUniversalStyle *child, m_childStyles) - child->inheritTheme(theme); + propagateTheme(); emit themeChanged(); emit paletteChanged(); } @@ -185,19 +164,27 @@ void QQuickUniversalStyle::inheritTheme(Theme theme) { if (!m_hasTheme && m_theme != theme) { m_theme = theme; - foreach (QQuickUniversalStyle *child, m_childStyles) - child->inheritTheme(theme); + propagateTheme(); emit themeChanged(); emit paletteChanged(); } } +void QQuickUniversalStyle::propagateTheme() +{ + foreach (QQuickStyle *child, childStyles()) { + QQuickUniversalStyle *universal = qobject_cast<QQuickUniversalStyle *>(child); + if (universal) + universal->inheritTheme(m_theme); + } +} + void QQuickUniversalStyle::resetTheme() { if (m_hasTheme) { m_hasTheme = false; - QQuickUniversalStyle *parentStyle = QQuickStyle::findParent<QQuickUniversalStyle>(parent()); - inheritTheme(parentStyle ? parentStyle->theme() : DefaultTheme); + QQuickUniversalStyle *universal = qobject_cast<QQuickUniversalStyle *>(parentStyle()); + inheritTheme(universal ? universal->theme() : DefaultTheme); } } @@ -215,8 +202,7 @@ void QQuickUniversalStyle::setAccent(Accent accent) m_hasAccent = true; if (m_accent != accent) { m_accent = accent; - foreach (QQuickUniversalStyle *child, m_childStyles) - child->inheritAccent(accent); + propagateAccent(); emit accentChanged(); } } @@ -225,18 +211,26 @@ void QQuickUniversalStyle::inheritAccent(Accent accent) { if (!m_hasAccent && m_accent != accent) { m_accent = accent; - foreach (QQuickUniversalStyle *child, m_childStyles) - child->inheritAccent(accent); + propagateAccent(); emit accentChanged(); } } +void QQuickUniversalStyle::propagateAccent() +{ + foreach (QQuickStyle *child, childStyles()) { + QQuickUniversalStyle *universal = qobject_cast<QQuickUniversalStyle *>(child); + if (universal) + universal->inheritAccent(m_accent); + } +} + void QQuickUniversalStyle::resetAccent() { if (m_hasAccent) { m_hasAccent = false; - QQuickUniversalStyle *parentStyle = QQuickStyle::findParent<QQuickUniversalStyle>(parent()); - inheritAccent(parentStyle ? parentStyle->accent() : DefaultAccent); + QQuickUniversalStyle *universal = qobject_cast<QQuickUniversalStyle *>(parentStyle()); + inheritAccent(universal ? universal->accent() : DefaultAccent); } } @@ -370,27 +364,13 @@ QColor QQuickUniversalStyle::getColor(SystemColor role) const return m_theme == QQuickUniversalStyle::Dark ? qquickuniversal_dark_color(role) : qquickuniversal_light_color(role); } -void QQuickUniversalStyle::reparent(QQuickUniversalStyle *style) -{ - if (m_parentStyle != style) { - if (m_parentStyle) - m_parentStyle->m_childStyles.remove(this); - m_parentStyle = style; - if (style) { - style->m_childStyles.insert(this); - inheritTheme(style->theme()); - inheritAccent(style->accent()); - } - } -} - -void QQuickUniversalStyle::itemParentChanged(QQuickItem *item, QQuickItem *parent) +void QQuickUniversalStyle::parentStyleChange(QQuickStyle *newParent, QQuickStyle *oldParent) { - QQuickUniversalStyle *style = QQuickStyle::instance<QQuickUniversalStyle>(item); - if (style) { - QQuickUniversalStyle *parentStyle = QQuickStyle::findParent<QQuickUniversalStyle>(parent); - if (parentStyle) - style->reparent(parentStyle); + Q_UNUSED(oldParent); + QQuickUniversalStyle *universal = qobject_cast<QQuickUniversalStyle *>(newParent); + if (universal) { + inheritTheme(universal->theme()); + inheritAccent(universal->accent()); } } diff --git a/src/imports/controls/universal/qquickuniversalstyle_p.h b/src/imports/controls/universal/qquickuniversalstyle_p.h index e8e9eafb..3bf0bb5d 100644 --- a/src/imports/controls/universal/qquickuniversalstyle_p.h +++ b/src/imports/controls/universal/qquickuniversalstyle_p.h @@ -48,18 +48,14 @@ // We mean it. // -#include <QtCore/qset.h> -#include <QtCore/qpointer.h> -#include <QtCore/qobject.h> #include <QtGui/qcolor.h> -#include <QtQml/qqml.h> -#include <QtQuick/private/qquickitemchangelistener_p.h> +#include <QtLabsControls/private/qquickstyle_p.h> QT_BEGIN_NAMESPACE class QQuickUniversalStylePrivate; -class QQuickUniversalStyle : public QObject, public QQuickItemChangeListener +class QQuickUniversalStyle : public QQuickStyle { Q_OBJECT Q_PROPERTY(Theme theme READ theme WRITE setTheme RESET resetTheme NOTIFY themeChanged FINAL) @@ -93,7 +89,6 @@ class QQuickUniversalStyle : public QObject, public QQuickItemChangeListener public: explicit QQuickUniversalStyle(QObject *parent = Q_NULLPTR); - ~QQuickUniversalStyle(); static QQuickUniversalStyle *qmlAttachedProperties(QObject *object); @@ -103,6 +98,7 @@ public: Theme theme() const; void setTheme(Theme theme); void inheritTheme(Theme theme); + void propagateTheme(); void resetTheme(); enum Accent { @@ -132,10 +128,10 @@ public: Accent accent() const; void setAccent(Accent accent); void inheritAccent(Accent accent); + void propagateAccent(); void resetAccent(); QColor accentColor() const; - QColor altHighColor() const; QColor altLowColor() const; QColor altMediumColor() const; @@ -196,16 +192,13 @@ Q_SIGNALS: void paletteChanged(); protected: - void reparent(QQuickUniversalStyle *parent); - void itemParentChanged(QQuickItem *item, QQuickItem *parent) Q_DECL_OVERRIDE; + void parentStyleChange(QQuickStyle *newParent, QQuickStyle *oldParent) Q_DECL_OVERRIDE; private: bool m_hasTheme; bool m_hasAccent; QQuickUniversalStyle::Theme m_theme; QQuickUniversalStyle::Accent m_accent; - QPointer<QQuickUniversalStyle> m_parentStyle; - QSet<QQuickUniversalStyle *> m_childStyles; }; QT_END_NAMESPACE diff --git a/src/imports/controls/universal/universal.pro b/src/imports/controls/universal/universal.pro index 4e96256b..41cef97d 100644 --- a/src/imports/controls/universal/universal.pro +++ b/src/imports/controls/universal/universal.pro @@ -17,7 +17,6 @@ RESOURCES += \ $$PWD/qtlabsuniversalstyleplugin.qrc include(universal.pri) -include(../shared/shared.pri) CONFIG += no_cxx_module load(qml_plugin) |