-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecordmodel.cpp
More file actions
947 lines (707 loc) · 22.1 KB
/
recordmodel.cpp
File metadata and controls
947 lines (707 loc) · 22.1 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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Firebird database editor *
* Copyright (C) 2016 Łukasz "Kuszki" Dróżdż lukasz.kuszki@gmail.com *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see http://www.gnu.org/licenses/. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "recordmodel.hpp"
RecordModel::RecordObject::RecordObject(int Uid, const QHash<int, QVariant>& Fields)
: Attributes(Fields), Index(Uid) {}
RecordModel::RecordObject::~RecordObject(void) {}
QHash<int, QVariant> RecordModel::RecordObject::getFields(void) const
{
return Attributes;
}
void RecordModel::RecordObject::setFields(const QHash<int, QVariant>& Fields, bool Replace)
{
if (!Replace) for (auto i = Fields.constBegin(); i != Fields.constEnd(); ++i)
{
Attributes[i.key()] = i.value();
}
else Attributes = Fields;
}
void RecordModel::RecordObject::setField(int Role, const QVariant& Value)
{
Attributes[Role] = Value;
}
QVariant RecordModel::RecordObject::getField(int Role) const
{
return Attributes.value(Role);
}
bool RecordModel::RecordObject::contain(const QHash<int, QVariant>& Fields) const
{
for (auto i = Fields.constBegin(); i != Fields.constEnd(); ++i)
{
if (!Attributes.contains(i.key()) || Attributes[i.key()] != i.value()) return false;
}
return true;
}
int RecordModel::RecordObject::getUid(void) const
{
return Index;
}
void RecordModel::RecordObject::setUid(int Uid)
{
Index = Uid;
}
RecordModel::GroupObject::GroupObject(int Level, const QHash<int, QVariant>& Fields)
: RecordObject(-1, Fields), Root(nullptr), Column(Level) {}
RecordModel::GroupObject::~GroupObject(void)
{
for (auto Child : Childs) delete dynamic_cast<GroupObject*>(Child);
}
void RecordModel::GroupObject::addChild(RecordModel::RecordObject* Object)
{
if (auto Group = dynamic_cast<GroupObject*>(Object))
{
Group->Root = this;
}
Childs.append(Object);
}
bool RecordModel::GroupObject::removeChild(const QHash<int, QVariant>& Fields)
{
for (auto& Child : Childs) if (Child->contain(Fields))
{
delete Child; Child = nullptr;
}
return Childs.removeAll(nullptr);
}
bool RecordModel::GroupObject::removeChild(RecordModel::RecordObject* Object)
{
if (Childs.removeOne(Object))
{
delete Object;
return true;
}
else return false;
}
bool RecordModel::GroupObject::removeChild(int Index)
{
if (Childs.size() > Index)
{
delete Childs.takeAt(Index);
return true;
}
else return false;
}
RecordModel::RecordObject* RecordModel::GroupObject::takeChild(const QHash<int, QVariant>& Fields)
{
for (auto& Child : Childs) if (Child->contain(Fields)) return Child; return nullptr;
}
RecordModel::RecordObject* RecordModel::GroupObject::takeChild(RecordModel::RecordObject* Object)
{
if (Childs.contains(Object))
{
Childs.removeOne(Object); return Object;
}
else return nullptr;
}
RecordModel::RecordObject* RecordModel::GroupObject::takeChild(int Index)
{
if (Childs.size() > Index) return Childs.takeAt(Index); else return nullptr;
}
QVector<RecordModel::GroupObject*> RecordModel::GroupObject::allGroups(void)
{
QVector<GroupObject*> List;
for (const auto Child : Childs) if (auto C = dynamic_cast<GroupObject*>(Child))
{
List.append(C); List.append(C->allGroups());
}
return List;
}
QVector<RecordModel::RecordObject*> RecordModel::GroupObject::getChilds(void)
{
return Childs;
}
QSet<int> RecordModel::GroupObject::allUids(void)
{
QSet<int> List;
for (const auto Child : Childs)
if (auto C = dynamic_cast<GroupObject*>(Child))
{
List |= C->allUids();
}
else
{
List.insert(Child->getUid());
}
return List;
}
RecordModel::GroupObject* RecordModel::GroupObject::getParent(void) const
{
return Root;
}
RecordModel::RecordObject* RecordModel::GroupObject::getChild(int Index)
{
return Childs.value(Index);
}
QVariant RecordModel::GroupObject::getData(void) const
{
return getField(Column);
}
int RecordModel::GroupObject::childrenCount(void) const
{
return Childs.size();
}
bool RecordModel::GroupObject::hasChids(void) const
{
return !Childs.isEmpty();
}
int RecordModel::GroupObject::getIndex(RecordModel::RecordObject* Child) const
{
return Childs.indexOf(Child);
}
int RecordModel::GroupObject::getColumn(void) const
{
return Column;
}
int RecordModel::GroupObject::getIndex(void) const
{
if (Root) return Root->Childs.indexOf((RecordObject*) this); else return 0;
}
int RecordModel::GroupObject::childIndex(RecordObject* Object) const
{
return Childs.indexOf(Object);
}
void RecordModel::GroupObject::sortChilds(const RecordModel::SortObject& Functor, QFutureSynchronizer<void>& Synchronizer)
{
static auto Sort = std::stable_sort<QVector<RecordObject*>::iterator, SortObject>;
for (auto& Child : Childs) if (auto Group = dynamic_cast<GroupObject*>(Child)) Group->sortChilds(Functor, Synchronizer);
Synchronizer.addFuture(QtConcurrent::run(Sort, Childs.begin(), Childs.end(), Functor));
}
RecordModel::SortObject::SortObject(int Column, bool Ascending) : Index(Column), Mode(Ascending) {}
bool RecordModel::SortObject::operator () (RecordModel::RecordObject* First, RecordModel::RecordObject* Second) const
{
const auto One = First->getField(Index);
const auto Two = Second->getField(Index);
if (One == Two) return false;
bool Compare = One < Two;
return Mode ? Compare : !Compare;
}
RecordModel::RecordModel(const QStringList& Head, QObject* Parent)
: QAbstractItemModel(Parent), Header(Head), Locker(QMutex::Recursive) {}
RecordModel::~RecordModel(void)
{
QMutexLocker Synchronizer(&Locker);
for (auto& Object : Objects) delete Object;
}
QModelIndex RecordModel::index(int Row, int Col, const QModelIndex& Parent) const
{
QMutexLocker Synchronizer(&Locker);
if (!hasIndex(Row, Col, Parent)) return QModelIndex();
if (!Root) return createIndex(Row, Col, Objects.value(Row));
GroupObject* parentObject = Parent.isValid() ? (GroupObject*) Parent.internalPointer() : Root;
if (auto Child = parentObject->getChild(Row))
{
return createIndex(Row, Col, Child);
}
else return QModelIndex();
}
QModelIndex RecordModel::parent(const QModelIndex& Index) const
{
QMutexLocker Synchronizer(&Locker);
if (!Root || !Index.isValid()) return QModelIndex();
RecordObject* Object = (RecordObject*) Index.internalPointer();
if (Roots.contains((GroupObject*) Object))
{
if (auto Group = dynamic_cast<GroupObject*>(Object))
{
if (auto Parent = Group->getParent()) if (Parent != Root)
{
return createIndex(Parent->getIndex(), 0, Parent);
}
}
}
else if (auto Parent = Parents.value(Object, nullptr))
{
return createIndex(Parent->getIndex(), 0, Parent);
}
return QModelIndex();
}
bool RecordModel::hasChildren(const QModelIndex& Parent) const
{
QMutexLocker Synchronizer(&Locker);
if (Parent == QModelIndex()) return true;
RecordObject* Object = (RecordObject*) Parent.internalPointer();
if (auto Group = dynamic_cast<GroupObject*>(Object))
{
return Group->hasChids();
}
else return false;
}
int RecordModel::rowCount(const QModelIndex& Parent) const
{
QMutexLocker Synchronizer(&Locker);
if (!Parent.isValid())
{
if (Root) return Root->childrenCount();
else return Objects.size();
}
RecordObject* Object = (RecordObject*) Parent.internalPointer();
if (auto Group = dynamic_cast<GroupObject*>(Object))
{
return Group->childrenCount();
}
else return 0;
}
int RecordModel::columnCount(const QModelIndex &Parent) const
{
QMutexLocker Synchronizer(&Locker); return Header.size();
}
QVariant RecordModel::headerData(int Section, Qt::Orientation Orientation, int Role) const
{
QMutexLocker Synchronizer(&Locker);
if (Orientation != Qt::Horizontal) return QVariant();
if (Role != Qt::DisplayRole) return QVariant();
else return Header.value(Section);
}
QVariant RecordModel::data(const QModelIndex &Index, int Role) const
{
QMutexLocker Synchronizer(&Locker);
if (!Index.isValid() || !(Role == Qt::DisplayRole || Role == Qt::EditRole)) return QVariant();
RecordObject* Object = (RecordObject*) Index.internalPointer();
if (Object) return Object->getField(Index.column());
else return QVariant();
}
Qt::ItemFlags RecordModel::flags(const QModelIndex& Index) const
{
QMutexLocker Synchronizer(&Locker);
if (!Index.isValid()) return Qt::ItemFlags(0);
if (!Root) return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
RecordObject* Object = (RecordObject*) Index.internalPointer();
const auto groupFlag = selectGroups ? Qt::ItemIsSelectable : Qt::NoItemFlags;
if (!Objects.contains(Object)) return Qt::ItemIsEnabled | groupFlag;
else return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
bool RecordModel::setData(const QModelIndex& Index, const QVariant& Value, int Role)
{
QMutexLocker Synchronizer(&Locker);
if (!Index.isValid() || !(Role == Qt::DisplayRole || Role == Qt::EditRole)) return false;
RecordObject* Object = (RecordObject*) Index.internalPointer();
if (dynamic_cast<GroupObject*>(Object)) return false;
if (Root && !Parents.contains(Object)) return false;
Object->setField(Index.column(), Value);
if (Root && !Object->contain(Parents[Object]->getFields()))
{
auto Parent = Parents[Object];
auto From = createIndex(Parent->getIndex(), 0, Parent);
int Row = Parent->childIndex(Object);
beginRemoveRows(From, Row, Row);
Parent->takeChild(Object);
Objects.removeOne(Object);
Parents.remove(Object);
Roots = Parents.values().toSet();
endRemoveRows();
removeEmpty(Parent->getParent());
appendItem(Object);
}
else emit dataChanged(Index, Index);
return true;
}
void RecordModel::sort(int Column, Qt::SortOrder Order)
{
QMutexLocker Synchronizer(&Locker);
const SortObject Sort(Column, Order == Qt::AscendingOrder);
beginResetModel();
if (!Root) std::stable_sort(Objects.begin(), Objects.end(), Sort);
else
{
QFutureSynchronizer<void> Synchronizer;
Root->sortChilds(Sort, Synchronizer);
Synchronizer.waitForFinished();
}
endResetModel();
}
bool RecordModel::setData(int Index, const QHash<int, QVariant>& Data, bool Replace)
{
QMutexLocker Synchronizer(&Locker);
for (const auto& Item : Objects) if (Item->getUid() == Index)
{
if (Root) return setData(createIndex(Parents[Item]->getIndex(Item), 0, Item), Data, Replace);
else return setData(createIndex(Objects.indexOf(Item), 0, Item), Data, Replace);
}
return false;
}
bool RecordModel::setData(const QModelIndex& Index, const QHash<int, QVariant>& Data, bool Replace)
{
QMutexLocker Synchronizer(&Locker);
if (!Index.isValid()) return false;
RecordObject* Object = (RecordObject*) Index.internalPointer();
if (dynamic_cast<GroupObject*>(Object)) return false;
Object->setFields(Data, Replace);
if (Root && !Object->contain(Parents[Object]->getFields()))
{
auto Parent = Parents[Object];
auto From = createIndex(Parent->getIndex(), 0, Parent);
int Row = Parent->childIndex(Object);
beginRemoveRows(From, Row, Row);
Parent->takeChild(Object);
Objects.removeOne(Object);
Parents.remove(Object);
Roots = Parents.values().toSet();
endRemoveRows();
removeEmpty(Parent->getParent());
appendItem(Object);
}
else emit dataChanged(Index, Index);
return true;
}
QHash<int, QVariant> RecordModel::fullData(const QModelIndex& Index) const
{
QMutexLocker Synchronizer(&Locker);
if (!Index.isValid()) return QHash<int, QVariant>();
RecordObject* Object = (RecordObject*) Index.internalPointer();
return Object->getFields();
}
QVariant RecordModel::fieldData(const QModelIndex& Index, int Col) const
{
QMutexLocker Synchronizer(&Locker);
if (!Index.isValid()) return QVariant();
RecordObject* Object = (RecordObject*) Index.internalPointer();
return Object->getField(Col);
}
QModelIndexList RecordModel::getIndexes(const QModelIndex& Parent) const
{
QMutexLocker Synchronizer(&Locker);
if (!hasChildren(Parent)) return QModelIndexList();
QModelIndexList List; int Count = rowCount(Parent);
for (int i = 0; i < Count; ++i) List.append(index(i, 0, Parent));
return List;
}
QSet<int> RecordModel::getUids(const QModelIndexList& Selection) const
{
QMutexLocker Synchronizer(&Locker); QSet<int> List;
for (const auto& Index : Selection)
{
RecordObject* Object = (RecordObject*) Index.internalPointer();
if (auto G = dynamic_cast<GroupObject*>(Object))
{
List |= G->allUids();
}
else List.insert(Object->getUid());
}
return List;
}
QSet<int> RecordModel::getUids(void) const
{
QMutexLocker Synchronizer(&Locker);
QSet<int> Uids; Uids.reserve(Objects.size());
for (const auto& O : Objects) Uids.insert(O->getUid());
return Uids;
}
int RecordModel::getUid(const QModelIndex& Index) const
{
QMutexLocker Synchronizer(&Locker);
RecordObject* Object = (RecordObject*) Index.internalPointer();
if (dynamic_cast<GroupObject*>(Object)) return -1;
return Object->getUid();
}
bool RecordModel::updateUid(int Old, int New)
{
QMutexLocker Synchronizer(&Locker);
for (const auto& Item : Objects)
{
if (Item->getUid() == Old)
{
Item->setUid(New);
return true;
}
}
return false;
}
bool RecordModel::saveToFile(const QString& Path, const QList<int>& Columns, const QModelIndexList& List, bool Names, const QString& Separator) const
{
QMutexLocker Synchronizer(&Locker); QSet<QStringList> Lines;
for (const auto& Index : Columns) if (Header.size() <= Index) return false;
QFile File(Path); if (!File.open(QFile::WriteOnly | QFile::Text)) return false;
QTextStream Stream(&File); const auto UIDS = getUids(List);
QStringList Line; Line.reserve(Columns.size());
for (const auto Object : Objects) if (UIDS.contains(Object->getUid()))
{
for (const auto& ID : Columns)
{
Line.append(Object->getField(ID).toString());
}
Lines.insert(Line); Line.clear();
}
if (Names) for (const auto& ID : Columns)
{
Stream << Header[ID];
if (ID != Columns.last())
{
Stream << Separator;
}
else Stream << endl;
}
for (const auto& Line : Lines)
{
Stream << Line.join(Separator) << endl;
}
return true;
}
bool RecordModel::removeItem(const QModelIndex& Index)
{
QMutexLocker Synchronizer(&Locker);
if (!Index.isValid()) return false;
RecordObject* Object = (RecordObject*) Index.internalPointer();
if (dynamic_cast<GroupObject*>(Object)) return false;
if (Root)
{
GroupObject* Parent = Parents[Object];
const int Row = Parent->childIndex(Object);
beginRemoveRows(parent(Index), Row, Row);
Parent->removeChild(Object);
Objects.removeOne(Object);
endRemoveRows();
removeEmpty(Parent);
}
else
{
const int Row = Objects.indexOf(Object);
beginRemoveRows(QModelIndex(), Row, Row);
Objects.removeOne(Object); delete Object;
endRemoveRows();
}
return true;
}
bool RecordModel::removeItem(int Index)
{
QMutexLocker Synchronizer(&Locker);
for (const auto& Item : Objects) if (Item->getUid() == Index)
{
if (Root) return removeItem(createIndex(Parents[Item]->getIndex(Item), 0, Item));
else return removeItem(createIndex(Objects.indexOf(Item), 0, Item));
}
return false;
}
int RecordModel::totalCount(void) const
{
QMutexLocker Synchronizer(&Locker); return Objects.count();
}
bool RecordModel::isGrouped(void) const
{
QMutexLocker Synchronizer(&Locker); return Root;
}
bool RecordModel::exists(int Index) const
{
QMutexLocker Synchronizer(&Locker);
for (const auto& Item : Objects)
{
if (Item->getUid() == Index) return true;
}
return false;
}
QModelIndex RecordModel::index(int Index) const
{
QMutexLocker Synchronizer(&Locker);
for (const auto& Item : Objects) if (Item->getUid() == Index)
{
if (Root)
{
const auto Parent = Parents.value(Item, nullptr);
if (Parent) return createIndex(Parent->getIndex(Item), 0, Item);
}
else return createIndex(Objects.indexOf(Item), 0, Item);
}
return QModelIndex();
}
QModelIndex RecordModel::find(int Index, QVariant Data) const
{
QMutexLocker Synchronizer(&Locker);
if (Objects.isEmpty() || Header.size() <= Index) return QModelIndex();
for (const auto& Object : Objects) if (Object->getField(Index) == Data)
{
return Root ? createIndex(Parents[Object]->getIndex(Object), 0, Object) :
createIndex(Objects.indexOf(Object), 0, Object);
}
return QModelIndex();
}
void RecordModel::setGroupsSelectable(bool Selectable)
{
selectGroups = Selectable;
}
RecordModel::GroupObject* RecordModel::createGroups(QList<QPair<int, QList<QVariant>>>::ConstIterator From, QList<QPair<int, QList<QVariant>>>::ConstIterator To, RecordModel::GroupObject* Parent)
{
QMutexLocker Synchronizer(&Locker);
if (!Parent) Parent = new GroupObject();
for (const auto& Field : (*From).second)
{
GroupObject* Child = new GroupObject((*From).first, Parent->getFields());
Child->setField((*From).first, Field);
Parent->addChild(Child);
if (From + 1 != To) createGroups(From + 1, To, Child);
else
{
const auto Fields = Child->getFields();
for (auto Object : Objects) if (Object->contain(Fields))
{
Parents.insert(Object, Child);
Child->addChild(Object);
Roots.insert(Child);
}
}
}
return Parent;
}
RecordModel::GroupObject* RecordModel::appendItem(RecordModel::RecordObject* Object)
{
QMutexLocker Synchronizer(&Locker);
if (!Root) return nullptr;
GroupObject* Current = Root;
GroupObject* Result = nullptr;
for (int i = 0; i < Groups.size(); ++i)
{
for (auto Group : Current->getChilds())
{
auto G = dynamic_cast<GroupObject*>(Group);
if (G->getData() == Object->getField(G->getColumn()))
{
Result = G;
}
}
if (!Result)
{
auto Index = createIndex(Current->getIndex(), 0, Current);
int Column = getIndex(Groups[i]);
const int Count = Current->childrenCount() + 1;
beginInsertRows(Current == Root ? QModelIndex() : Index, Count, Count);
Result = new GroupObject(Column, Current->getFields());
Result->setField(Column, Object->getField(Column));
Current->addChild(Result);
endInsertRows();
}
Current = Result;
Result = nullptr;
}
auto Index = createIndex(Current->getIndex(), 0, Current);
const int Count = Current->childrenCount() ? Current->childrenCount() + 1 : 0;
beginInsertRows(Index, Count, Count);
Parents.insert(Object, Current);
Objects.append(Object);
Current->addChild(Object);
Roots.insert(Current);
endInsertRows();
return Current;
}
int RecordModel::getIndex(const QString& Field) const
{
QMutexLocker Synchronizer(&Locker);
int i = 0; for (const auto& Item : Header)
{
if (Item == Field) return i; ++i;
}
return -1;
}
void RecordModel::removeEmpty(RecordModel::GroupObject* Parent, bool Emit)
{
QMutexLocker Synchronizer(&Locker);
if (Parent)
{
if (Parent->hasChids()) for (auto Child : Parent->getChilds())
{
if (GroupObject* Group = dynamic_cast<GroupObject*>(Child))
{
removeEmpty(Group, Emit);
}
}
else if (auto P = Parent->getParent())
{
auto From = createIndex(P->getIndex(), 0, P);
int Row = Parent->getIndex();
if (Emit) beginRemoveRows(From, Row, Row);
P->removeChild(Parent);
if (Emit) endRemoveRows();
while (P && P != Root && !P->hasChids())
{
auto Delete = P; P = P->getParent();
From = createIndex(P->getIndex(), 0, P);
Row = Delete->getIndex();
if (Emit) beginRemoveRows(From, Row, Row);
P->removeChild(Delete);
if (Emit) endRemoveRows();
}
}
}
}
void RecordModel::groupItems(void)
{
QMutexLocker Synchronizer(&Locker);
QList<QPair<int, QList<QVariant>>> Indexes;
if (Root) delete Root;
Parents.clear();
Roots.clear();
for (const auto& Group : Groups)
{
const int Index = getIndex(Group);
if (Index == -1) continue;
auto Current = qMakePair(Index, QList<QVariant>());
for (const auto& Object : Objects)
{
const QVariant Value = Object->getField(Index);
if (!Current.second.contains(Value)) Current.second.append(Value);
}
Indexes.append(Current);
}
Root = createGroups(Indexes.constBegin(), Indexes.constEnd(), Root);
}
void RecordModel::groupByInt(const QList<int>& Levels)
{
QMutexLocker Synchronizer(&Locker); QStringList Groupby;
for (const auto& I : Levels) if (I < Header.size()) Groupby.append(Header[I]);
groupByStr(Groupby);
}
void RecordModel::groupByStr(const QStringList& Groupby)
{
QMutexLocker Synchronizer(&Locker);
if (Groups == Groupby) { emit onGroupComplete(); return; } Groups = Groupby;
beginResetModel();
if (Root) { Roots.clear(); Parents.clear(); delete Root; Root = nullptr; }
if (!Groups.isEmpty()) { groupItems(); removeEmpty(Root, false); }
endResetModel();
emit onGroupComplete();
}
void RecordModel::addItem(int ID, const QHash<int, QVariant>& Attributes)
{
QMutexLocker Synchronizer(&Locker);
auto Object = new RecordObject(ID, Attributes);
if (Root) appendItem(Object);
else
{
const int Count = Objects.size() + 1;
beginInsertRows(QModelIndex(), Count, Count);
Objects.append(Object);
endInsertRows();
}
}
void RecordModel::addItems(const QHash<int, QHash<int, QVariant>>& Items)
{
QMutexLocker Synchronizer(&Locker);
Objects.reserve(Objects.size() + Items.size());
if (Root) for (auto i = Items.constBegin(); i != Items.constEnd(); ++i)
{
appendItem(new RecordObject(i.key(), i.value()));
}
else
{
const int Count = Objects.size() ? Objects.size() + 1 : 0;
beginInsertRows(QModelIndex(), Count, Count + Items.size());
for (auto i = Items.constBegin(); i != Items.constEnd(); ++i)
{
Objects.append(new RecordObject(i.key(), i.value()));
}
endInsertRows();
}
}