aboutsummaryrefslogtreecommitdiffstats
path: root/src/quicktemplates2/qquickcontrol.cpp
diff options
context:
space:
mode:
authorJ-P Nurmi <jpnurmi@qt.io>2017-04-18 16:59:58 +0200
committerJ-P Nurmi <jpnurmi@qt.io>2017-05-26 08:52:05 +0000
commit68aea0a4234e6f0faa108ea186643b7c27ab8ce7 (patch)
tree7d3f4cd51f5ad0e433d94ea4acdac05943550905 /src/quicktemplates2/qquickcontrol.cpp
parentbb49cde2decbee22f0d732d6e2f56a14c2970b24 (diff)
Add support for propagating palettes
Change-Id: Icd965d8a79f022f4375e2134621cbc3897014015 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/quicktemplates2/qquickcontrol.cpp')
-rw-r--r--src/quicktemplates2/qquickcontrol.cpp182
1 files changed, 182 insertions, 0 deletions
diff --git a/src/quicktemplates2/qquickcontrol.cpp b/src/quicktemplates2/qquickcontrol.cpp
index d285fdd2..e866a9a3 100644
--- a/src/quicktemplates2/qquickcontrol.cpp
+++ b/src/quicktemplates2/qquickcontrol.cpp
@@ -421,6 +421,111 @@ void QQuickControlPrivate::updateFontRecur(QQuickItem *item, const QFont &font)
}
}
+/*!
+ \internal
+
+ Returns the palette that the item inherits from its ancestors and
+ QGuiApplication::palette.
+*/
+QPalette QQuickControlPrivate::parentPalette(const QQuickItem *item)
+{
+ QQuickItem *p = item->parentItem();
+ while (p) {
+ if (QQuickControl *control = qobject_cast<QQuickControl *>(p))
+ return control->palette();
+ else if (QQuickLabel *label = qobject_cast<QQuickLabel *>(p))
+ return label->palette();
+ else if (QQuickTextField *textField = qobject_cast<QQuickTextField *>(p))
+ return textField->palette();
+ else if (QQuickTextArea *textArea = qobject_cast<QQuickTextArea *>(p))
+ return textArea->palette();
+
+ p = p->parentItem();
+ }
+
+ if (QQuickApplicationWindow *window = qobject_cast<QQuickApplicationWindow *>(item->window()))
+ return window->palette();
+
+ return themePalette(QPlatformTheme::SystemPalette);
+}
+
+QPalette QQuickControlPrivate::themePalette(QPlatformTheme::Palette type)
+{
+ if (QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme()) {
+ if (const QPalette *palette = theme->palette(type)) {
+ QPalette p = *palette;
+ if (type == QPlatformTheme::SystemPalette)
+ p.resolve(0);
+ return p;
+ }
+ }
+
+ return QPalette();
+}
+
+/*!
+ \internal
+
+ Determine which palette is implicitly imposed on this control by its ancestors
+ and QGuiApplication::palette, resolve this against its own palette (attributes from
+ the implicit palette are copied over). Then propagate this palette to this
+ control's children.
+*/
+void QQuickControlPrivate::resolvePalette()
+{
+ Q_Q(QQuickControl);
+ inheritPalette(parentPalette(q));
+}
+
+void QQuickControlPrivate::inheritPalette(const QPalette &palette)
+{
+ Q_Q(QQuickControl);
+ QPalette parentPalette = extra.isAllocated() ? extra->requestedPalette.resolve(palette) : palette;
+ parentPalette.resolve(extra.isAllocated() ? extra->requestedPalette.resolve() | palette.resolve() : palette.resolve());
+
+ const QPalette defaultPalette = q->defaultPalette();
+ const QPalette resolvedPalette = parentPalette.resolve(defaultPalette);
+
+ setPalette_helper(resolvedPalette);
+}
+
+/*!
+ \internal
+
+ Assign \a palette to this control, and propagate it to all children.
+*/
+void QQuickControlPrivate::updatePalette(const QPalette &palette)
+{
+ Q_Q(QQuickControl);
+ QPalette oldPalette = resolvedPalette;
+ resolvedPalette = palette;
+
+ if (oldPalette != palette)
+ q->paletteChange(palette, oldPalette);
+
+ QQuickControlPrivate::updatePaletteRecur(q, palette);
+
+ if (oldPalette != palette)
+ emit q->paletteChanged();
+}
+
+void QQuickControlPrivate::updatePaletteRecur(QQuickItem *item, const QPalette &palette)
+{
+ const auto childItems = item->childItems();
+ for (QQuickItem *child : childItems) {
+ if (QQuickControl *control = qobject_cast<QQuickControl *>(child))
+ QQuickControlPrivate::get(control)->inheritPalette(palette);
+ else if (QQuickLabel *label = qobject_cast<QQuickLabel *>(child))
+ QQuickLabelPrivate::get(label)->inheritPalette(palette);
+ else if (QQuickTextArea *textArea = qobject_cast<QQuickTextArea *>(child))
+ QQuickTextAreaPrivate::get(textArea)->inheritPalette(palette);
+ else if (QQuickTextField *textField = qobject_cast<QQuickTextField *>(child))
+ QQuickTextFieldPrivate::get(textField)->inheritPalette(palette);
+ else
+ QQuickControlPrivate::updatePaletteRecur(child, palette);
+ }
+}
+
QLocale QQuickControlPrivate::calcLocale(const QQuickItem *item)
{
const QQuickItem *p = item;
@@ -556,11 +661,15 @@ void QQuickControlPrivate::destroyDelegate(QObject *delegate, QObject *parent)
QQuickControl::QQuickControl(QQuickItem *parent)
: QQuickItem(*(new QQuickControlPrivate), parent)
{
+ // ### TODO: ItemEnabledChanged?
+ connect(this, &QQuickItem::enabledChanged, this, &QQuickControl::paletteChanged);
}
QQuickControl::QQuickControl(QQuickControlPrivate &dd, QQuickItem *parent)
: QQuickItem(dd, parent)
{
+ // ### TODO: ItemEnabledChanged?
+ connect(this, &QQuickItem::enabledChanged, this, &QQuickControl::paletteChanged);
}
void QQuickControl::itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &value)
@@ -577,6 +686,7 @@ void QQuickControl::itemChange(QQuickItem::ItemChange change, const QQuickItem::
case ItemParentHasChanged:
if (value.item) {
d->resolveFont();
+ d->resolvePalette();
if (!d->hasLocale)
d->updateLocale(QQuickControlPrivate::calcLocale(d->parentItem), false); // explicit=false
#if QT_CONFIG(quicktemplates2_hover)
@@ -1208,11 +1318,72 @@ void QQuickControl::setContentItem(QQuickItem *item)
d->setContentItem_helper(item, true);
}
+/*!
+ \since QtQuick.Controls 2.3
+ \qmlproperty palette QtQuick.Controls::Control::palette
+
+ This property holds the palette currently set for the control.
+
+ This property describes the control's requested palette. The palette is used by the control's
+ style when rendering standard components, and is available as a means to ensure that custom
+ controls can maintain consistency with the native platform's native look and feel. It's common
+ that different platforms, or different styles, define different palettes for an application.
+
+ The default palette depends on the system environment. ApplicationWindow maintains a system/theme
+ palette which serves as a default for all controls. There may also be special palette defaults for
+ certain types of controls. You can also set the default palette for controls by passing a custom
+ palette to QGuiApplication::setPalette(), before loading any QML.
+
+ Control propagates explicit palette properties from parent to children. If you change a specific
+ property on a control's palette, that property propagates to all of the control's children,
+ overriding any system defaults for that property.
+
+ \code
+ Page {
+ palette.text: "red"
+
+ Column {
+ Label {
+ text: qsTr("This will use red color...")
+ }
+
+ Switch {
+ text: qsTr("... and so will this")
+ }
+ }
+ }
+ \endcode
+*/
+QPalette QQuickControl::palette() const
+{
+ Q_D(const QQuickControl);
+ QPalette palette = d->resolvedPalette;
+ if (!isEnabled())
+ palette.setCurrentColorGroup(QPalette::Disabled);
+ return palette;
+}
+
+void QQuickControl::setPalette(const QPalette &palette)
+{
+ Q_D(QQuickControl);
+ if (d->extra.value().requestedPalette.resolve() == palette.resolve() && d->extra.value().requestedPalette == palette)
+ return;
+
+ d->extra.value().requestedPalette = palette;
+ d->resolvePalette();
+}
+
+void QQuickControl::resetPalette()
+{
+ setPalette(QPalette());
+}
+
void QQuickControl::classBegin()
{
Q_D(QQuickControl);
QQuickItem::classBegin();
d->resolveFont();
+ d->resolvePalette();
}
void QQuickControl::componentComplete()
@@ -1237,6 +1408,11 @@ QFont QQuickControl::defaultFont() const
return QQuickControlPrivate::themeFont(QPlatformTheme::SystemFont);
}
+QPalette QQuickControl::defaultPalette() const
+{
+ return QQuickControlPrivate::themePalette(QPlatformTheme::SystemPalette);
+}
+
void QQuickControl::focusInEvent(QFocusEvent *event)
{
QQuickItem::focusInEvent(event);
@@ -1420,6 +1596,12 @@ void QQuickControl::localeChange(const QLocale &newLocale, const QLocale &oldLoc
Q_UNUSED(oldLocale);
}
+void QQuickControl::paletteChange(const QPalette &newPalette, const QPalette &oldPalette)
+{
+ Q_UNUSED(newPalette);
+ Q_UNUSED(oldPalette);
+}
+
#if QT_CONFIG(accessibility)
QAccessible::Role QQuickControl::accessibleRole() const
{