diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2016-10-06 16:06:32 +0200 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2016-10-17 09:51:41 +0000 |
commit | bd5e078e5b908dc647b5395f9a772074ce206670 (patch) | |
tree | 1a9146ff01484fa43c554c7cdaefc35e483a5568 /src/quicktemplates2 | |
parent | a6e55c907e8b1f2928d7b642272e29522965ac6d (diff) |
Make hoverEnabled propagate to children
Hover effects can be sometimes a bit disturbing. This change makes it
possible to conveniently disable hover effects for a tree of controls
in one go. For example, to disable hover effects for a page including
its header, footer, and all list items:
Page {
hoverEnabled: false
header: ...
footer: ...
ListView {
delegate: ...
}
}
[ChangeLog][Controls][Important Behavior Changes] Control::hoverEnabled
has been made to inherit to children, to make it possible to disable
hover effects for a tree of controls in one place.
Change-Id: Ia87144f2cc04957a32f89d3313816b91d97db635
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: J-P Nurmi <jpnurmi@qt.io>
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/quicktemplates2')
-rw-r--r-- | src/quicktemplates2/qquickcontrol.cpp | 84 | ||||
-rw-r--r-- | src/quicktemplates2/qquickcontrol_p.h | 3 | ||||
-rw-r--r-- | src/quicktemplates2/qquickcontrol_p_p.h | 5 | ||||
-rw-r--r-- | src/quicktemplates2/qquickpopup.cpp | 2 | ||||
-rw-r--r-- | src/quicktemplates2/qquicktextarea.cpp | 40 | ||||
-rw-r--r-- | src/quicktemplates2/qquicktextarea_p.h | 3 | ||||
-rw-r--r-- | src/quicktemplates2/qquicktextarea_p_p.h | 3 | ||||
-rw-r--r-- | src/quicktemplates2/qquicktextfield.cpp | 38 | ||||
-rw-r--r-- | src/quicktemplates2/qquicktextfield_p.h | 3 | ||||
-rw-r--r-- | src/quicktemplates2/qquicktextfield_p_p.h | 3 |
10 files changed, 166 insertions, 18 deletions
diff --git a/src/quicktemplates2/qquickcontrol.cpp b/src/quicktemplates2/qquickcontrol.cpp index e89cc54d..1db532b9 100644 --- a/src/quicktemplates2/qquickcontrol.cpp +++ b/src/quicktemplates2/qquickcontrol.cpp @@ -109,7 +109,8 @@ QQuickControlPrivate::ExtraData::ExtraData() } QQuickControlPrivate::QQuickControlPrivate() : - hasTopPadding(false), hasLeftPadding(false), hasRightPadding(false), hasBottomPadding(false), hasLocale(false), hovered(false), wheelEnabled(false), + hasTopPadding(false), hasLeftPadding(false), hasRightPadding(false), hasBottomPadding(false), + hasLocale(false), hovered(false), wheelEnabled(false), explicitHoverEnabled(false), padding(0), topPadding(0), leftPadding(0), rightPadding(0), bottomPadding(0), spacing(0), focusPolicy(Qt::NoFocus), focusReason(Qt::OtherFocusReason), background(nullptr), contentItem(nullptr), accessibleAttached(nullptr) @@ -361,6 +362,62 @@ void QQuickControlPrivate::updateFontRecur(QQuickItem *item, const QFont &f) } } +void QQuickControlPrivate::updateHoverEnabled(bool enabled, bool xplicit) +{ + Q_Q(QQuickControl); + if (!xplicit && explicitHoverEnabled) + return; + + bool wasEnabled = q->isHoverEnabled(); + explicitHoverEnabled = xplicit; + if (wasEnabled != enabled) { + q->setAcceptHoverEvents(enabled); + QQuickControlPrivate::updateHoverEnabledRecur(q, enabled); + emit q->hoverEnabledChanged(); + } +} + +void QQuickControlPrivate::updateHoverEnabledRecur(QQuickItem *item, bool enabled) +{ + const auto childItems = item->childItems(); + for (QQuickItem *child : childItems) { + if (QQuickControl *control = qobject_cast<QQuickControl *>(child)) + QQuickControlPrivate::get(control)->updateHoverEnabled(enabled, false); + else + updateHoverEnabledRecur(child, enabled); + } +} + +bool QQuickControlPrivate::calcHoverEnabled(const QQuickItem *item) +{ + const QQuickItem *p = item; + while (p) { + // QQuickPopupItem accepts hover events to avoid leaking them through. + // Don't inherit that to the children of the popup, but fallback to the + // environment variable or style hint. + if (qobject_cast<const QQuickPopupItem *>(p)) + break; + + if (const QQuickControl *control = qobject_cast<const QQuickControl *>(p)) + return control->isHoverEnabled(); + + QVariant v = p->property("hoverEnabled"); + if (v.isValid() && v.userType() == QMetaType::Bool) + return v.toBool(); + + p = p->parentItem(); + } + + bool ok = false; + int env = qEnvironmentVariableIntValue("QT_QUICK_CONTROLS_HOVER_ENABLED", &ok); + if (ok) + return env != 0; + + // TODO: QQuickApplicationWindow::isHoverEnabled() + + return QGuiApplication::styleHints()->useHoverEffects(); +} + QString QQuickControl::accessibleName() const { #ifndef QT_NO_ACCESSIBILITY @@ -429,6 +486,8 @@ void QQuickControl::itemChange(QQuickItem::ItemChange change, const QQuickItem:: d->resolveFont(); if (!d->hasLocale) d->updateLocale(QQuickControlPrivate::calcLocale(d->parentItem), false); // explicit=false + if (!d->explicitHoverEnabled) + d->updateHoverEnabled(QQuickControlPrivate::calcHoverEnabled(d->parentItem), false); // explicit=false } break; case ItemActiveFocusHasChanged: @@ -958,7 +1017,11 @@ void QQuickControl::setHovered(bool hovered) /*! \qmlproperty bool QtQuick.Controls::Control::hoverEnabled - This property determines whether the control accepts hover events. The default value is \c false. + This property determines whether the control accepts hover events. The default value + is \c Qt.styleHints.useHoverEffects. + + Setting this property propagates the value to all child controls that do not have + \c hoverEnabled explicitly set. \sa hovered */ @@ -971,11 +1034,20 @@ bool QQuickControl::isHoverEnabled() const void QQuickControl::setHoverEnabled(bool enabled) { Q_D(QQuickControl); - if (enabled == d->hoverEnabled) + if (d->explicitHoverEnabled && enabled == d->hoverEnabled) + return; + + d->updateHoverEnabled(enabled, true); // explicit=true +} + +void QQuickControl::resetHoverEnabled() +{ + Q_D(QQuickControl); + if (!d->explicitHoverEnabled) return; - setAcceptHoverEvents(enabled); - emit hoverEnabledChanged(); + d->explicitHoverEnabled = false; + d->updateHoverEnabled(QQuickControlPrivate::calcHoverEnabled(d->parentItem), false); // explicit=false } /*! @@ -1122,6 +1194,8 @@ void QQuickControl::componentComplete() QQuickItem::componentComplete(); if (!d->hasLocale) d->locale = QQuickControlPrivate::calcLocale(d->parentItem); + if (!d->explicitHoverEnabled) + setAcceptHoverEvents(QQuickControlPrivate::calcHoverEnabled(d->parentItem)); #ifndef QT_NO_ACCESSIBILITY if (!d->accessibleAttached && QAccessible::isActive()) accessibilityActiveChanged(true); diff --git a/src/quicktemplates2/qquickcontrol_p.h b/src/quicktemplates2/qquickcontrol_p.h index 9027743a..81dd615f 100644 --- a/src/quicktemplates2/qquickcontrol_p.h +++ b/src/quicktemplates2/qquickcontrol_p.h @@ -74,7 +74,7 @@ class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickControl : public QQuickItem Q_PROPERTY(Qt::FocusReason focusReason READ focusReason WRITE setFocusReason NOTIFY focusReasonChanged FINAL) Q_PROPERTY(bool visualFocus READ hasVisualFocus NOTIFY visualFocusChanged FINAL) Q_PROPERTY(bool hovered READ isHovered NOTIFY hoveredChanged FINAL) - Q_PROPERTY(bool hoverEnabled READ isHoverEnabled WRITE setHoverEnabled NOTIFY hoverEnabledChanged FINAL) + Q_PROPERTY(bool hoverEnabled READ isHoverEnabled WRITE setHoverEnabled RESET resetHoverEnabled NOTIFY hoverEnabledChanged FINAL) Q_PROPERTY(bool wheelEnabled READ isWheelEnabled WRITE setWheelEnabled NOTIFY wheelEnabledChanged FINAL) Q_PROPERTY(QQuickItem *background READ background WRITE setBackground NOTIFY backgroundChanged FINAL) Q_PROPERTY(QQuickItem *contentItem READ contentItem WRITE setContentItem NOTIFY contentItemChanged FINAL) @@ -132,6 +132,7 @@ public: bool isHoverEnabled() const; void setHoverEnabled(bool enabled); + void resetHoverEnabled(); bool isWheelEnabled() const; void setWheelEnabled(bool enabled); diff --git a/src/quicktemplates2/qquickcontrol_p_p.h b/src/quicktemplates2/qquickcontrol_p_p.h index 26760510..cae1ae59 100644 --- a/src/quicktemplates2/qquickcontrol_p_p.h +++ b/src/quicktemplates2/qquickcontrol_p_p.h @@ -111,6 +111,10 @@ public: static void updateLocaleRecur(QQuickItem *item, const QLocale &l); static QLocale calcLocale(const QQuickItem *item); + void updateHoverEnabled(bool enabled, bool xplicit); + static void updateHoverEnabledRecur(QQuickItem *item, bool enabled); + static bool calcHoverEnabled(const QQuickItem *item); + void deleteDelegate(QObject *object); struct ExtraData { @@ -131,6 +135,7 @@ public: bool hasLocale; bool hovered; bool wheelEnabled; + bool explicitHoverEnabled; qreal padding; qreal topPadding; qreal leftPadding; diff --git a/src/quicktemplates2/qquickpopup.cpp b/src/quicktemplates2/qquickpopup.cpp index 45d0e67c..3f7d6907 100644 --- a/src/quicktemplates2/qquickpopup.cpp +++ b/src/quicktemplates2/qquickpopup.cpp @@ -406,7 +406,7 @@ QQuickPopupItem::QQuickPopupItem(QQuickPopup *popup) : setAcceptedMouseButtons(Qt::AllButtons); // TODO: switch to QStyleHints::useHoverEffects in Qt 5.8 - setAcceptHoverEvents(true); + setHoverEnabled(true); // setAcceptHoverEvents(QGuiApplication::styleHints()->useHoverEffects()); // connect(QGuiApplication::styleHints(), &QStyleHints::useHoverEffectsChanged, this, &QQuickItem::setAcceptHoverEvents); } diff --git a/src/quicktemplates2/qquicktextarea.cpp b/src/quicktemplates2/qquicktextarea.cpp index 92c85941..41980073 100644 --- a/src/quicktemplates2/qquicktextarea.cpp +++ b/src/quicktemplates2/qquicktextarea.cpp @@ -131,7 +131,8 @@ QT_BEGIN_NAMESPACE */ QQuickTextAreaPrivate::QQuickTextAreaPrivate() - : hovered(false), background(nullptr), focusReason(Qt::OtherFocusReason), accessibleAttached(nullptr), flickable(nullptr) + : hovered(false), explicitHoverEnabled(false), background(nullptr), + focusReason(Qt::OtherFocusReason), accessibleAttached(nullptr), flickable(nullptr) { #ifndef QT_NO_ACCESSIBILITY QAccessible::installActivationObserver(this); @@ -355,6 +356,21 @@ void QQuickTextAreaPrivate::inheritFont(const QFont &f) emit q->fontChanged(); } +void QQuickTextAreaPrivate::updateHoverEnabled(bool enabled, bool xplicit) +{ + Q_Q(QQuickTextArea); + if (!xplicit && explicitHoverEnabled) + return; + + bool wasEnabled = q->isHoverEnabled(); + explicitHoverEnabled = xplicit; + if (wasEnabled != enabled) { + q->setAcceptHoverEvents(enabled); + QQuickControlPrivate::updateHoverEnabledRecur(q, enabled); + emit q->hoverEnabledChanged(); + } +} + void QQuickTextAreaPrivate::_q_readOnlyChanged(bool isReadOnly) { #ifndef QT_NO_ACCESSIBILITY @@ -549,11 +565,20 @@ bool QQuickTextArea::isHoverEnabled() const void QQuickTextArea::setHoverEnabled(bool enabled) { Q_D(QQuickTextArea); - if (enabled == d->hoverEnabled) + if (d->explicitHoverEnabled && enabled == d->hoverEnabled) return; - setAcceptHoverEvents(enabled); - emit hoverEnabledChanged(); + d->updateHoverEnabled(enabled, true); // explicit=true +} + +void QQuickTextArea::resetHoverEnabled() +{ + Q_D(QQuickTextArea); + if (!d->explicitHoverEnabled) + return; + + d->explicitHoverEnabled = false; + d->updateHoverEnabled(QQuickControlPrivate::calcHoverEnabled(d->parentItem), false); // explicit=false } bool QQuickTextArea::contains(const QPointF &point) const @@ -575,6 +600,8 @@ void QQuickTextArea::componentComplete() { Q_D(QQuickTextArea); QQuickTextEdit::componentComplete(); + if (!d->explicitHoverEnabled) + setAcceptHoverEvents(QQuickControlPrivate::calcHoverEnabled(d->parentItem)); #ifndef QT_NO_ACCESSIBILITY if (!d->accessibleAttached && QAccessible::isActive()) d->accessibilityActiveChanged(true); @@ -588,8 +615,11 @@ void QQuickTextArea::itemChange(QQuickItem::ItemChange change, const QQuickItem: { Q_D(QQuickTextArea); QQuickTextEdit::itemChange(change, value); - if (change == ItemParentHasChanged && value.item) + if (change == ItemParentHasChanged && value.item) { d->resolveFont(); + if (!d->explicitHoverEnabled) + d->updateHoverEnabled(QQuickControlPrivate::calcHoverEnabled(d->parentItem), false); // explicit=false + } } void QQuickTextArea::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) diff --git a/src/quicktemplates2/qquicktextarea_p.h b/src/quicktemplates2/qquicktextarea_p.h index 20500ee6..35bddba0 100644 --- a/src/quicktemplates2/qquicktextarea_p.h +++ b/src/quicktemplates2/qquicktextarea_p.h @@ -68,7 +68,7 @@ class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickTextArea : public QQuickTextEdit Q_PROPERTY(QString placeholderText READ placeholderText WRITE setPlaceholderText NOTIFY placeholderTextChanged FINAL) Q_PROPERTY(Qt::FocusReason focusReason READ focusReason WRITE setFocusReason NOTIFY focusReasonChanged FINAL) Q_PROPERTY(bool hovered READ isHovered NOTIFY hoveredChanged FINAL REVISION 1) - Q_PROPERTY(bool hoverEnabled READ isHoverEnabled WRITE setHoverEnabled NOTIFY hoverEnabledChanged FINAL REVISION 1) + Q_PROPERTY(bool hoverEnabled READ isHoverEnabled WRITE setHoverEnabled RESET resetHoverEnabled NOTIFY hoverEnabledChanged FINAL REVISION 1) public: explicit QQuickTextArea(QQuickItem *parent = nullptr); @@ -93,6 +93,7 @@ public: bool isHoverEnabled() const; void setHoverEnabled(bool enabled); + void resetHoverEnabled(); bool contains(const QPointF &point) const override; diff --git a/src/quicktemplates2/qquicktextarea_p_p.h b/src/quicktemplates2/qquicktextarea_p_p.h index 3ffa29bc..ed74ac9a 100644 --- a/src/quicktemplates2/qquicktextarea_p_p.h +++ b/src/quicktemplates2/qquicktextarea_p_p.h @@ -81,6 +81,8 @@ public: void resolveFont(); void inheritFont(const QFont &f); + void updateHoverEnabled(bool h, bool e); + void attachFlickable(QQuickFlickable *flickable); void detachFlickable(); void ensureCursorVisible(); @@ -105,6 +107,7 @@ public: void deleteDelegate(QObject *object); bool hovered; + bool explicitHoverEnabled; QFont font; QQuickItem *background; QString placeholder; diff --git a/src/quicktemplates2/qquicktextfield.cpp b/src/quicktemplates2/qquicktextfield.cpp index 0ca0c6ce..8bdbe3cb 100644 --- a/src/quicktemplates2/qquicktextfield.cpp +++ b/src/quicktemplates2/qquicktextfield.cpp @@ -115,6 +115,7 @@ QT_BEGIN_NAMESPACE QQuickTextFieldPrivate::QQuickTextFieldPrivate() : hovered(false) + , explicitHoverEnabled(false) , background(nullptr) , focusReason(Qt::OtherFocusReason) , accessibleAttached(nullptr) @@ -221,6 +222,21 @@ void QQuickTextFieldPrivate::inheritFont(const QFont &f) emit q->fontChanged(); } +void QQuickTextFieldPrivate::updateHoverEnabled(bool enabled, bool xplicit) +{ + Q_Q(QQuickTextField); + if (!xplicit && explicitHoverEnabled) + return; + + bool wasEnabled = q->isHoverEnabled(); + explicitHoverEnabled = xplicit; + if (wasEnabled != enabled) { + q->setAcceptHoverEvents(enabled); + QQuickControlPrivate::updateHoverEnabledRecur(q, enabled); + emit q->hoverEnabledChanged(); + } +} + void QQuickTextFieldPrivate::_q_readOnlyChanged(bool isReadOnly) { #ifndef QT_NO_ACCESSIBILITY @@ -430,11 +446,20 @@ bool QQuickTextField::isHoverEnabled() const void QQuickTextField::setHoverEnabled(bool enabled) { Q_D(QQuickTextField); - if (enabled == d->hoverEnabled) + if (d->explicitHoverEnabled && enabled == d->hoverEnabled) return; - setAcceptHoverEvents(enabled); - emit hoverEnabledChanged(); + d->updateHoverEnabled(enabled, true); // explicit=true +} + +void QQuickTextField::resetHoverEnabled() +{ + Q_D(QQuickTextField); + if (!d->explicitHoverEnabled) + return; + + d->explicitHoverEnabled = false; + d->updateHoverEnabled(QQuickControlPrivate::calcHoverEnabled(d->parentItem), false); // explicit=false } void QQuickTextField::classBegin() @@ -448,6 +473,8 @@ void QQuickTextField::componentComplete() { Q_D(QQuickTextField); QQuickTextInput::componentComplete(); + if (!d->explicitHoverEnabled) + setAcceptHoverEvents(QQuickControlPrivate::calcHoverEnabled(d->parentItem)); #ifndef QT_NO_ACCESSIBILITY if (!d->accessibleAttached && QAccessible::isActive()) d->accessibilityActiveChanged(true); @@ -461,8 +488,11 @@ void QQuickTextField::itemChange(QQuickItem::ItemChange change, const QQuickItem { Q_D(QQuickTextField); QQuickTextInput::itemChange(change, value); - if (change == ItemParentHasChanged && value.item) + if (change == ItemParentHasChanged && value.item) { d->resolveFont(); + if (!d->explicitHoverEnabled) + d->updateHoverEnabled(QQuickControlPrivate::calcHoverEnabled(d->parentItem), false); // explicit=false + } } void QQuickTextField::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) diff --git a/src/quicktemplates2/qquicktextfield_p.h b/src/quicktemplates2/qquicktextfield_p.h index 5ef7a02d..57521592 100644 --- a/src/quicktemplates2/qquicktextfield_p.h +++ b/src/quicktemplates2/qquicktextfield_p.h @@ -67,7 +67,7 @@ class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickTextField : public QQuickTextInput Q_PROPERTY(QString placeholderText READ placeholderText WRITE setPlaceholderText NOTIFY placeholderTextChanged FINAL) Q_PROPERTY(Qt::FocusReason focusReason READ focusReason WRITE setFocusReason NOTIFY focusReasonChanged FINAL) Q_PROPERTY(bool hovered READ isHovered NOTIFY hoveredChanged FINAL REVISION 1) - Q_PROPERTY(bool hoverEnabled READ isHoverEnabled WRITE setHoverEnabled NOTIFY hoverEnabledChanged FINAL REVISION 1) + Q_PROPERTY(bool hoverEnabled READ isHoverEnabled WRITE setHoverEnabled RESET resetHoverEnabled NOTIFY hoverEnabledChanged FINAL REVISION 1) public: explicit QQuickTextField(QQuickItem *parent = nullptr); @@ -90,6 +90,7 @@ public: bool isHoverEnabled() const; void setHoverEnabled(bool enabled); + void resetHoverEnabled(); Q_SIGNALS: void fontChanged(); diff --git a/src/quicktemplates2/qquicktextfield_p_p.h b/src/quicktemplates2/qquicktextfield_p_p.h index 99e1c48c..fb973ad4 100644 --- a/src/quicktemplates2/qquicktextfield_p_p.h +++ b/src/quicktemplates2/qquicktextfield_p_p.h @@ -79,6 +79,8 @@ public: void resolveFont(); void inheritFont(const QFont &f); + void updateHoverEnabled(bool h, bool e); + qreal getImplicitWidth() const override; qreal getImplicitHeight() const override; @@ -96,6 +98,7 @@ public: void deleteDelegate(QObject *object); bool hovered; + bool explicitHoverEnabled; QFont font; QQuickItem *background; QString placeholder; |