aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJ-P Nurmi <jpnurmi@theqtcompany.com>2016-03-22 22:15:55 +0100
committerJ-P Nurmi <jpnurmi@theqtcompany.com>2016-04-11 09:00:25 +0000
commitdcf7ae894e5d6db7f0856f556a7c1983ba1ad7e9 (patch)
tree5f37239161d62e3c88354b6cc5615751df92b891 /src
parent7172c212faf73b6f4ab80b025e4cd45ecc7d6a86 (diff)
Add Control::activeKeyFocus
This is a convenience property that combines Item::activeFocus and Control::focusReason. This allows us to solve QTBUG-51796 in an elegant way - we can simply set Qt::StrongFocus policy on most of the interactive controls, such as buttons. The only thing we need to make sure is to use activeKeyFocus instead of activeFocus in the styles. This ensures that key focus is only visualized when actually interacting with keys - not when interacting via touch or mouse. This is a generalized version of the solution that was already used in the Universal style ApplicationWindow. Change-Id: Ifbf78e3e412f3791c8f7c369bb2de53af9ac6b0f Task-number: QTBUG-51796 Reviewed-by: Mitch Curtis <mitch.curtis@theqtcompany.com>
Diffstat (limited to 'src')
-rw-r--r--src/imports/controls/universal/ApplicationWindow.qml3
-rw-r--r--src/templates/qquickcontrol.cpp48
-rw-r--r--src/templates/qquickcontrol_p.h4
3 files changed, 48 insertions, 7 deletions
diff --git a/src/imports/controls/universal/ApplicationWindow.qml b/src/imports/controls/universal/ApplicationWindow.qml
index 3113e748..3acc70db 100644
--- a/src/imports/controls/universal/ApplicationWindow.qml
+++ b/src/imports/controls/universal/ApplicationWindow.qml
@@ -52,7 +52,6 @@ T.ApplicationWindow {
parent: window.activeFocusControl
width: parent ? parent.width : 0
height: parent ? parent.height : 0
- visible: parent && !!parent.useSystemFocusVisuals
- && (parent.focusReason === Qt.TabFocusReason || parent.focusReason === Qt.BacktabFocusReason)
+ visible: parent && !!parent.useSystemFocusVisuals && !!parent.activeKeyFocus
}
}
diff --git a/src/templates/qquickcontrol.cpp b/src/templates/qquickcontrol.cpp
index 71e783e7..2bee0ed2 100644
--- a/src/templates/qquickcontrol.cpp
+++ b/src/templates/qquickcontrol.cpp
@@ -73,6 +73,11 @@ QT_BEGIN_NAMESPACE
\labs
*/
+static bool isKeyFocusReason(Qt::FocusReason reason)
+{
+ return reason == Qt::TabFocusReason || reason == Qt::BacktabFocusReason || reason == Qt::ShortcutFocusReason;
+}
+
QQuickControlPrivate::QQuickControlPrivate() :
hasTopPadding(false), hasLeftPadding(false), hasRightPadding(false), hasBottomPadding(false), hasLocale(false), hovered(false), wheelEnabled(false),
padding(0), topPadding(0), leftPadding(0), rightPadding(0), bottomPadding(0), spacing(0),
@@ -379,10 +384,20 @@ void QQuickControl::itemChange(QQuickItem::ItemChange change, const QQuickItem::
{
Q_D(QQuickControl);
QQuickItem::itemChange(change, value);
- if (change == ItemParentHasChanged && value.item) {
- d->resolveFont();
- if (!d->hasLocale)
- d->updateLocale(QQuickControlPrivate::calcLocale(d->parentItem), false); // explicit=false
+ switch (change) {
+ case ItemParentHasChanged:
+ if (value.item) {
+ d->resolveFont();
+ if (!d->hasLocale)
+ d->updateLocale(QQuickControlPrivate::calcLocale(d->parentItem), false); // explicit=false
+ }
+ break;
+ case ItemActiveFocusHasChanged:
+ if (isKeyFocusReason(d->focusReason))
+ emit activeKeyFocusChanged();
+ break;
+ default:
+ break;
}
}
@@ -786,7 +801,7 @@ void QQuickControl::setFocusPolicy(Qt::FocusPolicy policy)
\value Qt.MenuBarFocusReason The menu bar took focus.
\value Qt.OtherFocusReason Another reason, usually application-specific.
- \sa Item::activeFocus
+ \sa activeKeyFocus, Item::activeFocus
*/
Qt::FocusReason QQuickControl::focusReason() const
{
@@ -800,8 +815,31 @@ void QQuickControl::setFocusReason(Qt::FocusReason reason)
if (d->focusReason == reason)
return;
+ Qt::FocusReason oldReason = d->focusReason;
d->focusReason = reason;
emit focusReasonChanged();
+ if (d->activeFocus && isKeyFocusReason(oldReason) != isKeyFocusReason(reason))
+ emit activeKeyFocusChanged();
+}
+
+/*!
+ \qmlproperty bool Qt.labs.controls::Control::activeKeyFocus
+ \readonly
+
+ This property holds whether the control has active focus and the focus
+ reason is either \c Qt.TabFocusReason, \c Qt.BacktabFocusReason, or
+ \c Qt.ShortcutFocusReason.
+
+ In general, for visualizing key focus, this property is preferred over
+ \l Item::activeFocus. This ensures that key focus is only visualized when
+ interacting with keys - not when interacting via touch or mouse.
+
+ \sa focusReason, Item::activeFocus
+*/
+bool QQuickControl::hasActiveKeyFocus() const
+{
+ Q_D(const QQuickControl);
+ return d->activeFocus && isKeyFocusReason(d->focusReason);
}
/*!
diff --git a/src/templates/qquickcontrol_p.h b/src/templates/qquickcontrol_p.h
index e393e827..4cd0b77c 100644
--- a/src/templates/qquickcontrol_p.h
+++ b/src/templates/qquickcontrol_p.h
@@ -72,6 +72,7 @@ class Q_QUICKTEMPLATES_EXPORT QQuickControl : public QQuickItem
Q_PROPERTY(bool mirrored READ isMirrored NOTIFY mirroredChanged FINAL)
Q_PROPERTY(Qt::FocusPolicy focusPolicy READ focusPolicy WRITE setFocusPolicy NOTIFY focusPolicyChanged FINAL)
Q_PROPERTY(Qt::FocusReason focusReason READ focusReason WRITE setFocusReason NOTIFY focusReasonChanged FINAL)
+ Q_PROPERTY(bool activeKeyFocus READ hasActiveKeyFocus NOTIFY activeKeyFocusChanged FINAL)
Q_PROPERTY(bool hovered READ isHovered NOTIFY hoveredChanged FINAL)
Q_PROPERTY(bool hoverEnabled READ isHoverEnabled WRITE setHoverEnabled NOTIFY hoverEnabledChanged FINAL)
Q_PROPERTY(bool wheelEnabled READ isWheelEnabled WRITE setWheelEnabled NOTIFY wheelEnabledChanged FINAL)
@@ -124,6 +125,8 @@ public:
Qt::FocusReason focusReason() const;
void setFocusReason(Qt::FocusReason reason);
+ bool hasActiveKeyFocus() const;
+
bool isHovered() const;
void setHovered(bool hovered);
@@ -153,6 +156,7 @@ Q_SIGNALS:
void mirroredChanged();
void focusPolicyChanged();
void focusReasonChanged();
+ void activeKeyFocusChanged();
void hoveredChanged();
void hoverEnabledChanged();
void wheelEnabledChanged();