From f765bca3f7d34db01d1fc453d4ebce66c278fe2a Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 22 Oct 2015 14:04:46 +0200 Subject: Share style inheritance related helpers Usage: # +material/material.pro include(../shared/shared.pri) // qquickmaterialstyle.cpp #include "qquickstyle_p.h" QQuickMaterialStyle *style = QQuickStyle::instance(item); // ... QQuickMaterialStyle *parent = QQuickStyle::findParent(this); // ... QList children = QQuickStyle::findChildren(object); // ... Change-Id: I2c01a1aa36805f16d83fa60ab25b2cc29e6a43cc Reviewed-by: Mitch Curtis --- src/imports/controls/controls.pro | 1 + src/imports/controls/qquicktheme.cpp | 116 +++----------------- src/imports/controls/qquicktheme_p.h | 1 + src/imports/controls/shared/qquickstyle_p.h | 157 ++++++++++++++++++++++++++++ src/imports/controls/shared/shared.pri | 4 + 5 files changed, 179 insertions(+), 100 deletions(-) create mode 100644 src/imports/controls/shared/qquickstyle_p.h create mode 100644 src/imports/controls/shared/shared.pri diff --git a/src/imports/controls/controls.pro b/src/imports/controls/controls.pro index 14e085dd..a7aec7e5 100644 --- a/src/imports/controls/controls.pro +++ b/src/imports/controls/controls.pro @@ -20,6 +20,7 @@ OTHER_FILES += \ $$PWD/theme.json include(controls.pri) +include(shared/shared.pri) include(designer/designer.pri) CONFIG += no_cxx_module diff --git a/src/imports/controls/qquicktheme.cpp b/src/imports/controls/qquicktheme.cpp index 7fec28c9..f0345df2 100644 --- a/src/imports/controls/qquicktheme.cpp +++ b/src/imports/controls/qquicktheme.cpp @@ -36,6 +36,7 @@ #include "qquicktheme_p.h" #include "qquickthemedata_p.h" +#include "qquickstyle_p.h" #include #include @@ -100,100 +101,6 @@ QT_BEGIN_NAMESPACE Q_GLOBAL_STATIC_WITH_ARGS(QQuickThemeData, globalThemeData, (QString::fromLatin1(":/qtlabscontrols/theme.json"))) -static QQuickThemeAttached *themeInstance(QQmlEngine *engine) -{ - QQuickThemeAttached *theme = engine->property("_q_quicktheme").value(); - if (!theme) { - theme = new QQuickThemeAttached(*globalThemeData(), engine); - engine->setProperty("_q_quicktheme", QVariant::fromValue(theme)); - } - return theme; -} - -static QQuickThemeAttached *attachedTheme(QObject *object) -{ - if (object) - return qobject_cast(qmlAttachedPropertiesObject(object, false)); - return Q_NULLPTR; -} - -static QQuickThemeAttached *findParentTheme(QObject *object) -{ - QQuickItem *item = qobject_cast(object); - if (item) { - // lookup parent items - QQuickItem *parent = item->parentItem(); - while (parent) { - QQuickThemeAttached *attached = attachedTheme(parent); - if (attached) - return attached; - parent = parent->parentItem(); - } - - // fallback to item's window theme - QQuickWindow *window = item->window(); - if (window) { - QQuickThemeAttached *attached = attachedTheme(window); - if (attached) - return attached; - } - } - - // lookup parent window theme - QQuickWindow *window = qobject_cast(object); - if (window) { - QQuickWindow *parentWindow = qobject_cast(window->parent()); - if (parentWindow) { - QQuickThemeAttached *attached = attachedTheme(window); - if (attached) - return attached; - } - } - - // fallback to global theme - if (object) { - QQmlEngine *engine = qmlEngine(object); - if (engine) - return themeInstance(engine); - } - - return Q_NULLPTR; -} - -static QList findChildThemes(QObject *object) -{ - QList themes; - - 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) { - QQuickThemeAttached *theme = attachedTheme(childWindow); - if (theme) - themes += theme; - } - } - } - } - - if (item) { - foreach (QQuickItem *child, item->childItems()) { - QQuickThemeAttached *theme = attachedTheme(child); - if (theme) - themes += theme; - else - themes += findChildThemes(child); - } - } - - return themes; -} - class QQuickThemeAttachedPrivate : public QObjectPrivate, public QQuickItemChangeListener { Q_DECLARE_PUBLIC(QQuickThemeAttached) @@ -433,20 +340,29 @@ void QQuickThemeAttachedPrivate::inherit(QQuickThemeAttached *theme) const QQuickThemeData &QQuickThemeAttachedPrivate::resolve() const { Q_Q(const QQuickThemeAttached); - QQuickThemeAttached *theme = findParentTheme(const_cast(q)); + QQuickThemeAttached *theme = QQuickStyle::findParent(const_cast(q)); return theme ? theme->d_func()->data : *globalThemeData(); } void QQuickThemeAttachedPrivate::itemParentChanged(QQuickItem *item, QQuickItem *) { - QQuickThemeAttached *theme = attachedTheme(item); + QQuickThemeAttached *theme = QQuickStyle::instance(item); if (theme) { - QQuickThemeAttached *parent = findParentTheme(theme); + QQuickThemeAttached *parent = QQuickStyle::findParent(theme); if (parent) theme->setParentTheme(parent); } } +QQuickThemeAttached::QQuickThemeAttached(QObject *parent) : + QObject(*(new QQuickThemeAttachedPrivate(*globalThemeData())), parent) +{ + Q_D(QQuickThemeAttached); + QQuickItem *item = qobject_cast(parent); + if (item) + QQuickItemPrivate::get(item)->addItemChangeListener(d, QQuickItemPrivate::Parent); +} + QQuickThemeAttached::QQuickThemeAttached(const QQuickThemeData &data, QObject *parent) : QObject(*(new QQuickThemeAttachedPrivate(data)), parent) { @@ -469,15 +385,15 @@ QQuickThemeAttached::~QQuickThemeAttached() QQuickThemeAttached *QQuickThemeAttached::qmlAttachedProperties(QObject *object) { QQuickThemeAttached *theme = Q_NULLPTR; - QQuickThemeAttached *parent = findParentTheme(object); + QQuickThemeAttached *parent = QQuickStyle::findParent(object); if (parent) { theme = new QQuickThemeAttached(parent->d_func()->data, object); theme->setParentTheme(parent); } else { - theme = new QQuickThemeAttached(*globalThemeData(), object); + theme = new QQuickThemeAttached(object); } - QList childThemes = findChildThemes(object); + QList childThemes = QQuickStyle::findChildren(object); foreach (QQuickThemeAttached *child, childThemes) child->setParentTheme(theme); return theme; diff --git a/src/imports/controls/qquicktheme_p.h b/src/imports/controls/qquicktheme_p.h index 02ad5619..0d204537 100644 --- a/src/imports/controls/qquicktheme_p.h +++ b/src/imports/controls/qquicktheme_p.h @@ -73,6 +73,7 @@ class QQuickThemeAttached : public QObject Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor RESET resetTextColor NOTIFY textColorChanged FINAL) public: + explicit QQuickThemeAttached(QObject *parent = Q_NULLPTR); explicit QQuickThemeAttached(const QQuickThemeData &data, QObject *parent = Q_NULLPTR); ~QQuickThemeAttached(); diff --git a/src/imports/controls/shared/qquickstyle_p.h b/src/imports/controls/shared/qquickstyle_p.h new file mode 100644 index 00000000..0fd4f612 --- /dev/null +++ b/src/imports/controls/shared/qquickstyle_p.h @@ -0,0 +1,157 @@ +/**************************************************************************** +** +** 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 new file mode 100644 index 00000000..2979a615 --- /dev/null +++ b/src/imports/controls/shared/shared.pri @@ -0,0 +1,4 @@ +INCLUDEPATH += $$PWD + +HEADERS += \ + $$PWD/qquickstyle_p.h -- cgit v1.2.3