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