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/controls | |
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/controls')
-rw-r--r-- | src/controls/controls.pri | 2 | ||||
-rw-r--r-- | src/controls/qquickstyle.cpp | 201 | ||||
-rw-r--r-- | src/controls/qquickstyle_p.h | 86 |
3 files changed, 289 insertions, 0 deletions
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 <QtQuick/private/qquickitem_p.h> + +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<QQuickStyle *>(qmlAttachedPropertiesObject(&idx, object, type, create)); +} + +static QQuickStyle *findParentStyle(const QMetaObject *type, QObject *object) +{ + QQuickItem *item = qobject_cast<QQuickItem *>(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<QQuickWindow *>(object); + if (window) { + QQuickWindow *parentWindow = qobject_cast<QQuickWindow *>(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<QQuickStyle*>(); + if (!style) { + style = attachedStyle(type, engine, true); + engine->setProperty(name, QVariant::fromValue(style)); + } + return style; + } + } + + return Q_NULLPTR; +} + +static QList<QQuickStyle *> findChildStyles(const QMetaObject *type, QObject *object) +{ + QList<QQuickStyle *> 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) { + 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<QQuickItem *>(parent); + if (item) + QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Parent); +} + +QQuickStyle::~QQuickStyle() +{ + QQuickItem *item = qobject_cast<QQuickItem *>(parent()); + if (item) + QQuickItemPrivate::get(item)->removeItemChangeListener(this, QQuickItemPrivate::Parent); + + setParentStyle(Q_NULLPTR); +} + +QList<QQuickStyle *> 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<QQuickStyle *> 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 <QtQml/qqml.h> +#include <QtCore/qlist.h> +#include <QtCore/qobject.h> +#include <QtCore/qpointer.h> +#include <QtQuick/private/qquickitemchangelistener_p.h> + +QT_BEGIN_NAMESPACE + +class QQuickStyle : public QObject, public QQuickItemChangeListener +{ + Q_OBJECT + +public: + explicit QQuickStyle(QObject *parent = Q_NULLPTR); + ~QQuickStyle(); + +protected: + void init(); + + QList<QQuickStyle *> 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<QQuickStyle *> m_childStyles; + QPointer<QQuickStyle> m_parentStyle; +}; + +QT_END_NAMESPACE + +#endif // QQUICKSTYLE_P_H |