Skip to content

Latest commit

 

History

History
5276 lines (4362 loc) · 144 KB

qquickitem.cpp

File metadata and controls

5276 lines (4362 loc) · 144 KB
 
Apr 27, 2011
Apr 27, 2011
1
2
3
4
5
6
7
8
9
10
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** 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
11
12
13
14
15
16
** 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
17
18
**
** In addition, as a special exception, Nokia gives you certain additional
Jul 8, 2011
Jul 8, 2011
19
** rights. These rights are described in the Nokia Qt LGPL Exception
Apr 27, 2011
Apr 27, 2011
20
21
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
Jul 8, 2011
Jul 8, 2011
22
23
24
25
26
27
28
29
30
31
32
** 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
33
34
35
36
37
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>
Apr 27, 2011
Apr 27, 2011
69
70
71
72
73
74
75
#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
76
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
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
107
/*!
Oct 21, 2011
Oct 21, 2011
108
\qmlclass Transform QQuickTransform
Aug 9, 2011
Aug 9, 2011
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
\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
130
\qmlclass Translate QQuickTranslate
Aug 9, 2011
Aug 9, 2011
131
132
133
134
135
136
137
138
139
140
141
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
\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
import QtQuick 1.0
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
172
\qmlclass Scale QQuickScale
Aug 9, 2011
Aug 9, 2011
173
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
\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
214
\qmlclass Rotation QQuickRotation
Aug 9, 2011
Aug 9, 2011
215
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
\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
269
QQuickTransformPrivate::QQuickTransformPrivate()
Apr 27, 2011
Apr 27, 2011
270
271
272
{
}
Oct 21, 2011
Oct 21, 2011
273
274
QQuickTransform::QQuickTransform(QObject *parent)
: QObject(*(new QQuickTransformPrivate), parent)
Apr 27, 2011
Apr 27, 2011
275
276
277
{
}
Oct 21, 2011
Oct 21, 2011
278
QQuickTransform::QQuickTransform(QQuickTransformPrivate &dd, QObject *parent)
Apr 27, 2011
Apr 27, 2011
279
280
281
282
: QObject(dd, parent)
{
}
Oct 21, 2011
Oct 21, 2011
283
QQuickTransform::~QQuickTransform()
Apr 27, 2011
Apr 27, 2011
284
{
Oct 21, 2011
Oct 21, 2011
285
Q_D(QQuickTransform);
Apr 27, 2011
Apr 27, 2011
286
for (int ii = 0; ii < d->items.count(); ++ii) {
Oct 21, 2011
Oct 21, 2011
287
QQuickItemPrivate *p = QQuickItemPrivate::get(d->items.at(ii));
Apr 27, 2011
Apr 27, 2011
288
p->transforms.removeOne(this);
Oct 21, 2011
Oct 21, 2011
289
p->dirty(QQuickItemPrivate::Transform);
Apr 27, 2011
Apr 27, 2011
290
291
292
}
}
Oct 21, 2011
Oct 21, 2011
293
void QQuickTransform::update()
Apr 27, 2011
Apr 27, 2011
294
{
Oct 21, 2011
Oct 21, 2011
295
Q_D(QQuickTransform);
Apr 27, 2011
Apr 27, 2011
296
for (int ii = 0; ii < d->items.count(); ++ii) {
Oct 21, 2011
Oct 21, 2011
297
298
QQuickItemPrivate *p = QQuickItemPrivate::get(d->items.at(ii));
p->dirty(QQuickItemPrivate::Transform);
Apr 27, 2011
Apr 27, 2011
299
300
301
}
}
Oct 21, 2011
Oct 21, 2011
302
QQuickContents::QQuickContents(QQuickItem *item)
Apr 27, 2011
Apr 27, 2011
303
304
305
306
: m_item(item), m_x(0), m_y(0), m_width(0), m_height(0)
{
}
Oct 21, 2011
Oct 21, 2011
307
QQuickContents::~QQuickContents()
Apr 27, 2011
Apr 27, 2011
308
{
Oct 21, 2011
Oct 21, 2011
309
QList<QQuickItem *> children = m_item->childItems();
Apr 27, 2011
Apr 27, 2011
310
for (int i = 0; i < children.count(); ++i) {
Oct 21, 2011
Oct 21, 2011
311
312
QQuickItem *child = children.at(i);
QQuickItemPrivate::get(child)->removeItemChangeListener(this, QQuickItemPrivate::Geometry | QQuickItemPrivate::Destroyed);
Apr 27, 2011
Apr 27, 2011
313
314
315
}
}
Dec 1, 2011
Dec 1, 2011
316
bool QQuickContents::calcHeight(QQuickItem *changed)
Apr 27, 2011
Apr 27, 2011
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
{
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
334
QList<QQuickItem *> children = m_item->childItems();
Apr 27, 2011
Apr 27, 2011
335
for (int i = 0; i < children.count(); ++i) {
Oct 21, 2011
Oct 21, 2011
336
QQuickItem *child = children.at(i);
Apr 27, 2011
Apr 27, 2011
337
338
339
340
341
342
343
344
345
346
347
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
348
return (m_height != oldheight || m_y != oldy);
Apr 27, 2011
Apr 27, 2011
349
350
}
Dec 1, 2011
Dec 1, 2011
351
bool QQuickContents::calcWidth(QQuickItem *changed)
Apr 27, 2011
Apr 27, 2011
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
{
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
369
QList<QQuickItem *> children = m_item->childItems();
Apr 27, 2011
Apr 27, 2011
370
for (int i = 0; i < children.count(); ++i) {
Oct 21, 2011
Oct 21, 2011
371
QQuickItem *child = children.at(i);
Apr 27, 2011
Apr 27, 2011
372
373
374
375
376
377
378
379
380
381
382
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
383
return (m_width != oldwidth || m_x != oldx);
Apr 27, 2011
Apr 27, 2011
384
385
}
Oct 21, 2011
Oct 21, 2011
386
void QQuickContents::complete()
Apr 27, 2011
Apr 27, 2011
387
{
Dec 1, 2011
Dec 1, 2011
388
389
QQuickItemPrivate::get(m_item)->addItemChangeListener(this, QQuickItemPrivate::Children);
Oct 21, 2011
Oct 21, 2011
390
QList<QQuickItem *> children = m_item->childItems();
Apr 27, 2011
Apr 27, 2011
391
for (int i = 0; i < children.count(); ++i) {
Oct 21, 2011
Oct 21, 2011
392
393
QQuickItem *child = children.at(i);
QQuickItemPrivate::get(child)->addItemChangeListener(this, QQuickItemPrivate::Geometry | QQuickItemPrivate::Destroyed);
Apr 27, 2011
Apr 27, 2011
394
395
396
397
398
//###what about changes to visibility?
}
calcGeometry();
}
Dec 1, 2011
Dec 1, 2011
399
400
401
402
403
void QQuickContents::updateRect()
{
QQuickItemPrivate::get(m_item)->emitChildrenRectChanged(rectF());
}
Oct 21, 2011
Oct 21, 2011
404
void QQuickContents::itemGeometryChanged(QQuickItem *changed, const QRectF &newGeometry, const QRectF &oldGeometry)
Apr 27, 2011
Apr 27, 2011
405
406
{
Q_UNUSED(changed)
Dec 1, 2011
Dec 1, 2011
407
408
bool wChanged = false;
bool hChanged = false;
Apr 27, 2011
Apr 27, 2011
409
410
//### 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
411
wChanged = calcWidth(/*changed*/);
Apr 27, 2011
Apr 27, 2011
412
if (newGeometry.height() != oldGeometry.height() || newGeometry.y() != oldGeometry.y())
Dec 1, 2011
Dec 1, 2011
413
414
415
hChanged = calcHeight(/*changed*/);
if (wChanged || hChanged)
updateRect();
Apr 27, 2011
Apr 27, 2011
416
417
}
Oct 21, 2011
Oct 21, 2011
418
void QQuickContents::itemDestroyed(QQuickItem *item)
Apr 27, 2011
Apr 27, 2011
419
420
{
if (item)
Oct 21, 2011
Oct 21, 2011
421
QQuickItemPrivate::get(item)->removeItemChangeListener(this, QQuickItemPrivate::Geometry | QQuickItemPrivate::Destroyed);
Apr 27, 2011
Apr 27, 2011
422
423
424
calcGeometry();
}
Dec 1, 2011
Dec 1, 2011
425
void QQuickContents::itemChildRemoved(QQuickItem *, QQuickItem *item)
Apr 27, 2011
Apr 27, 2011
426
427
{
if (item)
Oct 21, 2011
Oct 21, 2011
428
QQuickItemPrivate::get(item)->removeItemChangeListener(this, QQuickItemPrivate::Geometry | QQuickItemPrivate::Destroyed);
Apr 27, 2011
Apr 27, 2011
429
430
431
calcGeometry();
}
Dec 1, 2011
Dec 1, 2011
432
void QQuickContents::itemChildAdded(QQuickItem *, QQuickItem *item)
Apr 27, 2011
Apr 27, 2011
433
434
{
if (item)
Oct 21, 2011
Oct 21, 2011
435
QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry | QQuickItemPrivate::Destroyed);
Dec 1, 2011
Dec 1, 2011
436
calcGeometry(item);
Apr 27, 2011
Apr 27, 2011
437
438
}
Oct 21, 2011
Oct 21, 2011
439
QQuickItemKeyFilter::QQuickItemKeyFilter(QQuickItem *item)
Apr 27, 2011
Apr 27, 2011
440
441
: m_processPost(false), m_next(0)
{
Oct 21, 2011
Oct 21, 2011
442
QQuickItemPrivate *p = item?QQuickItemPrivate::get(item):0;
Apr 27, 2011
Apr 27, 2011
443
444
445
446
447
448
if (p) {
m_next = p->keyHandler;
p->keyHandler = this;
}
}
Oct 21, 2011
Oct 21, 2011
449
QQuickItemKeyFilter::~QQuickItemKeyFilter()
Apr 27, 2011
Apr 27, 2011
450
451
452
{
}
Oct 21, 2011
Oct 21, 2011
453
void QQuickItemKeyFilter::keyPressed(QKeyEvent *event, bool post)
Apr 27, 2011
Apr 27, 2011
454
455
456
457
{
if (m_next) m_next->keyPressed(event, post);
}
Oct 21, 2011
Oct 21, 2011
458
void QQuickItemKeyFilter::keyReleased(QKeyEvent *event, bool post)
Apr 27, 2011
Apr 27, 2011
459
460
461
462
{
if (m_next) m_next->keyReleased(event, post);
}
Oct 21, 2011
Oct 21, 2011
463
void QQuickItemKeyFilter::inputMethodEvent(QInputMethodEvent *event, bool post)
Apr 27, 2011
Apr 27, 2011
464
465
466
467
468
469
470
{
if (m_next)
m_next->inputMethodEvent(event, post);
else
event->ignore();
}
Oct 21, 2011
Oct 21, 2011
471
QVariant QQuickItemKeyFilter::inputMethodQuery(Qt::InputMethodQuery query) const
Apr 27, 2011
Apr 27, 2011
472
473
474
475
476
{
if (m_next) return m_next->inputMethodQuery(query);
return QVariant();
}
Oct 21, 2011
Oct 21, 2011
477
void QQuickItemKeyFilter::componentComplete()
Apr 27, 2011
Apr 27, 2011
478
479
480
{
if (m_next) m_next->componentComplete();
}
Aug 9, 2011
Aug 9, 2011
481
/*!
Oct 21, 2011
Oct 21, 2011
482
\qmlclass KeyNavigation QQuickKeyNavigationAttached
Aug 9, 2011
Aug 9, 2011
483
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
\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
544
Oct 21, 2011
Oct 21, 2011
545
546
547
QQuickKeyNavigationAttached::QQuickKeyNavigationAttached(QObject *parent)
: QObject(*(new QQuickKeyNavigationAttachedPrivate), parent),
QQuickItemKeyFilter(qobject_cast<QQuickItem*>(parent))
Apr 27, 2011
Apr 27, 2011
548
549
550
551
{
m_processPost = true;
}
Oct 21, 2011
Oct 21, 2011
552
553
QQuickKeyNavigationAttached *
QQuickKeyNavigationAttached::qmlAttachedProperties(QObject *obj)
Apr 27, 2011
Apr 27, 2011
554
{
Oct 21, 2011
Oct 21, 2011
555
return new QQuickKeyNavigationAttached(obj);
Apr 27, 2011
Apr 27, 2011
556
557
}
Oct 21, 2011
Oct 21, 2011
558
QQuickItem *QQuickKeyNavigationAttached::left() const
Apr 27, 2011
Apr 27, 2011
559
{
Oct 21, 2011
Oct 21, 2011
560
Q_D(const QQuickKeyNavigationAttached);
Apr 27, 2011
Apr 27, 2011
561
562
563
return d->left;
}
Oct 21, 2011
Oct 21, 2011
564
void QQuickKeyNavigationAttached::setLeft(QQuickItem *i)
Apr 27, 2011
Apr 27, 2011
565
{
Oct 21, 2011
Oct 21, 2011
566
Q_D(QQuickKeyNavigationAttached);
Apr 27, 2011
Apr 27, 2011
567
568
569
570
if (d->left == i)
return;
d->left = i;
d->leftSet = true;
Oct 21, 2011
Oct 21, 2011
571
572
QQuickKeyNavigationAttached* other =
qobject_cast<QQuickKeyNavigationAttached*>(qmlAttachedPropertiesObject<QQuickKeyNavigationAttached>(i));
Apr 27, 2011
Apr 27, 2011
573
if (other && !other->d_func()->rightSet){
Oct 21, 2011
Oct 21, 2011
574
other->d_func()->right = qobject_cast<QQuickItem*>(parent());
Apr 27, 2011
Apr 27, 2011
575
576
577
578
579
emit other->rightChanged();
}
emit leftChanged();
}
Oct 21, 2011
Oct 21, 2011
580
QQuickItem *QQuickKeyNavigationAttached::right() const
Apr 27, 2011
Apr 27, 2011
581
{
Oct 21, 2011
Oct 21, 2011
582
Q_D(const QQuickKeyNavigationAttached);
Apr 27, 2011
Apr 27, 2011
583
584
585
return d->right;
}
Oct 21, 2011
Oct 21, 2011
586
void QQuickKeyNavigationAttached::setRight(QQuickItem *i)
Apr 27, 2011
Apr 27, 2011
587
{
Oct 21, 2011
Oct 21, 2011
588
Q_D(QQuickKeyNavigationAttached);
Apr 27, 2011
Apr 27, 2011
589
590
591
592
if (d->right == i)
return;
d->right = i;
d->rightSet = true;
Oct 21, 2011
Oct 21, 2011
593
594
QQuickKeyNavigationAttached* other =
qobject_cast<QQuickKeyNavigationAttached*>(qmlAttachedPropertiesObject<QQuickKeyNavigationAttached>(i));
Apr 27, 2011
Apr 27, 2011
595
if (other && !other->d_func()->leftSet){
Oct 21, 2011
Oct 21, 2011
596
other->d_func()->left = qobject_cast<QQuickItem*>(parent());
Apr 27, 2011
Apr 27, 2011
597
598
599
600
601
emit other->leftChanged();
}
emit rightChanged();
}
Oct 21, 2011
Oct 21, 2011
602
QQuickItem *QQuickKeyNavigationAttached::up() const
Apr 27, 2011
Apr 27, 2011
603
{
Oct 21, 2011
Oct 21, 2011
604
Q_D(const QQuickKeyNavigationAttached);
Apr 27, 2011
Apr 27, 2011
605
606
607
return d->up;
}
Oct 21, 2011
Oct 21, 2011
608
void QQuickKeyNavigationAttached::setUp(QQuickItem *i)
Apr 27, 2011
Apr 27, 2011
609
{
Oct 21, 2011
Oct 21, 2011
610
Q_D(QQuickKeyNavigationAttached);
Apr 27, 2011
Apr 27, 2011
611
612
613
614
if (d->up == i)
return;
d->up = i;
d->upSet = true;
Oct 21, 2011
Oct 21, 2011
615
616
QQuickKeyNavigationAttached* other =
qobject_cast<QQuickKeyNavigationAttached*>(qmlAttachedPropertiesObject<QQuickKeyNavigationAttached>(i));
Apr 27, 2011
Apr 27, 2011
617
if (other && !other->d_func()->downSet){
Oct 21, 2011
Oct 21, 2011
618
other->d_func()->down = qobject_cast<QQuickItem*>(parent());
Apr 27, 2011
Apr 27, 2011
619
620
621
622
623
emit other->downChanged();
}
emit upChanged();
}
Oct 21, 2011
Oct 21, 2011
624
QQuickItem *QQuickKeyNavigationAttached::down() const
Apr 27, 2011
Apr 27, 2011
625
{
Oct 21, 2011
Oct 21, 2011
626
Q_D(const QQuickKeyNavigationAttached);
Apr 27, 2011
Apr 27, 2011
627
628
629
return d->down;
}
Oct 21, 2011
Oct 21, 2011
630
void QQuickKeyNavigationAttached::setDown(QQuickItem *i)
Apr 27, 2011
Apr 27, 2011
631
{
Oct 21, 2011
Oct 21, 2011
632
Q_D(QQuickKeyNavigationAttached);
Apr 27, 2011
Apr 27, 2011
633
634
635
636
if (d->down == i)
return;
d->down = i;
d->downSet = true;
Oct 21, 2011
Oct 21, 2011
637
638
QQuickKeyNavigationAttached* other =
qobject_cast<QQuickKeyNavigationAttached*>(qmlAttachedPropertiesObject<QQuickKeyNavigationAttached>(i));
Oct 17, 2011
Oct 17, 2011
639
if (other && !other->d_func()->upSet) {
Oct 21, 2011
Oct 21, 2011
640
other->d_func()->up = qobject_cast<QQuickItem*>(parent());
Apr 27, 2011
Apr 27, 2011
641
642
643
644
645
emit other->upChanged();
}
emit downChanged();
}
Oct 21, 2011
Oct 21, 2011
646
QQuickItem *QQuickKeyNavigationAttached::tab() const
Apr 27, 2011
Apr 27, 2011
647
{
Oct 21, 2011
Oct 21, 2011
648
Q_D(const QQuickKeyNavigationAttached);
Apr 27, 2011
Apr 27, 2011
649
650
651
return d->tab;
}
Oct 21, 2011
Oct 21, 2011
652
void QQuickKeyNavigationAttached::setTab(QQuickItem *i)
Apr 27, 2011
Apr 27, 2011
653
{
Oct 21, 2011
Oct 21, 2011
654
Q_D(QQuickKeyNavigationAttached);
Apr 27, 2011
Apr 27, 2011
655
656
657
658
if (d->tab == i)
return;
d->tab = i;
d->tabSet = true;
Oct 21, 2011
Oct 21, 2011
659
660
QQuickKeyNavigationAttached* other =
qobject_cast<QQuickKeyNavigationAttached*>(qmlAttachedPropertiesObject<QQuickKeyNavigationAttached>(i));
Oct 17, 2011
Oct 17, 2011
661
if (other && !other->d_func()->backtabSet) {
Oct 21, 2011
Oct 21, 2011
662
other->d_func()->backtab = qobject_cast<QQuickItem*>(parent());
Apr 27, 2011
Apr 27, 2011
663
664
665
666
667
emit other->backtabChanged();
}
emit tabChanged();
}
Oct 21, 2011
Oct 21, 2011
668
QQuickItem *QQuickKeyNavigationAttached::backtab() const
Apr 27, 2011
Apr 27, 2011
669
{
Oct 21, 2011
Oct 21, 2011
670
Q_D(const QQuickKeyNavigationAttached);
Apr 27, 2011
Apr 27, 2011
671
672
673
return d->backtab;
}
Oct 21, 2011
Oct 21, 2011
674
void QQuickKeyNavigationAttached::setBacktab(QQuickItem *i)
Apr 27, 2011
Apr 27, 2011
675
{
Oct 21, 2011
Oct 21, 2011
676
Q_D(QQuickKeyNavigationAttached);
Apr 27, 2011
Apr 27, 2011
677
678
679
680
if (d->backtab == i)
return;
d->backtab = i;
d->backtabSet = true;
Oct 21, 2011
Oct 21, 2011
681
682
QQuickKeyNavigationAttached* other =
qobject_cast<QQuickKeyNavigationAttached*>(qmlAttachedPropertiesObject<QQuickKeyNavigationAttached>(i));
Oct 17, 2011
Oct 17, 2011
683
if (other && !other->d_func()->tabSet) {
Oct 21, 2011
Oct 21, 2011
684
other->d_func()->tab = qobject_cast<QQuickItem*>(parent());
Apr 27, 2011
Apr 27, 2011
685
686
687
688
689
emit other->tabChanged();
}
emit backtabChanged();
}
Aug 9, 2011
Aug 9, 2011
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
/*!
\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
705
QQuickKeyNavigationAttached::Priority QQuickKeyNavigationAttached::priority() const
Apr 27, 2011
Apr 27, 2011
706
707
708
709
{
return m_processPost ? AfterItem : BeforeItem;
}
Oct 21, 2011
Oct 21, 2011
710
void QQuickKeyNavigationAttached::setPriority(Priority order)
Apr 27, 2011
Apr 27, 2011
711
712
713
714
715
716
717
718
{
bool processPost = order == AfterItem;
if (processPost != m_processPost) {
m_processPost = processPost;
emit priorityChanged();
}
}
Oct 21, 2011
Oct 21, 2011
719
void QQuickKeyNavigationAttached::keyPressed(QKeyEvent *event, bool post)
Apr 27, 2011
Apr 27, 2011
720
{
Oct 21, 2011
Oct 21, 2011
721
Q_D(QQuickKeyNavigationAttached);
Apr 27, 2011
Apr 27, 2011
722
723
724
event->ignore();
if (post != m_processPost) {
Oct 21, 2011
Oct 21, 2011
725
QQuickItemKeyFilter::keyPressed(event, post);
Apr 27, 2011
Apr 27, 2011
726
727
728
729
return;
}
bool mirror = false;
Oct 17, 2011
Oct 17, 2011
730
switch (event->key()) {
Apr 27, 2011
Apr 27, 2011
731
case Qt::Key_Left: {
Oct 21, 2011
Oct 21, 2011
732
733
734
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
735
736
737
738
739
740
741
if (leftItem) {
setFocusNavigation(leftItem, mirror ? "right" : "left");
event->accept();
}
break;
}
case Qt::Key_Right: {
Oct 21, 2011
Oct 21, 2011
742
743
744
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
745
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
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
779
if (!event->isAccepted()) QQuickItemKeyFilter::keyPressed(event, post);
Apr 27, 2011
Apr 27, 2011
780
781
}
Oct 21, 2011
Oct 21, 2011
782
void QQuickKeyNavigationAttached::keyReleased(QKeyEvent *event, bool post)
Apr 27, 2011
Apr 27, 2011
783
{
Oct 21, 2011
Oct 21, 2011
784
Q_D(QQuickKeyNavigationAttached);
Apr 27, 2011
Apr 27, 2011
785
786
787
event->ignore();
if (post != m_processPost) {
Oct 21, 2011
Oct 21, 2011
788
QQuickItemKeyFilter::keyReleased(event, post);
Apr 27, 2011
Apr 27, 2011
789
790
791
792
return;
}
bool mirror = false;
Oct 17, 2011
Oct 17, 2011
793
switch (event->key()) {
Apr 27, 2011
Apr 27, 2011
794
case Qt::Key_Left:
Oct 21, 2011
Oct 21, 2011
795
796
if (QQuickItem *parentItem = qobject_cast<QQuickItem*>(parent()))
mirror = QQuickItemPrivate::get(parentItem)->effectiveLayoutMirror;
Apr 27, 2011
Apr 27, 2011
797
798
799
800
if (mirror ? d->right : d->left)
event->accept();
break;
case Qt::Key_Right:
Oct 21, 2011
Oct 21, 2011
801
802
if (QQuickItem *parentItem = qobject_cast<QQuickItem*>(parent()))
mirror = QQuickItemPrivate::get(parentItem)->effectiveLayoutMirror;
Apr 27, 2011
Apr 27, 2011
803
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
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
830
if (!event->isAccepted()) QQuickItemKeyFilter::keyReleased(event, post);
Apr 27, 2011
Apr 27, 2011
831
832
}
Oct 21, 2011
Oct 21, 2011
833
void QQuickKeyNavigationAttached::setFocusNavigation(QQuickItem *currentItem, const char *dir)
Apr 27, 2011
Apr 27, 2011
834
{
Oct 21, 2011
Oct 21, 2011
835
QQuickItem *initialItem = currentItem;
Apr 27, 2011
Apr 27, 2011
836
837
838
839
840
841
842
bool isNextItem = false;
do {
isNextItem = false;
if (currentItem->isVisible() && currentItem->isEnabled()) {
currentItem->setFocus(true);
} else {
QObject *attached =
Oct 21, 2011
Oct 21, 2011
843
qmlAttachedPropertiesObject<QQuickKeyNavigationAttached>(currentItem, false);
Apr 27, 2011
Apr 27, 2011
844
if (attached) {
Oct 21, 2011
Oct 21, 2011
845
QQuickItem *tempItem = qvariant_cast<QQuickItem*>(attached->property(dir));
Apr 27, 2011
Apr 27, 2011
846
847
848
849
850
851
852
853
854
855
if (tempItem) {
currentItem = tempItem;
isNextItem = true;
}
}
}
}
while (currentItem != initialItem && isNextItem);
}
Oct 21, 2011
Oct 21, 2011
856
const QQuickKeysAttached::SigMap QQuickKeysAttached::sigMap[] = {
Apr 27, 2011
Apr 27, 2011
857
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
{ 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
888
bool QQuickKeysAttachedPrivate::isConnected(const char *signalName)
Apr 27, 2011
Apr 27, 2011
889
890
891
892
{
return isSignalConnected(signalIndex(signalName));
}
Aug 9, 2011
Aug 9, 2011
893
/*!
Oct 21, 2011
Oct 21, 2011
894
\qmlclass Keys QQuickKeysAttached
Aug 9, 2011
Aug 9, 2011
895
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
// ...
}