From 54a2a35f17d5e5d1793af63b76993a583e10ab41 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Fri, 11 Mar 2016 13:40:45 +0100 Subject: 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 --- src/controls/qquickstyle.cpp | 44 +++++++++++++++++++++++++++++--------------- src/controls/qquickstyle_p.h | 2 ++ 2 files changed, 31 insertions(+), 15 deletions(-) (limited to 'src/controls') 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(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(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(object); + if (popup) + return attachedStyle(type, popup->popupItem()->window()); } // lookup parent window @@ -138,14 +145,14 @@ static QList findChildStyles(const QMetaObject *type, QObject *ob QQuickStyle::QQuickStyle(QObject *parent) : QObject(parent) { - QQuickItem *item = qobject_cast(parent); + QQuickItem *item = parentItem(); if (item) QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Parent); } QQuickStyle::~QQuickStyle() { - QQuickItem *item = qobject_cast(parent()); + QQuickItem *item = parentItem(); if (item) QQuickItemPrivate::get(item)->removeItemChangeListener(this, QQuickItemPrivate::Parent); @@ -167,6 +174,17 @@ QSharedPointer QQuickStyle::settings(const QString &group) return QSharedPointer(); } +QQuickItem *QQuickStyle::parentItem() const +{ + QQuickItem *item = qobject_cast(parent()); + if (!item) { + QQuickPopup *popup = qobject_cast(parent()); + if (popup) + item = popup->popupItem(); + } + return item; +} + QList 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 childStyles() const; QQuickStyle *parentStyle() const; -- cgit v1.2.3