diff options
author | J-P Nurmi <jpnurmi@theqtcompany.com> | 2015-03-03 01:31:38 +0100 |
---|---|---|
committer | Jari-Pekka Nurmi <jpnurmi@theqtcompany.com> | 2015-03-03 13:38:11 +0200 |
commit | 91cb29aaeed7516b13157cc9385e715001da8c33 (patch) | |
tree | 9362481abd379a3ca8f0e842d907d94d3265f9e4 /src/controls | |
parent | cafb2669f1cfd46087c922c7078e205e68a6cdb8 (diff) |
QQuickStylable
Change-Id: I9017c192d5d42724589a5cbdbdb6705be4c552b5
Reviewed-by: Jari-Pekka Nurmi <jpnurmi@theqtcompany.com>
Diffstat (limited to 'src/controls')
-rw-r--r-- | src/controls/controls.pri | 2 | ||||
-rw-r--r-- | src/controls/qquickabstractapplicationwindow.cpp | 22 | ||||
-rw-r--r-- | src/controls/qquickabstractapplicationwindow_p.h | 10 | ||||
-rw-r--r-- | src/controls/qquickcontrol.cpp | 55 | ||||
-rw-r--r-- | src/controls/qquickcontrol_p.h | 19 | ||||
-rw-r--r-- | src/controls/qquickcontrol_p_p.h | 2 | ||||
-rw-r--r-- | src/controls/qquickstylable_p.h | 73 | ||||
-rw-r--r-- | src/controls/qquickstyle.cpp | 34 | ||||
-rw-r--r-- | src/controls/qquickstyle_p_p.h | 64 |
9 files changed, 224 insertions, 57 deletions
diff --git a/src/controls/controls.pri b/src/controls/controls.pri index 084c8387..189ed27e 100644 --- a/src/controls/controls.pri +++ b/src/controls/controls.pri @@ -31,6 +31,8 @@ HEADERS += \ $$PWD/qquickcontrol_p.h \ $$PWD/qquickcontrol_p_p.h \ $$PWD/qquickexclusivegroup_p.h \ + $$PWD/qquickstylable_p.h \ + $$PWD/qquickstyle_p_p.h \ $$PWD/qquickstyle_p.h SOURCES += \ diff --git a/src/controls/qquickabstractapplicationwindow.cpp b/src/controls/qquickabstractapplicationwindow.cpp index f6ad3084..1800e302 100644 --- a/src/controls/qquickabstractapplicationwindow.cpp +++ b/src/controls/qquickabstractapplicationwindow.cpp @@ -37,6 +37,7 @@ #include "qquickabstractapplicationwindow_p.h" #include "qquickstyle_p.h" +#include <QtCore/qcoreapplication.h> #include <QtQuick/private/qquickitem_p.h> #include <QtQuick/private/qquickitemchangelistener_p.h> @@ -59,7 +60,7 @@ class QQuickAbstractApplicationWindowPrivate : public QQuickItemChangeListener public: QQuickAbstractApplicationWindowPrivate() : contentWidth(0), contentHeight(0), - header(Q_NULLPTR), footer(Q_NULLPTR), style(Q_NULLPTR) { } + header(Q_NULLPTR), footer(Q_NULLPTR), hasStyle(false), style(Q_NULLPTR) { } void relayout(); @@ -70,6 +71,7 @@ public: qreal contentHeight; QQuickItem *header; QQuickItem *footer; + bool hasStyle; mutable QQuickStyle *style; QQuickAbstractApplicationWindow *q_ptr; }; @@ -191,10 +193,28 @@ void QQuickAbstractApplicationWindow::setStyle(QQuickStyle *style) Q_D(QQuickAbstractApplicationWindow); if (d->style != style) { d->style = style; + d->hasStyle = style; emit styleChanged(); + + QEvent change(QEvent::StyleChange); + foreach (QQuickItem *item, contentItem()->findChildren<QQuickItem *>()) { + if (qobject_cast<QQuickStylable *>(item)) + QCoreApplication::sendEvent(item, &change); + } } } +bool QQuickAbstractApplicationWindow::hasStyle() const +{ + Q_D(const QQuickAbstractApplicationWindow); + return d->hasStyle; +} + +void QQuickAbstractApplicationWindow::resetStyle() +{ + setStyle(Q_NULLPTR); +} + /*! \qmlproperty real QtQuickControls2::ApplicationWindow::contentWidth diff --git a/src/controls/qquickabstractapplicationwindow_p.h b/src/controls/qquickabstractapplicationwindow_p.h index d58db866..8438a278 100644 --- a/src/controls/qquickabstractapplicationwindow_p.h +++ b/src/controls/qquickabstractapplicationwindow_p.h @@ -50,15 +50,17 @@ #include <QtQuick/private/qquickwindowmodule_p.h> #include <QtQuickControls/private/qtquickcontrolsglobal_p.h> +#include <QtQuickControls/private/qquickstylable_p.h> QT_BEGIN_NAMESPACE class QQuickStyle; class QQuickAbstractApplicationWindowPrivate; -class Q_QUICKCONTROLS_EXPORT QQuickAbstractApplicationWindow : public QQuickWindowQmlImpl +class Q_QUICKCONTROLS_EXPORT QQuickAbstractApplicationWindow : public QQuickWindowQmlImpl, public QQuickStylable { Q_OBJECT + Q_INTERFACES(QQuickStylable) Q_PROPERTY(QQuickItem *header READ header WRITE setHeader NOTIFY headerChanged FINAL) Q_PROPERTY(QQuickItem *footer READ footer WRITE setFooter NOTIFY footerChanged FINAL) Q_PROPERTY(QQuickStyle *style READ style WRITE setStyle NOTIFY styleChanged FINAL) @@ -75,8 +77,10 @@ public: QQuickItem *footer() const; void setFooter(QQuickItem *footer); - QQuickStyle *style() const; - void setStyle(QQuickStyle *style); + QQuickStyle *style() const Q_DECL_OVERRIDE; + void setStyle(QQuickStyle *style) Q_DECL_OVERRIDE; + bool hasStyle() const Q_DECL_OVERRIDE; + void resetStyle() Q_DECL_OVERRIDE; qreal contentWidth() const; void setContentWidth(qreal width); diff --git a/src/controls/qquickcontrol.cpp b/src/controls/qquickcontrol.cpp index b76f9b3e..0520e5d5 100644 --- a/src/controls/qquickcontrol.cpp +++ b/src/controls/qquickcontrol.cpp @@ -36,7 +36,8 @@ #include "qquickcontrol_p.h" #include "qquickcontrol_p_p.h" -#include "qquickabstractapplicationwindow_p.h" +#include "qquickstyle_p_p.h" + #include <QtCore/qcoreapplication.h> QT_BEGIN_NAMESPACE @@ -52,7 +53,7 @@ QT_BEGIN_NAMESPACE */ QQuickControlPrivate::QQuickControlPrivate() : - style(Q_NULLPTR), attributes(QQuickControl::Attr_Count), background(Q_NULLPTR), + hasStyle(false), style(Q_NULLPTR), background(Q_NULLPTR), topPadding(0), leftPadding(0), rightPadding(0), bottomPadding(0) { } @@ -66,29 +67,7 @@ void QQuickControlPrivate::mirrorChange() void QQuickControlPrivate::resolveStyle(QQuickStyle *res) { Q_Q(QQuickControl); - if (!res && q->testAttribute(QQuickControl::Attr_HasStyle)) - return; - - // lookup parent style - if (!res) { - QQuickItem *item = q->parentItem(); - while (!res && item) { - QQuickControl *control = qobject_cast<QQuickControl *>(item); - if (control && control->testAttribute(QQuickControl::Attr_HasStyle)) - res = control->style(); - item = item->parentItem(); - } - } - - // fallback to window or global style - if (!res) { - QQuickAbstractApplicationWindow *aw = qobject_cast<QQuickAbstractApplicationWindow *>(window); - if (aw) - res = aw->style(); - if (!res) - res = QQuickStyle::instance(qmlEngine(q)); - } - + res = QQuickStylePrivate::resolve(q, res); if (style != res) { style = res; emit q->styleChanged(); @@ -190,15 +169,23 @@ void QQuickControl::setStyle(QQuickStyle *style) { Q_D(QQuickControl); if (d->style != style) { - setAttribute(Attr_HasStyle, style); + d->hasStyle = style; d->resolveStyle(style); QEvent change(QEvent::StyleChange); - foreach (QQuickControl *control, findChildren<QQuickControl *>()) - QCoreApplication::sendEvent(control, &change); + foreach (QQuickItem *item, findChildren<QQuickItem *>()) { + if (qobject_cast<QQuickStylable *>(item)) + QCoreApplication::sendEvent(item, &change); + } } } +bool QQuickControl::hasStyle() const +{ + Q_D(const QQuickControl); + return d->hasStyle; +} + void QQuickControl::resetStyle() { setStyle(Q_NULLPTR); @@ -230,18 +217,6 @@ void QQuickControl::setBackground(QQuickItem *background) } } -bool QQuickControl::testAttribute(Attribute attribute) const -{ - Q_D(const QQuickControl); - return d->attributes.testBit(attribute); -} - -void QQuickControl::setAttribute(Attribute attribute, bool on) -{ - Q_D(QQuickControl); - d->attributes.setBit(attribute, on); -} - bool QQuickControl::event(QEvent *event) { Q_D(QQuickControl); diff --git a/src/controls/qquickcontrol_p.h b/src/controls/qquickcontrol_p.h index 9b233660..17f94d7d 100644 --- a/src/controls/qquickcontrol_p.h +++ b/src/controls/qquickcontrol_p.h @@ -50,15 +50,17 @@ #include <QtQuick/qquickitem.h> #include <QtQuickControls/private/qtquickcontrolsglobal_p.h> +#include <QtQuickControls/private/qquickstylable_p.h> QT_BEGIN_NAMESPACE class QQuickStyle; class QQuickControlPrivate; -class Q_QUICKCONTROLS_EXPORT QQuickControl : public QQuickItem +class Q_QUICKCONTROLS_EXPORT QQuickControl : public QQuickItem, public QQuickStylable { Q_OBJECT + Q_INTERFACES(QQuickStylable) Q_PROPERTY(qreal topPadding READ topPadding WRITE setTopPadding NOTIFY topPaddingChanged FINAL) Q_PROPERTY(qreal leftPadding READ leftPadding WRITE setLeftPadding NOTIFY leftPaddingChanged FINAL) Q_PROPERTY(qreal rightPadding READ rightPadding WRITE setRightPadding NOTIFY rightPaddingChanged FINAL) @@ -81,21 +83,14 @@ public: qreal bottomPadding() const; void setBottomPadding(qreal padding); - QQuickStyle *style() const; - void setStyle(QQuickStyle *style); - void resetStyle(); + QQuickStyle *style() const Q_DECL_OVERRIDE; + void setStyle(QQuickStyle *style) Q_DECL_OVERRIDE; + bool hasStyle() const Q_DECL_OVERRIDE; + void resetStyle() Q_DECL_OVERRIDE; QQuickItem *background() const; void setBackground(QQuickItem *background); - enum Attribute { - Attr_HasStyle = 1, - Attr_Count - }; - - bool testAttribute(Attribute attribute) const; - void setAttribute(Attribute attribute, bool on = true); - Q_SIGNALS: void styleChanged(); void backgroundChanged(); diff --git a/src/controls/qquickcontrol_p_p.h b/src/controls/qquickcontrol_p_p.h index c16e59fe..cc593252 100644 --- a/src/controls/qquickcontrol_p_p.h +++ b/src/controls/qquickcontrol_p_p.h @@ -64,8 +64,8 @@ public: void resolveStyle(QQuickStyle *other = 0); + bool hasStyle; QQuickStyle *style; - QBitArray attributes; QQuickItem *background; qreal topPadding; qreal leftPadding; diff --git a/src/controls/qquickstylable_p.h b/src/controls/qquickstylable_p.h new file mode 100644 index 00000000..d3a72882 --- /dev/null +++ b/src/controls/qquickstylable_p.h @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick 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 QQUICKSTYLABLE_P_H +#define QQUICKSTYLABLE_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 <QtCore/qobject.h> +#include <QtQuickControls/private/qtquickcontrolsglobal_p.h> + +QT_BEGIN_NAMESPACE + +class QQuickStyle; + +class Q_QUICKCONTROLS_EXPORT QQuickStylable +{ +public: + virtual ~QQuickStylable() { } + + virtual QQuickStyle *style() const = 0; + virtual void setStyle(QQuickStyle *style) = 0; + virtual bool hasStyle() const = 0; + virtual void resetStyle() = 0; +}; + +QT_END_NAMESPACE + +Q_DECLARE_INTERFACE(QQuickStylable, "io.qt.QQuickStylable") + +#endif // QQUICKSTYLABLE_P_H diff --git a/src/controls/qquickstyle.cpp b/src/controls/qquickstyle.cpp index e52e8b3a..11514c17 100644 --- a/src/controls/qquickstyle.cpp +++ b/src/controls/qquickstyle.cpp @@ -35,6 +35,9 @@ ****************************************************************************/ #include "qquickstyle_p.h" +#include "qquickstyle_p_p.h" +#include "qquickstylable_p.h" + #include <QtCore/qfile.h> #include <QtCore/qjsondocument.h> #include <QtCore/qjsonobject.h> @@ -107,6 +110,37 @@ QT_BEGIN_NAMESPACE \qmlproperty real QtQuickControls2::Style::disabledOpacity */ +QQuickStyle *QQuickStylePrivate::resolve(QQuickItem *item, QQuickStyle *res) +{ + QQuickStylable *stylable = qobject_cast<QQuickStylable *>(item); + Q_ASSERT(stylable); + + if (!res && stylable->hasStyle()) + return stylable->style(); + + // lookup parent style + if (!res) { + QQuickItem *parent = item->parentItem(); + while (!res && parent) { + QQuickStylable *stylable = qobject_cast<QQuickStylable *>(parent); + if (stylable && stylable->hasStyle()) + res = stylable->style(); + parent = parent->parentItem(); + } + } + + // fallback to window or global style + if (!res) { + QQuickStylable *window = qobject_cast<QQuickStylable *>(item->window()); + if (window) + res = window->style(); + if (!res) + res = QQuickStyle::instance(qmlEngine(item)); + } + + return res; +} + static QColor readColorValue(const QJsonValue &value, const QColor &defaultValue) { if (value.isString()) diff --git a/src/controls/qquickstyle_p_p.h b/src/controls/qquickstyle_p_p.h new file mode 100644 index 00000000..276ce6aa --- /dev/null +++ b/src/controls/qquickstyle_p_p.h @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick 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_P_H +#define QQUICKSTYLE_P_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. +// + +QT_BEGIN_NAMESPACE + +class QQuickItem; +class QQuickStylable; + +class QQuickStylePrivate +{ +public: + static QQuickStyle *resolve(QQuickItem *item, QQuickStyle *other = 0); +}; + +QT_END_NAMESPACE + +#endif // QQUICKSTYLE_P_P_H |