Skip to content

Latest commit

 

History

History
5826 lines (4791 loc) · 158 KB

qquickitem.cpp

File metadata and controls

5826 lines (4791 loc) · 158 KB
 
Apr 27, 2011
Apr 27, 2011
1
2
/****************************************************************************
**
Jan 5, 2012
Jan 5, 2012
3
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
Jan 23, 2012
Jan 23, 2012
4
** Contact: http://www.qt-project.org/
Apr 27, 2011
Apr 27, 2011
5
6
7
8
9
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
Jul 8, 2011
Jul 8, 2011
10
11
12
13
14
15
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
Apr 27, 2011
Apr 27, 2011
16
17
**
** In addition, as a special exception, Nokia gives you certain additional
Jul 8, 2011
Jul 8, 2011
18
** rights. These rights are described in the Nokia Qt LGPL Exception
Apr 27, 2011
Apr 27, 2011
19
20
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
Jul 8, 2011
Jul 8, 2011
21
22
23
24
25
26
27
28
29
30
31
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
Apr 27, 2011
Apr 27, 2011
32
33
34
35
36
**
**
**
**
**
Jan 30, 2012
Jan 30, 2012
37
**
Apr 27, 2011
Apr 27, 2011
38
39
40
41
** $QT_END_LICENSE$
**
****************************************************************************/
Oct 21, 2011
Oct 21, 2011
42
#include "qquickitem.h"
Apr 27, 2011
Apr 27, 2011
43
Oct 21, 2011
Oct 21, 2011
44
#include "qquickcanvas.h"
Jul 29, 2011
Jul 29, 2011
45
#include <QtDeclarative/qjsengine.h>
Oct 21, 2011
Oct 21, 2011
46
#include "qquickcanvas_p.h"
Apr 27, 2011
Apr 27, 2011
47
Oct 21, 2011
Oct 21, 2011
48
#include "qquickevents_p_p.h"
Dec 9, 2011
Dec 9, 2011
49
#include "qquickscreen_p.h"
Apr 27, 2011
Apr 27, 2011
50
51
52
53
54
#include <QtDeclarative/qdeclarativeengine.h>
#include <QtDeclarative/qdeclarativecomponent.h>
#include <QtDeclarative/qdeclarativeinfo.h>
#include <QtGui/qpen.h>
Sep 1, 2011
Sep 1, 2011
55
#include <QtGui/qcursor.h>
Sep 8, 2011
Sep 8, 2011
56
57
#include <QtGui/qguiapplication.h>
#include <QtGui/qinputpanel.h>
Apr 27, 2011
Apr 27, 2011
58
59
60
61
62
#include <QtCore/qdebug.h>
#include <QtCore/qcoreevent.h>
#include <QtCore/qnumeric.h>
#include <private/qdeclarativeengine_p.h>
Dec 2, 2011
Dec 2, 2011
63
#include <QtQuick/private/qdeclarativestategroup_p.h>
Apr 27, 2011
Apr 27, 2011
64
#include <private/qdeclarativeopenmetaobject_p.h>
Dec 2, 2011
Dec 2, 2011
65
#include <QtQuick/private/qdeclarativestate_p.h>
Apr 27, 2011
Apr 27, 2011
66
#include <private/qlistmodelinterface_p.h>
Oct 21, 2011
Oct 21, 2011
67
#include <private/qquickitem_p.h>
Dec 5, 2011
Dec 5, 2011
68
#include <private/qdeclarativeaccessors_p.h>
Jan 2, 2012
Jan 2, 2012
69
#include <QtQuick/private/qquickaccessibleattached_p.h>
Apr 27, 2011
Apr 27, 2011
70
71
72
73
74
75
76
#include <float.h>
// XXX todo Check that elements that create items handle memory correctly after visual ownership change
QT_BEGIN_NAMESPACE
Dec 5, 2011
Dec 5, 2011
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
static void QQuickItem_parentNotifier(QObject *o, intptr_t, QDeclarativeNotifier **n)
{
QQuickItemPrivate *d = QQuickItemPrivate::get(static_cast<QQuickItem *>(o));
*n = &d->parentNotifier;
}
QML_PRIVATE_ACCESSOR(QQuickItem, QQuickItem *, parent, parentItem)
QML_PRIVATE_ACCESSOR(QQuickItem, qreal, x, x)
QML_PRIVATE_ACCESSOR(QQuickItem, qreal, y, y)
QML_PRIVATE_ACCESSOR(QQuickItem, qreal, width, width)
QML_PRIVATE_ACCESSOR(QQuickItem, qreal, height, height)
static QDeclarativeAccessors QQuickItem_parent = { QQuickItem_parentRead, QQuickItem_parentNotifier };
static QDeclarativeAccessors QQuickItem_x = { QQuickItem_xRead, 0 };
static QDeclarativeAccessors QQuickItem_y = { QQuickItem_yRead, 0 };
static QDeclarativeAccessors QQuickItem_width = { QQuickItem_widthRead, 0 };
static QDeclarativeAccessors QQuickItem_height = { QQuickItem_heightRead, 0 };
QML_DECLARE_PROPERTIES(QQuickItem) {
{ QML_PROPERTY_NAME(parent), 0, &QQuickItem_parent },
{ QML_PROPERTY_NAME(x), 0, &QQuickItem_x },
{ QML_PROPERTY_NAME(y), 0, &QQuickItem_y },
{ QML_PROPERTY_NAME(width), 0, &QQuickItem_width },
{ QML_PROPERTY_NAME(height), 0, &QQuickItem_height }
};
void QQuickItemPrivate::registerAccessorProperties()
{
QML_DEFINE_PROPERTIES(QQuickItem);
}
Aug 9, 2011
Aug 9, 2011
108
/*!
Oct 21, 2011
Oct 21, 2011
109
\qmlclass Transform QQuickTransform
Aug 9, 2011
Aug 9, 2011
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
\inqmlmodule QtQuick 2
\ingroup qml-transform-elements
\brief The Transform elements provide a way of building advanced transformations on Items.
The Transform element is a base type which cannot be instantiated directly.
The following concrete Transform types are available:
\list
\o \l Rotation
\o \l Scale
\o \l Translate
\endlist
The Transform elements let you create and control advanced transformations that can be configured
independently using specialized properties.
You can assign any number of Transform elements to an \l Item. Each Transform is applied in order,
one at a time.
*/
/*!
Oct 21, 2011
Oct 21, 2011
131
\qmlclass Translate QQuickTranslate
Aug 9, 2011
Aug 9, 2011
132
133
134
135
136
137
138
139
140
\inqmlmodule QtQuick 2
\ingroup qml-transform-elements
\brief The Translate object provides a way to move an Item without changing its x or y properties.
The Translate object provides independent control over position in addition to the Item's x and y properties.
The following example moves the Y axis of the \l Rectangle elements while still allowing the \l Row element
to lay the items out as if they had not been transformed:
\qml
Feb 2, 2012
Feb 2, 2012
141
import QtQuick 2.0
Aug 9, 2011
Aug 9, 2011
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
Row {
Rectangle {
width: 100; height: 100
color: "blue"
transform: Translate { y: 20 }
}
Rectangle {
width: 100; height: 100
color: "red"
transform: Translate { y: -20 }
}
}
\endqml
\image translate.png
*/
/*!
\qmlproperty real QtQuick2::Translate::x
The translation along the X axis.
*/
/*!
\qmlproperty real QtQuick2::Translate::y
The translation along the Y axis.
*/
/*!
Oct 21, 2011
Oct 21, 2011
173
\qmlclass Scale QQuickScale
Aug 9, 2011
Aug 9, 2011
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
\inqmlmodule QtQuick 2
\ingroup qml-transform-elements
\brief The Scale element provides a way to scale an Item.
The Scale element gives more control over scaling than using \l Item's \l{Item::scale}{scale} property. Specifically,
it allows a different scale for the x and y axes, and allows the scale to be relative to an
arbitrary point.
The following example scales the X axis of the Rectangle, relative to its interior point 25, 25:
\qml
Rectangle {
width: 100; height: 100
color: "blue"
transform: Scale { origin.x: 25; origin.y: 25; xScale: 3}
}
\endqml
\sa Rotation, Translate
*/
/*!
\qmlproperty real QtQuick2::Scale::origin.x
\qmlproperty real QtQuick2::Scale::origin.y
The point that the item is scaled from (i.e., the point that stays fixed relative to the parent as
the rest of the item grows). By default the origin is 0, 0.
*/
/*!
\qmlproperty real QtQuick2::Scale::xScale
The scaling factor for the X axis.
*/
/*!
\qmlproperty real QtQuick2::Scale::yScale
The scaling factor for the Y axis.
*/
/*!
Oct 21, 2011
Oct 21, 2011
215
\qmlclass Rotation QQuickRotation
Aug 9, 2011
Aug 9, 2011
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
\inqmlmodule QtQuick 2
\ingroup qml-transform-elements
\brief The Rotation object provides a way to rotate an Item.
The Rotation object gives more control over rotation than using \l Item's \l{Item::rotation}{rotation} property.
Specifically, it allows (z axis) rotation to be relative to an arbitrary point.
The following example rotates a Rectangle around its interior point 25, 25:
\qml
Rectangle {
width: 100; height: 100
color: "blue"
transform: Rotation { origin.x: 25; origin.y: 25; angle: 45}
}
\endqml
Rotation also provides a way to specify 3D-like rotations for Items. For these types of
rotations you must specify the axis to rotate around in addition to the origin point.
The following example shows various 3D-like rotations applied to an \l Image.
\snippet doc/src/snippets/declarative/rotation.qml 0
\image axisrotation.png
\sa {declarative/ui-components/dialcontrol}{Dial Control example}, {declarative/toys/clocks}{Clocks example}
*/
/*!
\qmlproperty real QtQuick2::Rotation::origin.x
\qmlproperty real QtQuick2::Rotation::origin.y
The origin point of the rotation (i.e., the point that stays fixed relative to the parent as
the rest of the item rotates). By default the origin is 0, 0.
*/
/*!
\qmlproperty real QtQuick2::Rotation::axis.x
\qmlproperty real QtQuick2::Rotation::axis.y
\qmlproperty real QtQuick2::Rotation::axis.z
The axis to rotate around. For simple (2D) rotation around a point, you do not need to specify an axis,
as the default axis is the z axis (\c{ axis { x: 0; y: 0; z: 1 } }).
For a typical 3D-like rotation you will usually specify both the origin and the axis.
\image 3d-rotation-axis.png
*/
/*!
\qmlproperty real QtQuick2::Rotation::angle
The angle to rotate, in degrees clockwise.
*/
Oct 21, 2011
Oct 21, 2011
270
QQuickTransformPrivate::QQuickTransformPrivate()
Apr 27, 2011
Apr 27, 2011
271
272
273
{
}
Oct 21, 2011
Oct 21, 2011
274
275
QQuickTransform::QQuickTransform(QObject *parent)
: QObject(*(new QQuickTransformPrivate), parent)
Apr 27, 2011
Apr 27, 2011
276
277
278
{
}
Oct 21, 2011
Oct 21, 2011
279
QQuickTransform::QQuickTransform(QQuickTransformPrivate &dd, QObject *parent)
Apr 27, 2011
Apr 27, 2011
280
281
282
283
: QObject(dd, parent)
{
}
Oct 21, 2011
Oct 21, 2011
284
QQuickTransform::~QQuickTransform()
Apr 27, 2011
Apr 27, 2011
285
{
Oct 21, 2011
Oct 21, 2011
286
Q_D(QQuickTransform);
Apr 27, 2011
Apr 27, 2011
287
for (int ii = 0; ii < d->items.count(); ++ii) {
Oct 21, 2011
Oct 21, 2011
288
QQuickItemPrivate *p = QQuickItemPrivate::get(d->items.at(ii));
Apr 27, 2011
Apr 27, 2011
289
p->transforms.removeOne(this);
Oct 21, 2011
Oct 21, 2011
290
p->dirty(QQuickItemPrivate::Transform);
Apr 27, 2011
Apr 27, 2011
291
292
293
}
}
Oct 21, 2011
Oct 21, 2011
294
void QQuickTransform::update()
Apr 27, 2011
Apr 27, 2011
295
{
Oct 21, 2011
Oct 21, 2011
296
Q_D(QQuickTransform);
Apr 27, 2011
Apr 27, 2011
297
for (int ii = 0; ii < d->items.count(); ++ii) {
Oct 21, 2011
Oct 21, 2011
298
299
QQuickItemPrivate *p = QQuickItemPrivate::get(d->items.at(ii));
p->dirty(QQuickItemPrivate::Transform);
Apr 27, 2011
Apr 27, 2011
300
301
302
}
}
Oct 21, 2011
Oct 21, 2011
303
QQuickContents::QQuickContents(QQuickItem *item)
Apr 27, 2011
Apr 27, 2011
304
305
306
307
: m_item(item), m_x(0), m_y(0), m_width(0), m_height(0)
{
}
Oct 21, 2011
Oct 21, 2011
308
QQuickContents::~QQuickContents()
Apr 27, 2011
Apr 27, 2011
309
{
Oct 21, 2011
Oct 21, 2011
310
QList<QQuickItem *> children = m_item->childItems();
Apr 27, 2011
Apr 27, 2011
311
for (int i = 0; i < children.count(); ++i) {
Oct 21, 2011
Oct 21, 2011
312
313
QQuickItem *child = children.at(i);
QQuickItemPrivate::get(child)->removeItemChangeListener(this, QQuickItemPrivate::Geometry | QQuickItemPrivate::Destroyed);
Apr 27, 2011
Apr 27, 2011
314
315
316
}
}
Dec 1, 2011
Dec 1, 2011
317
bool QQuickContents::calcHeight(QQuickItem *changed)
Apr 27, 2011
Apr 27, 2011
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
{
qreal oldy = m_y;
qreal oldheight = m_height;
if (changed) {
qreal top = oldy;
qreal bottom = oldy + oldheight;
qreal y = changed->y();
if (y + changed->height() > bottom)
bottom = y + changed->height();
if (y < top)
top = y;
m_y = top;
m_height = bottom - top;
} else {
qreal top = FLT_MAX;
qreal bottom = 0;
Oct 21, 2011
Oct 21, 2011
335
QList<QQuickItem *> children = m_item->childItems();
Apr 27, 2011
Apr 27, 2011
336
for (int i = 0; i < children.count(); ++i) {
Oct 21, 2011
Oct 21, 2011
337
QQuickItem *child = children.at(i);
Apr 27, 2011
Apr 27, 2011
338
339
340
341
342
343
344
345
346
347
348
qreal y = child->y();
if (y + child->height() > bottom)
bottom = y + child->height();
if (y < top)
top = y;
}
if (!children.isEmpty())
m_y = top;
m_height = qMax(bottom - top, qreal(0.0));
}
Dec 1, 2011
Dec 1, 2011
349
return (m_height != oldheight || m_y != oldy);
Apr 27, 2011
Apr 27, 2011
350
351
}
Dec 1, 2011
Dec 1, 2011
352
bool QQuickContents::calcWidth(QQuickItem *changed)
Apr 27, 2011
Apr 27, 2011
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
{
qreal oldx = m_x;
qreal oldwidth = m_width;
if (changed) {
qreal left = oldx;
qreal right = oldx + oldwidth;
qreal x = changed->x();
if (x + changed->width() > right)
right = x + changed->width();
if (x < left)
left = x;
m_x = left;
m_width = right - left;
} else {
qreal left = FLT_MAX;
qreal right = 0;
Oct 21, 2011
Oct 21, 2011
370
QList<QQuickItem *> children = m_item->childItems();
Apr 27, 2011
Apr 27, 2011
371
for (int i = 0; i < children.count(); ++i) {
Oct 21, 2011
Oct 21, 2011
372
QQuickItem *child = children.at(i);
Apr 27, 2011
Apr 27, 2011
373
374
375
376
377
378
379
380
381
382
383
qreal x = child->x();
if (x + child->width() > right)
right = x + child->width();
if (x < left)
left = x;
}
if (!children.isEmpty())
m_x = left;
m_width = qMax(right - left, qreal(0.0));
}
Dec 1, 2011
Dec 1, 2011
384
return (m_width != oldwidth || m_x != oldx);
Apr 27, 2011
Apr 27, 2011
385
386
}
Oct 21, 2011
Oct 21, 2011
387
void QQuickContents::complete()
Apr 27, 2011
Apr 27, 2011
388
{
Dec 1, 2011
Dec 1, 2011
389
390
QQuickItemPrivate::get(m_item)->addItemChangeListener(this, QQuickItemPrivate::Children);
Oct 21, 2011
Oct 21, 2011
391
QList<QQuickItem *> children = m_item->childItems();
Apr 27, 2011
Apr 27, 2011
392
for (int i = 0; i < children.count(); ++i) {
Oct 21, 2011
Oct 21, 2011
393
394
QQuickItem *child = children.at(i);
QQuickItemPrivate::get(child)->addItemChangeListener(this, QQuickItemPrivate::Geometry | QQuickItemPrivate::Destroyed);
Apr 27, 2011
Apr 27, 2011
395
396
397
398
399
//###what about changes to visibility?
}
calcGeometry();
}
Dec 1, 2011
Dec 1, 2011
400
401
402
403
404
void QQuickContents::updateRect()
{
QQuickItemPrivate::get(m_item)->emitChildrenRectChanged(rectF());
}
Oct 21, 2011
Oct 21, 2011
405
void QQuickContents::itemGeometryChanged(QQuickItem *changed, const QRectF &newGeometry, const QRectF &oldGeometry)
Apr 27, 2011
Apr 27, 2011
406
407
{
Q_UNUSED(changed)
Dec 1, 2011
Dec 1, 2011
408
409
bool wChanged = false;
bool hChanged = false;
Apr 27, 2011
Apr 27, 2011
410
411
//### we can only pass changed if the left edge has moved left, or the right edge has moved right
if (newGeometry.width() != oldGeometry.width() || newGeometry.x() != oldGeometry.x())
Dec 1, 2011
Dec 1, 2011
412
wChanged = calcWidth(/*changed*/);
Apr 27, 2011
Apr 27, 2011
413
if (newGeometry.height() != oldGeometry.height() || newGeometry.y() != oldGeometry.y())
Dec 1, 2011
Dec 1, 2011
414
415
416
hChanged = calcHeight(/*changed*/);
if (wChanged || hChanged)
updateRect();
Apr 27, 2011
Apr 27, 2011
417
418
}
Oct 21, 2011
Oct 21, 2011
419
void QQuickContents::itemDestroyed(QQuickItem *item)
Apr 27, 2011
Apr 27, 2011
420
421
{
if (item)
Oct 21, 2011
Oct 21, 2011
422
QQuickItemPrivate::get(item)->removeItemChangeListener(this, QQuickItemPrivate::Geometry | QQuickItemPrivate::Destroyed);
Apr 27, 2011
Apr 27, 2011
423
424
425
calcGeometry();
}
Dec 1, 2011
Dec 1, 2011
426
void QQuickContents::itemChildRemoved(QQuickItem *, QQuickItem *item)
Apr 27, 2011
Apr 27, 2011
427
428
{
if (item)
Oct 21, 2011
Oct 21, 2011
429
QQuickItemPrivate::get(item)->removeItemChangeListener(this, QQuickItemPrivate::Geometry | QQuickItemPrivate::Destroyed);
Apr 27, 2011
Apr 27, 2011
430
431
432
calcGeometry();
}
Dec 1, 2011
Dec 1, 2011
433
void QQuickContents::itemChildAdded(QQuickItem *, QQuickItem *item)
Apr 27, 2011
Apr 27, 2011
434
435
{
if (item)
Oct 21, 2011
Oct 21, 2011
436
QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry | QQuickItemPrivate::Destroyed);
Dec 1, 2011
Dec 1, 2011
437
calcGeometry(item);
Apr 27, 2011
Apr 27, 2011
438
439
}
Oct 21, 2011
Oct 21, 2011
440
QQuickItemKeyFilter::QQuickItemKeyFilter(QQuickItem *item)
Apr 27, 2011
Apr 27, 2011
441
442
: m_processPost(false), m_next(0)
{
Oct 21, 2011
Oct 21, 2011
443
QQuickItemPrivate *p = item?QQuickItemPrivate::get(item):0;
Apr 27, 2011
Apr 27, 2011
444
445
446
447
448
449
if (p) {
m_next = p->keyHandler;
p->keyHandler = this;
}
}
Oct 21, 2011
Oct 21, 2011
450
QQuickItemKeyFilter::~QQuickItemKeyFilter()
Apr 27, 2011
Apr 27, 2011
451
452
453
{
}
Oct 21, 2011
Oct 21, 2011
454
void QQuickItemKeyFilter::keyPressed(QKeyEvent *event, bool post)
Apr 27, 2011
Apr 27, 2011
455
456
457
458
{
if (m_next) m_next->keyPressed(event, post);
}
Oct 21, 2011
Oct 21, 2011
459
void QQuickItemKeyFilter::keyReleased(QKeyEvent *event, bool post)
Apr 27, 2011
Apr 27, 2011
460
461
462
463
{
if (m_next) m_next->keyReleased(event, post);
}
Oct 21, 2011
Oct 21, 2011
464
void QQuickItemKeyFilter::inputMethodEvent(QInputMethodEvent *event, bool post)
Apr 27, 2011
Apr 27, 2011
465
466
467
468
469
470
471
{
if (m_next)
m_next->inputMethodEvent(event, post);
else
event->ignore();
}
Oct 21, 2011
Oct 21, 2011
472
QVariant QQuickItemKeyFilter::inputMethodQuery(Qt::InputMethodQuery query) const
Apr 27, 2011
Apr 27, 2011
473
474
475
476
477
{
if (m_next) return m_next->inputMethodQuery(query);
return QVariant();
}
Oct 21, 2011
Oct 21, 2011
478
void QQuickItemKeyFilter::componentComplete()
Apr 27, 2011
Apr 27, 2011
479
480
481
{
if (m_next) m_next->componentComplete();
}
Aug 9, 2011
Aug 9, 2011
482
/*!
Oct 21, 2011
Oct 21, 2011
483
\qmlclass KeyNavigation QQuickKeyNavigationAttached
Aug 9, 2011
Aug 9, 2011
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
\inqmlmodule QtQuick 2
\ingroup qml-basic-interaction-elements
\brief The KeyNavigation attached property supports key navigation by arrow keys.
Key-based user interfaces commonly allow the use of arrow keys to navigate between
focusable items. The KeyNavigation attached property enables this behavior by providing a
convenient way to specify the item that should gain focus when an arrow or tab key is pressed.
The following example provides key navigation for a 2x2 grid of items:
\snippet doc/src/snippets/declarative/keynavigation.qml 0
The top-left item initially receives focus by setting \l {Item::}{focus} to
\c true. When an arrow key is pressed, the focus will move to the
appropriate item, as defined by the value that has been set for
the KeyNavigation \l left, \l right, \l up or \l down properties.
Note that if a KeyNavigation attached property receives the key press and release
events for a requested arrow or tab key, the event is accepted and does not
propagate any further.
By default, KeyNavigation receives key events after the item to which it is attached.
If the item accepts the key event, the KeyNavigation attached property will not
receive an event for that key. Setting the \l priority property to
\c KeyNavigation.BeforeItem allows the event to be used for key navigation
before the item, rather than after.
If item to which the focus is switching is not enabled or visible, an attempt will
be made to skip this item and focus on the next. This is possible if there are
a chain of items with the same KeyNavigation handler. If multiple items in a row are not enabled
or visible, they will also be skipped.
KeyNavigation will implicitly set the other direction to return focus to this item. So if you set
\l left to another item, \l right will be set on that item's KeyNavigation to set focus back to this
item. However, if that item's KeyNavigation has had right explicitly set then no change will occur.
This means that the above example could have been written, with the same behaviour, without specifing
KeyNavigation.right or KeyNavigation.down for any of the items.
\sa {Keys}{Keys attached property}
*/
/*!
\qmlproperty Item QtQuick2::KeyNavigation::left
\qmlproperty Item QtQuick2::KeyNavigation::right
\qmlproperty Item QtQuick2::KeyNavigation::up
\qmlproperty Item QtQuick2::KeyNavigation::down
\qmlproperty Item QtQuick2::KeyNavigation::tab
\qmlproperty Item QtQuick2::KeyNavigation::backtab
These properties hold the item to assign focus to
when the left, right, up or down cursor keys, or the
tab key are pressed.
*/
/*!
\qmlproperty Item QtQuick2::KeyNavigation::tab
\qmlproperty Item QtQuick2::KeyNavigation::backtab
These properties hold the item to assign focus to
when the Tab key or Shift+Tab key combination (Backtab) are pressed.
*/
Apr 27, 2011
Apr 27, 2011
545
Oct 21, 2011
Oct 21, 2011
546
547
548
QQuickKeyNavigationAttached::QQuickKeyNavigationAttached(QObject *parent)
: QObject(*(new QQuickKeyNavigationAttachedPrivate), parent),
QQuickItemKeyFilter(qobject_cast<QQuickItem*>(parent))
Apr 27, 2011
Apr 27, 2011
549
550
551
552
{
m_processPost = true;
}
Oct 21, 2011
Oct 21, 2011
553
554
QQuickKeyNavigationAttached *
QQuickKeyNavigationAttached::qmlAttachedProperties(QObject *obj)
Apr 27, 2011
Apr 27, 2011
555
{
Oct 21, 2011
Oct 21, 2011
556
return new QQuickKeyNavigationAttached(obj);
Apr 27, 2011
Apr 27, 2011
557
558
}
Oct 21, 2011
Oct 21, 2011
559
QQuickItem *QQuickKeyNavigationAttached::left() const
Apr 27, 2011
Apr 27, 2011
560
{
Oct 21, 2011
Oct 21, 2011
561
Q_D(const QQuickKeyNavigationAttached);
Apr 27, 2011
Apr 27, 2011
562
563
564
return d->left;
}
Oct 21, 2011
Oct 21, 2011
565
void QQuickKeyNavigationAttached::setLeft(QQuickItem *i)
Apr 27, 2011
Apr 27, 2011
566
{
Oct 21, 2011
Oct 21, 2011
567
Q_D(QQuickKeyNavigationAttached);
Apr 27, 2011
Apr 27, 2011
568
569
570
571
if (d->left == i)
return;
d->left = i;
d->leftSet = true;
Oct 21, 2011
Oct 21, 2011
572
573
QQuickKeyNavigationAttached* other =
qobject_cast<QQuickKeyNavigationAttached*>(qmlAttachedPropertiesObject<QQuickKeyNavigationAttached>(i));
Apr 27, 2011
Apr 27, 2011
574
if (other && !other->d_func()->rightSet){
Oct 21, 2011
Oct 21, 2011
575
other->d_func()->right = qobject_cast<QQuickItem*>(parent());
Apr 27, 2011
Apr 27, 2011
576
577
578
579
580
emit other->rightChanged();
}
emit leftChanged();
}
Oct 21, 2011
Oct 21, 2011
581
QQuickItem *QQuickKeyNavigationAttached::right() const
Apr 27, 2011
Apr 27, 2011
582
{
Oct 21, 2011
Oct 21, 2011
583
Q_D(const QQuickKeyNavigationAttached);
Apr 27, 2011
Apr 27, 2011
584
585
586
return d->right;
}
Oct 21, 2011
Oct 21, 2011
587
void QQuickKeyNavigationAttached::setRight(QQuickItem *i)
Apr 27, 2011
Apr 27, 2011
588
{
Oct 21, 2011
Oct 21, 2011
589
Q_D(QQuickKeyNavigationAttached);
Apr 27, 2011
Apr 27, 2011
590
591
592
593
if (d->right == i)
return;
d->right = i;
d->rightSet = true;
Oct 21, 2011
Oct 21, 2011
594
595
QQuickKeyNavigationAttached* other =
qobject_cast<QQuickKeyNavigationAttached*>(qmlAttachedPropertiesObject<QQuickKeyNavigationAttached>(i));
Apr 27, 2011
Apr 27, 2011
596
if (other && !other->d_func()->leftSet){
Oct 21, 2011
Oct 21, 2011
597
other->d_func()->left = qobject_cast<QQuickItem*>(parent());
Apr 27, 2011
Apr 27, 2011
598
599
600
601
602
emit other->leftChanged();
}
emit rightChanged();
}
Oct 21, 2011
Oct 21, 2011
603
QQuickItem *QQuickKeyNavigationAttached::up() const
Apr 27, 2011
Apr 27, 2011
604
{
Oct 21, 2011
Oct 21, 2011
605
Q_D(const QQuickKeyNavigationAttached);
Apr 27, 2011
Apr 27, 2011
606
607
608
return d->up;
}
Oct 21, 2011
Oct 21, 2011
609
void QQuickKeyNavigationAttached::setUp(QQuickItem *i)
Apr 27, 2011
Apr 27, 2011
610
{
Oct 21, 2011
Oct 21, 2011
611
Q_D(QQuickKeyNavigationAttached);
Apr 27, 2011
Apr 27, 2011
612
613
614
615
if (d->up == i)
return;
d->up = i;
d->upSet = true;
Oct 21, 2011
Oct 21, 2011
616
617
QQuickKeyNavigationAttached* other =
qobject_cast<QQuickKeyNavigationAttached*>(qmlAttachedPropertiesObject<QQuickKeyNavigationAttached>(i));
Apr 27, 2011
Apr 27, 2011
618
if (other && !other->d_func()->downSet){
Oct 21, 2011
Oct 21, 2011
619
other->d_func()->down = qobject_cast<QQuickItem*>(parent());
Apr 27, 2011
Apr 27, 2011
620
621
622
623
624
emit other->downChanged();
}
emit upChanged();
}
Oct 21, 2011
Oct 21, 2011
625
QQuickItem *QQuickKeyNavigationAttached::down() const
Apr 27, 2011
Apr 27, 2011
626
{
Oct 21, 2011
Oct 21, 2011
627
Q_D(const QQuickKeyNavigationAttached);
Apr 27, 2011
Apr 27, 2011
628
629
630
return d->down;
}
Oct 21, 2011
Oct 21, 2011
631
void QQuickKeyNavigationAttached::setDown(QQuickItem *i)
Apr 27, 2011
Apr 27, 2011
632
{
Oct 21, 2011
Oct 21, 2011
633
Q_D(QQuickKeyNavigationAttached);
Apr 27, 2011
Apr 27, 2011
634
635
636
637
if (d->down == i)
return;
d->down = i;
d->downSet = true;
Oct 21, 2011
Oct 21, 2011
638
639
QQuickKeyNavigationAttached* other =
qobject_cast<QQuickKeyNavigationAttached*>(qmlAttachedPropertiesObject<QQuickKeyNavigationAttached>(i));
Oct 17, 2011
Oct 17, 2011
640
if (other && !other->d_func()->upSet) {
Oct 21, 2011
Oct 21, 2011
641
other->d_func()->up = qobject_cast<QQuickItem*>(parent());
Apr 27, 2011
Apr 27, 2011
642
643
644
645
646
emit other->upChanged();
}
emit downChanged();
}
Oct 21, 2011
Oct 21, 2011
647
QQuickItem *QQuickKeyNavigationAttached::tab() const
Apr 27, 2011
Apr 27, 2011
648
{
Oct 21, 2011
Oct 21, 2011
649
Q_D(const QQuickKeyNavigationAttached);
Apr 27, 2011
Apr 27, 2011
650
651
652
return d->tab;
}
Oct 21, 2011
Oct 21, 2011
653
void QQuickKeyNavigationAttached::setTab(QQuickItem *i)
Apr 27, 2011
Apr 27, 2011
654
{
Oct 21, 2011
Oct 21, 2011
655
Q_D(QQuickKeyNavigationAttached);
Apr 27, 2011
Apr 27, 2011
656
657
658
659
if (d->tab == i)
return;
d->tab = i;
d->tabSet = true;
Oct 21, 2011
Oct 21, 2011
660
661
QQuickKeyNavigationAttached* other =
qobject_cast<QQuickKeyNavigationAttached*>(qmlAttachedPropertiesObject<QQuickKeyNavigationAttached>(i));
Oct 17, 2011
Oct 17, 2011
662
if (other && !other->d_func()->backtabSet) {
Oct 21, 2011
Oct 21, 2011
663
other->d_func()->backtab = qobject_cast<QQuickItem*>(parent());
Apr 27, 2011
Apr 27, 2011
664
665
666
667
668
emit other->backtabChanged();
}
emit tabChanged();
}
Oct 21, 2011
Oct 21, 2011
669
QQuickItem *QQuickKeyNavigationAttached::backtab() const
Apr 27, 2011
Apr 27, 2011
670
{
Oct 21, 2011
Oct 21, 2011
671
Q_D(const QQuickKeyNavigationAttached);
Apr 27, 2011
Apr 27, 2011
672
673
674
return d->backtab;
}
Oct 21, 2011
Oct 21, 2011
675
void QQuickKeyNavigationAttached::setBacktab(QQuickItem *i)
Apr 27, 2011
Apr 27, 2011
676
{
Oct 21, 2011
Oct 21, 2011
677
Q_D(QQuickKeyNavigationAttached);
Apr 27, 2011
Apr 27, 2011
678
679
680
681
if (d->backtab == i)
return;
d->backtab = i;
d->backtabSet = true;
Oct 21, 2011
Oct 21, 2011
682
683
QQuickKeyNavigationAttached* other =
qobject_cast<QQuickKeyNavigationAttached*>(qmlAttachedPropertiesObject<QQuickKeyNavigationAttached>(i));
Oct 17, 2011
Oct 17, 2011
684
if (other && !other->d_func()->tabSet) {
Oct 21, 2011
Oct 21, 2011
685
other->d_func()->tab = qobject_cast<QQuickItem*>(parent());
Apr 27, 2011
Apr 27, 2011
686
687
688
689
690
emit other->tabChanged();
}
emit backtabChanged();
}
Aug 9, 2011
Aug 9, 2011
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
/*!
\qmlproperty enumeration QtQuick2::KeyNavigation::priority
This property determines whether the keys are processed before
or after the attached item's own key handling.
\list
\o KeyNavigation.BeforeItem - process the key events before normal
item key processing. If the event is used for key navigation, it will be accepted and will not
be passed on to the item.
\o KeyNavigation.AfterItem (default) - process the key events after normal item key
handling. If the item accepts the key event it will not be
handled by the KeyNavigation attached property handler.
\endlist
*/
Oct 21, 2011
Oct 21, 2011
706
QQuickKeyNavigationAttached::Priority QQuickKeyNavigationAttached::priority() const
Apr 27, 2011
Apr 27, 2011
707
708
709
710
{
return m_processPost ? AfterItem : BeforeItem;
}
Oct 21, 2011
Oct 21, 2011
711
void QQuickKeyNavigationAttached::setPriority(Priority order)
Apr 27, 2011
Apr 27, 2011
712
713
714
715
716
717
718
719
{
bool processPost = order == AfterItem;
if (processPost != m_processPost) {
m_processPost = processPost;
emit priorityChanged();
}
}
Oct 21, 2011
Oct 21, 2011
720
void QQuickKeyNavigationAttached::keyPressed(QKeyEvent *event, bool post)
Apr 27, 2011
Apr 27, 2011
721
{
Oct 21, 2011
Oct 21, 2011
722
Q_D(QQuickKeyNavigationAttached);
Apr 27, 2011
Apr 27, 2011
723
724
725
event->ignore();
if (post != m_processPost) {
Oct 21, 2011
Oct 21, 2011
726
QQuickItemKeyFilter::keyPressed(event, post);
Apr 27, 2011
Apr 27, 2011
727
728
729
730
return;
}
bool mirror = false;
Oct 17, 2011
Oct 17, 2011
731
switch (event->key()) {
Apr 27, 2011
Apr 27, 2011
732
case Qt::Key_Left: {
Oct 21, 2011
Oct 21, 2011
733
734
735
if (QQuickItem *parentItem = qobject_cast<QQuickItem*>(parent()))
mirror = QQuickItemPrivate::get(parentItem)->effectiveLayoutMirror;
QQuickItem* leftItem = mirror ? d->right : d->left;
Apr 27, 2011
Apr 27, 2011
736
737
738
739
740
741
742
if (leftItem) {
setFocusNavigation(leftItem, mirror ? "right" : "left");
event->accept();
}
break;
}
case Qt::Key_Right: {
Oct 21, 2011
Oct 21, 2011
743
744
745
if (QQuickItem *parentItem = qobject_cast<QQuickItem*>(parent()))
mirror = QQuickItemPrivate::get(parentItem)->effectiveLayoutMirror;
QQuickItem* rightItem = mirror ? d->left : d->right;
Apr 27, 2011
Apr 27, 2011
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
if (rightItem) {
setFocusNavigation(rightItem, mirror ? "left" : "right");
event->accept();
}
break;
}
case Qt::Key_Up:
if (d->up) {
setFocusNavigation(d->up, "up");
event->accept();
}
break;
case Qt::Key_Down:
if (d->down) {
setFocusNavigation(d->down, "down");
event->accept();
}
break;
case Qt::Key_Tab:
if (d->tab) {
setFocusNavigation(d->tab, "tab");
event->accept();
}
break;
case Qt::Key_Backtab:
if (d->backtab) {
setFocusNavigation(d->backtab, "backtab");
event->accept();
}
break;
default:
break;
}
Oct 21, 2011
Oct 21, 2011
780
if (!event->isAccepted()) QQuickItemKeyFilter::keyPressed(event, post);
Apr 27, 2011
Apr 27, 2011
781
782
}
Oct 21, 2011
Oct 21, 2011
783
void QQuickKeyNavigationAttached::keyReleased(QKeyEvent *event, bool post)
Apr 27, 2011
Apr 27, 2011
784
{
Oct 21, 2011
Oct 21, 2011
785
Q_D(QQuickKeyNavigationAttached);
Apr 27, 2011
Apr 27, 2011
786
787
788
event->ignore();
if (post != m_processPost) {
Oct 21, 2011
Oct 21, 2011
789
QQuickItemKeyFilter::keyReleased(event, post);
Apr 27, 2011
Apr 27, 2011
790
791
792
793
return;
}
bool mirror = false;
Oct 17, 2011
Oct 17, 2011
794
switch (event->key()) {
Apr 27, 2011
Apr 27, 2011
795
case Qt::Key_Left:
Oct 21, 2011
Oct 21, 2011
796
797
if (QQuickItem *parentItem = qobject_cast<QQuickItem*>(parent()))
mirror = QQuickItemPrivate::get(parentItem)->effectiveLayoutMirror;
Apr 27, 2011
Apr 27, 2011
798
799
800
801
if (mirror ? d->right : d->left)
event->accept();
break;
case Qt::Key_Right:
Oct 21, 2011
Oct 21, 2011
802
803
if (QQuickItem *parentItem = qobject_cast<QQuickItem*>(parent()))
mirror = QQuickItemPrivate::get(parentItem)->effectiveLayoutMirror;
Apr 27, 2011
Apr 27, 2011
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
if (mirror ? d->left : d->right)
event->accept();
break;
case Qt::Key_Up:
if (d->up) {
event->accept();
}
break;
case Qt::Key_Down:
if (d->down) {
event->accept();
}
break;
case Qt::Key_Tab:
if (d->tab) {
event->accept();
}
break;
case Qt::Key_Backtab:
if (d->backtab) {
event->accept();
}
break;
default:
break;
}
Oct 21, 2011
Oct 21, 2011
831
if (!event->isAccepted()) QQuickItemKeyFilter::keyReleased(event, post);
Apr 27, 2011
Apr 27, 2011
832
833
}
Oct 21, 2011
Oct 21, 2011
834
void QQuickKeyNavigationAttached::setFocusNavigation(QQuickItem *currentItem, const char *dir)
Apr 27, 2011
Apr 27, 2011
835
{
Oct 21, 2011
Oct 21, 2011
836
QQuickItem *initialItem = currentItem;
Apr 27, 2011
Apr 27, 2011
837
838
839
840
841
842
843
bool isNextItem = false;
do {
isNextItem = false;
if (currentItem->isVisible() && currentItem->isEnabled()) {
currentItem->setFocus(true);
} else {
QObject *attached =
Oct 21, 2011
Oct 21, 2011
844
qmlAttachedPropertiesObject<QQuickKeyNavigationAttached>(currentItem, false);
Apr 27, 2011
Apr 27, 2011
845
if (attached) {
Oct 21, 2011
Oct 21, 2011
846
QQuickItem *tempItem = qvariant_cast<QQuickItem*>(attached->property(dir));
Apr 27, 2011
Apr 27, 2011
847
848
849
850
851
852
853
854
855
856
if (tempItem) {
currentItem = tempItem;
isNextItem = true;
}
}
}
}
while (currentItem != initialItem && isNextItem);
}
Oct 21, 2011
Oct 21, 2011
857
const QQuickKeysAttached::SigMap QQuickKeysAttached::sigMap[] = {
Apr 27, 2011
Apr 27, 2011
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
{ Qt::Key_Left, "leftPressed" },
{ Qt::Key_Right, "rightPressed" },
{ Qt::Key_Up, "upPressed" },
{ Qt::Key_Down, "downPressed" },
{ Qt::Key_Tab, "tabPressed" },
{ Qt::Key_Backtab, "backtabPressed" },
{ Qt::Key_Asterisk, "asteriskPressed" },
{ Qt::Key_NumberSign, "numberSignPressed" },
{ Qt::Key_Escape, "escapePressed" },
{ Qt::Key_Return, "returnPressed" },
{ Qt::Key_Enter, "enterPressed" },
{ Qt::Key_Delete, "deletePressed" },
{ Qt::Key_Space, "spacePressed" },
{ Qt::Key_Back, "backPressed" },
{ Qt::Key_Cancel, "cancelPressed" },
{ Qt::Key_Select, "selectPressed" },
{ Qt::Key_Yes, "yesPressed" },
{ Qt::Key_No, "noPressed" },
{ Qt::Key_Context1, "context1Pressed" },
{ Qt::Key_Context2, "context2Pressed" },
{ Qt::Key_Context3, "context3Pressed" },
{ Qt::Key_Context4, "context4Pressed" },
{ Qt::Key_Call, "callPressed" },
{ Qt::Key_Hangup, "hangupPressed" },
{ Qt::Key_Flip, "flipPressed" },
{ Qt::Key_Menu, "menuPressed" },
{ Qt::Key_VolumeUp, "volumeUpPressed" },
{ Qt::Key_VolumeDown, "volumeDownPressed" },
{ 0, 0 }
};
Oct 21, 2011
Oct 21, 2011
889
bool QQuickKeysAttachedPrivate::isConnected(const char *signalName)
Apr 27, 2011
Apr 27, 2011
890
891
892
893
{
return isSignalConnected(signalIndex(signalName));
}
Aug 9, 2011
Aug 9, 2011
894
/*!
Oct 21, 2011
Oct 21, 2011
895
\qmlclass Keys QQuickKeysAttached
Aug 9, 2011
Aug 9, 2011
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\inqmlmodule QtQuick 2
\ingroup qml-basic-interaction-elements
\brief The Keys attached property provides key handling to Items.
All visual primitives support key handling via the Keys
attached property. Keys can be handled via the onPressed
and onReleased signal properties.
The signal properties have a \l KeyEvent parameter, named
\e event which contains details of the event. If a key is
handled \e event.accepted should be set to true to prevent the
event from propagating up the item hierarchy.
\section1 Example Usage
The following example shows how the general onPressed handler can
be used to test for a certain key; in this case, the left cursor
key:
\snippet doc/src/snippets/declarative/keys/keys-pressed.qml key item
Some keys may alternatively be handled via specific signal properties,
for example \e onSelectPressed. These handlers automatically set
\e event.accepted to true.
\snippet doc/src/snippets/declarative/keys/keys-handler.qml key item
See \l{Qt::Key}{Qt.Key} for the list of keyboard codes.
\section1 Key Handling Priorities
The Keys attached property can be configured to handle key events
before or after the item it is attached to. This makes it possible
to intercept events in order to override an item's default behavior,
or act as a fallback for keys not handled by the item.
If \l priority is Keys.BeforeItem (default) the order of key event processing is:
\list 1
\o Items specified in \c forwardTo
\o specific key handlers, e.g. onReturnPressed
\o onKeyPress, onKeyRelease handlers
\o Item specific key handling, e.g. TextInput key handling
\o parent item
\endlist
If priority is Keys.AfterItem the order of key event processing is:
\list 1
\o Item specific key handling, e.g. TextInput key handling
\o Items specified in \c forwardTo
\o specific key handlers, e.g. onReturnPressed
\o onKeyPress, onKeyRelease handlers
\o parent item
\endlist
If the event is accepted during any of the above steps, key
propagation stops.
\sa KeyEvent, {KeyNavigation}{KeyNavigation attached property}
*/
/*!
\qmlproperty bool QtQuick2::Keys::enabled
This flags enables key handling if true (default); otherwise
no key handlers will be called.
*/
/*!
\qmlproperty enumeration QtQuick2::Keys::priority
This property determines whether the keys are processed before
or after the attached item's own key handling.
\list
\o Keys.BeforeItem (default) - process the key events before normal
item key processing. If the event is accepted it will not
be passed on to the item.
\o Keys.AfterItem - process the key events after normal item key
handling. If the item accepts the key event it will not be
handled by the Keys attached property handler.
\endlist
*/
/*!
\qmlproperty list<Object> QtQuick2::Keys::forwardTo
This property provides a way to forward key presses, key releases, and keyboard input
coming from input methods to other items. This can be useful when you want
one item to handle some keys (e.g. the up and down arrow keys), and another item to
handle other keys (e.g. the left and right arrow keys). Once an item that has been
forwarded keys accepts the event it is no longer forwarded to items later in the
list.
This example forwards key events to two lists:
\qml
Item {
ListView {
id: list1
// ...
}
ListView {
id: list2
// ...