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