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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
|
/****************************************************************************
**
** Copyright (C) 2010 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$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, 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.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "private/qdeclarativepathview_p.h"
#include "private/qdeclarativepathview_p_p.h"
#include <qdeclarativestate_p.h>
#include <qdeclarativeopenmetaobject_p.h>
#include <QDebug>
#include <QEvent>
#include <qlistmodelinterface_p.h>
#include <QGraphicsSceneEvent>
#include <qmath.h>
#include <math.h>
QT_BEGIN_NAMESPACE
inline qreal qmlMod(qreal x, qreal y)
{
#ifdef QT_USE_MATH_H_FLOATS
if(sizeof(qreal) == sizeof(float))
return fmodf(float(x), float(y));
else
#endif
return fmod(x, y);
}
static QDeclarativeOpenMetaObjectType *qPathViewAttachedType = 0;
QDeclarativePathViewAttached::QDeclarativePathViewAttached(QObject *parent)
: QObject(parent), m_view(0), m_onPath(false), m_isCurrent(false)
{
if (qPathViewAttachedType) {
m_metaobject = new QDeclarativeOpenMetaObject(this, qPathViewAttachedType);
m_metaobject->setCached(true);
} else {
m_metaobject = new QDeclarativeOpenMetaObject(this);
}
}
QDeclarativePathViewAttached::~QDeclarativePathViewAttached()
{
}
QVariant QDeclarativePathViewAttached::value(const QByteArray &name) const
{
return m_metaobject->value(name);
}
void QDeclarativePathViewAttached::setValue(const QByteArray &name, const QVariant &val)
{
m_metaobject->setValue(name, val);
}
void QDeclarativePathViewPrivate::init()
{
Q_Q(QDeclarativePathView);
offset = 0;
q->setAcceptedMouseButtons(Qt::LeftButton);
q->setFlag(QGraphicsItem::ItemIsFocusScope);
q->setFiltersChildEvents(true);
q->connect(&tl, SIGNAL(updated()), q, SLOT(ticked()));
lastPosTime.invalidate();
static int timelineCompletedIdx = -1;
static int movementEndingIdx = -1;
if (timelineCompletedIdx == -1) {
timelineCompletedIdx = QDeclarativeTimeLine::staticMetaObject.indexOfSignal("completed()");
movementEndingIdx = QDeclarativePathView::staticMetaObject.indexOfSlot("movementEnding()");
}
QMetaObject::connect(&tl, timelineCompletedIdx,
q, movementEndingIdx, Qt::DirectConnection);
}
QDeclarativeItem *QDeclarativePathViewPrivate::getItem(int modelIndex)
{
Q_Q(QDeclarativePathView);
requestedIndex = modelIndex;
QDeclarativeItem *item = model->item(modelIndex, false);
if (item) {
if (!attType) {
// pre-create one metatype to share with all attached objects
attType = new QDeclarativeOpenMetaObjectType(&QDeclarativePathViewAttached::staticMetaObject, qmlEngine(q));
foreach(const QString &attr, path->attributes())
attType->createProperty(attr.toUtf8());
}
qPathViewAttachedType = attType;
QDeclarativePathViewAttached *att = static_cast<QDeclarativePathViewAttached *>(qmlAttachedPropertiesObject<QDeclarativePathView>(item));
qPathViewAttachedType = 0;
if (att) {
att->m_view = q;
att->setOnPath(true);
}
item->setParentItem(q);
QDeclarativeItemPrivate *itemPrivate = static_cast<QDeclarativeItemPrivate*>(QGraphicsItemPrivate::get(item));
itemPrivate->addItemChangeListener(this, QDeclarativeItemPrivate::Geometry);
}
requestedIndex = -1;
return item;
}
void QDeclarativePathViewPrivate::releaseItem(QDeclarativeItem *item)
{
if (!item || !model)
return;
if (QDeclarativePathViewAttached *att = attached(item))
att->setOnPath(false);
QDeclarativeItemPrivate *itemPrivate = static_cast<QDeclarativeItemPrivate*>(QGraphicsItemPrivate::get(item));
itemPrivate->removeItemChangeListener(this, QDeclarativeItemPrivate::Geometry);
model->release(item);
}
QDeclarativePathViewAttached *QDeclarativePathViewPrivate::attached(QDeclarativeItem *item)
{
return static_cast<QDeclarativePathViewAttached *>(qmlAttachedPropertiesObject<QDeclarativePathView>(item, false));
}
void QDeclarativePathViewPrivate::clear()
{
for (int i=0; i<items.count(); i++){
QDeclarativeItem *p = items[i];
releaseItem(p);
}
items.clear();
}
void QDeclarativePathViewPrivate::updateMappedRange()
{
if (model && pathItems != -1 && pathItems < model->count())
mappedRange = qreal(pathItems)/model->count();
else
mappedRange = 1.0;
}
qreal QDeclarativePathViewPrivate::positionOfIndex(qreal index) const
{
qreal pos = -1.0;
if (model && index >= 0 && index < model->count()) {
qreal start = 0.0;
if (haveHighlightRange && highlightRangeMode != QDeclarativePathView::NoHighlightRange)
start = highlightRangeStart;
qreal globalPos = index + offset;
globalPos = qmlMod(globalPos, qreal(model->count())) / model->count();
if (pathItems != -1 && pathItems < model->count()) {
globalPos += start * mappedRange;
globalPos = qmlMod(globalPos, 1.0);
if (globalPos < mappedRange)
pos = globalPos / mappedRange;
} else {
pos = qmlMod(globalPos + start, 1.0);
}
}
return pos;
}
void QDeclarativePathViewPrivate::createHighlight()
{
Q_Q(QDeclarativePathView);
if (!q->isComponentComplete())
return;
bool changed = false;
if (highlightItem) {
delete highlightItem;
highlightItem = 0;
changed = true;
}
QDeclarativeItem *item = 0;
if (highlightComponent) {
QDeclarativeContext *highlightContext = new QDeclarativeContext(qmlContext(q));
QObject *nobj = highlightComponent->create(highlightContext);
if (nobj) {
QDeclarative_setParent_noEvent(highlightContext, nobj);
item = qobject_cast<QDeclarativeItem *>(nobj);
if (!item)
delete nobj;
} else {
delete highlightContext;
}
} else {
item = new QDeclarativeItem;
}
if (item) {
QDeclarative_setParent_noEvent(item, q);
item->setParentItem(q);
highlightItem = item;
changed = true;
}
if (changed)
emit q->highlightItemChanged();
}
void QDeclarativePathViewPrivate::updateHighlight()
{
Q_Q(QDeclarativePathView);
if (!q->isComponentComplete() || !isValid())
return;
if (highlightItem) {
if (haveHighlightRange && highlightRangeMode == QDeclarativePathView::StrictlyEnforceRange) {
updateItem(highlightItem, highlightRangeStart);
} else {
qreal target = currentIndex;
tl.reset(moveHighlight);
moveHighlight.setValue(highlightPosition);
const int duration = highlightMoveDuration;
if (target - highlightPosition > model->count()/2) {
highlightUp = false;
qreal distance = model->count() - target + highlightPosition;
tl.move(moveHighlight, 0.0, QEasingCurve(QEasingCurve::InQuad), int(duration * highlightPosition / distance));
tl.set(moveHighlight, model->count()-0.01);
tl.move(moveHighlight, target, QEasingCurve(QEasingCurve::OutQuad), int(duration * (model->count()-target) / distance));
} else if (target - highlightPosition <= -model->count()/2) {
highlightUp = true;
qreal distance = model->count() - highlightPosition + target;
tl.move(moveHighlight, model->count()-0.01, QEasingCurve(QEasingCurve::InQuad), int(duration * (model->count()-highlightPosition) / distance));
tl.set(moveHighlight, 0.0);
tl.move(moveHighlight, target, QEasingCurve(QEasingCurve::OutQuad), int(duration * target / distance));
} else {
highlightUp = highlightPosition - target < 0;
tl.move(moveHighlight, target, QEasingCurve(QEasingCurve::InOutQuad), duration);
}
}
}
}
void QDeclarativePathViewPrivate::setHighlightPosition(qreal pos)
{
if (pos != highlightPosition) {
qreal start = 0.0;
qreal end = 1.0;
if (haveHighlightRange && highlightRangeMode != QDeclarativePathView::NoHighlightRange) {
start = highlightRangeStart;
end = highlightRangeEnd;
}
qreal range = qreal(model->count());
// calc normalized position of highlight relative to offset
qreal relativeHighlight = qmlMod(pos + offset, range) / range;
if (!highlightUp && relativeHighlight > end * mappedRange) {
qreal diff = 1.0 - relativeHighlight;
setOffset(offset + diff * range);
} else if (highlightUp && relativeHighlight >= (end - start) * mappedRange) {
qreal diff = relativeHighlight - (end - start) * mappedRange;
setOffset(offset - diff * range - 0.00001);
}
highlightPosition = pos;
qreal pathPos = positionOfIndex(pos);
updateItem(highlightItem, pathPos);
if (QDeclarativePathViewAttached *att = attached(highlightItem))
att->setOnPath(pathPos != -1.0);
}
}
void QDeclarativePathViewPrivate::updateItem(QDeclarativeItem *item, qreal percent)
{
if (QDeclarativePathViewAttached *att = attached(item)) {
foreach(const QString &attr, path->attributes())
att->setValue(attr.toUtf8(), path->attributeAt(attr, percent));
}
QPointF pf = path->pointAt(percent);
item->setX(qRound(pf.x() - item->width()/2));
item->setY(qRound(pf.y() - item->height()/2));
}
void QDeclarativePathViewPrivate::regenerate()
{
Q_Q(QDeclarativePathView);
if (!q->isComponentComplete())
return;
clear();
if (!isValid())
return;
firstIndex = -1;
updateMappedRange();
q->refill();
}
/*!
\qmlclass PathView QDeclarativePathView
\ingroup qml-view-elements
\since 4.7
\brief The PathView element lays out model-provided items on a path.
\inherits Item
A PathView displays data from models created from built-in QML elements like ListModel
and XmlListModel, or custom model classes defined in C++ that inherit from
QAbstractListModel.
The view has a \l model, which defines the data to be displayed, and
a \l delegate, which defines how the data should be displayed.
The \l delegate is instantiated for each item on the \l path.
The items may be flicked to move them along the path.
For example, if there is a simple list model defined in a file \c ContactModel.qml like this:
\snippet doc/src/snippets/declarative/pathview/ContactModel.qml 0
This data can be represented as a PathView, like this:
\snippet doc/src/snippets/declarative/pathview/pathview.qml 0
\image pathview.gif
(Note the above example uses PathAttribute to scale and modify the
opacity of the items as they rotate. This additional code can be seen in the
PathAttribute documentation.)
The \c focus can be set to \c true to enable keyboard navigation.
The path view itself is a focus scope (see \l{qmlfocus#Acquiring Focus and Focus Scopes}{the focus documentation page} for more details).
Delegates are instantiated as needed and may be destroyed at any time.
State should \e never be stored in a delegate.
\bold Note that views do not enable \e clip automatically. If the view
is not clipped by another item or the screen, it will be necessary
to set \e {clip: true} in order to have the out of view items clipped
nicely.
\sa Path, {declarative/modelviews/pathview}{PathView example}
*/
QDeclarativePathView::QDeclarativePathView(QDeclarativeItem *parent)
: QDeclarativeItem(*(new QDeclarativePathViewPrivate), parent)
{
Q_D(QDeclarativePathView);
d->init();
}
QDeclarativePathView::~QDeclarativePathView()
{
Q_D(QDeclarativePathView);
d->clear();
if (d->attType)
d->attType->release();
if (d->ownModel)
delete d->model;
}
/*!
\qmlattachedproperty PathView PathView::view
This attached property holds the view that manages this delegate instance.
It is attached to each instance of the delegate.
*/
/*!
\qmlattachedproperty bool PathView::onPath
This attached property holds whether the item is currently on the path.
If a pathItemCount has been set, it is possible that some items may
be instantiated, but not considered to be currently on the path.
Usually, these items would be set invisible, for example:
\code
Component {
Rectangle {
visible: PathView.onPath
...
}
}
\endcode
It is attached to each instance of the delegate.
*/
/*!
\qmlattachedproperty bool PathView::isCurrentItem
This attached property is true if this delegate is the current item; otherwise false.
It is attached to each instance of the delegate.
This property may be used to adjust the appearance of the current item.
*/
/*!
\qmlproperty model PathView::model
This property holds the model providing data for the view.
The model provides a set of data that is used to create the items for the view.
For large or dynamic datasets the model is usually provided by a C++ model object.
Models can also be created directly in QML, using the ListModel element.
\sa {qmlmodels}{Data Models}
*/
QVariant QDeclarativePathView::model() const
{
Q_D(const QDeclarativePathView);
return d->modelVariant;
}
void QDeclarativePathView::setModel(const QVariant &model)
{
Q_D(QDeclarativePathView);
if (d->modelVariant == model)
return;
if (d->model) {
disconnect(d->model, SIGNAL(itemsInserted(int,int)), this, SLOT(itemsInserted(int,int)));
disconnect(d->model, SIGNAL(itemsRemoved(int,int)), this, SLOT(itemsRemoved(int,int)));
disconnect(d->model, SIGNAL(itemsMoved(int,int,int)), this, SLOT(itemsMoved(int,int,int)));
disconnect(d->model, SIGNAL(modelReset()), this, SLOT(modelReset()));
disconnect(d->model, SIGNAL(createdItem(int, QDeclarativeItem*)), this, SLOT(createdItem(int,QDeclarativeItem*)));
for (int i=0; i<d->items.count(); i++){
QDeclarativeItem *p = d->items[i];
d->model->release(p);
}
d->items.clear();
}
d->modelVariant = model;
QObject *object = qvariant_cast<QObject*>(model);
QDeclarativeVisualModel *vim = 0;
if (object && (vim = qobject_cast<QDeclarativeVisualModel *>(object))) {
if (d->ownModel) {
delete d->model;
d->ownModel = false;
}
d->model = vim;
} else {
if (!d->ownModel) {
d->model = new QDeclarativeVisualDataModel(qmlContext(this), this);
d->ownModel = true;
}
if (QDeclarativeVisualDataModel *dataModel = qobject_cast<QDeclarativeVisualDataModel*>(d->model))
dataModel->setModel(model);
}
if (d->model) {
connect(d->model, SIGNAL(itemsInserted(int,int)), this, SLOT(itemsInserted(int,int)));
connect(d->model, SIGNAL(itemsRemoved(int,int)), this, SLOT(itemsRemoved(int,int)));
connect(d->model, SIGNAL(itemsMoved(int,int,int)), this, SLOT(itemsMoved(int,int,int)));
connect(d->model, SIGNAL(modelReset()), this, SLOT(modelReset()));
connect(d->model, SIGNAL(createdItem(int, QDeclarativeItem*)), this, SLOT(createdItem(int,QDeclarativeItem*)));
}
if (d->model->count())
d->offset = qmlMod(d->offset, qreal(d->model->count()));
if (d->offset < 0)
d->offset = d->model->count() + d->offset;
d->regenerate();
d->fixOffset();
emit countChanged();
emit modelChanged();
}
/*!
\qmlproperty int PathView::count
This property holds the number of items in the model.
*/
int QDeclarativePathView::count() const
{
Q_D(const QDeclarativePathView);
return d->model ? d->model->count() : 0;
}
/*!
\qmlproperty Path PathView::path
\default
This property holds the path used to lay out the items.
For more information see the \l Path documentation.
*/
QDeclarativePath *QDeclarativePathView::path() const
{
Q_D(const QDeclarativePathView);
return d->path;
}
void QDeclarativePathView::setPath(QDeclarativePath *path)
{
Q_D(QDeclarativePathView);
if (d->path == path)
return;
if (d->path)
disconnect(d->path, SIGNAL(changed()), this, SLOT(refill()));
d->path = path;
connect(d->path, SIGNAL(changed()), this, SLOT(refill()));
if (d->isValid() && isComponentComplete()) {
d->clear();
if (d->attType) {
d->attType->release();
d->attType = 0;
}
d->regenerate();
}
emit pathChanged();
}
/*!
\qmlproperty int PathView::currentIndex
This property holds the index of the current item.
*/
int QDeclarativePathView::currentIndex() const
{
Q_D(const QDeclarativePathView);
return d->currentIndex;
}
void QDeclarativePathView::setCurrentIndex(int idx)
{
Q_D(QDeclarativePathView);
if (d->model && d->model->count())
idx = qAbs(idx % d->model->count());
if (d->model && idx != d->currentIndex) {
if (d->model->count()) {
int itemIndex = (d->currentIndex - d->firstIndex + d->model->count()) % d->model->count();
if (itemIndex < d->items.count()) {
if (QDeclarativeItem *item = d->items.at(itemIndex)) {
if (QDeclarativePathViewAttached *att = d->attached(item))
att->setIsCurrentItem(false);
}
}
}
d->currentItem = 0;
d->moveReason = QDeclarativePathViewPrivate::SetIndex;
d->currentIndex = idx;
if (d->model->count()) {
if (d->haveHighlightRange && d->highlightRangeMode == QDeclarativePathView::StrictlyEnforceRange)
d->snapToCurrent();
int itemIndex = (idx - d->firstIndex + d->model->count()) % d->model->count();
if (itemIndex < d->items.count()) {
d->currentItem = d->items.at(itemIndex);
d->currentItem->setFocus(true);
if (QDeclarativePathViewAttached *att = d->attached(d->currentItem))
att->setIsCurrentItem(true);
}
d->currentItemOffset = d->positionOfIndex(d->currentIndex);
d->updateHighlight();
}
emit currentIndexChanged();
}
}
/*!
\qmlmethod PathView::incrementCurrentIndex()
Increments the current index.
\bold Note: methods should only be called after the Component has completed.
*/
void QDeclarativePathView::incrementCurrentIndex()
{
setCurrentIndex(currentIndex()+1);
}
/*!
\qmlmethod PathView::decrementCurrentIndex()
Decrements the current index.
\bold Note: methods should only be called after the Component has completed.
*/
void QDeclarativePathView::decrementCurrentIndex()
{
Q_D(QDeclarativePathView);
if (d->model && d->model->count()) {
int idx = currentIndex()-1;
if (idx < 0)
idx = d->model->count() - 1;
setCurrentIndex(idx);
}
}
/*!
\qmlproperty real PathView::offset
The offset specifies how far along the path the items are from their initial positions.
This is a real number that ranges from 0.0 to the count of items in the model.
*/
qreal QDeclarativePathView::offset() const
{
Q_D(const QDeclarativePathView);
return d->offset;
}
void QDeclarativePathView::setOffset(qreal offset)
{
Q_D(QDeclarativePathView);
d->setOffset(offset);
d->updateCurrent();
}
void QDeclarativePathViewPrivate::setOffset(qreal o)
{
Q_Q(QDeclarativePathView);
if (offset != o) {
if (isValid() && q->isComponentComplete()) {
offset = qmlMod(o, qreal(model->count()));
if (offset < 0)
offset += qreal(model->count());
q->refill();
} else {
offset = o;
}
emit q->offsetChanged();
}
}
/*!
\qmlproperty Component PathView::highlight
This property holds the component to use as the highlight.
An instance of the highlight component will be created for each view.
The geometry of the resultant component instance will be managed by the view
so as to stay with the current item.
The below example demonstrates how to make a simple highlight. Note the use
of the \l{PathView::onPath}{PathView.onPath} attached property to ensure that
the highlight is hidden when flicked away from the path.
\code
Component {
Rectangle {
visible: PathView.onPath
...
}
}
\endcode
\sa highlightItem, highlightRangeMode
*/
QDeclarativeComponent *QDeclarativePathView::highlight() const
{
Q_D(const QDeclarativePathView);
return d->highlightComponent;
}
void QDeclarativePathView::setHighlight(QDeclarativeComponent *highlight)
{
Q_D(QDeclarativePathView);
if (highlight != d->highlightComponent) {
d->highlightComponent = highlight;
d->createHighlight();
d->updateHighlight();
emit highlightChanged();
}
}
/*!
\qmlproperty Item PathView::highlightItem
\c highlightItem holds the highlight item, which was created
from the \l highlight component.
\sa highlight
*/
QDeclarativeItem *QDeclarativePathView::highlightItem()
{
Q_D(const QDeclarativePathView);
return d->highlightItem;
}
/*!
\qmlproperty real PathView::preferredHighlightBegin
\qmlproperty real PathView::preferredHighlightEnd
\qmlproperty enumeration PathView::highlightRangeMode
These properties set the preferred range of the highlight (current item)
within the view. The preferred values must be in the range 0.0-1.0.
If highlightRangeMode is set to \e PathView.ApplyRange the view will
attempt to maintain the highlight within the range, however
the highlight can move outside of the range at the ends of the path
or due to a mouse interaction.
If highlightRangeMode is set to \e PathView.StrictlyEnforceRange the highlight will never
move outside of the range. This means that the current item will change
if a keyboard or mouse action would cause the highlight to move
outside of the range.
Note that this is the correct way to influence where the
current item ends up when the view moves. For example, if you want the
currently selected item to be in the middle of the path, then set the
highlight range to be 0.5,0.5 and highlightRangeMode to PathView.StrictlyEnforceRange.
Then, when the path scrolls,
the currently selected item will be the item at that position. This also applies to
when the currently selected item changes - it will scroll to within the preferred
highlight range. Furthermore, the behaviour of the current item index will occur
whether or not a highlight exists.
The default value is \e PathView.StrictlyEnforceRange.
Note that a valid range requires preferredHighlightEnd to be greater
than or equal to preferredHighlightBegin.
*/
qreal QDeclarativePathView::preferredHighlightBegin() const
{
Q_D(const QDeclarativePathView);
return d->highlightRangeStart;
}
void QDeclarativePathView::setPreferredHighlightBegin(qreal start)
{
Q_D(QDeclarativePathView);
if (d->highlightRangeStart == start || start < 0 || start > 1.0)
return;
d->highlightRangeStart = start;
d->haveHighlightRange = d->highlightRangeMode != NoHighlightRange && d->highlightRangeStart <= d->highlightRangeEnd;
emit preferredHighlightBeginChanged();
}
qreal QDeclarativePathView::preferredHighlightEnd() const
{
Q_D(const QDeclarativePathView);
return d->highlightRangeEnd;
}
void QDeclarativePathView::setPreferredHighlightEnd(qreal end)
{
Q_D(QDeclarativePathView);
if (d->highlightRangeEnd == end || end < 0 || end > 1.0)
return;
d->highlightRangeEnd = end;
d->haveHighlightRange = d->highlightRangeMode != NoHighlightRange && d->highlightRangeStart <= d->highlightRangeEnd;
emit preferredHighlightEndChanged();
}
QDeclarativePathView::HighlightRangeMode QDeclarativePathView::highlightRangeMode() const
{
Q_D(const QDeclarativePathView);
return d->highlightRangeMode;
}
void QDeclarativePathView::setHighlightRangeMode(HighlightRangeMode mode)
{
Q_D(QDeclarativePathView);
if (d->highlightRangeMode == mode)
return;
d->highlightRangeMode = mode;
d->haveHighlightRange = d->highlightRangeMode != NoHighlightRange && d->highlightRangeStart <= d->highlightRangeEnd;
emit highlightRangeModeChanged();
}
/*!
\qmlproperty int PathView::highlightMoveDuration
This property holds the move animation duration of the highlight delegate.
If the highlightRangeMode is StrictlyEnforceRange then this property
determines the speed that the items move along the path.
The default value for the duration is 300ms.
*/
int QDeclarativePathView::highlightMoveDuration() const
{
Q_D(const QDeclarativePathView);
return d->highlightMoveDuration;
}
void QDeclarativePathView::setHighlightMoveDuration(int duration)
{
Q_D(QDeclarativePathView);
if (d->highlightMoveDuration == duration)
return;
d->highlightMoveDuration = duration;
emit highlightMoveDurationChanged();
}
/*!
\qmlproperty real PathView::dragMargin
This property holds the maximum distance from the path that initiate mouse dragging.
By default the path can only be dragged by clicking on an item. If
dragMargin is greater than zero, a drag can be initiated by clicking
within dragMargin pixels of the path.
*/
qreal QDeclarativePathView::dragMargin() const
{
Q_D(const QDeclarativePathView);
return d->dragMargin;
}
void QDeclarativePathView::setDragMargin(qreal dragMargin)
{
Q_D(QDeclarativePathView);
if (d->dragMargin == dragMargin)
return;
d->dragMargin = dragMargin;
emit dragMarginChanged();
}
/*!
\qmlproperty real PathView::flickDeceleration
This property holds the rate at which a flick will decelerate.
The default is 100.
*/
qreal QDeclarativePathView::flickDeceleration() const
{
Q_D(const QDeclarativePathView);
return d->deceleration;
}
void QDeclarativePathView::setFlickDeceleration(qreal dec)
{
Q_D(QDeclarativePathView);
if (d->deceleration == dec)
return;
d->deceleration = dec;
emit flickDecelerationChanged();
}
/*!
\qmlproperty bool PathView::interactive
A user cannot drag or flick a PathView that is not interactive.
This property is useful for temporarily disabling flicking. This allows
special interaction with PathView's children.
*/
bool QDeclarativePathView::isInteractive() const
{
Q_D(const QDeclarativePathView);
return d->interactive;
}
void QDeclarativePathView::setInteractive(bool interactive)
{
Q_D(QDeclarativePathView);
if (interactive != d->interactive) {
d->interactive = interactive;
if (!interactive)
d->tl.clear();
emit interactiveChanged();
}
}
/*!
\qmlproperty bool PathView::moving
This property holds whether the view is currently moving
due to the user either dragging or flicking the view.
*/
bool QDeclarativePathView::isMoving() const
{
Q_D(const QDeclarativePathView);
return d->moving;
}
/*!
\qmlproperty bool PathView::flicking
This property holds whether the view is currently moving
due to the user flicking the view.
*/
bool QDeclarativePathView::isFlicking() const
{
Q_D(const QDeclarativePathView);
return d->flicking;
}
/*!
\qmlsignal PathView::onMovementStarted()
This handler is called when the view begins moving due to user
interaction.
*/
/*!
\qmlsignal PathView::onMovementEnded()
This handler is called when the view stops moving due to user
interaction. If a flick was generated, this handler will
be triggered once the flick stops. If a flick was not
generated, the handler will be triggered when the
user stops dragging - i.e. a mouse or touch release.
*/
/*!
\qmlsignal PathView::onFlickStarted()
This handler is called when the view is flicked. A flick
starts from the point that the mouse or touch is released,
while still in motion.
*/
/*!
\qmlsignal PathView::onFlickEnded()
This handler is called when the view stops moving due to a flick.
*/
/*!
\qmlproperty Component PathView::delegate
The delegate provides a template defining each item instantiated by the view.
The index is exposed as an accessible \c index property. Properties of the
model are also available depending upon the type of \l {qmlmodels}{Data Model}.
The number of elements in the delegate has a direct effect on the
flicking performance of the view when pathItemCount is specified. If at all possible, place functionality
that is not needed for the normal display of the delegate in a \l Loader which
can load additional elements when needed.
Note that the PathView will layout the items based on the size of the root
item in the delegate.
Here is an example delegate:
\snippet doc/src/snippets/declarative/pathview/pathview.qml 1
*/
QDeclarativeComponent *QDeclarativePathView::delegate() const
{
Q_D(const QDeclarativePathView);
if (d->model) {
if (QDeclarativeVisualDataModel *dataModel = qobject_cast<QDeclarativeVisualDataModel*>(d->model))
return dataModel->delegate();
}
return 0;
}
void QDeclarativePathView::setDelegate(QDeclarativeComponent *delegate)
{
Q_D(QDeclarativePathView);
if (delegate == this->delegate())
return;
if (!d->ownModel) {
d->model = new QDeclarativeVisualDataModel(qmlContext(this));
d->ownModel = true;
}
if (QDeclarativeVisualDataModel *dataModel = qobject_cast<QDeclarativeVisualDataModel*>(d->model)) {
dataModel->setDelegate(delegate);
d->regenerate();
emit delegateChanged();
}
}
/*!
\qmlproperty int PathView::pathItemCount
This property holds the number of items visible on the path at any one time.
*/
int QDeclarativePathView::pathItemCount() const
{
Q_D(const QDeclarativePathView);
return d->pathItems;
}
void QDeclarativePathView::setPathItemCount(int i)
{
Q_D(QDeclarativePathView);
if (i == d->pathItems)
return;
if (i < 1)
i = 1;
d->pathItems = i;
d->updateMappedRange();
if (d->isValid() && isComponentComplete()) {
d->regenerate();
}
emit pathItemCountChanged();
}
QPointF QDeclarativePathViewPrivate::pointNear(const QPointF &point, qreal *nearPercent) const
{
//XXX maybe do recursively at increasing resolution.
qreal mindist = 1e10; // big number
QPointF nearPoint = path->pointAt(0);
qreal nearPc = 0;
for (qreal i=1; i < 1000; i++) {
QPointF pt = path->pointAt(i/1000.0);
QPointF diff = pt - point;
qreal dist = diff.x()*diff.x() + diff.y()*diff.y();
if (dist < mindist) {
nearPoint = pt;
nearPc = i;
mindist = dist;
}
}
if (nearPercent)
*nearPercent = nearPc / 1000.0;
return nearPoint;
}
void QDeclarativePathView::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
Q_D(QDeclarativePathView);
if (!d->interactive || !d->items.count())
return;
QPointF scenePoint = mapToScene(event->pos());
int idx = 0;
for (; idx < d->items.count(); ++idx) {
QRectF rect = d->items.at(idx)->boundingRect();
rect = d->items.at(idx)->mapToScene(rect).boundingRect();
if (rect.contains(scenePoint))
break;
}
if (idx == d->items.count() && d->dragMargin == 0.) // didn't click on an item
return;
d->startPoint = d->pointNear(event->pos(), &d->startPc);
if (idx == d->items.count()) {
qreal distance = qAbs(event->pos().x() - d->startPoint.x()) + qAbs(event->pos().y() - d->startPoint.y());
if (distance > d->dragMargin)
return;
}
if (d->tl.isActive() && d->flicking)
d->stealMouse = true; // If we've been flicked then steal the click.
else
d->stealMouse = false;
d->lastElapsed = 0;
d->lastDist = 0;
QDeclarativeItemPrivate::start(d->lastPosTime);
d->tl.clear();
}
void QDeclarativePathView::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
Q_D(QDeclarativePathView);
if (!d->interactive || !d->lastPosTime.isValid())
return;
if (!d->stealMouse) {
QPointF delta = event->pos() - d->startPoint;
if (qAbs(delta.x()) > QApplication::startDragDistance() || qAbs(delta.y()) > QApplication::startDragDistance())
d->stealMouse = true;
}
if (d->stealMouse) {
d->moveReason = QDeclarativePathViewPrivate::Mouse;
qreal newPc;
d->pointNear(event->pos(), &newPc);
qreal diff = (newPc - d->startPc)*d->model->count()*d->mappedRange;
if (diff) {
setOffset(d->offset + diff);
if (diff > d->model->count()/2)
diff -= d->model->count();
else if (diff < -d->model->count()/2)
diff += d->model->count();
d->lastElapsed = QDeclarativeItemPrivate::restart(d->lastPosTime);
d->lastDist = diff;
d->startPc = newPc;
}
if (!d->moving) {
d->moving = true;
emit movingChanged();
emit movementStarted();
}
}
}
void QDeclarativePathView::mouseReleaseEvent(QGraphicsSceneMouseEvent *)
{
Q_D(QDeclarativePathView);
d->stealMouse = false;
setKeepMouseGrab(false);
if (!d->interactive || !d->lastPosTime.isValid())
return;
qreal elapsed = qreal(d->lastElapsed + QDeclarativeItemPrivate::elapsed(d->lastPosTime)) / 1000.;
qreal velocity = elapsed > 0. ? d->lastDist / elapsed : 0;
if (d->model && d->model->count() && qAbs(velocity) > 1.) {
qreal count = d->pathItems == -1 ? d->model->count() : d->pathItems;
if (qAbs(velocity) > count * 2) // limit velocity
velocity = (velocity > 0 ? count : -count) * 2;
// Calculate the distance to be travelled
qreal v2 = velocity*velocity;
qreal accel = d->deceleration/10;
// + 0.25 to encourage moving at least one item in the flick direction
qreal dist = qMin(qreal(d->model->count()-1), qreal(v2 / (accel * 2.0) + 0.25));
if (d->haveHighlightRange && d->highlightRangeMode == QDeclarativePathView::StrictlyEnforceRange) {
// round to nearest item.
if (velocity > 0.)
dist = qRound(dist + d->offset) - d->offset;
else
dist = qRound(dist - d->offset) + d->offset;
// Calculate accel required to stop on item boundary
if (dist <= 0.) {
dist = 0.;
accel = 0.;
} else {
accel = v2 / (2.0f * qAbs(dist));
}
}
d->moveOffset.setValue(d->offset);
d->tl.accel(d->moveOffset, velocity, accel, dist);
d->tl.callback(QDeclarativeTimeLineCallback(&d->moveOffset, d->fixOffsetCallback, d));
if (!d->flicking) {
d->flicking = true;
emit flickingChanged();
emit flickStarted();
}
} else {
d->fixOffset();
}
d->lastPosTime.invalidate();
ungrabMouse();
if (!d->tl.isActive())
movementEnding();
}
bool QDeclarativePathView::sendMouseEvent(QGraphicsSceneMouseEvent *event)
{
Q_D(QDeclarativePathView);
QGraphicsSceneMouseEvent mouseEvent(event->type());
QRectF myRect = mapToScene(QRectF(0, 0, width(), height())).boundingRect();
QGraphicsScene *s = scene();
QDeclarativeItem *grabber = s ? qobject_cast<QDeclarativeItem*>(s->mouseGrabberItem()) : 0;
if ((d->stealMouse || myRect.contains(event->scenePos().toPoint())) && (!grabber || !grabber->keepMouseGrab())) {
mouseEvent.setAccepted(false);
for (int i = 0x1; i <= 0x10; i <<= 1) {
if (event->buttons() & i) {
Qt::MouseButton button = Qt::MouseButton(i);
mouseEvent.setButtonDownPos(button, mapFromScene(event->buttonDownPos(button)));
}
}
mouseEvent.setScenePos(event->scenePos());
mouseEvent.setLastScenePos(event->lastScenePos());
mouseEvent.setPos(mapFromScene(event->scenePos()));
mouseEvent.setLastPos(mapFromScene(event->lastScenePos()));
switch(mouseEvent.type()) {
case QEvent::GraphicsSceneMouseMove:
mouseMoveEvent(&mouseEvent);
break;
case QEvent::GraphicsSceneMousePress:
mousePressEvent(&mouseEvent);
break;
case QEvent::GraphicsSceneMouseRelease:
mouseReleaseEvent(&mouseEvent);
break;
default:
break;
}
grabber = qobject_cast<QDeclarativeItem*>(s->mouseGrabberItem());
if (grabber && d->stealMouse && !grabber->keepMouseGrab() && grabber != this)
grabMouse();
return d->stealMouse;
} else if (d->lastPosTime.isValid()) {
d->lastPosTime.invalidate();
}
return false;
}
bool QDeclarativePathView::sceneEventFilter(QGraphicsItem *i, QEvent *e)
{
Q_D(QDeclarativePathView);
if (!isVisible() || !d->interactive)
return QDeclarativeItem::sceneEventFilter(i, e);
switch (e->type()) {
case QEvent::GraphicsSceneMousePress:
case QEvent::GraphicsSceneMouseMove:
case QEvent::GraphicsSceneMouseRelease:
{
bool ret = sendMouseEvent(static_cast<QGraphicsSceneMouseEvent *>(e));
if (e->type() == QEvent::GraphicsSceneMouseRelease)
return ret;
break;
}
default:
break;
}
return QDeclarativeItem::sceneEventFilter(i, e);
}
bool QDeclarativePathView::event(QEvent *event)
{
if (event->type() == QEvent::User) {
refill();
return true;
}
return QDeclarativeItem::event(event);
}
void QDeclarativePathView::componentComplete()
{
Q_D(QDeclarativePathView);
QDeclarativeItem::componentComplete();
d->createHighlight();
// It is possible that a refill has already happended to to Path
// bindings being handled in the componentComplete(). If so
// don't do it again.
if (d->items.count() == 0)
d->regenerate();
d->updateHighlight();
}
void QDeclarativePathView::refill()
{
Q_D(QDeclarativePathView);
if (!d->isValid() || !isComponentComplete())
return;
d->layoutScheduled = false;
bool currentVisible = false;
// first move existing items and remove items off path
int idx = d->firstIndex;
QList<QDeclarativeItem*>::iterator it = d->items.begin();
while (it != d->items.end()) {
qreal pos = d->positionOfIndex(idx);
QDeclarativeItem *item = *it;
if (pos >= 0.0) {
d->updateItem(item, pos);
if (idx == d->currentIndex) {
currentVisible = true;
d->currentItemOffset = pos;
}
++it;
} else {
// qDebug() << "release";
d->updateItem(item, 1.0);
d->releaseItem(item);
if (it == d->items.begin()) {
if (++d->firstIndex >= d->model->count())
d->firstIndex = 0;
}
it = d->items.erase(it);
}
++idx;
if (idx >= d->model->count())
idx = 0;
}
// add items to beginning and end
int count = d->pathItems == -1 ? d->model->count() : qMin(d->pathItems, d->model->count());
if (d->items.count() < count) {
int idx = qRound(d->model->count() - d->offset) % d->model->count();
qreal startPos = 0.0;
if (d->haveHighlightRange && d->highlightRangeMode != QDeclarativePathView::NoHighlightRange)
startPos = d->highlightRangeStart;
if (d->firstIndex >= 0) {
startPos = d->positionOfIndex(d->firstIndex);
idx = (d->firstIndex + d->items.count()) % d->model->count();
}
qreal pos = d->positionOfIndex(idx);
while ((pos > startPos || !d->items.count()) && d->items.count() < count) {
// qDebug() << "append" << idx;
QDeclarativeItem *item = d->getItem(idx);
if (d->model->completePending())
item->setZValue(idx+1);
if (d->currentIndex == idx) {
item->setFocus(true);
if (QDeclarativePathViewAttached *att = d->attached(item))
att->setIsCurrentItem(true);
currentVisible = true;
d->currentItemOffset = pos;
d->currentItem = item;
}
if (d->items.count() == 0)
d->firstIndex = idx;
d->items.append(item);
d->updateItem(item, pos);
if (d->model->completePending())
d->model->completeItem();
++idx;
if (idx >= d->model->count())
idx = 0;
pos = d->positionOfIndex(idx);
}
idx = d->firstIndex - 1;
if (idx < 0)
idx = d->model->count() - 1;
pos = d->positionOfIndex(idx);
while (pos >= 0.0 && pos < startPos) {
// qDebug() << "prepend" << idx;
QDeclarativeItem *item = d->getItem(idx);
if (d->model->completePending())
item->setZValue(idx+1);
if (d->currentIndex == idx) {
item->setFocus(true);
if (QDeclarativePathViewAttached *att = d->attached(item))
att->setIsCurrentItem(true);
currentVisible = true;
d->currentItemOffset = pos;
d->currentItem = item;
}
d->items.prepend(item);
d->updateItem(item, pos);
if (d->model->completePending())
d->model->completeItem();
d->firstIndex = idx;
idx = d->firstIndex - 1;
if (idx < 0)
idx = d->model->count() - 1;
pos = d->positionOfIndex(idx);
}
}
if (!currentVisible)
d->currentItemOffset = 1.0;
if (d->highlightItem && d->haveHighlightRange && d->highlightRangeMode == QDeclarativePathView::StrictlyEnforceRange) {
d->updateItem(d->highlightItem, d->highlightRangeStart);
if (QDeclarativePathViewAttached *att = d->attached(d->highlightItem))
att->setOnPath(true);
} else if (d->highlightItem && d->moveReason != QDeclarativePathViewPrivate::SetIndex) {
d->updateItem(d->highlightItem, d->currentItemOffset);
if (QDeclarativePathViewAttached *att = d->attached(d->highlightItem))
att->setOnPath(currentVisible);
}
}
void QDeclarativePathView::itemsInserted(int modelIndex, int count)
{
//XXX support animated insertion
Q_D(QDeclarativePathView);
if (!d->isValid() || !isComponentComplete())
return;
QList<QDeclarativeItem *> removedItems = d->items;
d->items.clear();
if (modelIndex <= d->currentIndex) {
d->currentIndex += count;
emit currentIndexChanged();
}
d->regenerate();
while (removedItems.count())
d->releaseItem(removedItems.takeLast());
d->updateCurrent();
emit countChanged();
}
void QDeclarativePathView::itemsRemoved(int modelIndex, int count)
{
//XXX support animated removal
Q_D(QDeclarativePathView);
if (!d->isValid() || !isComponentComplete())
return;
// fix current
bool currentChanged = false;
if (d->currentIndex >= modelIndex + count) {
d->currentIndex -= count;
currentChanged = true;
} else if (d->currentIndex >= modelIndex && d->currentIndex < modelIndex + count) {
// current item has been removed.
d->currentIndex = qMin(modelIndex, d->model->count()-1);
if (d->currentItem) {
if (QDeclarativePathViewAttached *att = d->attached(d->currentItem))
att->setIsCurrentItem(true);
}
currentChanged = true;
}
QList<QDeclarativeItem *> removedItems = d->items;
d->items.clear();
if (d->offset >= d->model->count())
d->offset = d->model->count() - 1;
d->regenerate();
while (removedItems.count())
d->releaseItem(removedItems.takeLast());
d->updateCurrent();
if (currentChanged)
emit currentIndexChanged();
emit countChanged();
}
void QDeclarativePathView::itemsMoved(int /*from*/, int /*to*/, int /*count*/)
{
Q_D(QDeclarativePathView);
if (!d->isValid() || !isComponentComplete())
return;
QList<QDeclarativeItem *> removedItems = d->items;
d->items.clear();
d->regenerate();
while (removedItems.count())
d->releaseItem(removedItems.takeLast());
// Fix current index
if (d->currentIndex >= 0 && d->currentItem) {
int oldCurrent = d->currentIndex;
d->currentIndex = d->model->indexOf(d->currentItem, this);
if (oldCurrent != d->currentIndex)
emit currentIndexChanged();
}
d->updateCurrent();
}
void QDeclarativePathView::modelReset()
{
Q_D(QDeclarativePathView);
d->regenerate();
emit countChanged();
}
void QDeclarativePathView::createdItem(int index, QDeclarativeItem *item)
{
Q_D(QDeclarativePathView);
if (d->requestedIndex != index) {
if (!d->attType) {
// pre-create one metatype to share with all attached objects
d->attType = new QDeclarativeOpenMetaObjectType(&QDeclarativePathViewAttached::staticMetaObject, qmlEngine(this));
foreach(const QString &attr, d->path->attributes())
d->attType->createProperty(attr.toUtf8());
}
qPathViewAttachedType = d->attType;
QDeclarativePathViewAttached *att = static_cast<QDeclarativePathViewAttached *>(qmlAttachedPropertiesObject<QDeclarativePathView>(item));
qPathViewAttachedType = 0;
if (att) {
att->m_view = this;
att->setOnPath(false);
}
item->setParentItem(this);
d->updateItem(item, index < d->firstIndex ? 0.0 : 1.0);
}
}
void QDeclarativePathView::destroyingItem(QDeclarativeItem *item)
{
Q_UNUSED(item);
}
void QDeclarativePathView::ticked()
{
Q_D(QDeclarativePathView);
d->updateCurrent();
}
void QDeclarativePathView::movementEnding()
{
Q_D(QDeclarativePathView);
if (d->flicking) {
d->flicking = false;
emit flickingChanged();
emit flickEnded();
}
if (d->moving && !d->stealMouse) {
d->moving = false;
emit movingChanged();
emit movementEnded();
}
}
// find the item closest to the snap position
int QDeclarativePathViewPrivate::calcCurrentIndex()
{
int current = -1;
if (model && items.count()) {
offset = qmlMod(offset, model->count());
if (offset < 0)
offset += model->count();
current = qRound(qAbs(qmlMod(model->count() - offset, model->count())));
current = current % model->count();
}
return current;
}
void QDeclarativePathViewPrivate::updateCurrent()
{
Q_Q(QDeclarativePathView);
if (moveReason != Mouse)
return;
if (!haveHighlightRange || highlightRangeMode != QDeclarativePathView::StrictlyEnforceRange)
return;
int idx = calcCurrentIndex();
if (model && idx != currentIndex) {
int itemIndex = (currentIndex - firstIndex + model->count()) % model->count();
if (itemIndex < items.count()) {
if (QDeclarativeItem *item = items.at(itemIndex)) {
if (QDeclarativePathViewAttached *att = attached(item))
att->setIsCurrentItem(false);
}
}
currentIndex = idx;
currentItem = 0;
itemIndex = (idx - firstIndex + model->count()) % model->count();
if (itemIndex < items.count()) {
currentItem = items.at(itemIndex);
currentItem->setFocus(true);
if (QDeclarativePathViewAttached *att = attached(currentItem))
att->setIsCurrentItem(true);
}
emit q->currentIndexChanged();
}
}
void QDeclarativePathViewPrivate::fixOffsetCallback(void *d)
{
((QDeclarativePathViewPrivate *)d)->fixOffset();
}
void QDeclarativePathViewPrivate::fixOffset()
{
Q_Q(QDeclarativePathView);
if (model && items.count()) {
if (haveHighlightRange && highlightRangeMode == QDeclarativePathView::StrictlyEnforceRange) {
int curr = calcCurrentIndex();
if (curr != currentIndex)
q->setCurrentIndex(curr);
else
snapToCurrent();
}
}
}
void QDeclarativePathViewPrivate::snapToCurrent()
{
if (!model || model->count() <= 0)
return;
qreal targetOffset = model->count() - currentIndex;
moveReason = Other;
tl.reset(moveOffset);
moveOffset.setValue(offset);
const int duration = highlightMoveDuration;
if (targetOffset - offset > model->count()/2) {
qreal distance = model->count() - targetOffset + offset;
tl.move(moveOffset, 0.0, QEasingCurve(QEasingCurve::InQuad), int(duration * offset / distance));
tl.set(moveOffset, model->count());
tl.move(moveOffset, targetOffset, QEasingCurve(QEasingCurve::OutQuad), int(duration * (model->count()-targetOffset) / distance));
} else if (targetOffset - offset <= -model->count()/2) {
qreal distance = model->count() - offset + targetOffset;
tl.move(moveOffset, model->count(), QEasingCurve(QEasingCurve::InQuad), int(duration * (model->count()-offset) / distance));
tl.set(moveOffset, 0.0);
tl.move(moveOffset, targetOffset, QEasingCurve(QEasingCurve::OutQuad), int(duration * targetOffset / distance));
} else {
tl.move(moveOffset, targetOffset, QEasingCurve(QEasingCurve::InOutQuad), duration);
}
}
QDeclarativePathViewAttached *QDeclarativePathView::qmlAttachedProperties(QObject *obj)
{
return new QDeclarativePathViewAttached(obj);
}
QT_END_NAMESPACE
|