/
qquickitem.cpp
5276 lines (4362 loc) · 144 KB
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
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.
17
18
**
** In addition, as a special exception, Nokia gives you certain additional
19
** rights. These rights are described in the Nokia Qt LGPL Exception
20
21
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
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.
33
34
35
36
37
38
39
40
41
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
42
#include "qquickitem.h"
43
44
#include "qquickcanvas.h"
45
#include <QtDeclarative/qjsengine.h>
46
#include "qquickcanvas_p.h"
47
48
#include "qquickevents_p_p.h"
49
#include "qquickscreen_p.h"
50
51
52
53
54
#include <QtDeclarative/qdeclarativeengine.h>
#include <QtDeclarative/qdeclarativecomponent.h>
#include <QtDeclarative/qdeclarativeinfo.h>
#include <QtGui/qpen.h>
55
#include <QtGui/qcursor.h>
56
57
#include <QtGui/qguiapplication.h>
#include <QtGui/qinputpanel.h>
58
59
60
61
62
#include <QtCore/qdebug.h>
#include <QtCore/qcoreevent.h>
#include <QtCore/qnumeric.h>
#include <private/qdeclarativeengine_p.h>
63
#include <QtQuick/private/qdeclarativestategroup_p.h>
64
#include <private/qdeclarativeopenmetaobject_p.h>
65
#include <QtQuick/private/qdeclarativestate_p.h>
66
#include <private/qlistmodelinterface_p.h>
67
#include <private/qquickitem_p.h>
68
#include <private/qdeclarativeaccessors_p.h>
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
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);
}
107
/*!
108
\qmlclass Transform QQuickTransform
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.
*/
/*!
130
\qmlclass Translate QQuickTranslate
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.
*/
/*!
172
\qmlclass Scale QQuickScale
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.
*/
/*!
214
\qmlclass Rotation QQuickRotation
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.
*/
269
QQuickTransformPrivate::QQuickTransformPrivate()
270
271
272
{
}
273
274
QQuickTransform::QQuickTransform(QObject *parent)
: QObject(*(new QQuickTransformPrivate), parent)
275
276
277
{
}
278
QQuickTransform::QQuickTransform(QQuickTransformPrivate &dd, QObject *parent)
279
280
281
282
: QObject(dd, parent)
{
}
283
QQuickTransform::~QQuickTransform()
284
{
285
Q_D(QQuickTransform);
286
for (int ii = 0; ii < d->items.count(); ++ii) {
287
QQuickItemPrivate *p = QQuickItemPrivate::get(d->items.at(ii));
288
p->transforms.removeOne(this);
289
p->dirty(QQuickItemPrivate::Transform);
290
291
292
}
}
293
void QQuickTransform::update()
294
{
295
Q_D(QQuickTransform);
296
for (int ii = 0; ii < d->items.count(); ++ii) {
297
298
QQuickItemPrivate *p = QQuickItemPrivate::get(d->items.at(ii));
p->dirty(QQuickItemPrivate::Transform);
299
300
301
}
}
302
QQuickContents::QQuickContents(QQuickItem *item)
303
304
305
306
: m_item(item), m_x(0), m_y(0), m_width(0), m_height(0)
{
}
307
QQuickContents::~QQuickContents()
308
{
309
QList<QQuickItem *> children = m_item->childItems();
310
for (int i = 0; i < children.count(); ++i) {
311
312
QQuickItem *child = children.at(i);
QQuickItemPrivate::get(child)->removeItemChangeListener(this, QQuickItemPrivate::Geometry | QQuickItemPrivate::Destroyed);
313
314
315
}
}
316
bool QQuickContents::calcHeight(QQuickItem *changed)
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;
334
QList<QQuickItem *> children = m_item->childItems();
335
for (int i = 0; i < children.count(); ++i) {
336
QQuickItem *child = children.at(i);
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));
}
348
return (m_height != oldheight || m_y != oldy);
349
350
}
351
bool QQuickContents::calcWidth(QQuickItem *changed)
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;
369
QList<QQuickItem *> children = m_item->childItems();
370
for (int i = 0; i < children.count(); ++i) {
371
QQuickItem *child = children.at(i);
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));
}
383
return (m_width != oldwidth || m_x != oldx);
384
385
}
386
void QQuickContents::complete()
387
{
388
389
QQuickItemPrivate::get(m_item)->addItemChangeListener(this, QQuickItemPrivate::Children);
390
QList<QQuickItem *> children = m_item->childItems();
391
for (int i = 0; i < children.count(); ++i) {
392
393
QQuickItem *child = children.at(i);
QQuickItemPrivate::get(child)->addItemChangeListener(this, QQuickItemPrivate::Geometry | QQuickItemPrivate::Destroyed);
394
395
396
397
398
//###what about changes to visibility?
}
calcGeometry();
}
399
400
401
402
403
void QQuickContents::updateRect()
{
QQuickItemPrivate::get(m_item)->emitChildrenRectChanged(rectF());
}
404
void QQuickContents::itemGeometryChanged(QQuickItem *changed, const QRectF &newGeometry, const QRectF &oldGeometry)
405
406
{
Q_UNUSED(changed)
407
408
bool wChanged = false;
bool hChanged = false;
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())
411
wChanged = calcWidth(/*changed*/);
412
if (newGeometry.height() != oldGeometry.height() || newGeometry.y() != oldGeometry.y())
413
414
415
hChanged = calcHeight(/*changed*/);
if (wChanged || hChanged)
updateRect();
416
417
}
418
void QQuickContents::itemDestroyed(QQuickItem *item)
419
420
{
if (item)
421
QQuickItemPrivate::get(item)->removeItemChangeListener(this, QQuickItemPrivate::Geometry | QQuickItemPrivate::Destroyed);
422
423
424
calcGeometry();
}
425
void QQuickContents::itemChildRemoved(QQuickItem *, QQuickItem *item)
426
427
{
if (item)
428
QQuickItemPrivate::get(item)->removeItemChangeListener(this, QQuickItemPrivate::Geometry | QQuickItemPrivate::Destroyed);
429
430
431
calcGeometry();
}
432
void QQuickContents::itemChildAdded(QQuickItem *, QQuickItem *item)
433
434
{
if (item)
435
QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry | QQuickItemPrivate::Destroyed);
436
calcGeometry(item);
437
438
}
439
QQuickItemKeyFilter::QQuickItemKeyFilter(QQuickItem *item)
440
441
: m_processPost(false), m_next(0)
{
442
QQuickItemPrivate *p = item?QQuickItemPrivate::get(item):0;
443
444
445
446
447
448
if (p) {
m_next = p->keyHandler;
p->keyHandler = this;
}
}
449
QQuickItemKeyFilter::~QQuickItemKeyFilter()
450
451
452
{
}
453
void QQuickItemKeyFilter::keyPressed(QKeyEvent *event, bool post)
454
455
456
457
{
if (m_next) m_next->keyPressed(event, post);
}
458
void QQuickItemKeyFilter::keyReleased(QKeyEvent *event, bool post)
459
460
461
462
{
if (m_next) m_next->keyReleased(event, post);
}
463
void QQuickItemKeyFilter::inputMethodEvent(QInputMethodEvent *event, bool post)
464
465
466
467
468
469
470
{
if (m_next)
m_next->inputMethodEvent(event, post);
else
event->ignore();
}
471
QVariant QQuickItemKeyFilter::inputMethodQuery(Qt::InputMethodQuery query) const
472
473
474
475
476
{
if (m_next) return m_next->inputMethodQuery(query);
return QVariant();
}
477
void QQuickItemKeyFilter::componentComplete()
478
479
480
{
if (m_next) m_next->componentComplete();
}
481
/*!
482
\qmlclass KeyNavigation QQuickKeyNavigationAttached
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.
*/
544
545
546
547
QQuickKeyNavigationAttached::QQuickKeyNavigationAttached(QObject *parent)
: QObject(*(new QQuickKeyNavigationAttachedPrivate), parent),
QQuickItemKeyFilter(qobject_cast<QQuickItem*>(parent))
548
549
550
551
{
m_processPost = true;
}
552
553
QQuickKeyNavigationAttached *
QQuickKeyNavigationAttached::qmlAttachedProperties(QObject *obj)
554
{
555
return new QQuickKeyNavigationAttached(obj);
556
557
}
558
QQuickItem *QQuickKeyNavigationAttached::left() const
559
{
560
Q_D(const QQuickKeyNavigationAttached);
561
562
563
return d->left;
}
564
void QQuickKeyNavigationAttached::setLeft(QQuickItem *i)
565
{
566
Q_D(QQuickKeyNavigationAttached);
567
568
569
570
if (d->left == i)
return;
d->left = i;
d->leftSet = true;
571
572
QQuickKeyNavigationAttached* other =
qobject_cast<QQuickKeyNavigationAttached*>(qmlAttachedPropertiesObject<QQuickKeyNavigationAttached>(i));
573
if (other && !other->d_func()->rightSet){
574
other->d_func()->right = qobject_cast<QQuickItem*>(parent());
575
576
577
578
579
emit other->rightChanged();
}
emit leftChanged();
}
580
QQuickItem *QQuickKeyNavigationAttached::right() const
581
{
582
Q_D(const QQuickKeyNavigationAttached);
583
584
585
return d->right;
}
586
void QQuickKeyNavigationAttached::setRight(QQuickItem *i)
587
{
588
Q_D(QQuickKeyNavigationAttached);
589
590
591
592
if (d->right == i)
return;
d->right = i;
d->rightSet = true;
593
594
QQuickKeyNavigationAttached* other =
qobject_cast<QQuickKeyNavigationAttached*>(qmlAttachedPropertiesObject<QQuickKeyNavigationAttached>(i));
595
if (other && !other->d_func()->leftSet){
596
other->d_func()->left = qobject_cast<QQuickItem*>(parent());
597
598
599
600
601
emit other->leftChanged();
}
emit rightChanged();
}
602
QQuickItem *QQuickKeyNavigationAttached::up() const
603
{
604
Q_D(const QQuickKeyNavigationAttached);
605
606
607
return d->up;
}
608
void QQuickKeyNavigationAttached::setUp(QQuickItem *i)
609
{
610
Q_D(QQuickKeyNavigationAttached);
611
612
613
614
if (d->up == i)
return;
d->up = i;
d->upSet = true;
615
616
QQuickKeyNavigationAttached* other =
qobject_cast<QQuickKeyNavigationAttached*>(qmlAttachedPropertiesObject<QQuickKeyNavigationAttached>(i));
617
if (other && !other->d_func()->downSet){
618
other->d_func()->down = qobject_cast<QQuickItem*>(parent());
619
620
621
622
623
emit other->downChanged();
}
emit upChanged();
}
624
QQuickItem *QQuickKeyNavigationAttached::down() const
625
{
626
Q_D(const QQuickKeyNavigationAttached);
627
628
629
return d->down;
}
630
void QQuickKeyNavigationAttached::setDown(QQuickItem *i)
631
{
632
Q_D(QQuickKeyNavigationAttached);
633
634
635
636
if (d->down == i)
return;
d->down = i;
d->downSet = true;
637
638
QQuickKeyNavigationAttached* other =
qobject_cast<QQuickKeyNavigationAttached*>(qmlAttachedPropertiesObject<QQuickKeyNavigationAttached>(i));
639
if (other && !other->d_func()->upSet) {
640
other->d_func()->up = qobject_cast<QQuickItem*>(parent());
641
642
643
644
645
emit other->upChanged();
}
emit downChanged();
}
646
QQuickItem *QQuickKeyNavigationAttached::tab() const
647
{
648
Q_D(const QQuickKeyNavigationAttached);
649
650
651
return d->tab;
}
652
void QQuickKeyNavigationAttached::setTab(QQuickItem *i)
653
{
654
Q_D(QQuickKeyNavigationAttached);
655
656
657
658
if (d->tab == i)
return;
d->tab = i;
d->tabSet = true;
659
660
QQuickKeyNavigationAttached* other =
qobject_cast<QQuickKeyNavigationAttached*>(qmlAttachedPropertiesObject<QQuickKeyNavigationAttached>(i));
661
if (other && !other->d_func()->backtabSet) {
662
other->d_func()->backtab = qobject_cast<QQuickItem*>(parent());
663
664
665
666
667
emit other->backtabChanged();
}
emit tabChanged();
}
668
QQuickItem *QQuickKeyNavigationAttached::backtab() const
669
{
670
Q_D(const QQuickKeyNavigationAttached);
671
672
673
return d->backtab;
}
674
void QQuickKeyNavigationAttached::setBacktab(QQuickItem *i)
675
{
676
Q_D(QQuickKeyNavigationAttached);
677
678
679
680
if (d->backtab == i)
return;
d->backtab = i;
d->backtabSet = true;
681
682
QQuickKeyNavigationAttached* other =
qobject_cast<QQuickKeyNavigationAttached*>(qmlAttachedPropertiesObject<QQuickKeyNavigationAttached>(i));
683
if (other && !other->d_func()->tabSet) {
684
other->d_func()->tab = qobject_cast<QQuickItem*>(parent());
685
686
687
688
689
emit other->tabChanged();
}
emit backtabChanged();
}
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
*/
705
QQuickKeyNavigationAttached::Priority QQuickKeyNavigationAttached::priority() const
706
707
708
709
{
return m_processPost ? AfterItem : BeforeItem;
}
710
void QQuickKeyNavigationAttached::setPriority(Priority order)
711
712
713
714
715
716
717
718
{
bool processPost = order == AfterItem;
if (processPost != m_processPost) {
m_processPost = processPost;
emit priorityChanged();
}
}
719
void QQuickKeyNavigationAttached::keyPressed(QKeyEvent *event, bool post)
720
{
721
Q_D(QQuickKeyNavigationAttached);
722
723
724
event->ignore();
if (post != m_processPost) {
725
QQuickItemKeyFilter::keyPressed(event, post);
726
727
728
729
return;
}
bool mirror = false;
730
switch (event->key()) {
731
case Qt::Key_Left: {
732
733
734
if (QQuickItem *parentItem = qobject_cast<QQuickItem*>(parent()))
mirror = QQuickItemPrivate::get(parentItem)->effectiveLayoutMirror;
QQuickItem* leftItem = mirror ? d->right : d->left;
735
736
737
738
739
740
741
if (leftItem) {
setFocusNavigation(leftItem, mirror ? "right" : "left");
event->accept();
}
break;
}
case Qt::Key_Right: {
742
743
744
if (QQuickItem *parentItem = qobject_cast<QQuickItem*>(parent()))
mirror = QQuickItemPrivate::get(parentItem)->effectiveLayoutMirror;
QQuickItem* rightItem = mirror ? d->left : d->right;
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;
}
779
if (!event->isAccepted()) QQuickItemKeyFilter::keyPressed(event, post);
780
781
}
782
void QQuickKeyNavigationAttached::keyReleased(QKeyEvent *event, bool post)
783
{
784
Q_D(QQuickKeyNavigationAttached);
785
786
787
event->ignore();
if (post != m_processPost) {
788
QQuickItemKeyFilter::keyReleased(event, post);
789
790
791
792
return;
}
bool mirror = false;
793
switch (event->key()) {
794
case Qt::Key_Left:
795
796
if (QQuickItem *parentItem = qobject_cast<QQuickItem*>(parent()))
mirror = QQuickItemPrivate::get(parentItem)->effectiveLayoutMirror;
797
798
799
800
if (mirror ? d->right : d->left)
event->accept();
break;
case Qt::Key_Right:
801
802
if (QQuickItem *parentItem = qobject_cast<QQuickItem*>(parent()))
mirror = QQuickItemPrivate::get(parentItem)->effectiveLayoutMirror;
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;
}
830
if (!event->isAccepted()) QQuickItemKeyFilter::keyReleased(event, post);
831
832
}
833
void QQuickKeyNavigationAttached::setFocusNavigation(QQuickItem *currentItem, const char *dir)
834
{
835
QQuickItem *initialItem = currentItem;
836
837
838
839
840
841
842
bool isNextItem = false;
do {
isNextItem = false;
if (currentItem->isVisible() && currentItem->isEnabled()) {
currentItem->setFocus(true);
} else {
QObject *attached =
843
qmlAttachedPropertiesObject<QQuickKeyNavigationAttached>(currentItem, false);
844
if (attached) {
845
QQuickItem *tempItem = qvariant_cast<QQuickItem*>(attached->property(dir));
846
847
848
849
850
851
852
853
854
855
if (tempItem) {
currentItem = tempItem;
isNextItem = true;
}
}
}
}
while (currentItem != initialItem && isNextItem);
}
856
const QQuickKeysAttached::SigMap QQuickKeysAttached::sigMap[] = {
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 }
};
888
bool QQuickKeysAttachedPrivate::isConnected(const char *signalName)
889
890
891
892
{
return isSignalConnected(signalIndex(signalName));
}
893
/*!
894
\qmlclass Keys QQuickKeysAttached
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
// ...
}