summaryrefslogtreecommitdiffstats
path: root/src/declarative/graphicsitems/qmlgraphicsvisualitemmodel.cpp
blob: bf3df036ff49c3e59ac8794651aa76d35acc5c66 (plain)
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
/****************************************************************************
**
** 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 "qmlgraphicsvisualitemmodel_p.h"

#include "qmlgraphicsitem.h"

#include <qmlcontext.h>
#include <qmlengine.h>
#include <qmlexpression.h>
#include <qmlpackage_p.h>
#include <qmlopenmetaobject_p.h>
#include <qmllistaccessor_p.h>
#include <qmlinfo.h>
#include <qmldeclarativedata_p.h>
#include <qmlpropertycache_p.h>
#include <qmlguard_p.h>

#include <qlistmodelinterface_p.h>
#include <qhash.h>
#include <qlist.h>
#include <qmetaobjectbuilder_p.h>
#include <QtCore/qdebug.h>

#include <private/qobject_p.h>

QT_BEGIN_NAMESPACE

QHash<QObject*, QmlGraphicsVisualItemModelAttached*> QmlGraphicsVisualItemModelAttached::attachedProperties;


class QmlGraphicsVisualItemModelPrivate : public QObjectPrivate
{
    Q_DECLARE_PUBLIC(QmlGraphicsVisualItemModel)
public:
    QmlGraphicsVisualItemModelPrivate() : QObjectPrivate() {}

    static void children_append(QmlListProperty<QmlGraphicsItem> *prop, QmlGraphicsItem *item) {
        item->QObject::setParent(prop->object);
        static_cast<QmlGraphicsVisualItemModelPrivate *>(prop->data)->children.append(item);
        static_cast<QmlGraphicsVisualItemModelPrivate *>(prop->data)->itemAppended();
        static_cast<QmlGraphicsVisualItemModelPrivate *>(prop->data)->emitChildrenChanged();
    }

    void itemAppended() {
        Q_Q(QmlGraphicsVisualItemModel);
        QmlGraphicsVisualItemModelAttached *attached = QmlGraphicsVisualItemModelAttached::properties(children.last());
        attached->setIndex(children.count()-1);
        emit q->itemsInserted(children.count()-1, 1);
        emit q->countChanged();
    }

    void emitChildrenChanged() {
        Q_Q(QmlGraphicsVisualItemModel);
        emit q->childrenChanged();
    }

    QList<QmlGraphicsItem *> children;
};


/*!
    \qmlclass VisualItemModel QmlGraphicsVisualItemModel
    \brief The VisualItemModel allows items to be provided to a view.

    The children of the VisualItemModel are provided in a model which
    can be used in a view.  Note that no delegate should be
    provided to a view since the VisualItemModel contains the
    visual delegate (items).

    An item can determine its index within the
    model via the VisualItemModel.index attached property.

    The example below places three colored rectangles in a ListView.
    \code
    Item {
        VisualItemModel {
            id: itemModel
            Rectangle { height: 30; width: 80; color: "red" }
            Rectangle { height: 30; width: 80; color: "green" }
            Rectangle { height: 30; width: 80; color: "blue" }
        }

        ListView {
            anchors.fill: parent
            model: itemModel
        }
    }
    \endcode
*/
QmlGraphicsVisualItemModel::QmlGraphicsVisualItemModel()
    : QmlGraphicsVisualModel(*(new QmlGraphicsVisualItemModelPrivate))
{
}

QmlListProperty<QmlGraphicsItem> QmlGraphicsVisualItemModel::children()
{
    Q_D(QmlGraphicsVisualItemModel);
    return QmlListProperty<QmlGraphicsItem>(this, d, QmlGraphicsVisualItemModelPrivate::children_append);
}

/*!
    \qmlproperty int VisualItemModel::count

    The number of items in the model.  This property is readonly.
*/
int QmlGraphicsVisualItemModel::count() const
{
    Q_D(const QmlGraphicsVisualItemModel);
    return d->children.count();
}

bool QmlGraphicsVisualItemModel::isValid() const
{
    return true;
}

QmlGraphicsItem *QmlGraphicsVisualItemModel::item(int index, bool)
{
    Q_D(QmlGraphicsVisualItemModel);
    return d->children.at(index);
}

QmlGraphicsVisualModel::ReleaseFlags QmlGraphicsVisualItemModel::release(QmlGraphicsItem *)
{
    // Nothing to do
    return 0;
}

void QmlGraphicsVisualItemModel::completeItem()
{
    // Nothing to do
}

QString QmlGraphicsVisualItemModel::stringValue(int index, const QString &name)
{
    Q_D(QmlGraphicsVisualItemModel);
    if (index < 0 || index >= d->children.count())
        return QString();
    return QmlEngine::contextForObject(d->children.at(index))->contextProperty(name).toString();
}

QVariant QmlGraphicsVisualItemModel::evaluate(int index, const QString &expression, QObject *objectContext)
{
    Q_D(QmlGraphicsVisualItemModel);
    if (index < 0 || index >= d->children.count())
        return QVariant();
    QmlContext *ccontext = qmlContext(this);
    QmlContext *ctxt = new QmlContext(ccontext);
    ctxt->addDefaultObject(d->children.at(index));
    QmlExpression e(ctxt, expression, objectContext);
    QVariant value = e.value();
    delete ctxt;
    return value;
}

int QmlGraphicsVisualItemModel::indexOf(QmlGraphicsItem *item, QObject *) const
{
    Q_D(const QmlGraphicsVisualItemModel);
    return d->children.indexOf(item);
}

QmlGraphicsVisualItemModelAttached *QmlGraphicsVisualItemModel::qmlAttachedProperties(QObject *obj)
{
    return QmlGraphicsVisualItemModelAttached::properties(obj);
}

//============================================================================

class VDMDelegateDataType : public QmlOpenMetaObjectType
{
public:
    VDMDelegateDataType(const QMetaObject *base, QmlEngine *engine) : QmlOpenMetaObjectType(base, engine) {}

    void propertyCreated(int, QMetaPropertyBuilder &prop) {
        prop.setWritable(false);
    }
};

class QmlGraphicsVisualDataModelParts;
class QmlGraphicsVisualDataModelData;
class QmlGraphicsVisualDataModelPrivate : public QObjectPrivate
{
public:
    QmlGraphicsVisualDataModelPrivate(QmlContext *);

    static QmlGraphicsVisualDataModelPrivate *get(QmlGraphicsVisualDataModel *m) {
        return static_cast<QmlGraphicsVisualDataModelPrivate *>(QObjectPrivate::get(m));
    }

    QmlGuard<QListModelInterface> m_listModelInterface;
    QmlGuard<QAbstractItemModel> m_abstractItemModel;
    QmlGuard<QmlGraphicsVisualDataModel> m_visualItemModel;
    QString m_part;

    QmlComponent *m_delegate;
    QmlContext *m_context;
    QList<int> m_roles;
    QHash<QByteArray,int> m_roleNames;
    void ensureRoles() {
        if (m_roleNames.isEmpty()) {
            if (m_listModelInterface) {
                m_roles = m_listModelInterface->roles();
                for (int ii = 0; ii < m_roles.count(); ++ii)
                    m_roleNames.insert(m_listModelInterface->toString(m_roles.at(ii)).toUtf8(), m_roles.at(ii));
                if (m_roles.count() == 1)
                    m_roleNames.insert("modelData", m_roles.at(0));
            } else if (m_abstractItemModel) {
                for (QHash<int,QByteArray>::const_iterator it = m_abstractItemModel->roleNames().begin();
                        it != m_abstractItemModel->roleNames().end(); ++it) {
                    m_roles.append(it.key());
                    m_roleNames.insert(*it, it.key());
                }
                if (m_roles.count() == 1)
                    m_roleNames.insert("modelData", m_roles.at(0));
            } else if (m_listAccessor) {
                m_roleNames.insert("modelData", 0);
                if (m_listAccessor->type() == QmlListAccessor::Instance) {
                    if (QObject *object = m_listAccessor->at(0).value<QObject*>()) {
                        int count = object->metaObject()->propertyCount();
                        for (int ii = 1; ii < count; ++ii) {
                            const QMetaProperty &prop = object->metaObject()->property(ii);
                            m_roleNames.insert(prop.name(), 0);
                        }
                    }
                }
            }
        }
    }

    void createMetaData() {
        if (!m_metaDataCreated) {
            ensureRoles();
            QHash<QByteArray, int>::const_iterator it = m_roleNames.begin();
            while (it != m_roleNames.end()) {
                m_delegateDataType->createProperty(it.key());
                ++it;
            }
            m_metaDataCreated = true;
        }
    }

    struct ObjectRef {
        ObjectRef(QObject *object=0) : obj(object), ref(1) {}
        QObject *obj;
        int ref;
    };
    class Cache : public QHash<int, ObjectRef> {
    public:
        QObject *getItem(int index) {
            QObject *item = 0;
            QHash<int,ObjectRef>::iterator it = find(index);
            if (it != end()) {
                (*it).ref++;
                item = (*it).obj;
            }
            return item;
        }
        QObject *item(int index) {
            QObject *item = 0;
            QHash<int, ObjectRef>::const_iterator it = find(index);
            if (it != end())
                item = (*it).obj;
            return item;
        }
        void insertItem(int index, QObject *obj) {
            insert(index, ObjectRef(obj));
        }
        bool releaseItem(QObject *obj) {
            QHash<int, ObjectRef>::iterator it = begin();
            for (; it != end(); ++it) {
                ObjectRef &objRef = *it;
                if (objRef.obj == obj) {
                    if (--objRef.ref == 0) {
                        erase(it);
                        return true;
                    }
                    break;
                }
            }
            return false;
        }
    };

    int modelCount() const {
        if (m_visualItemModel)
            return m_visualItemModel->count();
        if (m_listModelInterface)
            return m_listModelInterface->count();
        if (m_abstractItemModel)
            return m_abstractItemModel->rowCount(m_root);
        if (m_listAccessor)
            return m_listAccessor->count();
        return 0;
    }

    Cache m_cache;
    QHash<QObject *, QmlPackage*> m_packaged;

    QmlGraphicsVisualDataModelParts *m_parts;
    friend class QmlGraphicsVisualItemParts;

    VDMDelegateDataType *m_delegateDataType;
    friend class QmlGraphicsVisualDataModelData;
    bool m_metaDataCreated;
    bool m_metaDataCacheable;

    QmlGraphicsVisualDataModelData *data(QObject *item);

    QVariant m_modelVariant;
    QmlListAccessor *m_listAccessor;

    QModelIndex m_root;
};

class QmlGraphicsVisualDataModelDataMetaObject : public QmlOpenMetaObject
{
public:
    QmlGraphicsVisualDataModelDataMetaObject(QObject *parent, QmlOpenMetaObjectType *type)
    : QmlOpenMetaObject(parent, type) {}

    virtual QVariant initialValue(int);
    virtual int createProperty(const char *, const char *);

private:
    friend class QmlGraphicsVisualDataModelData;
    QHash<int,int> roleToProp;
};

class QmlGraphicsVisualDataModelData : public QObject
{
Q_OBJECT
public:
    QmlGraphicsVisualDataModelData(int index, QmlGraphicsVisualDataModel *model);
    ~QmlGraphicsVisualDataModelData();

    Q_PROPERTY(int index READ index NOTIFY indexChanged)
    int index() const;
    void setIndex(int index);

    int propForRole(int) const;
    void setValue(int, const QVariant &);

Q_SIGNALS:
    void indexChanged();

private:
    friend class QmlGraphicsVisualDataModelDataMetaObject;
    int m_index;
    QmlGuard<QmlGraphicsVisualDataModel> m_model;
    QmlGraphicsVisualDataModelDataMetaObject *m_meta;
};

int QmlGraphicsVisualDataModelData::propForRole(int id) const
{
    QHash<int,int>::const_iterator it = m_meta->roleToProp.find(id);
    if (it != m_meta->roleToProp.end())
        return m_meta->roleToProp[id];
    return -1;
}

void QmlGraphicsVisualDataModelData::setValue(int id, const QVariant &val)
{
    m_meta->setValue(id, val);
}

int QmlGraphicsVisualDataModelDataMetaObject::createProperty(const char *name, const char *type)
{
    QmlGraphicsVisualDataModelData *data =
        static_cast<QmlGraphicsVisualDataModelData *>(object());

    if (!data->m_model)
        return -1;

    QmlGraphicsVisualDataModelPrivate *model = QmlGraphicsVisualDataModelPrivate::get(data->m_model);

    if ((!model->m_listModelInterface || !model->m_abstractItemModel) && model->m_listAccessor) {
        if (model->m_listAccessor->type() == QmlListAccessor::ListProperty) {
            model->ensureRoles();
            QObject *object = model->m_listAccessor->at(data->m_index).value<QObject*>();
            if (object && (object->property(name).isValid() || qstrcmp(name,"modelData")==0))
                return QmlOpenMetaObject::createProperty(name, type);
        }
    }
    return -1;
}

QVariant QmlGraphicsVisualDataModelDataMetaObject::initialValue(int propId)
{
    QmlGraphicsVisualDataModelData *data =
        static_cast<QmlGraphicsVisualDataModelData *>(object());

    Q_ASSERT(data->m_model);
    QmlGraphicsVisualDataModelPrivate *model = QmlGraphicsVisualDataModelPrivate::get(data->m_model);

    QByteArray propName = name(propId);
    if ((!model->m_listModelInterface || !model->m_abstractItemModel) && model->m_listAccessor) {
        if (propName == "modelData") {
            if (model->m_listAccessor->type() == QmlListAccessor::Instance) {
                QObject *object = model->m_listAccessor->at(0).value<QObject*>();
                return object->metaObject()->property(1).read(object); // the first property after objectName
            }
            return model->m_listAccessor->at(data->m_index);
        } else {
            // return any property of a single object instance.
            QObject *object = model->m_listAccessor->at(data->m_index).value<QObject*>();
            return object->property(propName);
        }
    } else if (model->m_listModelInterface) {
        model->ensureRoles();
        QHash<QByteArray,int>::const_iterator it = model->m_roleNames.find(propName);
        if (it != model->m_roleNames.end()) {
            roleToProp.insert(*it, propId);
            QVariant value = model->m_listModelInterface->data(data->m_index, *it);
            return value;
        } else if (model->m_roles.count() == 1 && propName == "modelData") {
            //for compatability with other lists, assign modelData if there is only a single role
            roleToProp.insert(model->m_roles.first(), propId);
            QVariant value = model->m_listModelInterface->data(data->m_index, model->m_roles.first());
            return value;
        }
    } else if (model->m_abstractItemModel) {
        model->ensureRoles();
        QHash<QByteArray,int>::const_iterator it = model->m_roleNames.find(propName);
        if (it != model->m_roleNames.end()) {
            roleToProp.insert(*it, propId);
            QModelIndex index = model->m_abstractItemModel->index(data->m_index, 0, model->m_root);
            return model->m_abstractItemModel->data(index, *it);
        }
    }
    Q_ASSERT(!"Can never be reached");
    return QVariant();
}

QmlGraphicsVisualDataModelData::QmlGraphicsVisualDataModelData(int index,
                                               QmlGraphicsVisualDataModel *model)
: m_index(index), m_model(model), 
m_meta(new QmlGraphicsVisualDataModelDataMetaObject(this, QmlGraphicsVisualDataModelPrivate::get(model)->m_delegateDataType))
{
    QmlGraphicsVisualDataModelPrivate *modelPriv = QmlGraphicsVisualDataModelPrivate::get(model);
    if (modelPriv->m_metaDataCacheable) {
        if (!modelPriv->m_metaDataCreated)
            modelPriv->createMetaData();
        m_meta->setCached(true);
    }
}

QmlGraphicsVisualDataModelData::~QmlGraphicsVisualDataModelData()
{
}

int QmlGraphicsVisualDataModelData::index() const
{
    return m_index;
}

// This is internal only - it should not be set from qml
void QmlGraphicsVisualDataModelData::setIndex(int index)
{
    m_index = index;
    emit indexChanged();
}

//---------------------------------------------------------------------------

class QmlGraphicsVisualDataModelPartsMetaObject : public QmlOpenMetaObject
{
public:
    QmlGraphicsVisualDataModelPartsMetaObject(QObject *parent)
    : QmlOpenMetaObject(parent) {}

    virtual void propertyCreated(int, QMetaPropertyBuilder &);
    virtual QVariant initialValue(int);
};

class QmlGraphicsVisualDataModelParts : public QObject
{
Q_OBJECT
public:
    QmlGraphicsVisualDataModelParts(QmlGraphicsVisualDataModel *parent);

private:
    friend class QmlGraphicsVisualDataModelPartsMetaObject;
    QmlGraphicsVisualDataModel *model;
};

void QmlGraphicsVisualDataModelPartsMetaObject::propertyCreated(int, QMetaPropertyBuilder &prop)
{
    prop.setWritable(false);
}

QVariant QmlGraphicsVisualDataModelPartsMetaObject::initialValue(int id)
{
    QmlGraphicsVisualDataModel *m = new QmlGraphicsVisualDataModel;
    m->setParent(object());
    m->setPart(QString::fromUtf8(name(id)));
    m->setModel(QVariant::fromValue(static_cast<QmlGraphicsVisualDataModelParts *>(object())->model));

    QVariant var = QVariant::fromValue((QObject *)m);
    return var;
}

QmlGraphicsVisualDataModelParts::QmlGraphicsVisualDataModelParts(QmlGraphicsVisualDataModel *parent)
: QObject(parent), model(parent) 
{
    new QmlGraphicsVisualDataModelPartsMetaObject(this);
}

QmlGraphicsVisualDataModelPrivate::QmlGraphicsVisualDataModelPrivate(QmlContext *ctxt)
: m_listModelInterface(0), m_abstractItemModel(0), m_visualItemModel(0), m_delegate(0)
, m_context(ctxt), m_parts(0), m_delegateDataType(0), m_metaDataCreated(false)
, m_metaDataCacheable(false), m_listAccessor(0)
{
}

QmlGraphicsVisualDataModelData *QmlGraphicsVisualDataModelPrivate::data(QObject *item)
{
    QmlGraphicsVisualDataModelData *dataItem =
        item->findChild<QmlGraphicsVisualDataModelData *>();
    Q_ASSERT(dataItem);
    return dataItem;
}

//---------------------------------------------------------------------------

/*!
    \qmlclass VisualDataModel QmlGraphicsVisualDataModel
    \brief The VisualDataModel encapsulates a model and delegate

    A VisualDataModel encapsulates a model and the delegate that will
    be instantiated for items in the model.

    It is usually not necessary to create a VisualDataModel directly,
    since the QML views will create one internally.

    The example below illustrates using a VisualDataModel with a ListView.

    \code
    VisualDataModel {
        id: visualModel
        model: myModel
        delegate: Component {
            Rectangle {
                height: 25
                width: 100
                Text { text: "Name:" + name}
            }
        }
    }
    ListView {
        width: 100
        height: 100
        anchors.fill: parent
        model: visualModel
    }
    \endcode
*/

QmlGraphicsVisualDataModel::QmlGraphicsVisualDataModel()
: QmlGraphicsVisualModel(*(new QmlGraphicsVisualDataModelPrivate(0)))
{
}

QmlGraphicsVisualDataModel::QmlGraphicsVisualDataModel(QmlContext *ctxt)
: QmlGraphicsVisualModel(*(new QmlGraphicsVisualDataModelPrivate(ctxt)))
{
}

QmlGraphicsVisualDataModel::~QmlGraphicsVisualDataModel()
{
    Q_D(QmlGraphicsVisualDataModel);
    if (d->m_listAccessor)
        delete d->m_listAccessor;
    if (d->m_delegateDataType)
        d->m_delegateDataType->release();
}

/*!
    \qmlproperty model VisualDataModel::model
    This property holds the model providing data for the VisualDataModel.

    The model provides a set of data that is used to create the items
    for a view.  For large or dynamic datasets the model is usually
    provided by a C++ model object.  The C++ model object must be a \l
    {QAbstractItemModel} subclass or a simple list.

    Models can also be created directly in QML, using a \l{ListModel} or
    \l{XmlListModel}.

    \sa {qmlmodels}{Data Models}
*/
QVariant QmlGraphicsVisualDataModel::model() const
{
    Q_D(const QmlGraphicsVisualDataModel);
    return d->m_modelVariant;
}

void QmlGraphicsVisualDataModel::setModel(const QVariant &model)
{
    Q_D(QmlGraphicsVisualDataModel);
    delete d->m_listAccessor;
    d->m_listAccessor = 0;
    d->m_modelVariant = model;
    if (d->m_listModelInterface) {
        // Assume caller has released all items.
        QObject::disconnect(d->m_listModelInterface, SIGNAL(itemsChanged(int,int,QList<int>)),
                this, SLOT(_q_itemsChanged(int,int,QList<int>)));
        QObject::disconnect(d->m_listModelInterface, SIGNAL(itemsInserted(int,int)),
                this, SLOT(_q_itemsInserted(int,int)));
        QObject::disconnect(d->m_listModelInterface, SIGNAL(itemsRemoved(int,int)),
                this, SLOT(_q_itemsRemoved(int,int)));
        QObject::disconnect(d->m_listModelInterface, SIGNAL(itemsMoved(int,int,int)),
                this, SLOT(_q_itemsMoved(int,int,int)));
        d->m_listModelInterface = 0;
    } else if (d->m_abstractItemModel) {
        QObject::disconnect(d->m_abstractItemModel, SIGNAL(rowsInserted(const QModelIndex &,int,int)),
                            this, SLOT(_q_rowsInserted(const QModelIndex &,int,int)));
        QObject::disconnect(d->m_abstractItemModel, SIGNAL(rowsRemoved(const QModelIndex &,int,int)),
                            this, SLOT(_q_rowsRemoved(const QModelIndex &,int,int)));
        QObject::disconnect(d->m_abstractItemModel, SIGNAL(dataChanged(const QModelIndex&,const QModelIndex&)),
                            this, SLOT(_q_dataChanged(const QModelIndex&,const QModelIndex&)));
        QObject::disconnect(d->m_abstractItemModel, SIGNAL(rowsMoved(const QModelIndex&,int,int,const QModelIndex&,int)),
                            this, SLOT(_q_rowsMoved(const QModelIndex&,int,int,const QModelIndex&,int)));
    } else if (d->m_visualItemModel) {
        QObject::disconnect(d->m_visualItemModel, SIGNAL(itemsInserted(int,int)),
                         this, SIGNAL(itemsInserted(int,int)));
        QObject::disconnect(d->m_visualItemModel, SIGNAL(itemsRemoved(int,int)),
                         this, SIGNAL(itemsRemoved(int,int)));
        QObject::disconnect(d->m_visualItemModel, SIGNAL(itemsMoved(int,int,int)),
                         this, SIGNAL(itemsMoved(int,int,int)));
        QObject::disconnect(d->m_visualItemModel, SIGNAL(createdPackage(int,QmlPackage*)),
                         this, SLOT(_q_createdPackage(int,QmlPackage*)));
        QObject::disconnect(d->m_visualItemModel, SIGNAL(destroyingPackage(QmlPackage*)),
                         this, SLOT(_q_destroyingPackage(QmlPackage*)));
        d->m_visualItemModel = 0;
    }

    d->m_roles.clear();
    d->m_roleNames.clear();
    if (d->m_delegateDataType)
        d->m_delegateDataType->release();
    d->m_metaDataCreated = 0;
    d->m_metaDataCacheable = false;
    d->m_delegateDataType = new VDMDelegateDataType(&QmlGraphicsVisualDataModelData::staticMetaObject, d->m_context?d->m_context->engine():qmlEngine(this));

    QObject *object = qvariant_cast<QObject *>(model);
    if (object && (d->m_listModelInterface = qobject_cast<QListModelInterface *>(object))) {
        QObject::connect(d->m_listModelInterface, SIGNAL(itemsChanged(int,int,QList<int>)),
                         this, SLOT(_q_itemsChanged(int,int,QList<int>)));
        QObject::connect(d->m_listModelInterface, SIGNAL(itemsInserted(int,int)),
                         this, SLOT(_q_itemsInserted(int,int)));
        QObject::connect(d->m_listModelInterface, SIGNAL(itemsRemoved(int,int)),
                         this, SLOT(_q_itemsRemoved(int,int)));
        QObject::connect(d->m_listModelInterface, SIGNAL(itemsMoved(int,int,int)),
                         this, SLOT(_q_itemsMoved(int,int,int)));
        d->m_metaDataCacheable = true;
        if (d->m_delegate && d->m_listModelInterface->count())
            emit itemsInserted(0, d->m_listModelInterface->count());
        return;
    } else if (object && (d->m_abstractItemModel = qobject_cast<QAbstractItemModel *>(object))) {
        QObject::connect(d->m_abstractItemModel, SIGNAL(rowsInserted(const QModelIndex &,int,int)),
                            this, SLOT(_q_rowsInserted(const QModelIndex &,int,int)));
        QObject::connect(d->m_abstractItemModel, SIGNAL(rowsRemoved(const QModelIndex &,int,int)),
                            this, SLOT(_q_rowsRemoved(const QModelIndex &,int,int)));
        QObject::connect(d->m_abstractItemModel, SIGNAL(dataChanged(const QModelIndex&,const QModelIndex&)),
                            this, SLOT(_q_dataChanged(const QModelIndex&,const QModelIndex&)));
        QObject::connect(d->m_abstractItemModel, SIGNAL(rowsMoved(const QModelIndex&,int,int,const QModelIndex&,int)),
                            this, SLOT(_q_rowsMoved(const QModelIndex&,int,int,const QModelIndex&,int)));
        d->m_metaDataCacheable = true;
        return;
    }
    if ((d->m_visualItemModel = qvariant_cast<QmlGraphicsVisualDataModel *>(model))) {
        QObject::connect(d->m_visualItemModel, SIGNAL(itemsInserted(int,int)),
                         this, SIGNAL(itemsInserted(int,int)));
        QObject::connect(d->m_visualItemModel, SIGNAL(itemsRemoved(int,int)),
                         this, SIGNAL(itemsRemoved(int,int)));
        QObject::connect(d->m_visualItemModel, SIGNAL(itemsMoved(int,int,int)),
                         this, SIGNAL(itemsMoved(int,int,int)));
        QObject::connect(d->m_visualItemModel, SIGNAL(createdPackage(int,QmlPackage*)),
                         this, SLOT(_q_createdPackage(int,QmlPackage*)));
        QObject::connect(d->m_visualItemModel, SIGNAL(destroyingPackage(QmlPackage*)),
                         this, SLOT(_q_destroyingPackage(QmlPackage*)));
        return;
    }
    d->m_listAccessor = new QmlListAccessor;
    d->m_listAccessor->setList(model, d->m_context?d->m_context->engine():qmlEngine(this));
    if (d->m_listAccessor->type() != QmlListAccessor::ListProperty)
        d->m_metaDataCacheable = true;
    if (d->m_delegate && d->modelCount()) {
        emit itemsInserted(0, d->modelCount());
        emit countChanged();
    }
}

/*!
    \qmlproperty component VisualDataModel::delegate

    The delegate provides a template defining each item instantiated by a 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}.

    Here is an example delegate:
    \snippet doc/src/snippets/declarative/listview/listview.qml 0
*/
QmlComponent *QmlGraphicsVisualDataModel::delegate() const
{
    Q_D(const QmlGraphicsVisualDataModel);
    if (d->m_visualItemModel)
        return d->m_visualItemModel->delegate();
    return d->m_delegate;
}

void QmlGraphicsVisualDataModel::setDelegate(QmlComponent *delegate)
{
    Q_D(QmlGraphicsVisualDataModel);
    bool wasValid = d->m_delegate != 0;
    d->m_delegate = delegate;
    if (!wasValid && d->modelCount() && d->m_delegate) {
        emit itemsInserted(0, d->modelCount());
        emit countChanged();
    }
    if (wasValid && !d->m_delegate && d->modelCount()) {
        emit itemsRemoved(0, d->modelCount());
        emit countChanged();
    }
}

/*!
    \qmlproperty QModelIndex VisualDataModel::rootIndex

    QAbstractItemModel provides a heirachical tree of data, whereas
    QML only operates on list data. rootIndex allows the children of
    any node in a QAbstractItemModel to be provided by this model.

    This property only affects models of type QAbstractItemModel.

    \code
    // main.cpp
    Q_DECLARE_METATYPE(QModelIndex)

    class MyModel : public QDirModel
    {
        Q_OBJECT
    public:
        MyModel(QmlContext *ctxt) : QDirModel(), context(ctxt) {
            QHash<int,QByteArray> roles = roleNames();
            roles.insert(FilePathRole, "path");
            setRoleNames(roles);
            context->setContextProperty("myModel", this);
            context->setContextProperty("myRoot", QVariant::fromValue(index(0,0,QModelIndex())));
        }

        Q_INVOKABLE void setRoot(const QString &path) {
            QModelIndex root = index(path);
            context->setContextProperty("myRoot", QVariant::fromValue(root));
        }

        QmlContext *context;
    };

    int main(int argc, char ** argv)
    {
        QApplication app(argc, argv);

        QmlView view;

        MyModel model(view.rootContext());

        view.setSource(QUrl("qrc:view.qml"));
        view.show();

        return app.exec();
    }

    #include "main.moc"
    \endcode

    \code
    // view.qml
    import Qt 4.6

    ListView {
        width: 200
        height: 200
        model: VisualDataModel {
            model: myModel
            rootIndex: myRoot
            delegate: Component {
                Rectangle {
                    height: 25; width: 100
                    Text { text: path }
                    MouseRegion {
                        anchors.fill: parent;
                        onClicked: myModel.setRoot(path)
                    }
                }
            }
        }
    }
    \endcode

*/
QModelIndex QmlGraphicsVisualDataModel::rootIndex() const
{
    Q_D(const QmlGraphicsVisualDataModel);
    return d->m_root;
}

void QmlGraphicsVisualDataModel::setRootIndex(const QModelIndex &root)
{
    Q_D(QmlGraphicsVisualDataModel);
    if (d->m_root != root) {
        int oldCount = d->modelCount();
        d->m_root = root;
        int newCount = d->modelCount();
        if (d->m_delegate && oldCount)
            emit itemsRemoved(0, oldCount);
        if (d->m_delegate && newCount)
            emit itemsInserted(0, newCount);
        if (newCount != oldCount)
            emit countChanged();
        emit rootIndexChanged();
    }
}

QString QmlGraphicsVisualDataModel::part() const
{
    Q_D(const QmlGraphicsVisualDataModel);
    return d->m_part;
}

void QmlGraphicsVisualDataModel::setPart(const QString &part)
{
    Q_D(QmlGraphicsVisualDataModel);
    d->m_part = part;
}

int QmlGraphicsVisualDataModel::count() const
{
    Q_D(const QmlGraphicsVisualDataModel);
    return d->modelCount();
}

QmlGraphicsItem *QmlGraphicsVisualDataModel::item(int index, bool complete)
{
    Q_D(QmlGraphicsVisualDataModel);
    if (d->m_visualItemModel)
        return d->m_visualItemModel->item(index, d->m_part.toUtf8(), complete);
    return item(index, QByteArray(), complete);
}

/*
  Returns ReleaseStatus flags.
*/
QmlGraphicsVisualDataModel::ReleaseFlags QmlGraphicsVisualDataModel::release(QmlGraphicsItem *item)
{
    Q_D(QmlGraphicsVisualDataModel);
    if (d->m_visualItemModel)
        return d->m_visualItemModel->release(item);

    ReleaseFlags stat = 0;
    QObject *obj = item;
    bool inPackage = false;

    QHash<QObject*,QmlPackage*>::iterator it = d->m_packaged.find(item);
    if (it != d->m_packaged.end()) {
        QmlPackage *package = *it;
        d->m_packaged.erase(it);
        if (d->m_packaged.contains(item))
            stat |= Referenced;
        inPackage = true;
        obj = package; // fall through and delete
    }

    if (d->m_cache.releaseItem(obj)) {
        if (inPackage) {
            emit destroyingPackage(qobject_cast<QmlPackage*>(obj));
        } else {
            item->setVisible(false);
            static_cast<QGraphicsItem*>(item)->setParentItem(0);
        }
        stat |= Destroyed;
        obj->deleteLater();
    } else if (!inPackage) {
        stat |= Referenced;
    }

    return stat;
}

QObject *QmlGraphicsVisualDataModel::parts()
{
    Q_D(QmlGraphicsVisualDataModel);
    if (!d->m_parts) 
        d->m_parts = new QmlGraphicsVisualDataModelParts(this);
    return d->m_parts;
}

QmlGraphicsItem *QmlGraphicsVisualDataModel::item(int index, const QByteArray &viewId, bool complete)
{
    Q_D(QmlGraphicsVisualDataModel);
    if (d->m_visualItemModel)
        return d->m_visualItemModel->item(index, viewId, complete);

    if (d->modelCount() <= 0 || !d->m_delegate)
        return 0;
    QObject *nobj = d->m_cache.getItem(index);
    if (!nobj) {
        QmlContext *ccontext = d->m_context;
        if (!ccontext) ccontext = qmlContext(this);
        QmlContext *ctxt = new QmlContext(ccontext);
        QmlGraphicsVisualDataModelData *data = new QmlGraphicsVisualDataModelData(index, this);
        ctxt->setContextProperty(QLatin1String("model"), data);
        ctxt->addDefaultObject(data);
        nobj = d->m_delegate->beginCreate(ctxt);
        if (complete)
            d->m_delegate->completeCreate();
        if (nobj) {
            ctxt->setParent(nobj);
            data->setParent(nobj);
            d->m_cache.insertItem(index, nobj);
            if (QmlPackage *package = qobject_cast<QmlPackage *>(nobj))
                emit createdPackage(index, package);
        } else {
            delete data;
            delete ctxt;
            qWarning() << d->m_delegate->errors();
        }
    }
    QmlGraphicsItem *item = qobject_cast<QmlGraphicsItem *>(nobj);
    if (!item) {
        QmlPackage *package = qobject_cast<QmlPackage *>(nobj);
        if (package) {
            QObject *o = package->part(QString::fromUtf8(viewId));
            item = qobject_cast<QmlGraphicsItem *>(o);
            if (item)
                d->m_packaged.insertMulti(item, package);
        }
    }
    if (!item) {
        d->m_cache.releaseItem(nobj);
        qmlInfo(d->m_delegate) << QmlGraphicsVisualDataModel::tr("Delegate component must be Item type.");
    }

    return item;
}

void QmlGraphicsVisualDataModel::completeItem()
{
    Q_D(QmlGraphicsVisualDataModel);
    if (d->m_visualItemModel) {
        d->m_visualItemModel->completeItem();
        return;
    }

    d->m_delegate->completeCreate();
}

QString QmlGraphicsVisualDataModel::stringValue(int index, const QString &name)
{
    Q_D(QmlGraphicsVisualDataModel);
    if (d->m_visualItemModel)
        return d->m_visualItemModel->stringValue(index, name);

    if ((!d->m_listModelInterface && !d->m_abstractItemModel) || !d->m_delegate)
        return QString();

    QString val;
    QObject *data = 0;
    bool tempData = false;

    if (QObject *nobj = d->m_cache.item(index))
        data = d->data(nobj);
    if (!data) {
        data = new QmlGraphicsVisualDataModelData(index, this);
        tempData = true;
    }

    QmlDeclarativeData *ddata = QmlDeclarativeData::get(data);
    if (ddata && ddata->propertyCache) {
        QmlPropertyCache::Data *prop = ddata->propertyCache->property(name);
        if (prop) {
            if (prop->propType == QVariant::String) {
                void *args[] = { &val, 0 };
                QMetaObject::metacall(data, QMetaObject::ReadProperty, prop->coreIndex, args);
            } else if (prop->propType == qMetaTypeId<QVariant>()) {
                QVariant v;
                void *args[] = { &v, 0 };
                QMetaObject::metacall(data, QMetaObject::ReadProperty, prop->coreIndex, args);
                val = v.toString();
            }
        } else {
            val = data->property(name.toUtf8()).toString();
        }
    } else {
        val = data->property(name.toUtf8()).toString();
    }

    if (tempData)
        delete data;

    return val;
}

QVariant QmlGraphicsVisualDataModel::evaluate(int index, const QString &expression, QObject *objectContext)
{
    Q_D(QmlGraphicsVisualDataModel);
    if (d->m_visualItemModel)
        return d->m_visualItemModel->evaluate(index, expression, objectContext);

    if ((!d->m_listModelInterface && !d->m_abstractItemModel) || !d->m_delegate)
        return QVariant();

    QVariant value;
    QObject *nobj = d->m_cache.item(index);
    if (nobj) {
        QmlGraphicsItem *item = qobject_cast<QmlGraphicsItem *>(nobj);
        if (item) {
            QmlExpression e(qmlContext(item), expression, objectContext);
            value = e.value();
        }
    } else {
        QmlContext *ccontext = d->m_context;
        if (!ccontext) ccontext = qmlContext(this);
        QmlContext *ctxt = new QmlContext(ccontext);
        QmlGraphicsVisualDataModelData *data = new QmlGraphicsVisualDataModelData(index, this);
        ctxt->addDefaultObject(data);
        QmlExpression e(ctxt, expression, objectContext);
        value = e.value();
        delete data;
        delete ctxt;
    }

    return value;
}

int QmlGraphicsVisualDataModel::indexOf(QmlGraphicsItem *item, QObject *) const
{
    QVariant val = QmlEngine::contextForObject(item)->contextProperty(QLatin1String("index"));
        return val.toInt();
    return -1;
}

void QmlGraphicsVisualDataModel::_q_itemsChanged(int index, int count,
                                         const QList<int> &roles)
{
    Q_D(QmlGraphicsVisualDataModel);
    // XXX - highly inefficient
    for (int ii = index; ii < index + count; ++ii) {

        if (QObject *item = d->m_cache.item(ii)) {
            QmlGraphicsVisualDataModelData *data = d->data(item);

            for (int roleIdx = 0; roleIdx < roles.count(); ++roleIdx) {
                int role = roles.at(roleIdx);
                int propId = data->propForRole(role);
                if (propId != -1) {
                    if (d->m_listModelInterface) {
                        data->setValue(propId, d->m_listModelInterface->data(ii, QList<int>() << role).value(role));
                    } else if (d->m_abstractItemModel) {
                        QModelIndex index = d->m_abstractItemModel->index(ii, 0, d->m_root);
                        data->setValue(propId, d->m_abstractItemModel->data(index, role));
                    }
                }
            }
        }
    }
}

void QmlGraphicsVisualDataModel::_q_itemsInserted(int index, int count)
{
    Q_D(QmlGraphicsVisualDataModel);
    // XXX - highly inefficient
    QHash<int,QmlGraphicsVisualDataModelPrivate::ObjectRef> items;
    for (QHash<int,QmlGraphicsVisualDataModelPrivate::ObjectRef>::Iterator iter = d->m_cache.begin();
        iter != d->m_cache.end(); ) {

        if (iter.key() >= index) {
            QmlGraphicsVisualDataModelPrivate::ObjectRef objRef = *iter;
            int index = iter.key() + count;
            iter = d->m_cache.erase(iter);

            items.insert(index, objRef);

            QmlGraphicsVisualDataModelData *data = d->data(objRef.obj);
            data->setIndex(index);
        } else {
            ++iter;
        }
    }
    d->m_cache.unite(items);

    emit itemsInserted(index, count);
    emit countChanged();
}

void QmlGraphicsVisualDataModel::_q_itemsRemoved(int index, int count)
{
    Q_D(QmlGraphicsVisualDataModel);
    // XXX - highly inefficient
    QHash<int, QmlGraphicsVisualDataModelPrivate::ObjectRef> items;
    for (QHash<int, QmlGraphicsVisualDataModelPrivate::ObjectRef>::Iterator iter = d->m_cache.begin();
        iter != d->m_cache.end(); ) {
        if (iter.key() >= index && iter.key() < index + count) {
            QmlGraphicsVisualDataModelPrivate::ObjectRef objRef = *iter;
            iter = d->m_cache.erase(iter);
            items.insertMulti(-1, objRef); //XXX perhaps better to maintain separately
            QmlGraphicsVisualDataModelData *data = d->data(objRef.obj);
            data->setIndex(-1);
        } else if (iter.key() >= index + count) {
            QmlGraphicsVisualDataModelPrivate::ObjectRef objRef = *iter;
            int index = iter.key() - count;
            iter = d->m_cache.erase(iter);
            items.insert(index, objRef);
            QmlGraphicsVisualDataModelData *data = d->data(objRef.obj);
            data->setIndex(index);
        } else {
            ++iter;
        }
    }

    d->m_cache.unite(items);
    emit itemsRemoved(index, count);
    emit countChanged();
}

void QmlGraphicsVisualDataModel::_q_itemsMoved(int from, int to, int count)
{
    Q_D(QmlGraphicsVisualDataModel);
    // XXX - highly inefficient
    QHash<int,QmlGraphicsVisualDataModelPrivate::ObjectRef> items;
    for (QHash<int,QmlGraphicsVisualDataModelPrivate::ObjectRef>::Iterator iter = d->m_cache.begin();
        iter != d->m_cache.end(); ) {

        if (iter.key() >= from && iter.key() < from + count) {
            QmlGraphicsVisualDataModelPrivate::ObjectRef objRef = *iter;
            int index = iter.key() - from + to;
            iter = d->m_cache.erase(iter);

            items.insert(index, objRef);

            QmlGraphicsVisualDataModelData *data = d->data(objRef.obj);
            data->setIndex(index);
        } else {
            ++iter;
        }
    }
    for (QHash<int,QmlGraphicsVisualDataModelPrivate::ObjectRef>::Iterator iter = d->m_cache.begin();
        iter != d->m_cache.end(); ) {

        int diff = from > to ? count : -count;
        if (iter.key() >= qMin(from,to) && iter.key() < qMax(from+count,to+count)) {
            QmlGraphicsVisualDataModelPrivate::ObjectRef objRef = *iter;
            int index = iter.key() + diff;
            iter = d->m_cache.erase(iter);

            items.insert(index, objRef);

            QmlGraphicsVisualDataModelData *data = d->data(objRef.obj);
            data->setIndex(index);
        } else {
            ++iter;
        }
    }
    d->m_cache.unite(items);

    emit itemsMoved(from, to, count);
}

void QmlGraphicsVisualDataModel::_q_rowsInserted(const QModelIndex &parent, int begin, int end)
{
    if (!parent.isValid())
        _q_itemsInserted(begin, end - begin + 1);
}

void QmlGraphicsVisualDataModel::_q_rowsRemoved(const QModelIndex &parent, int begin, int end)
{
    if (!parent.isValid())
        _q_itemsRemoved(begin, end - begin + 1);
}

void QmlGraphicsVisualDataModel::_q_rowsMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow)
{
    const int count = sourceEnd - sourceStart + 1;
    if (!destinationParent.isValid() && !sourceParent.isValid()) {
        _q_itemsMoved(sourceStart, destinationRow, count);
    } else if (!sourceParent.isValid()) {
        _q_itemsRemoved(sourceStart, count);
    } else if (!destinationParent.isValid()) {
        _q_itemsInserted(destinationRow, count);
    }
}

void QmlGraphicsVisualDataModel::_q_dataChanged(const QModelIndex &begin, const QModelIndex &end)
{
    Q_D(QmlGraphicsVisualDataModel);
    if (!begin.parent().isValid())
        _q_itemsChanged(begin.row(), end.row() - begin.row() + 1, d->m_roles);
}

void QmlGraphicsVisualDataModel::_q_createdPackage(int index, QmlPackage *package)
{
    Q_D(QmlGraphicsVisualDataModel);
    emit createdItem(index, qobject_cast<QmlGraphicsItem*>(package->part(d->m_part)));
}

void QmlGraphicsVisualDataModel::_q_destroyingPackage(QmlPackage *package)
{
    Q_D(QmlGraphicsVisualDataModel);
    emit destroyingItem(qobject_cast<QmlGraphicsItem*>(package->part(d->m_part)));
}

QT_END_NAMESPACE

QML_DECLARE_TYPE(QListModelInterface)

#include <qmlgraphicsvisualitemmodel.moc>