aboutsummaryrefslogtreecommitdiffstats
path: root/src/templates
diff options
context:
space:
mode:
authorJ-P Nurmi <jpnurmi@theqtcompany.com>2015-11-29 21:56:36 +0100
committerJ-P Nurmi <jpnurmi@theqtcompany.com>2015-12-10 21:03:43 +0000
commiteba39a3ba543c04747aa837b064a35ea6b32ed89 (patch)
tree4e13f4f31b560cac3ffeb2c587016dbebad788e2 /src/templates
parentc47aa91ed7e0e120d810dc81863758715b928d1b (diff)
Add ApplicationWindow::activeFocusControl
This makes it possible for ApplicationWindow to visualize key focus navigation with a single focus rectangle that follows the currently active focus control. Change-Id: Ief92bcdc64891b7baa3503216137665a8badcd17 Reviewed-by: Mitch Curtis <mitch.curtis@theqtcompany.com>
Diffstat (limited to 'src/templates')
-rw-r--r--src/templates/qquickapplicationwindow.cpp95
-rw-r--r--src/templates/qquickapplicationwindow_p.h11
2 files changed, 91 insertions, 15 deletions
diff --git a/src/templates/qquickapplicationwindow.cpp b/src/templates/qquickapplicationwindow.cpp
index 32e53a6e..cdc4086e 100644
--- a/src/templates/qquickapplicationwindow.cpp
+++ b/src/templates/qquickapplicationwindow.cpp
@@ -37,6 +37,8 @@
#include "qquickapplicationwindow_p.h"
#include "qquickoverlay_p.h"
#include "qquickcontrol_p_p.h"
+#include "qquicktextarea_p.h"
+#include "qquicktextfield_p.h"
#include <QtCore/private/qobject_p.h>
#include <QtQuick/private/qquickitem_p.h>
@@ -93,6 +95,7 @@ public:
, header(Q_NULLPTR)
, footer(Q_NULLPTR)
, overlay(Q_NULLPTR)
+ , activeFocusControl(Q_NULLPTR)
{ }
void relayout();
@@ -108,12 +111,16 @@ public:
}
void resolveFont();
+ void _q_updateActiveFocus();
+ void setActiveFocusControl(QQuickItem *item);
+
bool complete;
QQuickItem *contentItem;
QQuickItem *header;
QQuickItem *footer;
QQuickOverlay *overlay;
QFont font;
+ QQuickItem *activeFocusControl;
QQuickApplicationWindow *q_ptr;
};
@@ -165,10 +172,44 @@ void QQuickApplicationWindowPrivate::itemImplicitHeightChanged(QQuickItem *item)
relayout();
}
+void QQuickApplicationWindowPrivate::_q_updateActiveFocus()
+{
+ Q_Q(QQuickApplicationWindow);
+ QQuickItem *item = q->activeFocusItem();
+ while (item) {
+ QQuickControl *control = qobject_cast<QQuickControl *>(item);
+ if (control) {
+ setActiveFocusControl(control);
+ break;
+ }
+ QQuickTextField *textField = qobject_cast<QQuickTextField *>(item);
+ if (textField) {
+ setActiveFocusControl(textField);
+ break;
+ }
+ QQuickTextArea *textArea = qobject_cast<QQuickTextArea *>(item);
+ if (textArea) {
+ setActiveFocusControl(textArea);
+ break;
+ }
+ item = item->parentItem();
+ }
+}
+
+void QQuickApplicationWindowPrivate::setActiveFocusControl(QQuickItem *control)
+{
+ Q_Q(QQuickApplicationWindow);
+ if (activeFocusControl != control) {
+ activeFocusControl = control;
+ emit q->activeFocusControlChanged();
+ }
+}
+
QQuickApplicationWindow::QQuickApplicationWindow(QWindow *parent) :
QQuickWindowQmlImpl(parent), d_ptr(new QQuickApplicationWindowPrivate)
{
d_ptr->q_ptr = this;
+ connect(this, SIGNAL(activeFocusItemChanged()), this, SLOT(_q_updateActiveFocus()));
}
QQuickApplicationWindow::~QQuickApplicationWindow()
@@ -277,6 +318,25 @@ QQuickItem *QQuickApplicationWindow::contentItem() const
}
/*!
+ \qmlproperty Control Qt.labs.controls::ApplicationWindow::activeFocusControl
+
+ This property holds the control that currently has active focus, or \c null if there is
+ no control with active focus.
+
+ The difference between \L Window::activeFocusItem and ApplicationWindow::activeFocusControl
+ is that the former may point to a building block of a control, whereas the latter points
+ to the enclosing control. For example, when SpinBox has focus, activeFocusItem points to
+ the editor and acticeFocusControl to the SpinBox itself.
+
+ \sa Window::activeFocusItem
+*/
+QQuickItem *QQuickApplicationWindow::activeFocusControl() const
+{
+ Q_D(const QQuickApplicationWindow);
+ return d->activeFocusControl;
+}
+
+/*!
\qmlproperty Item Qt.labs.controls::ApplicationWindow::overlay
\readonly
@@ -392,16 +452,16 @@ void QQuickApplicationWindowAttachedPrivate::windowChange(QQuickWindow *wnd)
if (window != newWindow) {
QQuickApplicationWindow *oldWindow = window;
if (oldWindow) {
- QObject::disconnect(oldWindow, &QQuickApplicationWindow::activeFocusItemChanged,
- q, &QQuickApplicationWindowAttached::activeFocusItemChanged);
+ QObject::disconnect(oldWindow, &QQuickApplicationWindow::activeFocusControlChanged,
+ q, &QQuickApplicationWindowAttached::activeFocusControlChanged);
QObject::disconnect(oldWindow, &QQuickApplicationWindow::headerChanged,
q, &QQuickApplicationWindowAttached::headerChanged);
QObject::disconnect(oldWindow, &QQuickApplicationWindow::footerChanged,
q, &QQuickApplicationWindowAttached::footerChanged);
}
if (newWindow) {
- QObject::connect(newWindow, &QQuickApplicationWindow::activeFocusItemChanged,
- q, &QQuickApplicationWindowAttached::activeFocusItemChanged);
+ QObject::connect(newWindow, &QQuickApplicationWindow::activeFocusControlChanged,
+ q, &QQuickApplicationWindowAttached::activeFocusControlChanged);
QObject::connect(newWindow, &QQuickApplicationWindow::headerChanged,
q, &QQuickApplicationWindowAttached::headerChanged);
QObject::connect(newWindow, &QQuickApplicationWindow::footerChanged,
@@ -413,8 +473,8 @@ void QQuickApplicationWindowAttachedPrivate::windowChange(QQuickWindow *wnd)
emit q->contentItemChanged();
emit q->overlayChanged();
- if ((oldWindow && oldWindow->activeFocusItem()) || (newWindow && newWindow->activeFocusItem()))
- emit q->activeFocusItemChanged();
+ if ((oldWindow && oldWindow->activeFocusControl()) || (newWindow && newWindow->activeFocusControl()))
+ emit q->activeFocusControlChanged();
if ((oldWindow && oldWindow->header()) || (newWindow && newWindow->header()))
emit q->headerChanged();
if ((oldWindow && oldWindow->footer()) || (newWindow && newWindow->footer()))
@@ -458,18 +518,27 @@ QQuickItem *QQuickApplicationWindowAttached::contentItem() const
}
/*!
- \qmlattachedproperty Item Qt.labs.controls::ApplicationWindow::activeFocusItem
+ \qmlattachedproperty Control Qt.labs.controls::ApplicationWindow::activeFocusControl
- This attached property holds the active focus item. The property can be attached
- to any item. The value is \c null if the item is not in an ApplicationWindow, or
- the window has no active focus.
+ This attached property holds the control that currently has active focus, or \c null
+ if there is no control with active focus. The property can be attached to any item.
+ The value is \c null if the item is not in an ApplicationWindow, or the window has
+ no active focus.
\sa Window::activeFocusItem
*/
-QQuickItem *QQuickApplicationWindowAttached::activeFocusItem() const
+
+/*!
+ \qmlattachedproperty Control Qt.labs.controls::ApplicationWindow::activeFocusControl
+
+ This attached property holds the active focus control. The property can be attached
+ to any item. The value is \c null if the item is not in an ApplicationWindow, or
+ the window has no active focus.
+*/
+QQuickItem *QQuickApplicationWindowAttached::activeFocusControl() const
{
Q_D(const QQuickApplicationWindowAttached);
- return d->window ? d->window->activeFocusItem() : Q_NULLPTR;
+ return d->window ? d->window->activeFocusControl() : Q_NULLPTR;
}
/*!
@@ -511,3 +580,5 @@ QQuickItem *QQuickApplicationWindowAttached::overlay() const
}
QT_END_NAMESPACE
+
+#include "moc_qquickapplicationwindow_p.cpp"
diff --git a/src/templates/qquickapplicationwindow_p.h b/src/templates/qquickapplicationwindow_p.h
index a4c64e19..4d207a08 100644
--- a/src/templates/qquickapplicationwindow_p.h
+++ b/src/templates/qquickapplicationwindow_p.h
@@ -63,6 +63,7 @@ class Q_LABSTEMPLATES_EXPORT QQuickApplicationWindow : public QQuickWindowQmlImp
Q_OBJECT
Q_PROPERTY(QQuickItem *contentItem READ contentItem CONSTANT FINAL)
Q_PROPERTY(QQmlListProperty<QObject> data READ contentData FINAL)
+ Q_PROPERTY(QQuickItem *activeFocusControl READ activeFocusControl NOTIFY activeFocusControlChanged FINAL)
Q_PROPERTY(QQuickItem *header READ header WRITE setHeader NOTIFY headerChanged FINAL)
Q_PROPERTY(QQuickItem *footer READ footer WRITE setFooter NOTIFY footerChanged FINAL)
Q_PROPERTY(QQuickItem *overlay READ overlay CONSTANT FINAL)
@@ -76,6 +77,8 @@ public:
QQuickItem *contentItem() const;
QQmlListProperty<QObject> contentData();
+ QQuickItem *activeFocusControl() const;
+
QQuickItem *header() const;
void setHeader(QQuickItem *header);
@@ -91,6 +94,7 @@ public:
static QQuickApplicationWindowAttached *qmlAttachedProperties(QObject *object);
Q_SIGNALS:
+ void activeFocusControlChanged();
void headerChanged();
void footerChanged();
void fontChanged();
@@ -103,6 +107,7 @@ protected:
private:
Q_DISABLE_COPY(QQuickApplicationWindow)
Q_DECLARE_PRIVATE(QQuickApplicationWindow)
+ Q_PRIVATE_SLOT(d_func(), void _q_updateActiveFocus())
QScopedPointer<QQuickApplicationWindowPrivate> d_ptr;
};
@@ -111,7 +116,7 @@ class Q_LABSTEMPLATES_EXPORT QQuickApplicationWindowAttached : public QObject
Q_OBJECT
Q_PROPERTY(QQuickApplicationWindow *window READ window NOTIFY windowChanged FINAL)
Q_PROPERTY(QQuickItem *contentItem READ contentItem NOTIFY contentItemChanged FINAL)
- Q_PROPERTY(QQuickItem *activeFocusItem READ activeFocusItem NOTIFY activeFocusItemChanged FINAL)
+ Q_PROPERTY(QQuickItem *activeFocusControl READ activeFocusControl NOTIFY activeFocusControlChanged FINAL)
Q_PROPERTY(QQuickItem *header READ header NOTIFY headerChanged FINAL)
Q_PROPERTY(QQuickItem *footer READ footer NOTIFY footerChanged FINAL)
Q_PROPERTY(QQuickItem *overlay READ overlay NOTIFY overlayChanged FINAL)
@@ -121,7 +126,7 @@ public:
QQuickApplicationWindow *window() const;
QQuickItem *contentItem() const;
- QQuickItem *activeFocusItem() const;
+ QQuickItem *activeFocusControl() const;
QQuickItem *header() const;
QQuickItem *footer() const;
QQuickItem *overlay() const;
@@ -129,7 +134,7 @@ public:
Q_SIGNALS:
void windowChanged();
void contentItemChanged();
- void activeFocusItemChanged();
+ void activeFocusControlChanged();
void headerChanged();
void footerChanged();
void overlayChanged();