diff options
author | J-P Nurmi <jpnurmi@theqtcompany.com> | 2016-03-11 13:40:45 +0100 |
---|---|---|
committer | J-P Nurmi <jpnurmi@theqtcompany.com> | 2016-03-15 20:04:58 +0000 |
commit | 54a2a35f17d5e5d1793af63b76993a583e10ab41 (patch) | |
tree | 4608034b8431148367d2fef0f26be014427721e6 | |
parent | 28ce9872fd83e82c8ece0c70624c45ea2be5c58d (diff) |
Change style inheritance for popups
The decision to make popups inherit theme & accent from its parent item
was driven by ComboBox. However, in many cases it has an undesired side
effect, so we've changed Popup to inherit its style attributes from the
parent window instead, just like we did for fonts. The only exception
to this is ComboBox, where the popup is an integral part of the control.
This special case is now handled in the respective style implementation.
A concrete example is that we can now specify dark theme by default for
Material style ToolBar to get a better matching light text against the
colorful background. In Gallery, this won't effect the options menu,
which is a child of a ToolButton. The menu retains light theme along the
rest of the application.
Change-Id: Ibdc8fcf5b5fa258d853410a9b40368472424a8c6
Reviewed-by: Mitch Curtis <mitch.curtis@theqtcompany.com>
-rw-r--r-- | src/controls/qquickstyle.cpp | 44 | ||||
-rw-r--r-- | src/controls/qquickstyle_p.h | 2 | ||||
-rw-r--r-- | src/imports/controls/material/ComboBox.qml | 4 | ||||
-rw-r--r-- | src/imports/controls/universal/ComboBox.qml | 3 | ||||
-rw-r--r-- | tests/auto/material/data/tst_material.qml | 4 | ||||
-rw-r--r-- | tests/auto/universal/data/tst_universal.qml | 4 |
6 files changed, 44 insertions, 17 deletions
diff --git a/src/controls/qquickstyle.cpp b/src/controls/qquickstyle.cpp index 76524629..c38950d8 100644 --- a/src/controls/qquickstyle.cpp +++ b/src/controls/qquickstyle.cpp @@ -56,22 +56,29 @@ static QQuickStyle *findParentStyle(const QMetaObject *type, QObject *object) { QQuickItem *item = qobject_cast<QQuickItem *>(object); if (item) { - // lookup parent items + // lookup parent items and popups QQuickItem *parent = item->parentItem(); while (parent) { QQuickStyle *style = attachedStyle(type, parent); if (style) return style; + + QQuickPopup *popup = qobject_cast<QQuickPopup *>(parent->parent()); + if (popup) + return attachedStyle(type, popup); + parent = parent->parentItem(); } // fallback to item's window - QQuickWindow *window = item->window(); - if (window) { - QQuickStyle *style = attachedStyle(type, window); - if (style) - return style; - } + QQuickStyle *style = attachedStyle(type, item->window()); + if (style) + return style; + } else { + // lookup popup's window + QQuickPopup *popup = qobject_cast<QQuickPopup *>(object); + if (popup) + return attachedStyle(type, popup->popupItem()->window()); } // lookup parent window @@ -138,14 +145,14 @@ static QList<QQuickStyle *> findChildStyles(const QMetaObject *type, QObject *ob QQuickStyle::QQuickStyle(QObject *parent) : QObject(parent) { - QQuickItem *item = qobject_cast<QQuickItem *>(parent); + QQuickItem *item = parentItem(); if (item) QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Parent); } QQuickStyle::~QQuickStyle() { - QQuickItem *item = qobject_cast<QQuickItem *>(parent()); + QQuickItem *item = parentItem(); if (item) QQuickItemPrivate::get(item)->removeItemChangeListener(this, QQuickItemPrivate::Parent); @@ -167,6 +174,17 @@ QSharedPointer<QSettings> QQuickStyle::settings(const QString &group) return QSharedPointer<QSettings>(); } +QQuickItem *QQuickStyle::parentItem() const +{ + QQuickItem *item = qobject_cast<QQuickItem *>(parent()); + if (!item) { + QQuickPopup *popup = qobject_cast<QQuickPopup *>(parent()); + if (popup) + item = popup->popupItem(); + } + return item; +} + QList<QQuickStyle *> QQuickStyle::childStyles() const { return m_childStyles; @@ -209,12 +227,8 @@ void QQuickStyle::parentStyleChange(QQuickStyle *newParent, QQuickStyle *oldPare void QQuickStyle::itemParentChanged(QQuickItem *item, QQuickItem *parent) { - QQuickStyle *style = attachedStyle(metaObject(), item); - if (style) { - QQuickStyle *parentStyle = findParentStyle(metaObject(), parent); - if (parentStyle) - style->setParentStyle(parentStyle); - } + Q_UNUSED(parent); + setParentStyle(findParentStyle(metaObject(), item)); } QT_END_NAMESPACE diff --git a/src/controls/qquickstyle_p.h b/src/controls/qquickstyle_p.h index 02ee667a..c5bfed0f 100644 --- a/src/controls/qquickstyle_p.h +++ b/src/controls/qquickstyle_p.h @@ -72,6 +72,8 @@ public: protected: void init(); + QQuickItem *parentItem() const; + QList<QQuickStyle *> childStyles() const; QQuickStyle *parentStyle() const; diff --git a/src/imports/controls/material/ComboBox.qml b/src/imports/controls/material/ComboBox.qml index 1c293567..97724585 100644 --- a/src/imports/controls/material/ComboBox.qml +++ b/src/imports/controls/material/ComboBox.qml @@ -121,6 +121,10 @@ T.ComboBox { topMargin: 12 bottomMargin: 12 + Material.theme: control.Material.theme + Material.accent: control.Material.accent + Material.primary: control.Material.primary + enter: Transition { // grow_fade_in NumberAnimation { property: "scale"; from: 0.9; to: 1.0; easing.type: Easing.OutQuint; duration: 220 } diff --git a/src/imports/controls/universal/ComboBox.qml b/src/imports/controls/universal/ComboBox.qml index 03278e10..31a928a3 100644 --- a/src/imports/controls/universal/ComboBox.qml +++ b/src/imports/controls/universal/ComboBox.qml @@ -113,6 +113,9 @@ T.ComboBox { topMargin: 8 bottomMargin: 8 + Universal.theme: control.Universal.theme + Universal.accent: control.Universal.accent + contentItem: ListView { id: listview clip: true diff --git a/tests/auto/material/data/tst_material.qml b/tests/auto/material/data/tst_material.qml index 2636bec0..e8ee47ca 100644 --- a/tests/auto/material/data/tst_material.qml +++ b/tests/auto/material/data/tst_material.qml @@ -98,7 +98,7 @@ TestCase { Component { id: menu - Item { + ApplicationWindow { Material.primary: Material.Blue Material.accent: Material.Red property alias menu: popup @@ -281,6 +281,8 @@ TestCase { var container = menu.createObject(testCase) verify(container) verify(container.menu) + container.menu.open() + verify(container.menu.visible) var child = container.menu.itemAt(0) verify(child) compare(container.Material.theme, Material.Light) diff --git a/tests/auto/universal/data/tst_universal.qml b/tests/auto/universal/data/tst_universal.qml index c232e7c1..82c07b23 100644 --- a/tests/auto/universal/data/tst_universal.qml +++ b/tests/auto/universal/data/tst_universal.qml @@ -96,7 +96,7 @@ TestCase { Component { id: menu - Item { + ApplicationWindow { Universal.accent: Universal.Red property alias menu: popup Menu { @@ -256,6 +256,8 @@ TestCase { var container = menu.createObject(testCase) verify(container) verify(container.menu) + container.menu.open() + verify(container.menu.visible) var child = container.menu.itemAt(0) verify(child) compare(container.Universal.theme, Universal.Light) |