From 4d05ededdd5730af746b6c5dd5f7625ff80533d4 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Sat, 28 Nov 2015 11:13:15 +0100 Subject: 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 --- src/controls/controls.pri | 2 + src/controls/qquickstyle.cpp | 201 +++++++++++++++++++++ src/controls/qquickstyle_p.h | 86 +++++++++ src/imports/controls/controls.pro | 1 - src/imports/controls/material/material.pro | 1 - .../controls/material/qquickmaterialstyle.cpp | 120 +++++------- .../controls/material/qquickmaterialstyle_p.h | 18 +- src/imports/controls/shared/qquickstyle_p.h | 157 ---------------- src/imports/controls/shared/shared.pri | 4 - .../controls/universal/qquickuniversalstyle.cpp | 94 ++++------ .../controls/universal/qquickuniversalstyle_p.h | 17 +- src/imports/controls/universal/universal.pro | 1 - 12 files changed, 387 insertions(+), 315 deletions(-) create mode 100644 src/controls/qquickstyle.cpp create mode 100644 src/controls/qquickstyle_p.h delete mode 100644 src/imports/controls/shared/qquickstyle_p.h delete mode 100644 src/imports/controls/shared/shared.pri diff --git a/src/controls/controls.pri b/src/controls/controls.pri index 4db9fea4..005ee839 100644 --- a/src/controls/controls.pri +++ b/src/controls/controls.pri @@ -1,10 +1,12 @@ HEADERS += \ $$PWD/qquickproxytheme_p.h \ + $$PWD/qquickstyle_p.h \ $$PWD/qquickstyleselector_p.h \ $$PWD/qquickstyleselector_p_p.h \ $$PWD/qquickpaddedrectangle_p.h SOURCES += \ $$PWD/qquickproxytheme.cpp \ + $$PWD/qquickstyle.cpp \ $$PWD/qquickstyleselector.cpp \ $$PWD/qquickpaddedrectangle.cpp diff --git a/src/controls/qquickstyle.cpp b/src/controls/qquickstyle.cpp new file mode 100644 index 00000000..b83f8c2a --- /dev/null +++ b/src/controls/qquickstyle.cpp @@ -0,0 +1,201 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Labs Controls module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. 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 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qquickstyle_p.h" + +#include + +QT_BEGIN_NAMESPACE + +static QQuickStyle *attachedStyle(const QMetaObject *type, QObject *object, bool create = false) +{ + if (!object) + return Q_NULLPTR; + int idx = -1; + return qobject_cast(qmlAttachedPropertiesObject(&idx, object, type, create)); +} + +static QQuickStyle *findParentStyle(const QMetaObject *type, QObject *object) +{ + QQuickItem *item = qobject_cast(object); + if (item) { + // lookup parent items + QQuickItem *parent = item->parentItem(); + while (parent) { + QQuickStyle *style = attachedStyle(type, parent); + if (style) + return style; + parent = parent->parentItem(); + } + + // fallback to item's window + QQuickWindow *window = item->window(); + if (window) { + QQuickStyle *style = attachedStyle(type, window); + if (style) + return style; + } + } + + // lookup parent window + QQuickWindow *window = qobject_cast(object); + if (window) { + QQuickWindow *parentWindow = qobject_cast(window->parent()); + if (parentWindow) { + QQuickStyle *style = attachedStyle(type, window); + if (style) + return style; + } + } + + // fallback to engine (global) + if (object) { + QQmlEngine *engine = qmlEngine(object); + if (engine) { + QByteArray name = QByteArray("_q_") + type->className(); + QQuickStyle *style = engine->property(name).value(); + if (!style) { + style = attachedStyle(type, engine, true); + engine->setProperty(name, QVariant::fromValue(style)); + } + return style; + } + } + + return Q_NULLPTR; +} + +static QList findChildStyles(const QMetaObject *type, QObject *object) +{ + QList children; + + QQuickItem *item = qobject_cast(object); + if (!item) { + QQuickWindow *window = qobject_cast(object); + if (window) { + item = window->contentItem(); + + foreach (QObject *child, window->children()) { + QQuickWindow *childWindow = qobject_cast(child); + if (childWindow) { + QQuickStyle *style = attachedStyle(type, childWindow); + if (style) + children += style; + } + } + } + } + + if (item) { + foreach (QQuickItem *child, item->childItems()) { + QQuickStyle *style = attachedStyle(type, child); + if (style) + children += style; + else + children += findChildStyles(type, child); + } + } + + return children; +} + +QQuickStyle::QQuickStyle(QObject *parent) : QObject(parent) +{ + QQuickItem *item = qobject_cast(parent); + if (item) + QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Parent); +} + +QQuickStyle::~QQuickStyle() +{ + QQuickItem *item = qobject_cast(parent()); + if (item) + QQuickItemPrivate::get(item)->removeItemChangeListener(this, QQuickItemPrivate::Parent); + + setParentStyle(Q_NULLPTR); +} + +QList QQuickStyle::childStyles() const +{ + return m_childStyles; +} + +QQuickStyle *QQuickStyle::parentStyle() const +{ + return m_parentStyle; +} + +void QQuickStyle::setParentStyle(QQuickStyle *style) +{ + if (m_parentStyle != style) { + QQuickStyle *oldParent = m_parentStyle; + if (m_parentStyle) + m_parentStyle->m_childStyles.removeOne(this); + m_parentStyle = style; + if (style) + style->m_childStyles.append(this); + parentStyleChange(style, oldParent); + } +} + +void QQuickStyle::init() +{ + QQuickStyle *parentStyle = findParentStyle(metaObject(), parent()); + if (parentStyle) + setParentStyle(parentStyle); + + QList children = findChildStyles(metaObject(), parent()); + foreach (QQuickStyle *child, children) + child->setParentStyle(this); +} + +void QQuickStyle::parentStyleChange(QQuickStyle *newParent, QQuickStyle *oldParent) +{ + Q_UNUSED(newParent); + Q_UNUSED(oldParent); +} + +void QQuickStyle::itemParentChanged(QQuickItem *item, QQuickItem *parent) +{ + QQuickStyle *style = attachedStyle(metaObject(), item); + if (style) { + QQuickStyle *parentStyle = findParentStyle(metaObject(), parent); + if (parentStyle) + style->setParentStyle(parentStyle); + } +} + +QT_END_NAMESPACE diff --git a/src/controls/qquickstyle_p.h b/src/controls/qquickstyle_p.h new file mode 100644 index 00000000..3d4431cd --- /dev/null +++ b/src/controls/qquickstyle_p.h @@ -0,0 +1,86 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Labs Controls module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. 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 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKSTYLE_P_H +#define QQUICKSTYLE_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class QQuickStyle : public QObject, public QQuickItemChangeListener +{ + Q_OBJECT + +public: + explicit QQuickStyle(QObject *parent = Q_NULLPTR); + ~QQuickStyle(); + +protected: + void init(); + + QList childStyles() const; + + QQuickStyle *parentStyle() const; + void setParentStyle(QQuickStyle *style); + + virtual void parentStyleChange(QQuickStyle *newParent, QQuickStyle *oldParent); + + void itemParentChanged(QQuickItem *item, QQuickItem *parent) Q_DECL_OVERRIDE; + +private: + QList m_childStyles; + QPointer m_parentStyle; +}; + +QT_END_NAMESPACE + +#endif // QQUICKSTYLE_P_H diff --git a/src/imports/controls/controls.pro b/src/imports/controls/controls.pro index 8c83eb6a..c3c5702c 100644 --- a/src/imports/controls/controls.pro +++ b/src/imports/controls/controls.pro @@ -19,7 +19,6 @@ RESOURCES += \ $$PWD/qtlabscontrolsplugin.qrc include(controls.pri) -include(shared/shared.pri) include(designer/designer.pri) CONFIG += no_cxx_module diff --git a/src/imports/controls/material/material.pro b/src/imports/controls/material/material.pro index 8391740d..06b9ed7d 100644 --- a/src/imports/controls/material/material.pro +++ b/src/imports/controls/material/material.pro @@ -17,7 +17,6 @@ RESOURCES += \ $$PWD/qtlabsmaterialstyleplugin.qrc include(material.pri) -include(../shared/shared.pri) CONFIG += no_cxx_module load(qml_plugin) diff --git a/src/imports/controls/material/qquickmaterialstyle.cpp b/src/imports/controls/material/qquickmaterialstyle.cpp index 24fbb62c..a3d2329a 100644 --- a/src/imports/controls/material/qquickmaterialstyle.cpp +++ b/src/imports/controls/material/qquickmaterialstyle.cpp @@ -35,12 +35,8 @@ ****************************************************************************/ #include "qquickmaterialstyle_p.h" -#include "qquickstyle_p.h" -#include -#include -#include -#include +#include QT_BEGIN_NAMESPACE @@ -419,7 +415,7 @@ static const QColor switchDisabledTrackColorDark = "#19FFFFFF"; static const QColor checkBoxUncheckedRippleColorLight = "#10000000"; static const QColor checkBoxUncheckedRippleColorDark = "#20FFFFFF"; -QQuickMaterialStyle::QQuickMaterialStyle(QObject *parent) : QObject(parent), +QQuickMaterialStyle::QQuickMaterialStyle(QObject *parent) : QQuickStyle(parent), m_explicitTheme(false), m_explicitPrimary(false), m_explicitAccent(false), @@ -427,31 +423,12 @@ QQuickMaterialStyle::QQuickMaterialStyle(QObject *parent) : QObject(parent), m_primary(defaultPrimary), m_accent(defaultAccent) { - QQuickItem *item = qobject_cast(parent); - if (item) - QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Parent); -} - -QQuickMaterialStyle::~QQuickMaterialStyle() -{ - QQuickItem *item = qobject_cast(parent()); - if (item) - QQuickItemPrivate::get(item)->removeItemChangeListener(this, QQuickItemPrivate::Parent); - - reparent(Q_NULLPTR); + init(); // TODO: lazy init? } QQuickMaterialStyle *QQuickMaterialStyle::qmlAttachedProperties(QObject *object) { - QQuickMaterialStyle *style = new QQuickMaterialStyle(object); - QQuickMaterialStyle *parent = QQuickStyle::findParent(object); - if (parent) - style->reparent(parent); - - QList childStyles = QQuickStyle::findChildren(object); - foreach (QQuickMaterialStyle *child, childStyles) - child->reparent(style); - return style; + return new QQuickMaterialStyle(object); } QQuickMaterialStyle::Theme QQuickMaterialStyle::theme() const @@ -464,9 +441,7 @@ void QQuickMaterialStyle::setTheme(Theme theme) m_explicitTheme = true; if (m_theme != theme) { m_theme = theme; - foreach (QQuickMaterialStyle *child, m_childStyles) { - child->inheritTheme(theme); - } + propagateTheme(); emit themeChanged(); emit paletteChanged(); } @@ -476,20 +451,27 @@ void QQuickMaterialStyle::inheritTheme(Theme theme) { if (!m_explicitTheme && m_theme != theme) { m_theme = theme; - foreach (QQuickMaterialStyle *child, m_childStyles) { - child->inheritTheme(theme); - } + propagateTheme(); emit themeChanged(); emit paletteChanged(); } } +void QQuickMaterialStyle::propagateTheme() +{ + foreach (QQuickStyle *child, childStyles()) { + QQuickMaterialStyle *material = qobject_cast(child); + if (material) + material->inheritTheme(m_theme); + } +} + void QQuickMaterialStyle::resetTheme() { if (m_explicitTheme) { m_explicitTheme = false; - QQuickMaterialStyle *attachedParent = QQuickStyle::findParent(parent()); - inheritTheme(attachedParent ? attachedParent->theme() : defaultTheme); + QQuickMaterialStyle *material = qobject_cast(parentStyle()); + inheritTheme(material ? material->theme() : defaultTheme); } } @@ -503,11 +485,9 @@ void QQuickMaterialStyle::setPrimary(QQuickMaterialStyle::Color color) m_explicitPrimary = true; if (m_primary != color) { m_primary = color; + propagatePrimary(); emit primaryChanged(); emit paletteChanged(); - - foreach (QQuickMaterialStyle *child, m_childStyles) - child->inheritPrimary(color); } } @@ -515,18 +495,26 @@ void QQuickMaterialStyle::inheritPrimary(QQuickMaterialStyle::Color color) { if (!m_explicitPrimary && m_primary != color) { m_primary = color; - foreach (QQuickMaterialStyle *child, m_childStyles) - child->inheritPrimary(color); + propagatePrimary(); emit primaryChanged(); } } +void QQuickMaterialStyle::propagatePrimary() +{ + foreach (QQuickStyle *child, childStyles()) { + QQuickMaterialStyle *material = qobject_cast(child); + if (material) + material->inheritPrimary(m_primary); + } +} + void QQuickMaterialStyle::resetPrimary() { if (m_explicitPrimary) { m_explicitPrimary = false; - QQuickMaterialStyle *attachedParent = QQuickStyle::findParent(parent()); - inheritPrimary(attachedParent ? attachedParent->primary() : defaultPrimary); + QQuickMaterialStyle *material = qobject_cast(parentStyle()); + inheritPrimary(material ? material->primary() : defaultPrimary); } } @@ -540,11 +528,9 @@ void QQuickMaterialStyle::setAccent(QQuickMaterialStyle::Color color) m_explicitAccent = true; if (m_accent != color) { m_accent = color; + propagateAccent(); emit accentChanged(); emit paletteChanged(); - - foreach (QQuickMaterialStyle *child, m_childStyles) - child->inheritAccent(color); } } @@ -552,18 +538,26 @@ void QQuickMaterialStyle::inheritAccent(QQuickMaterialStyle::Color color) { if (!m_explicitAccent && m_accent != color) { m_accent = color; - foreach (QQuickMaterialStyle *child, m_childStyles) - child->inheritAccent(color); + propagateAccent(); emit accentChanged(); } } +void QQuickMaterialStyle::propagateAccent() +{ + foreach (QQuickStyle *child, childStyles()) { + QQuickMaterialStyle *material = qobject_cast(child); + if (material) + material->inheritAccent(m_accent); + } +} + void QQuickMaterialStyle::resetAccent() { if (m_explicitAccent) { m_explicitAccent = false; - QQuickMaterialStyle *attachedParent = QQuickStyle::findParent(parent()); - inheritAccent(attachedParent ? attachedParent->accent() : defaultAccent); + QQuickMaterialStyle *material = qobject_cast(parentStyle()); + inheritAccent(material ? material->accent() : defaultAccent); } } @@ -743,28 +737,14 @@ QColor QQuickMaterialStyle::color(QQuickMaterialStyle::Color color, QQuickMateri return colors[color][shade]; } -void QQuickMaterialStyle::reparent(QQuickMaterialStyle *style) -{ - if (m_parentStyle != style) { - if (m_parentStyle) - m_parentStyle->m_childStyles.remove(this); - m_parentStyle = style; - if (style) { - style->m_childStyles.insert(this); - inheritPrimary(style->primary()); - inheritAccent(style->accent()); - inheritTheme(style->theme()); - } - } -} - -void QQuickMaterialStyle::itemParentChanged(QQuickItem *item, QQuickItem *parentItem) +void QQuickMaterialStyle::parentStyleChange(QQuickStyle *newParent, QQuickStyle *oldParent) { - QQuickMaterialStyle *style = QQuickStyle::instance(item); - if (style) { - QQuickMaterialStyle *parent = QQuickStyle::findParent(parentItem); - if (parent) - style->reparent(parent); + Q_UNUSED(oldParent); + QQuickMaterialStyle *material = qobject_cast(newParent); + if (material) { + inheritPrimary(material->primary()); + inheritAccent(material->accent()); + inheritTheme(material->theme()); } } diff --git a/src/imports/controls/material/qquickmaterialstyle_p.h b/src/imports/controls/material/qquickmaterialstyle_p.h index 1d2033e7..69891b9c 100644 --- a/src/imports/controls/material/qquickmaterialstyle_p.h +++ b/src/imports/controls/material/qquickmaterialstyle_p.h @@ -48,18 +48,14 @@ // We mean it. // -#include -#include #include -#include -#include -#include +#include QT_BEGIN_NAMESPACE class QQuickMaterialStylePrivate; -class QQuickMaterialStyle : public QObject, public QQuickItemChangeListener +class QQuickMaterialStyle : public QQuickStyle { Q_OBJECT Q_PROPERTY(Theme theme READ theme WRITE setTheme RESET resetTheme NOTIFY themeChanged FINAL) @@ -147,13 +143,13 @@ public: Q_ENUM(Shade) explicit QQuickMaterialStyle(QObject *parent = Q_NULLPTR); - ~QQuickMaterialStyle(); static QQuickMaterialStyle *qmlAttachedProperties(QObject *object); Theme theme() const; void setTheme(Theme theme); void inheritTheme(Theme theme); + void propagateTheme(); void resetTheme(); QColor primaryColorLight() const; @@ -161,6 +157,7 @@ public: Color primary() const; void setPrimary(Color color); void inheritPrimary(Color color); + void propagatePrimary(); void resetPrimary(); QColor primaryColorDark() const; @@ -168,6 +165,7 @@ public: Color accent() const; void setAccent(Color color); void inheritAccent(Color color); + void propagateAccent(); void resetAccent(); QColor accentColor() const; @@ -211,13 +209,9 @@ Q_SIGNALS: void paletteChanged(); protected: - void reparent(QQuickMaterialStyle *theme); - void itemParentChanged(QQuickItem *item, QQuickItem *parent) Q_DECL_OVERRIDE; + void parentStyleChange(QQuickStyle *newParent, QQuickStyle *oldParent) Q_DECL_OVERRIDE; private: - QPointer m_parentStyle; - QSet m_childStyles; - bool m_explicitTheme; bool m_explicitPrimary; bool m_explicitAccent; diff --git a/src/imports/controls/shared/qquickstyle_p.h b/src/imports/controls/shared/qquickstyle_p.h deleted file mode 100644 index 0fd4f612..00000000 --- a/src/imports/controls/shared/qquickstyle_p.h +++ /dev/null @@ -1,157 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2015 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ -** -** This file is part of the Qt Labs Controls module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL3$ -** 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 The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. 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 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPLv3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or later as published by the Free -** Software Foundation and appearing in the file LICENSE.GPL included in -** the packaging of this file. Please review the following information to -** ensure the GNU General Public License version 2.0 requirements will be -** met: http://www.gnu.org/licenses/gpl-2.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QQUICKSTYLE_P_H -#define QQUICKSTYLE_P_H - -// -// W A R N I N G -// ------------- -// -// This file is not part of the Qt API. It exists purely as an -// implementation detail. This header file may change from version to -// version without notice, or even be removed. -// -// We mean it. -// - -#include -#include -#include -#include - -QT_BEGIN_NAMESPACE - -namespace QQuickStyle -{ - template - static T *instance(QObject *object) - { - if (object) - return qobject_cast(qmlAttachedPropertiesObject(object, false)); - return Q_NULLPTR; - } - - template - static T *findParent(QObject *object) - { - QQuickItem *item = qobject_cast(object); - if (item) { - // lookup parent items - QQuickItem *parent = item->parentItem(); - while (parent) { - T *attached = instance(parent); - if (attached) - return attached; - parent = parent->parentItem(); - } - - // fallback to item's window - QQuickWindow *window = item->window(); - if (window) { - T *attached = instance(window); - if (attached) - return attached; - } - } - - // lookup parent window - QQuickWindow *window = qobject_cast(object); - if (window) { - QQuickWindow *parentWindow = qobject_cast(window->parent()); - if (parentWindow) { - T *attached = instance(window); - if (attached) - return attached; - } - } - - // fallback to engine (global) - if (object) { - QQmlEngine *engine = qmlEngine(object); - if (engine) { - QByteArray name = QByteArray("_q_") + T::staticMetaObject.className(); - T *instance = engine->property(name).value(); - if (!instance) { - instance = new T(engine); - engine->setProperty(name, QVariant::fromValue(instance)); - } - return instance; - } - } - - return Q_NULLPTR; - } - - template - static QList findChildren(QObject *object) - { - QList children; - - QQuickItem *item = qobject_cast(object); - if (!item) { - QQuickWindow *window = qobject_cast(object); - if (window) { - item = window->contentItem(); - - foreach (QObject *child, window->children()) { - QQuickWindow *childWindow = qobject_cast(child); - if (childWindow) { - T *attached = instance(childWindow); - if (attached) - children += attached; - } - } - } - } - - if (item) { - foreach (QQuickItem *child, item->childItems()) { - T *attached = instance(child); - if (attached) - children += attached; - else - children += findChildren(child); - } - } - - return children; - } -} - -QT_END_NAMESPACE - -#endif // QQUICKSTYLE_P_H diff --git a/src/imports/controls/shared/shared.pri b/src/imports/controls/shared/shared.pri deleted file mode 100644 index 2979a615..00000000 --- a/src/imports/controls/shared/shared.pri +++ /dev/null @@ -1,4 +0,0 @@ -INCLUDEPATH += $$PWD - -HEADERS += \ - $$PWD/qquickstyle_p.h 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 -#include +#include +#include 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(parent); - if (item) - QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Parent); -} - -QQuickUniversalStyle::~QQuickUniversalStyle() -{ - QQuickItem *item = qobject_cast(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(object); - if (parentStyle) - style->reparent(parentStyle); - - QList children = QQuickStyle::findChildren(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(child); + if (universal) + universal->inheritTheme(m_theme); + } +} + void QQuickUniversalStyle::resetTheme() { if (m_hasTheme) { m_hasTheme = false; - QQuickUniversalStyle *parentStyle = QQuickStyle::findParent(parent()); - inheritTheme(parentStyle ? parentStyle->theme() : DefaultTheme); + QQuickUniversalStyle *universal = qobject_cast(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(child); + if (universal) + universal->inheritAccent(m_accent); + } +} + void QQuickUniversalStyle::resetAccent() { if (m_hasAccent) { m_hasAccent = false; - QQuickUniversalStyle *parentStyle = QQuickStyle::findParent(parent()); - inheritAccent(parentStyle ? parentStyle->accent() : DefaultAccent); + QQuickUniversalStyle *universal = qobject_cast(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(item); - if (style) { - QQuickUniversalStyle *parentStyle = QQuickStyle::findParent(parent); - if (parentStyle) - style->reparent(parentStyle); + Q_UNUSED(oldParent); + QQuickUniversalStyle *universal = qobject_cast(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 -#include -#include #include -#include -#include +#include 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 m_parentStyle; - QSet 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) -- cgit v1.2.3