forked from z1dev/zkanji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
groups.cpp
2795 lines (2292 loc) · 69.3 KB
/
groups.cpp
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
/*
** Copyright 2007-2013, 2017-2018 Sólyom Zoltán
** This file is part of zkanji, a free software released under the terms of the
** GNU General Public License version 3. See the file LICENSE for details.
**/
#include <QFile>
#include <QDataStream>
#include <QSet>
#include <QStringBuilder>
#include <stack>
#include "zkanjimain.h"
#include "groups.h"
#include "words.h"
#include "ranges.h"
#include "kanji.h"
#include "groupstudy.h"
#include "grammar_enums.h"
#include "zdictionarymodel.h"
#include "zkanjigridmodel.h"
#include "checked_cast.h"
//-------------------------------------------------------------
GroupBase::GroupBase(GroupCategoryBase *parent) : parent(parent)
{
}
GroupBase::GroupBase(GroupCategoryBase *parent, QString name) : parent(parent), _name(name)
{
}
GroupBase::GroupBase(GroupBase &&src) : parent(nullptr)
{
*this = std::move(src);
}
GroupBase& GroupBase::operator=(GroupBase &&src)
{
std::swap(parent, src.parent);
std::swap(_name, src._name);
return *this;
}
GroupBase::~GroupBase()
{
}
void GroupBase::load(QDataStream &stream)
{
stream >> make_zstr(_name, ZStrFormat::Byte);
}
void GroupBase::save(QDataStream &stream) const
{
// TODO: don't allow group names longer than 255 anywhere in the UI. (Saving is safe because make_zstr trims it.)
stream << make_zstr(_name, ZStrFormat::Byte);
}
void GroupBase::copy(GroupBase *src)
{
_name = src->_name;
}
bool GroupBase::isCategory() const
{
return false;
}
//Dictionary* GroupBase::dictionary()
//{
// return owner->dictionary();
//}
//
//const Dictionary* GroupBase::dictionary() const
//{
// return owner->dictionary();
//}
GroupCategoryBase* GroupBase::parentCategory()
{
return parent;
}
const GroupCategoryBase* GroupBase::parentCategory() const
{
return parent;
}
const QString& GroupBase::name() const
{
return _name;
}
void GroupBase::setName(QString newname)
{
// TODO: Check the validity of the passed name everywhere where this is called. Replace
// the TAB character with space. Duplicate names are not allowed either. Figure out
// whether the / character or some other should be forbidden and used as path separator.
_name = newname;
dictionary()->setToUserModified();
}
const QString GroupBase::fullEncodedName() const
{
QString str;
if (parent)
{
str = parent->fullEncodedName();
if (!str.isEmpty())
str += QChar('/');
}
// Replace characters like TAB, / and ^ in the names with ^ as escape character.
QString encname;
int from = 0;
int to = 0;
int len = _name.size();
QString enc("^ ");
while (to != len)
{
ushort ch = _name.at(to).unicode();
if (ch == '/' || ch == '^' || ch == '[')
{
// Add anything between from and to - 1 into encname, and the last character
// escaped with ^.
if (from < to)
encname += _name.mid(from, to - from);
enc[1] = QChar(ch);
encname += enc;
from = to + 1;
to = from - 1;
}
++to;
}
if (from < to)
encname += _name.mid(from, to - from);
return str + encname;
}
const QString GroupBase::fullName(GroupCategoryBase *root) const
{
QString str;
if (parent && parent != root)
{
str = parent->fullName(root);
if (!str.isEmpty())
str += QChar('/');
}
return str + _name;
}
//Groups* GroupBase::getOwner()
//{
//#ifdef _DEBUG
// if (!parent)
// throw "no parent.";
//#endif
// return parent->getOwner();
//}
//
//const Groups* GroupBase::getOwner() const
//{
//#ifdef _DEBUG
// if (!parent)
// throw "no parent.";
//#endif
// return parent->getOwner();
//}
//GroupCategoryBase* GroupBase::topParent()
//{
// GroupBase *p = this;
// while (p->parent)
// p = p->parent;
// return p->parent;
//}
//
//const GroupCategoryBase* GroupBase::topParent() const
//{
// const GroupBase *p = this;
// while (p->parent)
// p = p->parent;
// return p->parent;
//}
//-------------------------------------------------------------
//GroupCategoryBase::GroupCategoryBase(GroupCategoryBase *parent) : base(parent, QString()), owner(parent ? parent->owner : nullptr)
//{
// if (owner == nullptr)
// owner = this;
//}
GroupCategoryBase::GroupCategoryBase(GroupCategoryBase *parent, QString name) : base(parent, name), owner(parent ? parent->owner : nullptr)
{
if (owner == nullptr)
owner = this;
}
GroupCategoryBase::GroupCategoryBase(GroupCategoryBase &&src) : base(nullptr)
{
*this = std::forward<GroupCategoryBase>(src);
}
GroupCategoryBase& GroupCategoryBase::operator=(GroupCategoryBase &&src)
{
base::operator=(std::forward<GroupCategoryBase>(src));
std::swap(list, src.list);
std::swap(groups, src.groups);
std::swap(owner, src.owner);
return *this;
}
GroupCategoryBase::~GroupCategoryBase()
{
}
void GroupCategoryBase::load(QDataStream &stream)
{
clear();
base::load(stream);
// Reading categories.
qint32 i;
stream >> i;
list.reserve(i);
for (int ix = 0; ix != i; ++ix)
{
list.push_back(createCategory(QString()));
list.back()->load(stream);
}
// Reading groups.
stream >> i;
groups.reserve(i);
for (int ix = 0; ix != i; ++ix)
{
groups.push_back(createGroup(QString()));
groups[ix]->load(stream);
}
}
void GroupCategoryBase::save(QDataStream &stream) const
{
base::save(stream);
// Saving categories.
stream << (qint32)list.size();
for (int ix = 0, siz = tosigned(list.size()); ix != siz; ++ix)
list[ix]->save(stream);
// Saving groups;
stream << (qint32)groups.size();
for (int ix = 0, siz = tosigned(groups.size()); ix != siz; ++ix)
groups[ix]->save(stream);
}
bool GroupCategoryBase::isEmpty() const
{
return groups.empty() && list.empty();
}
bool GroupCategoryBase::isCategory() const
{
return true;
}
Dictionary* GroupCategoryBase::dictionary()
{
return owner->dictionary();
}
const Dictionary* GroupCategoryBase::dictionary() const
{
return owner->dictionary();
}
void GroupCategoryBase::copy(GroupCategoryBase *src)
{
clear();
base::copy(src);
list.reserve(src->list.size());
groups.reserve(src->groups.size());
for (int ix = 0, siz = tosigned(src->list.size()); ix != siz; ++ix)
{
list.push_back(createCategory(src->list[ix]->name()));
list.back()->copy(src->list[ix]);
}
for (int ix = 0, siz = tosigned(src->groups.size()); ix != siz; ++ix)
{
groups.push_back(createGroup(src->groups[ix]->name()));
groups.back()->copy(src->groups[ix]);
}
}
void GroupCategoryBase::clear()
{
list.clear();
groups.clear();
//while (!list.empty())
// deleteCategory(list.size() - 1);
//while (!groups.empty())
// deleteGroup(groups.size() - 1);
}
int GroupCategoryBase::addCategory(QString name)
{
list.push_back(createCategory(name));
dictionary()->setToUserModified();
emit owner->categoryAdded(this, tosigned(list.size()) - 1);
return tosigned(list.size()) - 1;
}
int GroupCategoryBase::addGroup(QString name)
{
groups.push_back(createGroup(name));
dictionary()->setToUserModified();
emit owner->groupAdded(this, tosigned(groups.size()) - 1);
return tosigned(groups.size()) - 1;
}
void GroupCategoryBase::deleteCategory(int index)
{
#ifdef _DEBUG
if (index < 0 || index >= tosigned(list.size()))
throw "Invalid category index.";
#endif
list[index]->emitDeleted();
void *oldptr = list[index];
emit owner->categoryAboutToBeDeleted(this, index, list[index]);
list.erase(list.begin() + index);
dictionary()->setToUserModified();
emit owner->categoryDeleted(this, index, oldptr);
}
//void GroupCategoryBase::deleteCategory(GroupCategoryBase *child)
//{
// deleteCategory(categoryIndex(child));
//}
void GroupCategoryBase::deleteGroup(int index)
{
#ifdef _DEBUG
if (index < 0 || index >= tosigned(groups.size()))
throw "Invalid category index.";
#endif
void *oldptr = groups[index];
emit owner->groupAboutToBeDeleted(this, index, groups[index]);
groups.erase(groups.begin() + index);
dictionary()->setToUserModified();
owner->emitGroupDeleted(this, index, oldptr);
//emit owner->groupDeleted(this, index, oldptr);
}
void GroupCategoryBase::remove(const std::vector<GroupBase*>& which) {
std::vector<GroupBase*> items;
selectTopItems(which, items);
for (int ix = 0, siz = tosigned(items.size()); ix != siz; ++ix) {
GroupCategoryBase* p = items[ix]->parentCategory();
GroupCategoryBase* categoryPtr = dynamic_cast<GroupCategoryBase*>(items[ix]);
if (categoryPtr != nullptr) {
categoryPtr->emitDeleted();
int index = p->categoryIndex(categoryPtr);
emit owner->categoryAboutToBeDeleted(p, index, p->list[index]);
p->list.erase(p->list.begin() + index);
emit owner->categoryDeleted(p, index, categoryPtr);
} else if (WordGroup* wordGroupPtr = dynamic_cast<WordGroup*>(items[ix])) {
int index = p->groupIndex(wordGroupPtr);
emit owner->groupAboutToBeDeleted(p, index, p->groups[index]);
p->groups.erase(p->groups.begin() + index);
owner->emitGroupDeleted(p, index, wordGroupPtr);
} else if (KanjiGroup* kanjiGroupPtr = dynamic_cast<KanjiGroup*>(items[ix])) {
// Handle KanjiGroup objects (similar logic as WordGroup)
int index = p->groupIndex(kanjiGroupPtr);
emit owner->groupAboutToBeDeleted(p, index, p->groups[index]);
p->groups.erase(p->groups.begin() + index);
owner->emitGroupDeleted(p, index, kanjiGroupPtr);
} else {
// Handle other potential GroupBase derived types or log an error.
}
}
dictionary()->setToUserModified();
}
bool GroupCategoryBase::moveCategory(GroupCategoryBase *what, GroupCategoryBase *destparent, int destindex)
{
#ifdef _DEBUG
if (destindex < 0 || destindex > tosigned(destparent->list.size()))
throw "Index out of range.";
#endif
GroupCategoryBase *p = what->parentCategory();
int ix = p->categoryIndex(what);
if (p == destparent)
{
// Same as the current position. No move is necessary.
if (ix == destindex || ix == destindex - 1)
return false;
GroupCategoryBase **ldata = destparent->list.data();
// Moving the other groups up or down by one position.
memmove(ldata + std::min(destindex + 1, ix), ldata + std::min(destindex, ix + 1), (ix < destindex ? (destindex - ix - 1) : (ix - destindex)) * sizeof(GroupCategoryBase*));
// Inserting the item where it belongs. If moving up in the same parent, destindex is
// the index where it'd be placed if it were not removed first. After it's removed
// the real index will be one less.
if (destindex < ix)
*(ldata + destindex) = what;
else
*(ldata + destindex - 1) = what;
}
else
{
QString wname = what->name().toLower();
for (const GroupCategoryBase* g : destparent->list)
{
if (g != what && g->name().toLower() == wname)
return false;
}
p->list.removeAt(p->list.begin() + ix);
what->parent = destparent;
destparent->list.insert(destparent->list.begin() + destindex, what);
}
dictionary()->setToUserModified();
emit categoryMoved(p, ix, destparent, destindex);
return true;
}
bool GroupCategoryBase::moveGroup(GroupBase *what, GroupCategoryBase *destparent, int destindex) {
#ifdef _DEBUG
if (destindex < 0 || destindex > tosigned(destparent->groups.size()))
throw "Index out of range.";
#endif
GroupCategoryBase *p = what->parentCategory();
int ix = p->groupIndex(what);
// Ensure moving within the same parent
if (p == destparent) {
if (ix == destindex || ix == destindex - 1)
return false; // No move necessary
// Attempt downcasts for type-specific handling
GroupCategoryBase* categoryPtr = dynamic_cast<GroupCategoryBase*>(what);
WordGroup* wordGroupPtr = dynamic_cast<WordGroup*>(what);
KanjiGroup* kanjiGroupPtr = dynamic_cast<KanjiGroup*>(what);
if (categoryPtr) {
// Handle GroupCategoryBase (likely need to adjust memmove logic)
} else if (wordGroupPtr) {
// Handle WordGroup (potentially adjust memmove logic)
} else if (kanjiGroupPtr) {
// Handle KanjiGroup (potentially adjust memmove logic)
} else {
// Handle other derived types or log an error
}
} else {
// ... (rest of your existing code for moving between parents) ...
}
dictionary()->setToUserModified();
emit groupMoved(p, ix, destparent, destindex);
return true;
}
void GroupCategoryBase::moveCategories(const std::vector<GroupCategoryBase*> &moved, GroupCategoryBase *destparent, int destindex)
{
#ifdef _DEBUG
if (destindex < 0 || destindex > tosigned(destparent->list.size()))
throw "Index out of range.";
#endif
if (moved.empty())
return;
GroupCategoryBase *p = moved.front()->parentCategory();
//int ix = p->categoryIndex(what);
// Source indexes.
std::vector<int> indexes;
indexes.reserve(moved.size());
for (int ix = 0, siz = tosigned(moved.size()); ix != siz; ++ix)
indexes.push_back(p->categoryIndex(moved[ix]));
std::sort(indexes.begin(), indexes.end());
smartvector<Range> ranges;
_rangeFromIndexes(indexes, ranges);
if (p == destparent)
{
GroupCategoryBase **ldata = destparent->list.data();
int lsiz = tosigned(destparent->list.size());
_moveRanges(ranges, destindex, [this, &ldata, lsiz, p](const Range &range, int pos) {
_moveRange(ldata,
#ifdef _DEBUG
lsiz,
#endif
range, pos);
emit categoriesMoved(p, range, p, pos);
});
}
else
{
// Erasing groups from source and placing them in dest going backwards, one range at a
// time.
for (int ix = tosigned(ranges.size()) - 1; ix != -1; --ix)
{
Range *r = ranges[ix];
for (auto b = p->list.begin() + r->first, e = p->list.begin() + (r->last + 1); b != e; ++b)
(*b)->parent = destparent;
destparent->list.insert(destparent->list.begin() + destindex, p->list.mbegin() + r->first, p->list.mbegin() + (r->last + 1));
p->list.erase(p->list.begin() + r->first, p->list.begin() + (r->last + 1));
emit categoriesMoved(p, *r, destparent, destindex);
}
}
dictionary()->setToUserModified();
}
void GroupCategoryBase::moveGroups(const std::vector<GroupBase*> &moved, GroupCategoryBase *destparent, int destindex) {
#ifdef _DEBUG
if (destindex < 0 || destindex > tosigned(destparent->groups.size()))
throw "Index out of range.";
#endif
if (moved.empty())
return;
GroupCategoryBase *p = moved.front()->parentCategory();
// Type-specific handling within the loop
std::vector<int> indexes;
indexes.reserve(moved.size());
for (int ix = 0, siz = tosigned(moved.size()); ix != siz; ++ix) {
GroupCategoryBase* categoryPtr = dynamic_cast<GroupCategoryBase*>(moved[ix]);
WordGroup* wordGroupPtr = dynamic_cast<WordGroup*>(moved[ix]);
KanjiGroup* kanjiGroupPtr = dynamic_cast<KanjiGroup*>(moved[ix]);
if (categoryPtr) {
// Handle GroupCategoryBase objects
indexes.push_back(p->groupIndex(categoryPtr));
} else if (wordGroupPtr) {
// Handle WordGroup objects
indexes.push_back(p->groupIndex(wordGroupPtr));
} else if (kanjiGroupPtr) {
// Handle KanjiGroup objects
indexes.push_back(p->groupIndex(kanjiGroupPtr));
} else {
// Handle other derived types or log an error
}
}
std::sort(indexes.begin(), indexes.end());
smartvector<Range> ranges;
_rangeFromIndexes(indexes, ranges);
if (p == destparent)
{
GroupBase **ldata = destparent->groups.data();
int lsiz = tosigned(destparent->groups.size());
_moveRanges(ranges, destindex, [this, &ldata, lsiz, p](const Range &range, int pos) {
_moveRange(ldata,
#ifdef _DEBUG
lsiz,
#endif
range, pos);
emit groupsMoved(p, range, p, pos);
});
}
else
{
// Erasing groups from source and placing them in dest going backwards, one range at a
// time.
for (int ix = tosigned(ranges.size()) - 1; ix != -1; --ix)
{
Range *r = ranges[ix];
for (auto b = p->groups.begin() + r->first, e = p->groups.begin() + (r->last + 1); b != e; ++b)
(*b)->parent = destparent;
destparent->groups.insert(destparent->groups.begin() + destindex, p->groups.mbegin() + r->first, p->groups.mbegin() + (r->last + 1));
p->groups.erase(p->groups.begin() + r->first, p->groups.begin() + (r->last + 1));
emit groupsMoved(p, *r, destparent, destindex);
}
}
dictionary()->setToUserModified();
}
int GroupCategoryBase::categoryIndex(GroupCategoryBase *child)
{
return list.indexOf(child);
}
int GroupCategoryBase::categoryIndex(QString name)
{
name = name.toLower();
for (int ix = 0, siz = tosigned(list.size()); ix != siz; ++ix)
if (list[ix]->name().toLower() == name)
return ix;
return -1;
}
int GroupCategoryBase::categoryCount() const
{
return tosigned(list.size());
}
GroupCategoryBase* GroupCategoryBase::categories(int index)
{
return list[index];
}
const GroupCategoryBase* GroupCategoryBase::categories(int index) const
{
return list[index];
}
int GroupCategoryBase::groupCount() const
{
int cnt = 0;
for (int ix = 0, siz = tosigned(list.size()); ix != siz; ++ix)
cnt += list[ix]->groupCount();
return cnt + tosigned(groups.size());
}
size_t GroupCategoryBase::size() const
{
return groups.size();
}
GroupBase* GroupCategoryBase::items(int ix)
{
return groups[ix];
}
const GroupBase* GroupCategoryBase::items(int ix) const
{
return groups[ix];
}
int GroupCategoryBase::groupIndex(QString name) const
{
name = name.toLower();
for (int ix = 0, siz = tosigned(groups.size()); ix != siz; ++ix)
if (groups[ix]->name().toLower() == name)
return ix;
return -1;
}
int GroupCategoryBase::groupIndex(GroupBase *group) const
{
for (int ix = 0, siz = tosigned(groups.size()); ix != siz; ++ix)
if (groups[ix] == group)
return ix;
return -1;
}
bool GroupCategoryBase::categoryNameTaken(QString name) const
{
if (name.isEmpty())
return false;
name = name.toLower();
for (const GroupCategoryBase* g : list)
if (g->name().toLower() == name)
return true;
return false;
}
bool GroupCategoryBase::groupNameTaken(QString name) const
{
if (name.isEmpty())
return false;
name = name.toLower();
for (const GroupBase* g : groups)
if (g->name().toLower() == name)
return true;
return false;
}
void GroupCategoryBase::setName(QString newname)
{
if (name() == newname)
return;
base::setName(newname);
GroupCategoryBase *p = parentCategory();
if (p == nullptr)
return;
emit owner->categoryRenamed(p, p->categoryIndex(this));
}
int GroupCategoryBase::addGroup(QString name, GroupCategoryBase *cat)
{
return cat->addGroup(name);
}
int GroupCategoryBase::addCategory(QString name, GroupCategoryBase *cat)
{
return cat->addCategory(name);
}
void GroupCategoryBase::walkGroups(const std::function<void(GroupBase*)> &callback)
{
std::stack<GroupCategoryBase*> stack;
stack.push(this);
while (!stack.empty())
{
GroupCategoryBase *pos = stack.top();
stack.pop();
for (int ix = 0; ix != tosigned(pos->groups.size()); ++ix)
callback(pos->groups[ix]);
for (int ix = 0; ix != tosigned(pos->list.size()); ++ix)
stack.push(pos->list[ix]);
}
}
bool GroupCategoryBase::isChild(GroupBase *item, bool recursive) const
{
for (int ix = 0, siz = tosigned(size()); ix != siz; ++ix)
if (groups[ix] == item)
return true;
if (!recursive)
return false;
for (int ix = 0, siz = categoryCount(); ix != siz; ++ix)
if (list[ix]->isChild(item, true))
return true;
return false;
}
void GroupCategoryBase::emitDeleted()
{
for (GroupCategoryBase *cat : list)
cat->emitDeleted();
for (GroupBase *grp : groups)
{
emit owner->groupAboutToBeDeleted(nullptr, -1, grp);
owner->emitGroupDeleted(nullptr, -1, (void*)grp);
//emit owner->groupDeleted(nullptr, -1, (void*)grp);
}
}
void GroupCategoryBase::emitGroupDeleted(GroupCategoryBase *p, int index, void *oldaddress)
{
emit groupDeleted(p, index, oldaddress);
}
void GroupCategoryBase::selectTopItems(const std::vector<GroupBase*> &src, std::vector<GroupBase*> &items) const
{
// Rebuild the list to only contain categories and groups not already found in which.
items = src;
// Positions of items found in which.
std::map<GroupBase*, int> pos;
for (int ix = 0, siz = tosigned(items.size()); ix != siz; ++ix)
pos.insert(std::make_pair(items[ix], ix));
QSet<GroupBase*> found;
for (int ix = 0, siz = tosigned(items.size()); ix != siz; ++ix)
{
GroupBase *g = items[ix];
if (!g->isCategory())
continue;
GroupCategoryBase *cat = (GroupCategoryBase*)g;
std::list<GroupCategoryBase*> stack;
stack.push_back(cat);
while (!stack.empty())
{
cat = stack.front();
stack.pop_front();
for (int iy = 0, ysiz = cat->categoryCount(); iy != ysiz; ++iy)
{
GroupCategoryBase *c = cat->categories(iy);
found.insert(c);
stack.push_back(c);
}
for (int iy = 0, ysiz = tosigned(cat->size()); iy != ysiz; ++iy)
found.insert(cat->items(iy));
}
}
for (GroupBase *g : found)
{
auto it = pos.find(g);
if (it != pos.end())
items[it->second] = nullptr;
}
items.resize(std::remove(items.begin(), items.end(), nullptr) - items.begin());
}
//-------------------------------------------------------------
//template<typename T, typename O>
//GroupCategory<T, O>::GroupCategory(O *owner) : base(nullptr), owner(owner)
//{
// ;
//}
//
//template<typename T, typename O>
//GroupCategory<T, O>::GroupCategory(GroupCategory<T, O> *parent) : base(parent), owner(((self_type*)parent)->getOwner())
//{
// ;
//}
//
//template<typename T, typename O>
//GroupCategory<T, O>::GroupCategory(GroupCategory<T, O> *parent, QString name) : base(parent, name), owner(((self_type*)parent)->getOwner())
//{
// ;
//}
//
//template<typename T, typename O>
//GroupCategory<T, O>::GroupCategory(GroupCategory<T, O> &&src) : base(nullptr), owner(nullptr)
//{
// *this = std::forward<GroupCategory<T, O>>(src);
//}
//
//template<typename T, typename O>
//GroupCategory<T, O>& GroupCategory<T, O>::operator=(GroupCategory<T, O> &&src)
//{
// base::operator=(std::move(src));
// std::swap(list, src.list);
// std::swap(owner, src.owner);
// return *this;
//}
//template<typename T, typename O>
//GroupCategory<T, O>::~GroupCategory()
//{
// clear();
//}
//
//template<typename T, typename O>
//void GroupCategory<T, O>::load(QDataStream &stream)
//{
// base::load(stream);
//
// qint32 i;
// stream >> i;
// list.reserve(i);
// for (int ix = 0; ix != i; ++ix)
// {
// list.push_back(new T(this));
// list[ix]->load(stream);
// }
//}
//
//template<typename T, typename O>
//void GroupCategory<T, O>::save(QDataStream &stream) const
//{
// base::save(stream);
//
// stream << (qint32)list.size();
// for (int ix = 0; ix != list.size(); ++ix)
// list[ix]->save(stream);
//}
//template<typename T, typename O>
//Dictionary* GroupCategory<T, O>::dictionary()
//{
// return owner->dictionary();
//}
//
//template<typename T, typename O>
//const Dictionary* GroupCategory<T, O>::dictionary() const
//{
// return owner->dictionary();
//}
//template<typename T, typename O>
//void GroupCategory<T, O>::clear()
//{
// list.clear();
// base::clear();
//}
//template<typename T, typename O>
//int GroupCategory<T, O>::addGroup(QString name)
//{
// list.push_back(new T(this, name));
// return list.size() - 1;
//}
//template<typename T, typename O>
//void GroupCategory<T, O>::deleteGroup(int ix)
//{
// list.erase(list.begin() + ix);
//}
//template<typename T, typename O>
//int GroupCategory<T, O>::size() const
//{
// return list.size();
//}
//
//template<typename T, typename O>
//T* GroupCategory<T, O>::items(int ix)
//{
// return list[ix];
//}
//
//template<typename T, typename O>
//const T* GroupCategory<T, O>::items(int ix) const
//{
// return list[ix];
//}
//template<typename T, typename O>
//typename GroupCategory<T, O>::self_type* GroupCategory<T, O>::categories(int index)
//{
// return (self_type*)base::categories(index);
//}
//
//template<typename T, typename O>
//const typename GroupCategory<T, O>::self_type* GroupCategory<T, O>::categories(int index) const
//{
// return (const self_type*)base::categories(index);
//}
//template<typename T, typename O>
//GroupCategoryBase* GroupCategory<T, O>::createCategory()
//{
// return new GroupCategory<T, O>(this);
//}
//template<typename T, typename O>
//bool GroupCategory<T, O>::validItemName(QString name) const
//{
// for (int ix = list.size() - 1; ix != -1; --ix)
// if (list[ix]->name().toLower() == name)
// return false;
// return true;
//}
//
//template<typename T, typename O>