Repository navigation
Expand file tree
/
Copy pathqvariantmodel.cpp
More file actions
executable file
·2208 lines (2076 loc) · 94 KB
/
Copy pathqvariantmodel.cpp
File metadata and controls
executable file
·2208 lines (2076 loc) · 94 KB
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
/************************************************************************************************
*
* src: https://github.com/JohnMcLaren/QVariantModelWidget
* license: GPLv3 2007 - https://www.gnu.org/licenses/gpl-3.0.html
* 15.08.2022 🄯 JML
************************************************************************************************/
#include "qvariantmodel.h"
// includes for getListValue/setListValue complite class only
#include <QBitArray>
#include <QDate>
#include <QUrl>
#include <QSize>
#include <QRect>
#include <QLine>
#include <QEasingCurve>
#include <QUuid>
#include <QImage>
#include <QBitmap>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
#include <QCborMap>
#include <QCborArray>
/*
#include <QCursor>
#include <QColorSpace>
#include <QKeySequence>
#include <QPalette>
#include <QIcon>
#include <QMatrix4x4>
#include <QVector2D>
#include <QTextFormat>
#include <QSizePolicy>
//*/
const QVariant QVariantModel::NULLVAR; // for ref from static functions
uint QVariantModel::UpdateEventID;
/******************************************************************************************
*
******************************************************************************************/
quint64
QVariantModelIndex::indexOfKey(const QVariant &node, const QVariant &key)
{
if(isInteger(key))
{
if(isListType(node) && key.toULongLong() < lengthList(node)) // List<any>
return(key.toULongLong());
else
if(isUIntMap(node))
return(_indexOfKey(reinterpret_cast<const QUIntMap &>(node), key.toULongLong())); // UIntMap
else
if(isUIntHash(node))
return(_indexOfKey(reinterpret_cast<const QUIntHash &>(node), key.toULongLong())); // UIntHash
}
else
if(isString(key))
{
if(isMap(node))
return(_indexOfKey(reinterpret_cast<const QVariantMap &>(node), key.toString())); // VariantMap
else
if(isHash(node))
return(_indexOfKey(reinterpret_cast<const QVariantHash &>(node), key.toString())); // VariantHash
}
return(-1);
}
/******************************************************************************************
*
******************************************************************************************/
void
QVariantModelIndex::keyOfIndex(const QVariant &node, const quint64 index, OUT QVariant &key)
{
// List<any>
if(isListType(node))
if(index < lengthList(node))
key =index;
else
return;
// Map
if(isMap(node))
_keyOfIndex(reinterpret_cast<const QVariantMap &>(node), index, key);
// UIntMap
if(isUIntMap(node))
_keyOfIndex(reinterpret_cast<const QUIntMap &>(node), index, key);
// Hash
if(isHash(node))
_keyOfIndex(reinterpret_cast<const QVariantHash &>(node), index, key);
// UIntHash
if(isUIntHash(node))
_keyOfIndex(reinterpret_cast<const QUIntHash &>(node), index, key);
}
/******************************************************************************************
* Model constructors.
******************************************************************************************/
QVariantModel::QVariantModel(QObject *parent)
: QAbstractItemModel{parent}
{
resetNodeIndex();
}
QVariantModel::QVariantModel(const QVariant &rootValue, QObject *parent)
: QAbstractItemModel(parent)
{
resetNodeIndex();
setData(QVMI {}, rootValue);
}
/******************************************************************************************
* The main function used by widgets to retrieve model data.
******************************************************************************************/
QVariant
QVariantModel::data(const QModelIndex &index, int role) const
{
if(!index.internalPointer() || !columns)
return QVariant();
// if(role == Qt::TextAlignmentRole && Mode == Table)
// return(AlignmentTable);
if(role != Qt::DisplayRole && role != Qt::EditRole)
return QVariant(); // [IMPORTANT]
QVariant key;
const QVariant &value =data(data(((NodeIndex *)index.internalPointer())->index), index.row(), &key);
if(Mode == Tree) /*** Tree mode ***/
{
// Switch the return value depending on the number of columns of the tree view.
switch(columns)
{
case 1:
return(QString("%1: %2").arg(formatKeyName(key, isNode(value)).toString()).arg(value.toString()));
case 2:
case 3:
switch(index.column()) // current cell column (of tree)
{
case 0:
return(formatKeyName(key, isNode(value)));
case 2:
return(value.typeName());
}
}
}
else /*** Table mode ***/
if(Mode == Table)
{
const QVariant &valueCell =data(value, index.column(), &key);
if(isNode(valueCell))
return(formatKeyName(key, true));
else
return(valueCell);
}
//return(QString("[%1,%2] 0x%3").arg(index.row()).arg(index.column()).arg((qlonglong)index.internalPointer(), 8, 16)); // DEBUG
return(value);
}
/******************************************************************************************
*
******************************************************************************************/
const QVariant &
QVariantModel::data(const QVariant &node, const uint position, OUT QVariant * const pKey) const
{
if(position < lengthNode(node))
if(isListType(node))
{
if(pKey)
*pKey =position;
if(isList(node)) // List
return(reinterpret_cast<const QVariantList &>(node)[position]);
else // List<any>
TEMPVAR =getListValue(node, position); // using temp QVariant for ref to 'QVariant &' from 'any' value
return(TEMPVAR); // This is possible because QTreeView makes a copy of the data for itself.
}
else
if(isMapType(node)) // Map
{
const auto &it=std::next(reinterpret_cast<const QVariantMap &>(node).begin(), position);
if(pKey)
if(isMap(node))
*pKey =it.key();
else
*pKey =reinterpret_cast<const QUIntMap::ConstIterator &>(it).key();
return(*it);
}
else
if(isHashType(node)) // Hash
{
const auto &it =std::next(reinterpret_cast<const QVariantHash &>(node).begin(), position);
if(pKey)
if(isHash(node))
*pKey =it.key();
else
*pKey =reinterpret_cast<const QUIntHash::ConstIterator &>(it).key();
return(*it);
}
return(NULLVAR);
}
/*********************************************************************************************************
* If the value of QList<any> is returned, the function will return a reference to the node QList<any>
* and the 'key' of the value in this node, for subsequent processing of the result by the caller.
* This function can calculate the ModelIndex while the VariantIndex is unwound,
* just specify a valid receiver address 'pModelIndex'.
**********************************************************************************************************/
const QVariant &
QVariantModel::data(const QVariant &node, const QVariantModelIndex &index, OUT QVariant * const pKey, const bool bReturnParent, OUT QModelIndex * const pModelIndex) const
{
QVariant *pNode =const_cast<QVariant *>(&node);
int c =(-1), rowTree =0, rowTable =0;
// while the index was not completely unwound.
while(++c < (index.length() - (int)bReturnParent))
{
// if there is a ModellIndex calculation request
if(pModelIndex)
if((c + 1) == index.length()) // if 'c' is last key of index
rowTree =QVMI::indexOfKey(*pNode, index[c]);
else
if((c + 2) == index.length()) // if 'c' is penultimate (parent) key of index
rowTable =QVMI::indexOfKey(*pNode, index[c]);
// Check 'index' key must be a integer or string
if(isInteger(index[c]))
// Check if the 'node' is a container of a known type.
/******************************************************************************************
* [QT-NOTE] Here we cannot take constant references to nested nodes.
* Otherwise, due to QVariant optimization, when assigning such a reference as a new value,
* it will copy the reference to the object instead of copying it.
******************************************************************************************/
if(isList(*pNode)) // QVariantList
if(index[c].toULongLong() < lengthList(*pNode))
pNode = &reinterpret_cast<QVariantList &>(*pNode)[index[c].toULongLong()];
else
return(NULLVAR); // index out of range list.
else
if(isListType(*pNode)) // QList<any> cannot contain nodes - exit in any case // QList<any>
if(pKey)
break; // stop unwind index and go return ref to parent list with child key
else
return(NULLVAR); // Cannot return a reference to a list value because there is nowhere to save the child key name.
else
if(isUIntMap(*pNode)) // QUIntMap
pNode = &_valueOfNode(reinterpret_cast<QUIntMap &>(*pNode), index[c].toULongLong());
else
if(isUIntHash(*pNode)) // QUIntHash
pNode = &_valueOfNode(reinterpret_cast<QUIntHash &>(*pNode), index[c].toULongLong());
else
return(NULLVAR); // The 'node' is not a known type container for integer key
else
if(isString(index[c])) // if 'node' is Map or Hash
if(isMap(*pNode)) // QVariantMap
pNode = &_valueOfNode(reinterpret_cast<QVariantMap &>(*pNode), reinterpret_cast<const QString &>(index[c]));
else
if(isHash(*pNode)) // QVariantHash
pNode = &_valueOfNode(reinterpret_cast<QVariantHash &>(*pNode), reinterpret_cast<const QString &>(index[c]));
else
return(NULLVAR); // The 'node' is not a known type container for string key.
else // wrong key type
return(NULLVAR);
// check ..
if(!isValid(*pNode))
return(NULLVAR); // wrong index / the key does not exist in the node
}
if(pKey) // if(bReturnParent)
if((c + 1) == index.length()) // The caller is ready to accept parent node + key value, and index is unwound.
*pKey =index[c]; // [NOTE] the list key 'index[c]' is not checked for existence
else
*pKey ={};
if(pModelIndex && index.size())
if(bReturnParent)
*pModelIndex =createIndex(rowTable, rowTree, (void *)getParentNodeIndex(index)->parent);
else
*pModelIndex =createIndex(rowTree, 0, (void *)getParentNodeIndex(index));
return(*pNode); // OK. The index is unwound.
}
/******************************************************************************************
* This overridden QAbstractItemModel method is only used by the widget.
* It is this method that 'QAbstractItemModel' will be called when the manual editing of the value is finished.
******************************************************************************************/
bool
QVariantModel::setData(const QModelIndex &modelIndex, const QVariant &value, int role)
{
if(!modelIndex.internalPointer())
return(false);
// convert ModellIndex to VariantModelIndex.
QVariantModelIndex index =((NodeIndex *)modelIndex.internalPointer())->index;
const QVariant &parentNode =data(index);
if(!isValid(parentNode))
return(false);
index.appendKeyByPosition(parentNode, modelIndex.row());
if(Mode == Table)
index.appendKeyByPosition(data(index), modelIndex.column());
bool AllowChanges =true;
emit dataWillBeChanged(index, value, AllowChanges);
if(!AllowChanges)
return(false);
return(setData(index, value));
}
/******************************************************************************************
* This is the main method of modifying the model data.
* The method only changes the data by the existing index (the root index '{}' always exists).
******************************************************************************************/
bool
QVariantModel::setData(const QVariantModelIndex &index, const QVariant &value)
{
QVariant key, &nodeValue =const_cast<QVariant &>(data(index, &key, false)); // get reference to data value of node
if(!isValid(nodeValue)) // 'index' not valid
return(false);
NodeIndexAction action =NoAction;
if(!key.isValid()) // 'nodeValue' is normal node or value (not node)
{
if(isNode(nodeValue) ^ isNode(value)) // 'node' ='value' OR 'value' ='node'
if(isNode(nodeValue)) // 'node' ='value'
if(index.size())
action =NodeRemoved;
else
return(false); // A root can only be a Node but not a normal Value.
else // 'value' ='node'
action =NodeInserted;
else
if(isNode(nodeValue)) // 'node' ='node'
action =(NodeIndexAction)(NodeRemoved | NodeInserted);
nodeValue =value; // copy data by ref: 'value/node' ='value/node'
}
else // QList<any> special case
if(!setListValue(nodeValue, key.toUInt(), value))
return(false);
updateView(index, action); // NodeChanged =0 - in all cases setData, because the number of child elements in the node does not change.
return(true);
}
/******************************************************************************************
*
*******************************************************************************************/
bool
QVariantModel::insertData(const QVariantModelIndex &index, const QVariant &value)
{
if(!index.size())
return(false); // It is not possible to insert one more root node.
QVariant key, &parentNode =const_cast<QVariant &>(data(index, &key, true));
if(!insertKey(parentNode, key, value))
return(false);
updateView(index, (NodeIndexAction)(NodeInserted | NodeChanged));
return(true);
}
/******************************************************************************************
* removeData
* Deletes data by VarIndex along with the key and fix the list of nodes.
*******************************************************************************************/
bool
QVariantModel::removeData(const QVariantModelIndex &index)
{
if(!index.size())
return(false); // It is not possible to remove the root node.
QVariant key, &parentNode =const_cast<QVariant &>(data(index, &key, true));
if(!removeKey(parentNode, key))
return(false);
updateView(index, (NodeIndexAction)(NodeRemoved | NodeChanged));
return(true);
}
/******************************************************************************************
* Cross-connection of change/update signals between models.
* Models must be connected to the root of the same data. Otherwise the VariantModelIndexes in models will be different.
*******************************************************************************************/
bool
QVariantModel::connectToChanges(const QVariantModel *anotherModel) const
{
if(anotherModel == this || !RootDataSource || RootDataSource != anotherModel->RootDataSource)
return(false);
// connect to signal of another model
return(connect(anotherModel,
SIGNAL(dataChanged(const QVariantModelIndex &, const NodeIndexAction)),
SLOT(changeNodeIndex(const QVariantModelIndex &, const NodeIndexAction)),
Qt::UniqueConnection) &&
// connect another model to 'this' model signal
connect(this,
SIGNAL(dataChanged(const QVariantModelIndex &, const NodeIndexAction)),
anotherModel,
SLOT(changeNodeIndex(const QVariantModelIndex &, const NodeIndexAction)),
Qt::UniqueConnection));
}
/******************************************************************************************
* Insert Key of Node
* Inserts a new key into a node of any type with data value.
******************************************************************************************/
bool
QVariantModel::insertKey(QVariant &node, const QVariant &key, const QVariant &value)
{
if(node.isValid())
if(isInteger(key))
// Check if the 'node' is a container of a known type.
if(isList(node)) // QVariantList
if(key.toUInt() <= lengthList(node))
reinterpret_cast<QVariantList &>(node).insert(key.toUInt(), value);
else
return(false); // index out of range list.
else
if(isListType(node)) // // QList<any>
return(insertListValue(node, key.toUInt(), value));
else
if(isUIntMap(node)) // QUIntMap
reinterpret_cast<QUIntMap &>(node)[key.toULongLong()] =value;
else
if(isUIntHash(node)) // QUIntHash
reinterpret_cast<QUIntHash &>(node)[key.toULongLong()] =value;
else
return(false); // The 'node' is not a known type container for integer key
else
if(isString(key)) // if 'node' is Map or Hash
if(isMap(node)) // QVariantMap
reinterpret_cast<QVariantMap &>(node)[key.toString()] =value;
else
if(isHash(node)) // QVariantHash
reinterpret_cast<QVariantHash &>(node)[key.toString()] =value;
else
return(false); // The 'node' is not a known type container for string key.
else // wrong index value type
return(false);
else // invalid variant type
return(false);
return(true);
}
/******************************************************************************************
* Remove Key of Node
* Removes an existing key from a node of any type, along with the data.
******************************************************************************************/
bool
QVariantModel::removeKey(QVariant &node, const QVariant &key)
{
if(node.isValid())
if(isInteger(key))
// Check if the 'node' is a container of a known type.
if(isList(node)) // QVariantList
if(key.toULongLong() < lengthList(node))
reinterpret_cast<QVariantList &>(node).removeAt(key.toUInt()); // squeeze() ??
else
return(false); // index out of range list.
else
if(isListType(node)) // QList<any> cannot contain nodes - exit in any case // QList<any>
return(removeListValue(node, key.toUInt()));
else
if(isUIntMap(node)) // QUIntMap
reinterpret_cast<QUIntMap &>(node).remove(key.toULongLong());
else
if(isUIntHash(node)) // QUIntHash
reinterpret_cast<QUIntHash &>(node).remove(key.toULongLong());
else
return(false); // The 'node' is not a known type container for integer key
else
if(isString(key)) // if 'node' is Map or Hash
if(isMap(node)) // QVariantMap
reinterpret_cast<QVariantMap &>(node).remove(key.toString());
else
if(isHash(node)) // QVariantHash
reinterpret_cast<QVariantHash &>(node).remove(key.toString());
else
return(false); // The 'node' is not a known type container for string key.
else // wrong index type
return(false);
else // invalid variant type
return(false);
return(true);
}
/******************************************************************************************
*
******************************************************************************************/
bool
QVariantModel::setDataSource(QVariant &dataRoot) // set data root
{
resetNodeIndex();
_data.clear();
RootDataSource =static_cast<QVariant *>(&dataRoot);
return(createNodeIndex({}, dataRoot));
}
/******************************************************************************************
* Parses data and inserts node structures 'NodeIndex' into the list (Nodes).
* Return absolut position index of list
******************************************************************************************/
int
QVariantModel::createNodeIndex(const QVariantModelIndex &index, const QVariant &data, OUT int * const pNodeIndexPosition, int position, const NodeIndex *pParentNodeIndex)
{
QVariantModelIndex _index =index;
if(!position)
position =getNewNodeIndexPosition(index, &pParentNodeIndex);
if(pNodeIndexPosition)
*pNodeIndexPosition =position;
OldNodeTag +=2;
NewNodeTag =(OldNodeTag + 1);
return(createNodeIndex(_index, data, position - 1, pParentNodeIndex));
}
// *** recursive ***
int
QVariantModel::createNodeIndex(QVariantModelIndex &index, const QVariant &data, int position, const NodeIndex *pParentNodeIndex)
{
if(isNode(data)) // all allowable types of Nodes.
{
while(position < Nodes.size() && index > Nodes[position].index) // skip garbage nodes if exist
++position;
// There, position <= Nodes.size() AND index <= Nodes[position].index.
if(position == Nodes.size() || index < Nodes[position].index) // new position OR
Nodes.insert(position, NodeIndex {pParentNodeIndex, index, false, NewNodeTag});
else
Nodes[position].ExistenceTag =OldNodeTag; // Node already exists.
pParentNodeIndex = &Nodes[position];
++position;
index.push_back({}); // create last key node index
if(isList(data)) // VariantList [NOTE] QList<any> cannot contain Nodes
{
const auto &list =reinterpret_cast<const QVariantList &>(data);
const_cast<NodeIndex *>(pParentNodeIndex)->IsList =true;
for(auto it =list.cbegin(); it != list.cend(); ++it)
{
index[index.size() - 1] =std::distance(list.cbegin(), it); // set last key node index
position =createNodeIndex(index, *it, position, pParentNodeIndex);
}
}
else // Map & UIntMap
if(isMap(data))
{
const auto &map =reinterpret_cast<const QVariantMap &>(data);
for(auto it =map.cbegin(); it != map.cend(); ++it)
{
index[index.size() - 1] =it.key();
position =createNodeIndex(index, *it, position, pParentNodeIndex);
}
}
else
if(isUIntMap(data))
{
const auto &umap =reinterpret_cast<const QUIntMap &>(data);
for(auto it =umap.cbegin(); it != umap.cend(); ++it)
{
index[index.size() - 1] =it.key();
position =createNodeIndex(index, *it, position, pParentNodeIndex);
}
}
else // Hash & UIntHash
if(isHash(data))
{
const auto &hash =reinterpret_cast<const QVariantHash &>(data);
for(auto it =hash.cbegin(); it != hash.cend(); ++it)
{
index[index.size() - 1] =it.key();
position =createNodeIndex(index, *it, position, pParentNodeIndex);
}
}
else
if(isUIntHash(data))
{
const auto &uhash =reinterpret_cast<const QUIntHash &>(data);
for(auto it =uhash.cbegin(); it != uhash.cend(); ++it)
{
index[index.size() - 1] =it.key();
position =createNodeIndex(index, *it, position, pParentNodeIndex);
}
}
index.pop_back(); // remove last key node index
}
return(position);
}
/******************************************************************************************
* Inserts node structures 'NodeIndex' to the list.
******************************************************************************************/
int
QVariantModel::insertNodeIndex(const QVariantModelIndex &index, const QVariant &data, const bool bFixChildList)
{
const NodeIndex *parent;
int position =getNewNodeIndexPosition(index, &parent);
if(parent) // skip if null-parent
if(parent->IsList && bFixChildList) // if parent list AND need fix indexes 'NodeIndex' after insert child node 'index'
{
auto it =Nodes.begin() + (position - 1);
while(it != Nodes.end() && it->index.size() >= index.size())
{
it->index[index.size() - 1] =(it->index[index.size() - 1].toInt() + 1); // fix current list indexes for 1 inserted node
++it;
}
}
else
if(Nodes[position - 1].index == index) // Node has a non enumerated key type (map/hash) OR the fix of enumerated keys is not explicitly allowed by 'bFixChildList'
return(0); // but the key to be inserted already exists.
return(createNodeIndex(index, data, nullptr, position, parent) - position); // creates new NodeIndex structs
}
/******************************************************************************************
* Removes node structures 'NodeIndex' from the list.
* Returns the total number of NodIndexes removed (parent 'index' + childs).
******************************************************************************************/
int
QVariantModel::removeNodeIndex(const QVariantModelIndex &index, const bool bFixChildList)
{
if(!index.size()) // if root index
return(0); // It is not possible to remove the root node.
const auto &begin =std::lower_bound(Nodes.begin(),
Nodes.end(),
index,
[](const NodeIndex &_node, const QVariantModelIndex &_index) { return(_node.index < _index); });
// if node found
if(begin != Nodes.end() && begin->index.size() == index.size())
{
auto it =begin;
int childs =0; // child NodeIndexes
int LastKeyPosition =(index.size() - 1); // for fix enumerated keys
bool bParentIslist =it->parent->IsList; // for checking the enumeration of a node's keys
// find end of delete index of node
while(++it != Nodes.end() && index.isParentOf(it->index))
++childs;
// delete all 'NodeIndex' where 'index' is parent node (include 'index')
it =Nodes.erase(begin, it); // squeeze() ??
if(bFixChildList && bParentIslist) // if parent list need fix indexes after delete child node 'index'
while(it != Nodes.end() && it->index.size() > LastKeyPosition)
{
it->index[LastKeyPosition] =(it->index[LastKeyPosition].toInt() - 1); // fix current list indexes for 1 deleted node
++it;
}
return(childs + 1);
}
return(0);
}
/******************************************************************************************
*
******************************************************************************************/
void
QVariantModel::changeNodeIndex(const QVariantModelIndex &index, const NodeIndexAction action)
{
if(LastUpdateEventID == UpdateEventID) // If event has already been processed
return; // exit
LastUpdateEventID =UpdateEventID;
QModelIndex topLeft; // full update view by default
if(action) // The NodeIndex list will change only if there have been actions with nodes.
{
QVariant key;
bool bIsTableCell =(Mode == Table) ? (index.size() - RootViewIndex.size() == 2 ? true : false) : false;
bool bIsTableRow =(Mode == Table) ? (index.size() - RootViewIndex.size() == 1 ? true : false) : false;
bool bIsTable =(Mode == Table) ? (index.size() - RootViewIndex.size() == 0 ? true : false) : false;
bool bIsNodeToNode =((action & NodeRemoved) && (action & NodeInserted));
bool bRepaintRows =(Mode == Tree) || bIsTableRow;
const QVariant &nodeValue =convertVMItoMI(index, &topLeft, &key, bIsTableCell);
// NodeIndex actions
// Table cell & Node to Node action - special cases
if(bIsNodeToNode)
{
if(index.size()) // if not root index
{
int position;
createNodeIndex(index, nodeValue, &position); // from start 'position' to end of Node 'index'
if(bRepaintRows)
{
beginInsertRows(topLeft.parent(), 0, -1); // Updating the view after inserting new nodes
endInsertRows();
}
// Well, now let's delete the nodes that no longer exist in the new data.
while(position < Nodes.size() && index.isParentOf(Nodes[position].index)) // walk on all childs of 'index'
if(Nodes[position].ExistenceTag != NewNodeTag && Nodes[position].ExistenceTag != OldNodeTag) // If a node is not present in the new data.
{
if(bIsTable || bRepaintRows)
{ // Notify widget about changes
removePersistentModelIndexesFor(Nodes[position].index);
if(bRepaintRows)
{// [QT-NOTE] No other methods(insert*/remove*) change the appearance of the widget.
beginRemoveRows(createIndex(0, 0, (void *)Nodes[position].parent).parent(), 0, -1);
endRemoveRows();
}
}
removeNodeIndex(Nodes[position].index, false);
}
else
++position;
}
else // Full replacement content of the root node.
{
resetNodeIndex();
createNodeIndex(index, nodeValue);
}
topLeft =QModelIndex(); // full update view
}
else
if(action & NodeRemoved)
{
if(bIsTable || bRepaintRows)
{
removePersistentModelIndexesFor(index);
if(bRepaintRows)
{
beginRemoveRows(topLeft.parent(), topLeft.row(), topLeft.row());
endRemoveRows();
}
}
removeNodeIndex(index, action & NodeChanged);
}
else
if(action & NodeInserted)
{
insertNodeIndex(index, nodeValue, action & NodeChanged);
if(bRepaintRows)
{
beginInsertRows(topLeft.parent(), topLeft.row(), topLeft.row());
endInsertRows();
}
}
}
emit QAbstractItemModel::dataChanged(topLeft, topLeft); // signal to widget
emit dataChanged(index, action); // signal to another model
}
/******************************************************************************************
* Standard QAbstractItemModel methods *
******************************************************************************************/
// Return child QModelIndex by [row, column] position
QModelIndex
QVariantModel::index(int row, int column, const QModelIndex &parentIndex) const
{
if(parentIndex.isValid() && parentIndex.column() != 0) // ??
return(QModelIndex());
if(!parentIndex.internalPointer() && Nodes.size())
return(createIndex(row, column, (void *)&Nodes[0]));
if(parentIndex.internalPointer())
{
const NodeIndex *nodeIndex =getNodeIndex(((NodeIndex *)parentIndex.internalPointer())->index, parentIndex.row()); // child NodeIndex
if(nodeIndex)
return(createIndex(row, column, (void *)nodeIndex)); // create ModelIndex for child
}
return(QModelIndex());
}
//-------------------------------------------------------------------------------------
QModelIndex
QVariantModel::parent(const QModelIndex &index) const
{
if(index.internalPointer())
{
NodeIndex nodeIndex = *(NodeIndex *)index.internalPointer();
if(nodeIndex.index.size())
{
QVariant key =nodeIndex.index.takeLast(); // pop last key (child) of index and make parent index
return(createIndex(QVMI::indexOfKey(data(nodeIndex.index), key), 0, (void *)nodeIndex.parent)); // [NOTE] column always 0
}
}
return(QModelIndex());
}
//...............................................................................................
int
QVariantModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
if(!columns)
emit const_cast<QVM *>(this)->updateColumns(); // signal to widget: columns =0
return(columns);
}
//...............................................................................................
int
QVariantModel::rowCount(const QModelIndex &parent) const
{
if(parent.isValid() && parent.column() > 0) // Headers ?
return(0);
if(!parent.isValid()) // root - dataSource
return(RootDataSource ? lengthNode(*RootDataSource) : 0);
if(parent.internalPointer())
return(lengthNode(data(data(((NodeIndex *)parent.internalPointer())->index), parent.row()))); // data (node, row)
return(0);
}
//...............................................................................................
Qt::ItemFlags
QVariantModel::flags(const QModelIndex &index) const
{
if(!index.isValid())
return(Qt::NoItemFlags);
if(Mode == Tree)
return(QAbstractItemModel::flags(index) | (ReadOnly ? Qt::NoItemFlags : (index.column() == (bool)(columns - 1) ? Qt::ItemIsEditable : Qt::NoItemFlags)));
/* Table */
return(QAbstractItemModel::flags(index) | (ReadOnly ? Qt::NoItemFlags : Qt::ItemIsEditable));
}
/*******************************************************************************************************
* Hardcore
* The fastest 'switch' possible.
* The '&&' operator is only available in the GCC compiler.
*
*******************************************************************************************************/
uint QVariantModel::MinUserTypeId =0;
void *QVariantModel::GetList$JumpTable[QMetaType::HighestInternalId + 1] ={},
*QVariantModel::SetList$JumpTable[QMetaType::HighestInternalId + 1] ={},
*QVariantModel::RemoveList$JumpTable[QMetaType::HighestInternalId + 1] ={},
*QVariantModel::InsertList$JumpTable[QMetaType::HighestInternalId + 1] ={};
//................................................................
const QVariant
QVariantModel::getListValue(const QVariant &list, const uint index)
{
if(!GetList$JumpTable[0])
goto init;
begin: // User types
if(isListType(list) && index < lengthList(list))
if(list.type() == QVariant::UserType && list.type() <= (MinUserTypeId + QMetaType::HighestInternalId))
goto *(GetList$JumpTable[list.userType() - MinUserTypeId]);
else // standard types
if(list.type() == QVariant::StringList)
return(reinterpret_cast<const QStringList &>(list)[index]);
Unknown:
return(QVariant());
QListBool:
return(reinterpret_cast<const QList<bool> &>(list)[index]);
QListInt:
return(reinterpret_cast<const QList<int> &>(list)[index]);
QListUInt:
return(reinterpret_cast<const QList<uint> &>(list)[index]);
QListLongLong:
return(reinterpret_cast<const QList<qlonglong> &>(list)[index]);
QListULongLong:
return(reinterpret_cast<const QList<qulonglong> &>(list)[index]);
QListDouble:
return(reinterpret_cast<const QList<double> &>(list)[index]);
QListQChar:
return(reinterpret_cast<const QList<QChar> &>(list)[index]);
QListVariantMap:
return(reinterpret_cast<const QList<QVariantMap> &>(list)[index]);
//QListVariantList: not UserType
// return(reinterpret_cast<const QList<QVariantList> &>(list)[index]);
QListString:
return(reinterpret_cast<const QList<QString> &>(list)[index]);
QListStringList:
return(reinterpret_cast<const QList<QStringList> &>(list)[index]);
//QListByteArray: not UserType
// return(reinterpret_cast<const QList<QByteArray> &>(list)[index]);
QListBitArray:
return(reinterpret_cast<const QList<QBitArray> &>(list)[index]);
QListDate:
return(reinterpret_cast<const QList<QDate> &>(list)[index]);
QListTime:
return(reinterpret_cast<const QList<QTime> &>(list)[index]);
QListDateTime:
return(reinterpret_cast<const QList<QDateTime> &>(list)[index]);
QListUrl:
return(reinterpret_cast<const QList<QUrl> &>(list)[index]);
QListLocale:
return(reinterpret_cast<const QList<QLocale> &>(list)[index]);
QListRect:
return(reinterpret_cast<const QList<QRect> &>(list)[index]);
QListRectF:
return(reinterpret_cast<const QList<QRectF> &>(list)[index]);
QListSize:
return(reinterpret_cast<const QList<QSize> &>(list)[index]);
QListSizeF:
return(reinterpret_cast<const QList<QSizeF> &>(list)[index]);
QListLine:
return(reinterpret_cast<const QList<QLine> &>(list)[index]);
QListLineF:
return(reinterpret_cast<const QList<QLineF> &>(list)[index]);
QListPoint:
return(reinterpret_cast<const QList<QPoint> &>(list)[index]);
QListPointF:
return(reinterpret_cast<const QList<QPointF> &>(list)[index]);
QListRegExp:
return(reinterpret_cast<const QList<QREGEXP> &>(list)[index]);
QListVariantHash:
return(reinterpret_cast<const QList<QVariantHash> &>(list)[index]);
QListEasingCurve:
return(reinterpret_cast<const QList<QEasingCurve> &>(list)[index]);
QListUuid:
return(reinterpret_cast<const QList<QUuid> &>(list)[index]);
QListVoidStar:
return(reinterpret_cast<const QList<const void *> &>(list)[index]);
QListLong:
return(QVariant::fromValue(reinterpret_cast<const QList<long> &>(list)[index]));
QListShort:
return(reinterpret_cast<const QList<short> &>(list)[index]);
QListChar:
return(reinterpret_cast<const QList<char> &>(list)[index]);
QListULong:
return(QVariant::fromValue(reinterpret_cast<const QList<ulong> &>(list)[index]));
QListUShort:
return(reinterpret_cast<const QList<ushort> &>(list)[index]);
QListUChar:
return(reinterpret_cast<const QList<uchar> &>(list)[index]);
QListFloat:
return(reinterpret_cast<const QList<float> &>(list)[index]);
QListObjectStar:
return(reinterpret_cast<const QList<const QObject *> &>(list)[index]);
QListSChar: // what is ??
return(reinterpret_cast<const QList<char> &>(list)[index]);
//QListVariant: not UserType
// return(reinterpret_cast<const QList<QVariant> &>(list)[index]);
QListModelIndex:
return(reinterpret_cast<const QList<QModelIndex> &>(list)[index]);
//QListVoid: empty result
// return(QVariant());
QListRegularExpression:
return(reinterpret_cast<const QList<QRegularExpression> &>(list)[index]);
QListJsonValue:
return(reinterpret_cast<const QList<QJsonValue> &>(list)[index]);
QListJsonObject:
return(reinterpret_cast<const QList<QJsonObject> &>(list)[index]);
QListJsonArray:
return(reinterpret_cast<const QList<QJsonArray> &>(list)[index]);
QListJsonDocument:
return(reinterpret_cast<const QList<QJsonDocument> &>(list)[index]);
QListByteArrayList:
return(QVariant::fromValue(reinterpret_cast<const QList<QByteArrayList> &>(list)[index]));
QListPersistentModelIndex:
return(reinterpret_cast<const QList<QPersistentModelIndex> &>(list)[index]);
//QListNullptr: empty result
// return(QVariant());
QListCborSimpleType:
return(reinterpret_cast<const QList<quint8> &>(list)[index]);
QListCborValue:
return(QVariant::fromValue(reinterpret_cast<const QList<QCborValue> &>(list)[index]));
QListCborArray:
return(QVariant::fromValue(reinterpret_cast<const QList<QCborArray> &>(list)[index]));
QListCborMap:
return(QVariant::fromValue(reinterpret_cast<const QList<QCborMap> &>(list)[index]));
// don't think it will ever come in handy.
/*
QListFont:
return(QVariant::fromValue(reinterpret_cast<const QList<QFont> &>(list)[index]));
QListPixmap:
return(QVariant::fromValue(reinterpret_cast<const QList<QPixmap> &>(list)[index]));