aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJ-P Nurmi <jpnurmi@theqtcompany.com>2015-10-22 14:04:46 +0200
committerJ-P Nurmi <jpnurmi@theqtcompany.com>2015-10-22 13:19:42 +0000
commitf765bca3f7d34db01d1fc453d4ebce66c278fe2a (patch)
tree58034556bea8012a1754d71717cb1609d7c097d1
parent7e037903e9efdc593c9d51da8f303a061e7e08e7 (diff)
Share style inheritance related helpers
Usage: # +material/material.pro include(../shared/shared.pri) // qquickmaterialstyle.cpp #include "qquickstyle_p.h" QQuickMaterialStyle *style = QQuickStyle::instance<QQuickMaterialStyle>(item); // ... QQuickMaterialStyle *parent = QQuickStyle::findParent<QQuickMaterialStyle>(this); // ... QList<QQuickMaterialStyle *> children = QQuickStyle::findChildren<QQuickMaterialStyle>(object); // ... Change-Id: I2c01a1aa36805f16d83fa60ab25b2cc29e6a43cc Reviewed-by: Mitch Curtis <mitch.curtis@theqtcompany.com>
-rw-r--r--src/imports/controls/controls.pro1
-rw-r--r--src/imports/controls/qquicktheme.cpp116
-rw-r--r--src/imports/controls/qquicktheme_p.h1
-rw-r--r--src/imports/controls/shared/qquickstyle_p.h157
-rw-r--r--src/imports/controls/shared/shared.pri4
5 files changed, 179 insertions, 100 deletions
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 <QtCore/qset.h>
#include <QtCore/qpointer.h>
@@ -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<QQuickThemeAttached *>();
- 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<QQuickThemeAttached*>(qmlAttachedPropertiesObject<QQuickThemeAttached>(object, false));
- return Q_NULLPTR;
-}
-
-static QQuickThemeAttached *findParentTheme(QObject *object)
-{
- QQuickItem *item = qobject_cast<QQuickItem *>(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<QQuickWindow *>(object);
- if (window) {
- QQuickWindow *parentWindow = qobject_cast<QQuickWindow *>(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<QQuickThemeAttached *> findChildThemes(QObject *object)
-{
- QList<QQuickThemeAttached *> themes;
-
- QQuickItem *item = qobject_cast<QQuickItem *>(object);
- if (!item) {
- QQuickWindow *window = qobject_cast<QQuickWindow *>(object);
- if (window) {
- item = window->contentItem();
-
- foreach (QObject *child, window->children()) {
- QQuickWindow *childWindow = qobject_cast<QQuickWindow *>(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<QQuickThemeAttached *>(q));
+ QQuickThemeAttached *theme = QQuickStyle::findParent<QQuickThemeAttached>(const_cast<QQuickThemeAttached *>(q));
return theme ? theme->d_func()->data : *globalThemeData();
}
void QQuickThemeAttachedPrivate::itemParentChanged(QQuickItem *item, QQuickItem *)
{
- QQuickThemeAttached *theme = attachedTheme(item);
+ QQuickThemeAttached *theme = QQuickStyle::instance<QQuickThemeAttached>(item);
if (theme) {
- QQuickThemeAttached *parent = findParentTheme(theme);
+ QQuickThemeAttached *parent = QQuickStyle::findParent<QQuickThemeAttached>(theme);
if (parent)
theme->setParentTheme(parent);
}
}
+QQuickThemeAttached::QQuickThemeAttached(QObject *parent) :
+ QObject(*(new QQuickThemeAttachedPrivate(*globalThemeData())), parent)
+{
+ Q_D(QQuickThemeAttached);
+ QQuickItem *item = qobject_cast<QQuickItem *>(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<QQuickThemeAttached>(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<QQuickThemeAttached *> childThemes = findChildThemes(object);
+ QList<QQuickThemeAttached *> childThemes = QQuickStyle::findChildren<QQuickThemeAttached>(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 <QtQml/qqml.h>
+#include <QtQml/qqmlengine.h>
+#include <QtQuick/qquickitem.h>
+#include <QtQuick/qquickwindow.h>
+
+QT_BEGIN_NAMESPACE
+
+namespace QQuickStyle
+{
+ template <typename T>
+ static T *instance(QObject *object)
+ {
+ if (object)
+ return qobject_cast<T*>(qmlAttachedPropertiesObject<T>(object, false));
+ return Q_NULLPTR;
+ }
+
+ template <typename T>
+ static T *findParent(QObject *object)
+ {
+ QQuickItem *item = qobject_cast<QQuickItem *>(object);
+ if (item) {
+ // lookup parent items
+ QQuickItem *parent = item->parentItem();
+ while (parent) {
+ T *attached = instance<T>(parent);
+ if (attached)
+ return attached;
+ parent = parent->parentItem();
+ }
+
+ // fallback to item's window
+ QQuickWindow *window = item->window();
+ if (window) {
+ T *attached = instance<T>(window);
+ if (attached)
+ return attached;
+ }
+ }
+
+ // lookup parent window
+ QQuickWindow *window = qobject_cast<QQuickWindow *>(object);
+ if (window) {
+ QQuickWindow *parentWindow = qobject_cast<QQuickWindow *>(window->parent());
+ if (parentWindow) {
+ T *attached = instance<T>(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<T*>();
+ if (!instance) {
+ instance = new T(engine);
+ engine->setProperty(name, QVariant::fromValue(instance));
+ }
+ return instance;
+ }
+ }
+
+ return Q_NULLPTR;
+ }
+
+ template <typename T>
+ static QList<T *> findChildren(QObject *object)
+ {
+ QList<T *> children;
+
+ QQuickItem *item = qobject_cast<QQuickItem *>(object);
+ if (!item) {
+ QQuickWindow *window = qobject_cast<QQuickWindow *>(object);
+ if (window) {
+ item = window->contentItem();
+
+ foreach (QObject *child, window->children()) {
+ QQuickWindow *childWindow = qobject_cast<QQuickWindow *>(child);
+ if (childWindow) {
+ T *attached = instance<T>(childWindow);
+ if (attached)
+ children += attached;
+ }
+ }
+ }
+ }
+
+ if (item) {
+ foreach (QQuickItem *child, item->childItems()) {
+ T *attached = instance<T>(child);
+ if (attached)
+ children += attached;
+ else
+ children += findChildren<T>(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