File: kreparentingproxymodel.cpp

package info (click to toggle)
kitemmodels 5.116.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,844 kB
  • sloc: cpp: 18,018; python: 26; sh: 13; makefile: 7
file content (1498 lines) | stat: -rw-r--r-- 50,593 bytes parent folder | download | duplicates (2)
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
/*
    SPDX-FileCopyrightText: 2009 Stephen Kelly <steveire@gmail.com>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "kreparentingproxymodel.h"
#include <QDebug>
#include <QVector>

#include <algorithm>
#include <functional>

class KReparentingProxyModelPrivate
{
    KReparentingProxyModelPrivate(KReparentingProxyModel *proxyModel)
        : m_nextId(0)
        , q_ptr(proxyModel)
    {
    }

    qint64 newId() const
    {
        return m_nextId++;
    }

    enum MapStrategy {
        MapDescendants,
        MapChildrenOnly,
    };

    /**
      Creates mappings of indexes in the source model between @p start
      and @p end which should be represented in the proxy model as descendants
      of @p parent.
    */
    QHash<QModelIndex, QModelIndexList> recreateMappings(const QModelIndex &parent, int start, int end = -1, int strategy = MapChildrenOnly) const;

    /**
      Merges all indexes from @p mappings which are descendants of @p parent into the model.
      Returns the remaining mappings.

      Note that this changes the internal model structure and must only be called between begin/end insert/remove/move/reset calls.
    */
    QHash<QModelIndex, QModelIndexList> mergeDescendants(QHash<QModelIndex, QModelIndexList> mappings, const QModelIndex &parent, int start);

    /**
      Verifies that the indexes below @p parent between @p start and rowCount(parent)
      are in the correct positions in the proxy model. Repositions them if not.
    */
    void verifyStructure(const QModelIndex &parent, int start);

    /**
      Returns the index vertically below index in the model @p model.
      If @p model is 0, the sourceModel is used
      Returns an invalid index if there is no index below @p index.
    */
    QModelIndex getIndexBelow(const QModelIndex &index, QAbstractItemModel *model = nullptr) const;

    /**
      Returns the last descendant of @p index or itself if it has no children
    */
    QModelIndex getLastDescendant(const QModelIndex &index) const;

    bool isDescendantInModel(const QModelIndex &ancestor, const QModelIndex &descendant) const;

    /**
      Returns the ancestors of @p descendant that are already in the proxy model.

      Note that @p descendant does not have to be in the proxy yet, and it is not part of the
      result list.
    */
    QVector<QModelIndex> getExistingAncestors(const QModelIndex &descendant) const;

    void sourceRowsAboutToBeInserted(const QModelIndex &parent, int start, int end);
    void sourceRowsInserted(const QModelIndex &parent, int start, int end);
    void sourceRowsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
    void sourceRowsRemoved(const QModelIndex &parent, int start, int end);
    void sourceRowsAboutToBeMoved(const QModelIndex &parent, int start, int end, const QModelIndex &destParent, int destRow);
    void sourceRowsMoved(const QModelIndex &parent, int start, int end, const QModelIndex &destParent, int destRow);
    void sourceModelAboutToBeReset();
    void endResetProxy();
    void sourceModelReset();
    void sourceLayoutAboutToBeChanged();
    void sourceLayoutChanged();
    void sourceDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);

    mutable QHash<qint64, QPersistentModelIndex> m_parents;
    mutable QHash<QPersistentModelIndex, QList<QPersistentModelIndex>> m_childIndexes;

    struct PendingInsertion {
        PendingInsertion()
            : parentId(-1)
            , start(-1)
            , end(-1)
        {
        }

        PendingInsertion(const QModelIndex &_index, int _start, int _end)
            : index(_index)
            , start(_start)
            , end(_end)
        {
        }

        QPersistentModelIndex index;
        QModelIndex sourceIndex;
        qint64 parentId;
        int start;
        int end;
    };

    struct PendingRemoval : PendingInsertion {
        int numTrailing;
    };
    // Needed between the beginRemoveRows and endRemoveRows signals.
    mutable QHash<qint64, QPersistentModelIndex> m_pendingRemovalParents;
    mutable QHash<QPersistentModelIndex, QList<QPersistentModelIndex>> m_pendingRemovalChildIndexes;

    QHash<QModelIndex, QModelIndexList> insertTree(QHash<QModelIndex, QModelIndexList> mappings, const QModelIndex &parent);

    void handleInsertion(const PendingInsertion &pendingInsertion);

    void handleRemoval(const PendingRemoval &pendingRemoval);

    mutable QHash<QModelIndex, PendingInsertion> m_pendingInsertions;
    mutable QVector<PendingRemoval> m_pendingRemovals;

    mutable qint64 m_nextId;

    Q_DECLARE_PUBLIC(KReparentingProxyModel)
    KReparentingProxyModel *q_ptr;

    QList<QPersistentModelIndex> m_layoutChangePersistentIndexes;
    QModelIndexList m_proxyIndexes;

    void emitDataChangedSignals(const QModelIndex &parent, int maxChanged);

    /**
      Given @p parent in the proxy model, return the last index lying between @p start and @p end
      which is also a descendant of @p parent.
    */
    QModelIndex findLastInParent(QModelIndex parent, int start, int end);

    /**
      Removes @P idx (which is a source model index) and its children from the model data structures.
    */
    void removeTree(const QPersistentModelIndex &idx, int start = 0, int end = -1);

    int pendingRemovalRowCount(const QModelIndex &sourceIndex) const;
};

class LessThan
{
    const KReparentingProxyModel *m_model;

public:
    LessThan(const KReparentingProxyModel *model)
        : m_model(model)
    {
    }
    bool operator()(const QModelIndex &ancestor, const QModelIndex &descendant)
    {
        return m_model->isDescendantOf(ancestor, descendant);
    }
};

QModelIndex KReparentingProxyModelPrivate::getIndexBelow(const QModelIndex &index, QAbstractItemModel *model) const
{
    Q_Q(const KReparentingProxyModel);

    //   qDebug() << index.data() << index;

    if (!model) {
        model = q->sourceModel();
    }

    if (model->hasChildren(index)) {
        return model->index(0, 0, index);
    }

    QModelIndex sibling = index.sibling(index.row() + 1, index.column());
    if (sibling.isValid()) {
        return sibling;
    }

    QModelIndex parent = index.parent();

    if (!parent.isValid()) {
        return QModelIndex();
    }

    int affectedRow = index.row();
    const int column = 0;

    while (parent.isValid()) {
        //     qDebug() << "parent" << parent.data() << model->rowCount(parent) << affectedRow;
        if (affectedRow < model->rowCount(parent) - 1) {
            return model->index(affectedRow + 1, column, parent);
        }

        affectedRow = parent.row();
        parent = parent.parent();
    }

    if (model->rowCount(parent) >= affectedRow) {
        return model->index(affectedRow + 1, column, parent);
    }

    return QModelIndex();
}

QModelIndex KReparentingProxyModelPrivate::getLastDescendant(const QModelIndex &index) const
{
    Q_Q(const KReparentingProxyModel);

    QModelIndex proxyIndex = q->mapFromSource(index);

    while (q->hasChildren(proxyIndex)) {
        proxyIndex = q->index(q->rowCount(proxyIndex), proxyIndex.column(), proxyIndex);
        if (!proxyIndex.isValid()) {
            break;
        }
    }
    return q->mapToSource(proxyIndex);
}

QVector<QModelIndex> KReparentingProxyModelPrivate::getExistingAncestors(const QModelIndex &descendant) const
{
    Q_Q(const KReparentingProxyModel);

    QVector<QModelIndex> vector;
    if (!descendant.isValid()) {
        return vector;
    }

    QModelIndex parent = q->mapFromSource(descendant).parent();
    QModelIndex sourceParent = q->mapToSource(parent);
    if (!sourceParent.isValid()) {
        return vector;
    }

    vector.append(sourceParent);
    while (parent.isValid()) {
        parent = parent.parent();
        sourceParent = q->mapToSource(parent);
        if (!sourceParent.isValid()) {
            return vector;
        }
        vector.prepend(sourceParent);
    }
    return vector;
}

QHash<QModelIndex, QModelIndexList> KReparentingProxyModelPrivate::recreateMappings(const QModelIndex &ancestor, int start, int end, int strategy) const
{
    Q_Q(const KReparentingProxyModel);
    const int column = 0;

    QHash<QModelIndex, QModelIndexList> mappings;
    // Handle listing the root QModelIndex().
    if (!ancestor.isValid() && !q->sourceModel()->hasChildren())
    // Empty model. Nothing to do.
    {
        return mappings;
    }

    // A
    // - B
    // - - C
    // - D
    // If start refers to D, existing ancestors will contain only A.
    // We need to go 'up' to C and get its ancestors in case D is to be made a child of B or C (for example if B and C have just been inserted)
    QModelIndex indexAbove;
    if (start > 0) {
        indexAbove = getLastDescendant(q->sourceModel()->index(start - 1, column, ancestor));
    } else {
        indexAbove = ancestor;
    }

    QVector<QModelIndex> ancestors = getExistingAncestors(indexAbove);

    //   ancestors.append(indexAbove);
    //   qDebug() << ancestors;
    QModelIndex nextIndex = ancestor;

    for (int row = start; (row <= end || end == -1);) {
        // A
        // - B
        // - - C
        // - D
        // The nextIndex of the invalid QModelIndex is A,
        // The nextIndex of A is B,
        // The nextIndex of B is C,
        // The nextIndex of C is D,
        // The nextIndex of D is invalid,
        // When the nextIndex is invalid we're finished creating mappings.
        if (MapDescendants == strategy) {
            nextIndex = getIndexBelow(nextIndex);
        } else {
            nextIndex = q->sourceModel()->index(row, column, ancestor);
        }

        if (!nextIndex.isValid()) {
            break;
        }

        const QVector<QModelIndex>::iterator ancestorIt = std::lower_bound(ancestors.begin(), ancestors.end(), nextIndex, LessThan(q));

        ancestors.erase(ancestorIt, ancestors.end());

        QModelIndex parent;
        if (ancestorIt != ancestors.begin()) {
            parent = *(ancestorIt - 1);
        }

        ancestors.append(nextIndex);

        mappings[parent].append(nextIndex);
    }

    return mappings;
}

void KReparentingProxyModelPrivate::verifyStructure(const QModelIndex &sourceParent, int sourceStart)
{
    Q_Q(KReparentingProxyModel);

    // If the start structure is:

    // C
    // D
    // E

    // and then A and B are inserted, we may need to move C D and E. Not all of the siblings will
    // necessarily be moved to the same destination parent.
    // Some example finished scenarios depending on the outcome of isDescendantOf:

    // A
    // B
    // C
    // D
    // E

    // A
    // B
    // - C
    // - D
    // - E

    // A
    // - B
    // - C
    // - D
    // - E

    // A
    // - B
    // - - C
    // - D
    // E

    // Local variable mappings now contains all the information about finished state
    // When we locate the first child to be moved, we process it and its siblings

    QHash<QModelIndex, QModelIndexList> mappings = recreateMappings(sourceParent, sourceStart, -1);

    if (mappings.isEmpty()) {
        return;
    }

    QModelIndex sourceFirstIndex = q->sourceModel()->index(sourceStart, 0, sourceParent);

    QModelIndex destinationParent;
    QModelIndexList movedIndexes;

    QHashIterator<QModelIndex, QModelIndexList> it(mappings);
    while (it.hasNext()) {
        it.next();
        //     qDebug() << it.key() << it.key().data() << it.value();
        if (it.value().at(0) == sourceFirstIndex) {
            destinationParent = it.key();
            movedIndexes = it.value();
            break;
        }
    }

    Q_FOREVER {
        if (destinationParent == sourceParent)
        // No indexes moved
        {
            return;
        }

        Q_ASSERT(destinationParent.isValid());
        Q_ASSERT(!movedIndexes.isEmpty());

        // It's only possible for things to move right, and even that's only an option
        // for children of parent, but not their descendants. ie, children of C D and E will not need to be reparented.
        // They are already in the correct positions.

        QList<QPersistentModelIndex> &existingSourceIndexes = m_childIndexes[sourceParent];
        QList<QPersistentModelIndex> existingDestinationIndexes = m_childIndexes[destinationParent];

        QModelIndex proxySourceParent = q->mapFromSource(sourceParent);
        QModelIndex proxyDestinationParent = q->mapFromSource(destinationParent);

        // That is, start position of indexes to be moved from the source parent.
        int proxySourceStart = m_childIndexes.value(sourceParent).indexOf(movedIndexes.at(0));
        int proxySourceEnd = proxySourceStart + movedIndexes.size() - 1;

        // The moved indexes are appended to the destinationParent. Nothing else is possible.
        // If they were to be inserted in the middle somewhere, they would already be there.

        int destinationRow = existingDestinationIndexes.size();

        bool allowMove = q->beginMoveRows(proxySourceParent, proxySourceStart, proxySourceEnd, proxyDestinationParent, destinationRow);
        Q_ASSERT(allowMove);

        for (int row = proxySourceEnd; row >= proxySourceStart; --row) {
            existingSourceIndexes.removeAt(row);
        }

        QHash<QModelIndex, QModelIndexList> mapping;
        mapping.insert(destinationParent, movedIndexes);
        mergeDescendants(mapping, destinationParent, existingDestinationIndexes.size());

        q->endMoveRows();

        if (!mappings.contains(q->mapToSource(proxyDestinationParent.parent()))) {
            break;
        }

        destinationParent = q->mapToSource(proxyDestinationParent.parent());
        movedIndexes = mappings.value(destinationParent);
    }
}

KReparentingProxyModel::KReparentingProxyModel(QObject *parent)
    : QAbstractProxyModel(parent)
    , d_ptr(new KReparentingProxyModelPrivate(this))
{
}

KReparentingProxyModel::~KReparentingProxyModel()
{
    delete d_ptr;
}

bool KReparentingProxyModelPrivate::isDescendantInModel(const QModelIndex &ancestor, const QModelIndex &descendant) const
{
    //   qDebug() << ancestor.data() << descendant.data();

    //   if (!ancestor.isValid())
    //     return true;

    QModelIndex _ancestor = descendant.parent();
    while (_ancestor.isValid()) {
        if (_ancestor == ancestor) {
            return true;
        }
        _ancestor = _ancestor.parent();
    }
    return (!ancestor.isValid() && descendant.isValid());
}

bool KReparentingProxyModel::isDescendantOf(const QModelIndex &ancestor, const QModelIndex &descendant) const
{
    Q_D(const KReparentingProxyModel);
    return d->isDescendantInModel(ancestor, descendant);
    //   return (!ancestor.isValid() && descendant.isValid());
}

QModelIndex KReparentingProxyModel::mapFromSource(const QModelIndex &sourceIndex) const
{
    Q_D(const KReparentingProxyModel);
    if (!sourceIndex.isValid()) {
        return QModelIndex();
    }

    QModelIndex sourceIndexFirstColumn = sourceIndex.sibling(sourceIndex.row(), 0);

    QHash<QPersistentModelIndex, QList<QPersistentModelIndex>>::const_iterator it;
    const QHash<QPersistentModelIndex, QList<QPersistentModelIndex>>::const_iterator begin = d->m_childIndexes.constBegin();
    const QHash<QPersistentModelIndex, QList<QPersistentModelIndex>>::const_iterator end = d->m_childIndexes.constEnd();

    for (it = begin; it != end; ++it) {
        QList<QPersistentModelIndex> list = it.value();
        if (list.contains(sourceIndexFirstColumn)) {
            QModelIndex sourceParent = it.key();
            int row = list.indexOf(sourceIndexFirstColumn);

            // There must have been a mapping made for it.
            Q_ASSERT(d->m_parents.values().contains(sourceParent));

            qint64 id = d->m_parents.key(sourceParent);

            // id refers to the parent.
            return createIndex(row, sourceIndex.column(), reinterpret_cast<void *>(id));
        }
    }
    return QModelIndex();
}

QModelIndex KReparentingProxyModel::mapToSource(const QModelIndex &proxyIndex) const
{
    Q_D(const KReparentingProxyModel);

    //   qDebug() << "MMMMMM" << proxyIndex;

    if (!proxyIndex.isValid()) {
        return QModelIndex();
    }

    qint64 id = reinterpret_cast<qint64>(proxyIndex.internalPointer());

    //   if (!d->m_parents.contains(id))
    //     qDebug() << d->m_parents << id;

    QModelIndex sourceParent;
    if (d->m_pendingRemovalParents.contains(id)) {
        //     qDebug() << "pending";
        sourceParent = d->m_pendingRemovalParents.value(id);
    } else {
        Q_ASSERT(d->m_parents.contains(id));
        sourceParent = d->m_parents.value(id);
    }

    // qDebug() <<  sourceParent << sourceParent.data();

    QModelIndex sourceIndexFirstColumn;
    if (d->m_pendingRemovalChildIndexes.contains(sourceParent)) {
        //     qDebug() << "#############";

        for (const KReparentingProxyModelPrivate::PendingRemoval &pendingRemoval : std::as_const(d->m_pendingRemovals)) {
            //       qDebug() << "In" << pendingRemoval.index << pendingRemoval.sourceIndex << sourceParent;
            if (pendingRemoval.sourceIndex == sourceParent) {
                //         qDebug() << "Out" << pendingRemoval.sourceIndex << sourceParent;
                int proxyRow = proxyIndex.row();
                int row = proxyRow - pendingRemoval.start;

                //         qDebug() << d->m_pendingRemovalChildIndexes.value(sourceParent) << proxyRow << row << pendingRemoval.end;

                if (proxyRow > pendingRemoval.end) {
                    Q_ASSERT(d->m_childIndexes.contains(sourceParent));
                    row = proxyRow - (pendingRemoval.end - pendingRemoval.start + 1);
                    //           qDebug() << "new row" << row;
                    sourceIndexFirstColumn = d->m_childIndexes.value(sourceParent).at(row);
                } else {
                    sourceIndexFirstColumn = d->m_pendingRemovalChildIndexes.value(sourceParent).at(row);
                }
                break;
            }
        }
    } else {
        Q_ASSERT(d->m_childIndexes.contains(sourceParent));
        sourceIndexFirstColumn = d->m_childIndexes.value(sourceParent).at(proxyIndex.row());
    }

    Q_ASSERT(sourceIndexFirstColumn.isValid());

    return sourceIndexFirstColumn.sibling(sourceIndexFirstColumn.row(), proxyIndex.column());
}

int KReparentingProxyModel::columnCount(const QModelIndex &parent) const
{
    Q_D(const KReparentingProxyModel);

    if (!sourceModel()) {
        return 0;
    }

    if (!parent.isValid()) {
        return sourceModel()->columnCount();
    }

    if (parent.column() > 0) {
        return 0;
    }
    QModelIndex sourceIndex = mapToSource(parent);

    return (d->m_childIndexes.value(sourceIndex).size() > 0) ? sourceModel()->columnCount() : 0;
}

QVariant KReparentingProxyModel::data(const QModelIndex &proxyIndex, int role) const
{
    return QAbstractProxyModel::data(proxyIndex, role);
}

QModelIndex KReparentingProxyModel::index(int row, int column, const QModelIndex &parent) const
{
    Q_D(const KReparentingProxyModel);

    if (!hasIndex(row, column, parent)) {
        return QModelIndex();
    }

    QModelIndex sourceParent = mapToSource(parent);

    //   if (!d->m_pendingRemovals.isEmpty())
    //     qDebug() << sourceParent << sourceParent.data();

    // ### This is where we need to have the children of removed indexes stored.

    //   if (!d->m_parents.values().contains(sourceParent))
    //   {
    //     qDebug() << d->m_pendingRemovalParents.values() << sourceParent << d->m_pendingRemovalParents.values().contains(sourceParent);
    //   }

    qint64 id;
    if (d->m_pendingRemovalParents.values().contains(sourceParent)) {
        id = d->m_pendingRemovalParents.key(sourceParent);
    } else {
        // There must have been a mapping made for it.
        Q_ASSERT(d->m_parents.values().contains(sourceParent));
        id = d->m_parents.key(sourceParent);
    }
    return createIndex(row, column, reinterpret_cast<void *>(id));
}

QModelIndex KReparentingProxyModel::parent(const QModelIndex &child) const
{
    Q_D(const KReparentingProxyModel);

    if (!child.isValid()) {
        return QModelIndex();
    }

    QModelIndex sourceIndex = mapToSource(child);

    QModelIndex firstColumnChild = sourceIndex;
    if (sourceIndex.column() > 0) {
        firstColumnChild = sourceIndex.sibling(sourceIndex.row(), 0);
    }

    QHashIterator<QPersistentModelIndex, QList<QPersistentModelIndex>> itPending(d->m_pendingRemovalChildIndexes);

    while (itPending.hasNext()) {
        itPending.next();

        if (itPending.value().contains(firstColumnChild)) {
            return mapFromSource(itPending.key());
        }
    }

    QHashIterator<QPersistentModelIndex, QList<QPersistentModelIndex>> it(d->m_childIndexes);

    while (it.hasNext()) {
        it.next();

        if (it.value().contains(firstColumnChild)) {
            return mapFromSource(it.key());
        }
    }
    return QModelIndex();
}

int KReparentingProxyModelPrivate::pendingRemovalRowCount(const QModelIndex &sourceIndex) const
{
    for (const PendingRemoval &pendingRemoval : std::as_const(m_pendingRemovals)) {
        //     qDebug() << pendingRemoval.sourceIndex;
        if (pendingRemoval.sourceIndex == sourceIndex) {
            return pendingRemoval.end - pendingRemoval.start + 1;
        }
    }
    return 0;
}

int KReparentingProxyModel::rowCount(const QModelIndex &parent) const
{
    Q_D(const KReparentingProxyModel);

    if (parent.column() > 0) {
        return 0;
    }

    QModelIndex sourceIndex = mapToSource(parent);

    int size = d->m_childIndexes.value(sourceIndex).size() + d->m_pendingRemovalChildIndexes.value(sourceIndex).size();

    //   qDebug() << d->m_pendingRemovalChildIndexes.value(sourceIndex).size();

    //   if (!d->m_pendingRemovals.isEmpty())
    //   {
    //     qDebug() << "SIZE" << sourceIndex << sourceIndex.data() << size << d->m_pendingRemovals.size() << d->pendingRemovalRowCount(sourceIndex);
    //   }

    return size;
}

bool KReparentingProxyModel::hasChildren(const QModelIndex &parent) const
{
    return rowCount(parent) > 0;
}

void KReparentingProxyModel::setSourceModel(QAbstractItemModel *sourceModel)
{
    Q_D(KReparentingProxyModel);

    beginResetModel();

    disconnect(sourceModel, SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int)), this, SLOT(sourceRowsAboutToBeInserted(QModelIndex, int, int)));
    disconnect(sourceModel, SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(sourceRowsInserted(QModelIndex, int, int)));
    disconnect(sourceModel, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)), this, SLOT(sourceRowsAboutToBeRemoved(QModelIndex, int, int)));
    disconnect(sourceModel, SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(sourceRowsRemoved(QModelIndex, int, int)));
    disconnect(sourceModel,
               SIGNAL(rowsAboutToBeMoved(QModelIndex, int, int, QModelIndex, int)),
               this,
               SLOT(sourceRowsAboutToBeMoved(QModelIndex, int, int, QModelIndex, int)));
    disconnect(sourceModel, SIGNAL(rowsMoved(QModelIndex, int, int, QModelIndex, int)), this, SLOT(sourceRowsMoved(QModelIndex, int, int, QModelIndex, int)));
    disconnect(sourceModel, SIGNAL(modelAboutToBeReset()), this, SLOT(sourceModelAboutToBeReset()));
    disconnect(sourceModel, SIGNAL(modelReset()), this, SLOT(sourceModelReset()));
    disconnect(sourceModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(sourceDataChanged(QModelIndex, QModelIndex)));
    disconnect(sourceModel, SIGNAL(layoutAboutToBeChanged()), this, SLOT(sourceLayoutAboutToBeChanged()));
    disconnect(sourceModel, SIGNAL(layoutChanged()), this, SLOT(sourceLayoutChanged()));

    QAbstractProxyModel::setSourceModel(sourceModel);

    //   qDebug() << "set";
    QHash<QModelIndex, QModelIndexList> mappings =
        d->recreateMappings(QModelIndex(), 0, sourceModel->rowCount() - 1, KReparentingProxyModelPrivate::MapDescendants);
    //   qDebug() << "begin";
    d->mergeDescendants(mappings, QModelIndex(), 0);

    connect(sourceModel, SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int)), SLOT(sourceRowsAboutToBeInserted(QModelIndex, int, int)));
    connect(sourceModel, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(sourceRowsInserted(QModelIndex, int, int)));
    connect(sourceModel, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)), SLOT(sourceRowsAboutToBeRemoved(QModelIndex, int, int)));
    connect(sourceModel, SIGNAL(rowsRemoved(QModelIndex, int, int)), SLOT(sourceRowsRemoved(QModelIndex, int, int)));
    connect(sourceModel,
            SIGNAL(rowsAboutToBeMoved(QModelIndex, int, int, QModelIndex, int)),
            SLOT(sourceRowsAboutToBeMoved(QModelIndex, int, int, QModelIndex, int)));
    connect(sourceModel, SIGNAL(rowsMoved(QModelIndex, int, int, QModelIndex, int)), SLOT(sourceRowsMoved(QModelIndex, int, int, QModelIndex, int)));
    connect(sourceModel, SIGNAL(modelAboutToBeReset()), SLOT(sourceModelAboutToBeReset()));
    connect(sourceModel, SIGNAL(modelReset()), SLOT(sourceModelReset()));
    connect(sourceModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), SLOT(sourceDataChanged(QModelIndex, QModelIndex)));
    connect(sourceModel, SIGNAL(layoutAboutToBeChanged()), SLOT(sourceLayoutAboutToBeChanged()));
    connect(sourceModel, SIGNAL(layoutChanged()), SLOT(sourceLayoutChanged()));

    endResetModel();
}

void KReparentingProxyModelPrivate::sourceRowsAboutToBeInserted(const QModelIndex &parent, int start, int end)
{
    Q_UNUSED(parent);
    Q_UNUSED(start);
    Q_UNUSED(end);

    Q_Q(KReparentingProxyModel);
    return q->beginResetModel();
#if 0
    // We can't figure out the structure until the indexes are in the model.
    // Store the signal until the new rows are actually there in sourceRowsInserted.
    PendingInsertion pendingInsertion(parent, start, end);
    m_pendingInsertions.insert(parent, pendingInsertion);
#endif
}

QHash<QModelIndex, QModelIndexList>
KReparentingProxyModelPrivate::mergeDescendants(QHash<QModelIndex, QModelIndexList> mappings, const QModelIndex &parent, int start)
{
    const QModelIndexList childIndexes = mappings.take(parent);
    //   qDebug() << childIndexes;
    if (!childIndexes.isEmpty()) {
        if (!m_parents.values().contains(parent)) {
            m_parents.insert(newId(), QPersistentModelIndex(parent));
        }
    }
    int row = start;
    for (const QModelIndex &idx : childIndexes) {
        m_childIndexes[parent].insert(row++, QPersistentModelIndex(idx));
        mappings = mergeDescendants(mappings, idx, 0);
    }
    return mappings;
}

QHash<QModelIndex, QModelIndexList> KReparentingProxyModelPrivate::insertTree(QHash<QModelIndex, QModelIndexList>, const QModelIndex &)
{
    return QHash<QModelIndex, QModelIndexList>();
}

void KReparentingProxyModelPrivate::handleInsertion(const PendingInsertion &pendingInsertion)
{
    Q_Q(KReparentingProxyModel);
    QModelIndex parent = pendingInsertion.index;
    int start = pendingInsertion.start;
    int end = pendingInsertion.end;

    //   qDebug() << parent << parent.data() << start << end;

    //   for (int i = start; i < end; ++i)
    //   {
    //     QModelIndex idx = q->sourceModel()->index(i, 0, parent);
    //     qDebug() << idx << idx.data();
    //   }

    QHash<QModelIndex, QModelIndexList> newItemMappings = recreateMappings(parent, start, end, KReparentingProxyModelPrivate::MapDescendants);

    // iterate over keys. if key in keys iterate up. This gives list of top level parents.
    // Pick the one whose parent is @p parent. Insert it. Look up until find the parent of another one and insert that.
    // If one of the parents is invalid it is necessarily the last one to be processed (if there are more to process, they'll be children of it)
    // That case should work too.

    //   qDebug() << "new item mappings" << newItemMappings;

    const int column = 0;

    //   qDebug() << m_childIndexes.contains(parent);

    if (newItemMappings.contains(parent)) {
        QModelIndexList newItemList = newItemMappings.value(parent);
        //     qDebug() << "newItemList" << newItemList;
        int proxyStart = 0;

        // A single insertion in the source model might be multiple insertions in the proxy model.
        Q_FOREVER {
            if (newItemList.isEmpty()) {
                if (!newItemMappings.contains(parent.parent())) {
                    break;
                }

                newItemList = newItemMappings.value(parent.parent());
                continue;
            }

            proxyStart = 0;

            QModelIndex proxyParent = q->mapFromSource(parent);
            if (start > 0) {
                QModelIndex lastDesc = q->mapFromSource(getLastDescendant(q->sourceModel()->index(start - 1, column, parent)));

                while (lastDesc.parent() != proxyParent) {
                    lastDesc = lastDesc.parent();
                }
                proxyStart = lastDesc.row() + 1;
            }

            q->beginInsertRows(proxyParent, proxyStart, proxyStart + newItemList.size() - 1);

            newItemMappings = mergeDescendants(newItemMappings, parent, proxyStart);

            q->endInsertRows();

            if (!newItemMappings.contains(parent.parent())) {
                break;
            }

            newItemList = newItemMappings.value(parent.parent());
        }
    }

    //   // The rest are not descendants of pendingInsertion.index in the proxy model, but are elsewhere.
    //   Q_FOREACH(const QModelIndex &parent, newItemMappings.keys())
    //   {
    //
    //   }

    return;
}

void KReparentingProxyModelPrivate::sourceRowsInserted(const QModelIndex &parent, int, int end)
{
    Q_UNUSED(parent);
    Q_UNUSED(end);

    return endResetProxy();

#if 0
    Q_Q(KReparentingProxyModel);
    if (m_pendingInsertions.contains(parent)) {
        PendingInsertion pendingInsertion = m_pendingInsertions.value(parent);
        handleInsertion(pendingInsertion);

        if (q->sourceModel()->rowCount(parent) <= (end + 1)) {
            return;
        }

        // The presence of new rows might affect the structure of indexes below.
        verifyStructure(parent, end + 1);
    }
#endif
}

void KReparentingProxyModelPrivate::removeTree(const QPersistentModelIndex &idxToRemove, int start, int end)
{
    if (!m_childIndexes.contains(idxToRemove)) {
        return;
    }

    //   qDebug() << "idxToRemove" << idxToRemove << start << end;

    QList<QPersistentModelIndex> &toRemove = m_childIndexes[idxToRemove];
    //   qDebug() << toRemove << toRemove.size();

    //   QList<int> intList;
    //   intList << 1 << 2 << 3 << 4 << 5;
    //
    //   QList<int>::iterator intit = intList.begin();
    //   QList<int>::iterator intendIt = intList.end();
    //
    //   if (end == 0)
    //     intendIt = intit + 1;
    //
    //   if (end > 0)
    //   {
    //     intendIt = intit + (end - start + 1) + 1;
    //     qDebug() << "intend" << *intendIt;
    //   }
    //   intit += start;
    //
    //   while (intit != intendIt)
    //   {
    //     int i = *intit;
    //     qDebug() << i;
    //     intit = intList.erase(intit);
    //   }

    QList<QPersistentModelIndex>::iterator it = toRemove.begin();
    QList<QPersistentModelIndex>::iterator endIt = toRemove.end();

    if (end == 0) {
        endIt = it + 1;
    }

    if (end > 0) {
        endIt = it + (end - start + 1) + 1;
    }
    it += start;

    int i = start;
    while (it != endIt) {
        QPersistentModelIndex idx = *it;
        //     qDebug() << "removing" << idx << idx.data();

        if (m_parents.values().contains(idx)) {
            qint64 key = m_parents.key(idx);
            QPersistentModelIndex value = m_parents.take(key);
            m_pendingRemovalParents.insert(key, value);
            //       qDebug() << "take from parent" << value;
        }
        removeTree(idx);

        ++i;

        m_pendingRemovalChildIndexes[idxToRemove].append(idx);
        //     qDebug() << idxToRemove << idxToRemove.data() << idx << idx.data();

        it = toRemove.erase(it);
        //     qDebug() << (it == endIt);
        //     if (i > end)
        //       break;

        //     if (it == toRemove.end())
        //       break;
    }

    //   qDebug() << "toRemove" << toRemove;

    //   for(int i = start; (i <= end || (end == -1 && toRemove.size() > i)); )
    //   {
    //     qDebug() << i;
    //     QPersistentModelIndex idx = toRemove.takeAt(i);
    //     --end;
    //
    //     qDebug() << "removing" << idx.data();
    //
    //     if (m_parents.values().contains(idx))
    //     {
    //       QPersistentModelIndex bah = m_parents.take(m_parents.key(idx));
    // //       qDebug() << "take from parent" << bah;
    //     }
    //     removeTree(idx);
    //   }
}

void KReparentingProxyModelPrivate::sourceRowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
{
    Q_UNUSED(parent);
    Q_UNUSED(start);
    Q_UNUSED(end);

    Q_Q(KReparentingProxyModel);
    q->beginResetModel();

    return;
#if 0
//   qDebug() << parent << start << end;

    // This is really tricky.
    //
    // We could have something like:
    //
    //   A              A
    //   B              - B
    //   C    ->        - - C
    //   D              D
    //   E              - E
    //
    // And have to remove something like B to D. That would mean a remove signal for B, move E to its grandparent, remove D.

//   QHashIterator<QPersistentModelIndex, QList< QPersistentModelIndex> > it(m_childIndexes);
//   while (it.hasNext())
//   {
//     it.next();
//     qDebug() << it.key() << it.key().data();
//     qDebug() << it.value();
//   }

    const int column = 0;

    QModelIndex firstAffectedIndex = q->mapFromSource(q->sourceModel()->index(start, column, parent));
    QModelIndex lastAffectedIndex = q->mapFromSource(q->sourceModel()->index(end, column, parent));

//   qDebug() << "firstAffectedIndex" << firstAffectedIndex.data();
//   qDebug() << "lastAffectedIndex" << lastAffectedIndex.data();

    QModelIndex proxyParent = firstAffectedIndex.parent();

    Q_ASSERT(firstAffectedIndex.isValid() && lastAffectedIndex.isValid());

    Q_FOREVER {
    if (isDescendantInModel(proxyParent, lastAffectedIndex))
        {
            // They share a common ancestor.

            QModelIndex _parent = lastAffectedIndex.parent();
            QModelIndex lastAffectedAncestor = lastAffectedIndex;
//       qDebug() << "last affected ancestor" << lastAffectedAncestor.data();
            while (_parent != proxyParent) {
                lastAffectedAncestor = _parent;
                _parent = _parent.parent();
            }

            if (q->hasChildren(lastAffectedAncestor)) {
                QModelIndex next = q->index(0, 0, lastAffectedAncestor);

                QModelIndex proxySourceParent = lastAffectedAncestor;
                int startRow = next.row();
                int lastRow = q->rowCount(lastAffectedAncestor) - 1;

                QList<QPersistentModelIndex> &existingSourceIndexes = m_childIndexes[q->mapToSource(proxySourceParent)];
                QList<QPersistentModelIndex> &existingDestinationIndexes = m_childIndexes[q->mapToSource(proxyParent)];

                int destRow = lastAffectedAncestor.row() + 1;

//         qDebug() << "Move from" << lastAffectedAncestor.data() << startRow << lastRow << " To " << proxyParent.data() << destRow;
                bool allowMove = q->beginMoveRows(lastAffectedAncestor, startRow, lastRow, proxyParent, destRow);
                Q_ASSERT(allowMove);

                for (int i = startRow; i <= lastRow; ++i) {
                    QPersistentModelIndex movingIdx = existingSourceIndexes.takeAt(startRow);
                    existingDestinationIndexes.insert(destRow + (i - startRow), movingIdx);
                }

                // TODO: If source was a parent before, it might not be now.
                // dest was already a parent.

                q->endMoveRows();
            }
            PendingRemoval removal;
            removal.index = proxyParent;
            removal.start = firstAffectedIndex.row();
            removal.end = lastAffectedAncestor.row();
            removal.parentId = proxyParent.internalId();
            removal.sourceIndex = q->mapToSource(proxyParent);
            m_pendingRemovals.append(removal);

            removeTree(q->mapToSource(proxyParent), removal.start, removal.end);

//       qDebug() << "beg rem 1";
            q->beginRemoveRows(proxyParent, removal.start, removal.end);

            return;

        } else {
            QModelIndex next = getIndexBelow(firstAffectedIndex);

            proxyParent = next.parent();

            while (isDescendantInModel(proxyParent, next))
            {
                next = getIndexBelow(next);
            }
            QModelIndex _parent = next.parent();
            QModelIndex lastAffectedAncestor = next;

            while (_parent != proxyParent)
            {
                lastAffectedAncestor = _parent;
                _parent = _parent.parent();
            }

            PendingRemoval removal;
            removal.index = proxyParent;
            removal.start = firstAffectedIndex.row();
            removal.end = lastAffectedAncestor.row();
            removal.parentId = proxyParent.internalId();
            removal.sourceIndex = q->mapToSource(proxyParent);
            m_pendingRemovals.append(removal);

            removeTree(q->mapToSource(proxyParent), removal.start, removal.end);

//       qDebug() << "beg rem 1";
            q->beginRemoveRows(proxyParent, removal.start, removal.end);

            proxyParent = next.parent();
        }
    }

// //   qDebug() << proxyParent.data() << lastAffectedIndex.parent().data() << proxyParent << lastAffectedIndex.parent();
//   if (proxyParent == lastAffectedIndex.parent())
//   {
//     PendingRemoval removal;
//     removal.index = proxyParent;
//     removal.start = firstAffectedIndex.row();
//     removal.end = lastAffectedIndex.row();
//     removal.parentId = proxyParent.internalId();
//     removal.sourceIndex = q->mapToSource(proxyParent);
//     m_pendingRemovals.append(removal);
//
//     // Also need to store a removal object for each of the descendants.
//
//     removeTree(q->mapToSource(proxyParent), removal.start, removal.end);
//
// //     qDebug() << "beg rem 1";
//     q->beginRemoveRows(proxyParent, removal.start, removal.end);
//     return;
//   }
//
//   QModelIndex lastParent = lastAffectedIndex.parent();
//   while (lastParent.parent().isValid())
//   {
//     if (lastParent.parent() == proxyParent)
//     {
//       PendingRemoval removal;
//       removal.index = proxyParent;
//       removal.start = firstAffectedIndex.row();
//       removal.end = lastParent.row();
//       removal.parentId = proxyParent.internalId();
//       removal.sourceIndex = q->mapToSource(proxyParent);
//       m_pendingRemovals.append(removal);
//
// //       qDebug() << "beg rem 2";
//       q->beginRemoveRows(proxyParent, removal.start, removal.end);
//       return;
//     }
//     lastParent = lastParent.parent();
//   }
//
//   // Several blocks need to be removed from the proxy model.
//   // Divide and conquer to find them.
//
//   int proxyStart = firstAffectedIndex.row();
//   int proxyEnd = proxyStart + (end - start);
//   int processedUntil = start;
//
//   while (processedUntil <= end)
//   {
//     QModelIndex lastInParent = findLastInParent(proxyParent, proxyStart, proxyEnd);
//     qDebug() << "lastInParent" << lastInParent;
//
//     QModelIndex sourceLast = q->mapToSource(lastInParent);
//     processedUntil = sourceLast.row();
//
//     PendingRemoval removal;
//     removal.index = proxyParent;
//     removal.start = proxyStart;
//     removal.end = lastInParent.row();
//     removal.parentId = proxyParent.internalId();
//     removal.sourceIndex = q->mapToSource(proxyParent);
//     m_pendingRemovals.append(removal);
//
//     qDebug() << "beg rem 3";
//     q->beginRemoveRows(proxyParent, removal.start, removal.end);
//
//     QModelIndex proxyIndexBelow = getIndexBelow(lastInParent, q);
//
//     if (!proxyIndexBelow.isValid())
//       return;
//
//     proxyParent = proxyIndexBelow.parent();
//     proxyStart = proxyIndexBelow.row();
//   }
#endif
}

QModelIndex KReparentingProxyModelPrivate::findLastInParent(QModelIndex parent, int start, int end)
{
    Q_Q(KReparentingProxyModel);

    const int column = 0;

    if (start == end) {
        return q->index(start, column, parent);
    }

    int middle = start + (end - start / 2);

    QModelIndex sourceParent = q->mapToSource(parent);
    QModelIndex middleIndex = q->mapFromSource(q->sourceModel()->index(middle, column, sourceParent));

    if (middleIndex.parent() == parent) {
        return findLastInParent(parent, middle, end);
    } else {
        return findLastInParent(parent, start + ((middle - start) / 2), middle);
    }
}

//   qDebug() << affectedIndex << affectedIndex.data() << proxyParent;
//
//   QHash<QModelIndex, PendingRemoval> pendingRemovals;
//
//   int i = start;
//   while (i <= end)
//   {
//     affectedIndex = affectedIndex.sibling(i, column);
//
// //     affectedIndex = getIndexBelow(affectedIndex, q);
//     if (!affectedIndex.isValid())
//       break;
// //     Q_ASSERT(affectedIndex.isValid());
//
//     if (affectedIndex.parent() != proxyParent)
//     {
//       // affectedIndex.parent() must be left of proxyParent
//
//       PendingRemoval removal;
//       removal.index = proxyParent;
//       removal.start = start;
//       removal.end = i;
//       pendingRemovals.insert(proxyParent, removal);
//
//       Q_EMIT q->rowsAboutToBeRemoved(proxyParent, start, i);
//       proxyParent = affectedIndex.parent();
//
//       end -= (i - start + 1);
//       start = affectedIndex.row();
//       i = start;
//     }
//
//     ++i;
//   }

// Move younger siblings out of the way so that the rows can be removed easily
// No. It's easier to use verifyStructure afterward.

//   // Removing rows in the source model could require sending the children to their grandparents.
//
//   QHash<QModelIndex, QModelIndexList> mappings;
//   recreateMappings(parent, start, end);
//
//   QHashIterator<QModelIndex, QModelIndexList> it(mappings);
//   while (it.hasNext())
//   {
//     it.next();
//     QModelIndexList removedList = it.value();
//     PendingRemoval pendingRemoval;
//     pendingRemoval.index = it.key();
//     pendingRemoval.start = q->mapFromSource(removedList.at(0)).row();
//     pendingRemoval.end = pendingRemoval.start + removedList.size() - 1;
//     m_pendingRemovals.insert(parent, pendingRemoval);
//   }
// }

void KReparentingProxyModelPrivate::handleRemoval(const PendingRemoval &pendingRemoval)
{
    Q_UNUSED(pendingRemoval)
    //   Q_Q(KReparentingProxyModel);
    //   q->beginRemoveRows(pendingRemoval.index, pendingRemoval.start, pendingRemoval.end);
    //   m_childIndexes.remove(pendingRemoval.index);
    //   // Remove stuff from m_parents.
    //   q->endRemoveRows();
}

void KReparentingProxyModelPrivate::sourceRowsRemoved(const QModelIndex &parent, int, int)
{
    return endResetProxy();

    Q_Q(KReparentingProxyModel);

    // loop over pending removals and process each one. Then look after the last one
    // to move displaced rows to where they should be.

    int lastAffectedRow = m_pendingRemovals.last().end;
    QModelIndex lastAffectedIndex = m_pendingRemovals.last().index;

    QMutableVectorIterator<PendingRemoval> it(m_pendingRemovals);

    while (it.hasNext()) {
        PendingRemoval removal = it.next();
        m_pendingRemovalChildIndexes.remove(removal.sourceIndex);
        m_pendingRemovalParents.remove(parent.internalId());
        it.remove();

        Q_EMIT q->endRemoveRows();
    }
    //   qDebug() << "Remove done ##########";

    //   qDebug() << lastAffectedIndex << lastAffectedIndex.data() << lastAffectedRow;

    verifyStructure(lastAffectedIndex, lastAffectedRow - 1);
}

void KReparentingProxyModelPrivate::sourceRowsAboutToBeMoved(const QModelIndex &parent, int start, int end, const QModelIndex &, int)
{
    // This could be several individual moves in the proxy model, or it could be no moves at all.
    // We can get the top indexes of the moved list and move those.
    // because their children won't be moved anywhere different.

    // I could look at the indexes between start and end (proxied could be several blocks), and move them to dest.
    // Then verify structure.
    // This could lead to an illegal move.
    // If we have
    //
    // Source:        Proxy:
    // A              A
    // B              B
    // C              - C
    // D              - D
    // E              E
    //
    // then source can legally move B to between C and D, however, implemented naively the proxymodel would attempt an illegal move.
    // We must first reparent everything below destRow in the proxy to the parent of parent in this case, then perform the move, then
    // verifyStructure.
    //
    // Moving B C and D to below E would be a legal move in the proxy model.
    //
    // Children of moved indexes which are not themselves moved must be first sent to their grandparents.
    // So if B and C were moved in the source model above to below E, D would first be moved to its grandparent, then B would be moved below E,
    // then the structure would need to be verified.
    //
    // Proxy start state:     Intermediate state:   Intermediate or final state:   Possible alternative final state:
    //       A                      A                     A                              A
    //       B                      B                     E                              E
    //       - C                    - C                   D                              - D
    //       - D                    D                     B                              B
    //       E                      E                     - C                            - C

    // So, I could iterate from start to end in proxySourceParent and if the depth goes less than parent, emit a block move, then start again.

    QHash<QModelIndex, QModelIndexList> newMappings = recreateMappings(parent, start, end);
}

void KReparentingProxyModelPrivate::sourceRowsMoved(const QModelIndex &, int, int, const QModelIndex &, int)
{
}

void KReparentingProxyModelPrivate::sourceLayoutAboutToBeChanged()
{
    Q_Q(KReparentingProxyModel);

    q->beginResetModel();
    return;
#if 0

    Q_EMIT q->layoutAboutToBeChanged();

    Q_FOREACH (QPersistentModelIndex proxyPersistentIndex, q->persistentIndexList()) {
        m_proxyIndexes << proxyPersistentIndex;
        m_layoutChangePersistentIndexes << QPersistentModelIndex(q->mapToSource(proxyPersistentIndex));
    }
#endif
}

void KReparentingProxyModelPrivate::sourceLayoutChanged()
{
    endResetProxy();
    return;
#if 0
    Q_Q(KReparentingProxyModel);

    for (int i = 0; i < m_proxyIndexes.size(); ++i) {
        q->changePersistentIndex(m_proxyIndexes.at(i), q->mapFromSource(m_layoutChangePersistentIndexes.at(i)));
    }

    m_layoutChangePersistentIndexes.clear();
    m_proxyIndexes.clear();

    Q_EMIT q->layoutChanged();
#endif
}

void KReparentingProxyModelPrivate::sourceModelAboutToBeReset()
{
    Q_Q(KReparentingProxyModel);
    q->beginResetModel();
}

void KReparentingProxyModelPrivate::endResetProxy()
{
    Q_Q(KReparentingProxyModel);

    m_parents.clear();
    m_childIndexes.clear();
    m_nextId = 0;
    m_pendingInsertions.clear();
    m_pendingRemovals.clear();
    m_pendingRemovalChildIndexes.clear();
    m_pendingRemovalParents.clear();
    //   qDebug() << q->sourceModel()->rowCount();
    QHash<QModelIndex, QModelIndexList> mappings =
        recreateMappings(QModelIndex(), 0, q->sourceModel()->rowCount() - 1, KReparentingProxyModelPrivate::MapDescendants);
    qDebug() << mappings;

    mergeDescendants(mappings, QModelIndex(), 0);
    q->endResetModel();
}

void KReparentingProxyModelPrivate::sourceModelReset()
{
    endResetProxy();
}

void KReparentingProxyModelPrivate::emitDataChangedSignals(const QModelIndex &startIndex, int maxChanged)
{
    Q_Q(KReparentingProxyModel);

    QModelIndex proxyParent = startIndex.parent();

    int numChanged = 1;

    QModelIndex lastAffectedSibling = startIndex;
    QModelIndex proxySibling = getIndexBelow(startIndex, q);

    Q_FOREVER {
        if (proxySibling.parent() != proxyParent || numChanged >= maxChanged) {
            break;
        }

        numChanged++;
        lastAffectedSibling = proxySibling;

        proxySibling = getIndexBelow(proxySibling);
    }

    Q_EMIT q->dataChanged(startIndex, lastAffectedSibling);
    if (numChanged < maxChanged) {
        emitDataChangedSignals(proxySibling, maxChanged - numChanged);
    }
}

void KReparentingProxyModelPrivate::sourceDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
{
    Q_UNUSED(topLeft);
    Q_UNUSED(bottomRight);

    Q_Q(KReparentingProxyModel);

    q->beginResetModel();
    endResetProxy();
    return;
#if 0
    QModelIndex parent = topLeft.parent();
    const int start = topLeft.row();
    const int end = bottomRight.row();
    const int column = 0;
    const int maxChanged = end - start + 1;

    // Create mappings to the end because changing data can affect structure of siblings.
    verifyStructure(parent, start);

    // mapFromSource and emit signals.

    QModelIndex proxyStartIndex = q->mapFromSource(q->sourceModel()->index(start, column, parent));

    emitDataChangedSignals(proxyStartIndex, maxChanged);
#endif
}

Qt::DropActions KReparentingProxyModel::supportedDropActions() const
{
    Q_ASSERT(sourceModel());
    return sourceModel()->supportedDropActions();
}

void KReparentingProxyModel::beginChangeRule()
{
    Q_D(KReparentingProxyModel);
    d->sourceModelAboutToBeReset();
    //   beginResetModel();
    //   d->m_childIndexes.clear();
    //   d->m_layoutChangePersistentIndexes.clear();
    //   d->m_nextId = 1;
    //   d->m_parents.clear();
    //   d->m_pendingInsertions.clear();
    //   d->m_pendingRemovalChildIndexes.clear();
    //   d->m_pendingRemovalParents.clear();
    //   d->m_pendingRemovals.clear();
    //   d->m_proxyIndexes.clear();
}

void KReparentingProxyModel::endChangeRule()
{
    Q_D(KReparentingProxyModel);
    d->endResetProxy();
    return;
}

#include "moc_kreparentingproxymodel.cpp"