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
|
#include "filetreemodel.h"
#include "organizercore.h"
#include "shared/directoryentry.h"
#include "shared/fileentry.h"
#include "shared/filesorigin.h"
#include "shared/util.h"
#include <log.h>
#include <moassert.h>
using namespace MOBase;
using namespace MOShared;
namespace fs = std::filesystem;
// in mainwindow.cpp
QString UnmanagedModName();
#define trace(f)
// about queueRemoveItem(), queueSortItems() and the `forFetching` parameter
//
// update() can be called when refreshing the tree or expanding a node;
// there are certain operations that cannot be done during node expansion,
// namely removing or moving items
//
// 1) removing items
// beginRemoveRows()/endRemoveRows() clears the internal list of visible
// items in a QTreeView, which is repopulated during the next layout
//
// doing this _during_ layout (which happens during node expansion) crashes
// in Qt because it assumes the list of visible items doesn't change; that
// is, fetchMore() cannot _remove_ items from the tree, it can only _add_
// items
//
// 2) moving items
// when a QSortFilterProxyModel is used between a tree and the FileTreeModel,
// it maintains a mapping of indices between the source and proxy models;
// calling layoutAboutToBeChanged()/layoutChanged() clears that mapping
//
// the only time this is called in FileTreeModel is when sorting items,
// which can happen during layout because since it calls fetchMore()
//
// doing this _during_ layout (which happens during node expansion) crashes
// in Qt because the QSortFilterProxyModel assumes the mapping doesn't
// change; that is, fetchMore() cannot _move_ items in the tree, it can
// only _add_ items
//
//
// therefore, these two operations are queued in a 1ms timer which is processed
// immediately after update()
//
// note that not all instances of removing items are currently getting queued
// (such as when removeDisappearingFiles()) because they cannot happen while
// fetching
// tracks a contiguous range in the model to avoid calling begin*Rows(), etc.
// for every single item that's added/removed
//
class FileTreeModel::Range
{
public:
// note that file ranges can start from an index higher than 0 if there are
// directories
//
Range(FileTreeModel* model, FileTreeItem& parentItem, int start = 0)
: m_model(model), m_parentItem(parentItem), m_first(-1), m_current(start)
{}
// includes the current index in the range
//
void includeCurrent()
{
// just remember the start of the range, m_current will be used in add()
// or remove() to figure out the actual range
if (m_first == -1) {
m_first = m_current;
}
}
// moves to the next row
//
void next() { ++m_current; }
// returns the current row
//
int current() const { return m_current; }
// manually set this range
//
void set(int first, int last)
{
m_first = first;
m_current = last;
}
// adds the given items to this range
//
void add(FileTreeItem::Children toAdd)
{
if (m_first == -1) {
// nothing to add
MO_ASSERT(toAdd.empty());
return;
}
const auto last = m_current - 1;
const auto parentIndex = m_model->indexFromItem(m_parentItem);
// make sure the number of items is the same as the size of this range
MO_ASSERT(static_cast<int>(toAdd.size()) == (last - m_first + 1));
trace(log::debug("Range::add() {} to {}", m_first, last));
m_model->beginInsertRows(parentIndex, m_first, last);
m_parentItem.insert(std::make_move_iterator(toAdd.begin()),
std::make_move_iterator(toAdd.end()),
static_cast<std::size_t>(m_first));
m_model->endInsertRows();
// reset
m_first = -1;
}
// removes the item in this range, returns an iterator to first item passed
// this range once removed, which can be end()
//
FileTreeItem::Children::const_iterator remove()
{
if (m_first >= 0) {
const auto last = m_current - 1;
const auto parentIndex = m_model->indexFromItem(m_parentItem);
trace(log::debug("Range::remove() {} to {}", m_first, last));
m_model->beginRemoveRows(parentIndex, m_first, last);
m_parentItem.remove(static_cast<std::size_t>(m_first),
static_cast<std::size_t>(last - m_first + 1));
m_model->endRemoveRows();
m_model->removePendingIcons(parentIndex, m_first, last);
// adjust current row to account for those that were just removed
m_current -= (m_current - m_first);
// reset
m_first = -1;
}
if (m_current >= m_parentItem.children().size()) {
return m_parentItem.children().end();
}
return m_parentItem.children().begin() + m_current + 1;
}
static void removeChildren(FileTreeModel* model, FileTreeItem& parentItem)
{
Range r(model, parentItem);
r.set(0, static_cast<int>(parentItem.children().size()));
r.remove();
parentItem.clear();
}
private:
FileTreeModel* m_model;
FileTreeItem& m_parentItem;
int m_first;
int m_current;
};
FileTreeItem* getItem(const QModelIndex& index)
{
return static_cast<FileTreeItem*>(index.internalPointer());
}
void* makeInternalPointer(FileTreeItem* item)
{
return item;
}
FileTreeModel::FileTreeModel(OrganizerCore& core, QObject* parent)
: QAbstractItemModel(parent), m_core(core), m_enabled(true),
m_root(FileTreeItem::createDirectory(this, nullptr, L"", L"")),
m_flags(HiddenFiles), m_fullyLoaded(false), m_sortingEnabled(true)
{
m_root->setExpanded(true);
m_sortTimer.setSingleShot(true);
connect(&m_removeTimer, &QTimer::timeout, [&] {
removeItems();
});
connect(&m_sortTimer, &QTimer::timeout, [&] {
sortItems();
});
connect(&m_iconPendingTimer, &QTimer::timeout, [&] {
updatePendingIcons();
});
}
void FileTreeModel::refresh()
{
TimeThis tt("FileTreeModel::refresh()");
m_fullyLoaded = false;
update(*m_root, *m_core.directoryStructure(), L"", false);
sortItem(*m_root, false);
}
void FileTreeModel::clear()
{
m_fullyLoaded = false;
beginResetModel();
m_root->clear();
endResetModel();
}
void FileTreeModel::recursiveFetchMore(const QModelIndex& m)
{
if (canFetchMore(m)) {
doFetchMore(m, false, false);
}
for (int i = 0; i < rowCount(m); ++i) {
recursiveFetchMore(index(i, 0, m));
}
}
void FileTreeModel::ensureFullyLoaded()
{
if (!m_fullyLoaded) {
TimeThis tt("FileTreeModel:: fully loading for search");
recursiveFetchMore(QModelIndex());
sortItem(*m_root, false);
m_fullyLoaded = true;
}
}
bool FileTreeModel::enabled() const
{
return m_enabled;
}
void FileTreeModel::setEnabled(bool b)
{
m_enabled = b;
}
void FileTreeModel::aboutToExpandAll()
{
m_sortingEnabled = false;
}
void FileTreeModel::expandedAll()
{
m_sortingEnabled = true;
sortItem(*m_root, false);
}
const FileTreeModel::SortInfo& FileTreeModel::sortInfo() const
{
return m_sort;
}
bool FileTreeModel::showArchives() const
{
return (m_flags.testFlag(Archives) && m_core.settings().archiveParsing());
}
bool FileTreeModel::showHiddenFiles() const
{
return m_flags.testAnyFlag(HiddenFiles);
}
QModelIndex FileTreeModel::index(int row, int col, const QModelIndex& parentIndex) const
{
if (auto* parentItem = itemFromIndex(parentIndex)) {
if (row < 0 || row >= parentItem->children().size()) {
log::error("row {} out of range for {}", row, parentItem->debugName());
return {};
}
return createIndex(row, col, makeInternalPointer(parentItem));
}
log::error("FileTreeModel::index(): parentIndex has no internal pointer");
return {};
}
QModelIndex FileTreeModel::parent(const QModelIndex& index) const
{
if (!index.isValid()) {
return {};
}
auto* parentItem = getItem(index);
if (!parentItem) {
log::error("FileTreeModel::parent(): no internal pointer");
return {};
}
return indexFromItem(*parentItem);
}
int FileTreeModel::rowCount(const QModelIndex& parent) const
{
if (auto* item = itemFromIndex(parent)) {
return static_cast<int>(item->children().size());
}
return 0;
}
int FileTreeModel::columnCount(const QModelIndex&) const
{
return ColumnCount;
}
bool FileTreeModel::hasChildren(const QModelIndex& parent) const
{
if (!m_enabled) {
return false;
}
if (auto* item = itemFromIndex(parent)) {
if (parent.column() <= 0) {
return item->hasChildren();
}
}
return false;
}
bool FileTreeModel::canFetchMore(const QModelIndex& parent) const
{
if (!m_enabled) {
return false;
}
if (auto* item = itemFromIndex(parent)) {
return !item->isLoaded();
}
return false;
}
void FileTreeModel::fetchMore(const QModelIndex& parent)
{
doFetchMore(parent, true, true);
}
void FileTreeModel::doFetchMore(const QModelIndex& parent, bool forFetch, bool doSort)
{
FileTreeItem* item = itemFromIndex(parent);
if (!item) {
return;
}
const auto path = item->dataRelativeFilePath();
auto* parentEntry =
m_core.directoryStructure()->findSubDirectoryRecursive(path.toStdWString());
if (!parentEntry) {
log::error("FileTreeModel::fetchMore(): directory '{}' not found", path);
return;
}
const auto parentPath = item->dataRelativeParentPath();
update(*item, *parentEntry, parentPath.toStdWString(), forFetch);
if (!forFetch && doSort) {
sortItem(*item, false);
}
}
QVariant FileTreeModel::data(const QModelIndex& index, int role) const
{
switch (role) {
case Qt::DisplayRole: {
if (auto* item = itemFromIndex(index)) {
return displayData(item, index.column());
}
break;
}
case Qt::FontRole: {
if (auto* item = itemFromIndex(index)) {
return item->font();
}
break;
}
case Qt::ToolTipRole: {
if (auto* item = itemFromIndex(index)) {
return makeTooltip(*item);
}
return {};
}
case Qt::ForegroundRole: {
if (index.column() == 1) {
if (auto* item = itemFromIndex(index)) {
if (item->isConflicted()) {
return QBrush(Qt::red);
}
}
}
break;
}
case Qt::DecorationRole: {
if (index.column() == 0) {
if (auto* item = itemFromIndex(index)) {
return makeIcon(*item, index);
}
}
break;
}
}
return {};
}
QVariant FileTreeModel::headerData(int i, Qt::Orientation ori, int role) const
{
static const std::array<QString, ColumnCount> names = {
tr("Name"), tr("Mod"), tr("Type"), tr("Size"), tr("Date modified")};
if (role == Qt::DisplayRole) {
if (i >= 0 && i < static_cast<int>(names.size())) {
return names[static_cast<std::size_t>(i)];
}
}
return {};
}
Qt::ItemFlags FileTreeModel::flags(const QModelIndex& index) const
{
auto f = QAbstractItemModel::flags(index);
if (auto* item = itemFromIndex(index)) {
if (!item->hasChildren()) {
f |= Qt::ItemNeverHasChildren;
}
}
return f;
}
void FileTreeModel::sortItem(FileTreeItem& item, bool force)
{
emit layoutAboutToBeChanged({}, QAbstractItemModel::VerticalSortHint);
const auto oldList = persistentIndexList();
std::vector<std::pair<FileTreeItem*, int>> oldItems;
const auto itemCount = oldList.size();
oldItems.reserve(static_cast<std::size_t>(itemCount));
for (int i = 0; i < itemCount; ++i) {
const QModelIndex& index = oldList[i];
oldItems.push_back({itemFromIndex(index), index.column()});
}
item.sort(m_sort.column, m_sort.order, force);
QModelIndexList newList;
newList.reserve(itemCount);
for (int i = 0; i < itemCount; ++i) {
const auto& pair = oldItems[static_cast<std::size_t>(i)];
newList.append(indexFromItem(*pair.first, pair.second));
}
changePersistentIndexList(oldList, newList);
emit layoutChanged({}, QAbstractItemModel::VerticalSortHint);
}
void FileTreeModel::sort(int column, Qt::SortOrder order)
{
m_sort.column = column;
m_sort.order = order;
sortItem(*m_root, true);
}
FileTreeItem* FileTreeModel::itemFromIndex(const QModelIndex& index) const
{
if (!index.isValid()) {
return m_root.get();
}
auto* parentItem = getItem(index);
if (!parentItem) {
log::error("FileTreeModel::itemFromIndex(): no internal pointer");
return nullptr;
}
if (index.row() < 0 || index.row() >= parentItem->children().size()) {
log::error("FileTreeModel::itemFromIndex(): row {} is out of range for {}",
index.row(), parentItem->debugName());
return nullptr;
}
return parentItem->children()[index.row()].get();
}
QModelIndex FileTreeModel::indexFromItem(FileTreeItem& item, int col) const
{
auto* parent = item.parent();
if (!parent) {
return {};
}
const int index = parent->childIndex(item);
if (index == -1) {
log::error("FileTreeMode::indexFromItem(): item {} not found in parent",
item.debugName());
return {};
}
return createIndex(index, col, makeInternalPointer(parent));
}
void FileTreeModel::update(FileTreeItem& parentItem,
const MOShared::DirectoryEntry& parentEntry,
const std::wstring& parentPath, bool forFetching)
{
trace(log::debug("updating {}", parentItem.debugName()));
auto path = parentPath;
if (!parentEntry.isTopLevel()) {
if (!path.empty()) {
path += L"\\";
}
path += parentEntry.getName();
}
parentItem.setLoaded(true);
bool added = false;
if (updateDirectories(parentItem, path, parentEntry, forFetching)) {
added = true;
}
if (updateFiles(parentItem, path, parentEntry)) {
added = true;
}
if (added) {
parentItem.makeSortingStale();
// see comment at the top of this file
if (forFetching) {
// don't pass a specific item, this will start a timer and re-sort the
// whole tree, which is faster than potentially queuing every single
// node if the whole tree is expanded
queueSortItem(nullptr);
}
}
}
bool FileTreeModel::updateDirectories(FileTreeItem& parentItem,
const std::wstring& parentPath,
const MOShared::DirectoryEntry& parentEntry,
bool forFetching)
{
// removeDisappearingDirectories() will add directories that are in the
// tree and still on the filesystem to this set; addNewDirectories() will
// use this to figure out if a directory is new or not
std::unordered_set<std::wstring_view> seen;
removeDisappearingDirectories(parentItem, parentEntry, parentPath, seen, forFetching);
return addNewDirectories(parentItem, parentEntry, parentPath, seen);
}
void FileTreeModel::removeDisappearingDirectories(
FileTreeItem& parentItem, const MOShared::DirectoryEntry& parentEntry,
const std::wstring& parentPath, std::unordered_set<std::wstring_view>& seen,
bool forFetching)
{
auto& children = parentItem.children();
auto itor = children.begin();
// keeps track of the contiguous directories that need to be removed to
// avoid calling beginRemoveRows(), etc. for each item
Range range(this, parentItem);
// for each item in this tree item
while (itor != children.end()) {
const auto& item = *itor;
if (!item->isDirectory()) {
// directories are always first, no point continuing once a file has
// been seen
break;
}
auto d = parentEntry.findSubDirectory(item->filenameWsLowerCase(), true);
if (d) {
trace(log::debug("dir {} still there", item->filename()));
// directory is still there
seen.emplace(d->getName());
bool currentRemoved = false;
if (item->areChildrenVisible()) {
// the item is currently expanded, update it
update(*item, *d, parentPath, forFetching);
}
if (shouldShowFolder(*d, item.get())) {
// folder should be left in the list
if (!item->areChildrenVisible() && item->isLoaded()) {
if (!d->isEmpty()) {
// the item is loaded (previously expanded but now collapsed) and
// has children, mark it as unloaded so it updates when next
// expanded
item->setLoaded(false);
if (!item->children().empty()) {
// if the item had children, remove them from the tree
// see comment at the top of this file
if (forFetching) {
queueRemoveItem(item.get());
} else {
Range::removeChildren(this, *item);
}
}
}
}
} else {
// item wouldn't have any children, prune it
trace(log::debug("dir {} is empty and pruned", item->filename()));
range.includeCurrent();
currentRemoved = true;
++itor;
}
if (!currentRemoved) {
// if there were directories before this row that need to be removed,
// do it now
itor = range.remove();
}
} else {
// directory is gone from the parent entry
trace(log::debug("dir {} is gone", item->filename()));
range.includeCurrent();
++itor;
}
range.next();
}
// remove the last directory range, if any
range.remove();
}
bool FileTreeModel::addNewDirectories(FileTreeItem& parentItem,
const MOShared::DirectoryEntry& parentEntry,
const std::wstring& parentPath,
const std::unordered_set<std::wstring_view>& seen)
{
// keeps track of the contiguous directories that need to be added to
// avoid calling beginAddRows(), etc. for each item
Range range(this, parentItem);
std::vector<FileTreeItem::Ptr> toAdd;
bool added = false;
// for each directory on the filesystem
for (auto&& d : parentEntry.getSubDirectories()) {
if (seen.contains(d->getName())) {
// already seen in the parent item
// if there were directories before this row that need to be added,
// do it now
//
// todo: if the directory was actually removed in
// removeDisappearingDirectories(), the range doesn't need to be added
// now and could be extended further
range.add(std::move(toAdd));
toAdd.clear();
} else {
if (!shouldShowFolder(*d, nullptr)) {
// this is a new directory, but it doesn't contain anything interesting
trace(log::debug("new dir {}, empty and pruned",
QString::fromStdWString(d->getName())));
// act as if this directory doesn't exist at all
continue;
}
// this is a new directory
trace(log::debug("new dir {}", QString::fromStdWString(d->getName())));
toAdd.push_back(createDirectoryItem(parentItem, parentPath, *d));
added = true;
range.includeCurrent();
}
range.next();
}
// add the last directory range, if any
range.add(std::move(toAdd));
return added;
}
bool FileTreeModel::updateFiles(FileTreeItem& parentItem,
const std::wstring& parentPath,
const MOShared::DirectoryEntry& parentEntry)
{
// removeDisappearingFiles() will add files that are in the tree and still on
// the filesystem to this set; addNewFiless() will use this to figure out if
// a file is new or not
std::unordered_set<FileIndex> seen;
int firstFileRow = 0;
removeDisappearingFiles(parentItem, parentEntry, firstFileRow, seen);
return addNewFiles(parentItem, parentEntry, parentPath, firstFileRow, seen);
}
void FileTreeModel::removeDisappearingFiles(FileTreeItem& parentItem,
const MOShared::DirectoryEntry& parentEntry,
int& firstFileRow,
std::unordered_set<FileIndex>& seen)
{
auto& children = parentItem.children();
auto itor = children.begin();
firstFileRow = -1;
// keeps track of the contiguous directories that need to be removed to
// avoid calling beginRemoveRows(), etc. for each item
Range range(this, parentItem);
// for each item in this tree item
while (itor != children.end()) {
const auto& item = *itor;
if (!item->isDirectory()) {
if (firstFileRow == -1) {
firstFileRow = range.current();
}
auto f = parentEntry.findFile(item->key());
if (f && shouldShowFile(*f)) {
trace(log::debug("file {} still there", item->filename()));
// file is still there
seen.emplace(f->getIndex());
if (f->getOrigin() != item->originID()) {
// origin has changed
updateFileItem(*item, *f);
}
// if there were files before this row that need to be removed,
// do it now
itor = range.remove();
} else {
// file is gone from the parent entry
trace(log::debug("file {} is gone", item->filename()));
range.includeCurrent();
++itor;
}
} else {
++itor;
}
range.next();
}
// remove the last file range, if any
range.remove();
if (firstFileRow == -1) {
firstFileRow = static_cast<int>(children.size());
}
}
bool FileTreeModel::addNewFiles(FileTreeItem& parentItem,
const MOShared::DirectoryEntry& parentEntry,
const std::wstring& parentPath, const int firstFileRow,
const std::unordered_set<FileIndex>& seen)
{
// keeps track of the contiguous files that need to be added to
// avoid calling beginAddRows(), etc. for each item
std::vector<FileTreeItem::Ptr> toAdd;
Range range(this, parentItem, firstFileRow);
bool added = false;
// for each directory on the filesystem
parentEntry.forEachFileIndex([&](auto&& fileIndex) {
if (seen.contains(fileIndex)) {
// already seen in the parent item
// if there were directories before this row that need to be added,
// do it now
range.add(std::move(toAdd));
toAdd.clear();
} else {
const auto file = parentEntry.getFileByIndex(fileIndex);
if (!file) {
log::error("FileTreeModel::addNewFiles(): file index {} in path {} not found",
fileIndex, parentPath);
return true;
}
if (shouldShowFile(*file)) {
// this is a new file
trace(log::debug("new file {}", QString::fromStdWString(file->getName())));
toAdd.push_back(createFileItem(parentItem, parentPath, *file));
added = true;
range.includeCurrent();
} else {
// this is a new file, but it shouldn't be shown
trace(log::debug("new file {}, not shown",
QString::fromStdWString(file->getName())));
return true;
}
}
range.next();
return true;
});
// add the last file range, if any
range.add(std::move(toAdd));
return added;
}
void FileTreeModel::queueRemoveItem(FileTreeItem* item)
{
trace(log::debug("queuing {} for removal", item->debugName()));
m_removeItems.push_back(item);
m_removeTimer.start(1);
}
void FileTreeModel::removeItems()
{
// see comment at the top of this file
trace(log::debug("remove item timer: removing {} items", m_removeItems.size()));
auto copy = std::move(m_removeItems);
m_removeItems.clear();
m_removeTimer.stop();
for (auto&& f : copy) {
Range::removeChildren(this, *f);
}
}
void FileTreeModel::queueSortItem(FileTreeItem* item)
{
if (!m_sortingEnabled) {
return;
}
if (item) {
m_sortItems.push_back(item);
}
m_sortTimer.start(1);
}
void FileTreeModel::sortItems()
{
// see comment at the top of this file
if (m_sortItems.empty()) {
sortItem(*m_root, false);
} else {
log::debug("sort item timer: sorting {} items", m_sortItems.size());
auto items = std::move(m_sortItems);
m_sortItems.clear();
for (auto* item : items) {
sortItem(*item, false);
}
}
}
FileTreeItem::Ptr FileTreeModel::createDirectoryItem(FileTreeItem& parentItem,
const std::wstring& parentPath,
const DirectoryEntry& d)
{
auto item = FileTreeItem::createDirectory(this, &parentItem, parentPath, d.getName());
if (d.isEmpty()) {
// if this directory is empty, mark the item as loaded so the expand
// arrow doesn't show
item->setLoaded(true);
}
return item;
}
FileTreeItem::Ptr FileTreeModel::createFileItem(FileTreeItem& parentItem,
const std::wstring& parentPath,
const FileEntry& file)
{
auto item = FileTreeItem::createFile(this, &parentItem, parentPath, file.getName());
updateFileItem(*item, file);
item->setLoaded(true);
return item;
}
void FileTreeModel::updateFileItem(FileTreeItem& item, const MOShared::FileEntry& file)
{
bool isArchive = false;
int originID = file.getOrigin(isArchive);
FileTreeItem::Flags flags = FileTreeItem::NoFlags;
if (isArchive) {
flags |= FileTreeItem::FromArchive;
}
if (!file.getAlternatives().empty()) {
flags |= FileTreeItem::Conflicted;
}
item.setOrigin(originID, file.getFullPath(), flags, makeModName(file, originID));
if (file.getFileSize() != FileEntry::NoFileSize) {
item.setFileSize(file.getFileSize());
}
if (file.getCompressedFileSize() != FileEntry::NoFileSize) {
item.setCompressedFileSize(file.getCompressedFileSize());
}
}
bool FileTreeModel::shouldShowFile(const FileEntry& file) const
{
if (showConflictsOnly() &&
((file.getAlternatives().size() == 0) ||
QString::fromStdWString(file.getName())
.endsWith(ModInfo::s_HiddenExt, Qt::CaseInsensitive))) {
// only conflicts should be shown, but this file is hidden or not conflicted
return false;
}
if (!showArchives() && file.isFromArchive()) {
// files from archives shouldn't be shown, but this file is from an archive
return false;
}
if (!showHiddenFiles() && QString::fromStdWString(file.getName())
.endsWith(ModInfo::s_HiddenExt, Qt::CaseInsensitive)) {
// hidden files shouldn't be shown, but this file is hidden
return false;
}
return true;
}
bool FileTreeModel::shouldShowFolder(const DirectoryEntry& dir,
const FileTreeItem* item) const
{
if ((!showHiddenFiles() || showConflictsOnly()) &&
QString::fromStdWString(dir.getName())
.endsWith(ModInfo::s_HiddenExt, Qt::CaseInsensitive)) {
return false;
}
bool shouldPrune = m_flags.testFlag(PruneDirectories);
if (m_core.settings().archiveParsing()) {
if (!m_flags.testFlag(Archives)) {
// archive parsing is enabled but the tree shouldn't show archives; this
// is a bit of a special case for folders because they have to be hidden
// regardless of the PruneDirectories flag if they only exist in archives
//
// note that this test is inaccurate: if a loose folder exists but is
// empty, and the same folder exists in an archive but is _not_ empty,
// then it's considered to exist _only_ in an archive and will be pruned
//
// if directories are ever made first-class so they can retain their
// origins, this test can be made more accurate
shouldPrune = true;
}
}
if (!shouldPrune) {
// always show folders regardless of their content
return true;
}
if (item) {
if (item->isLoaded() && item->children().empty()) {
// item is loaded and has no children; prune it
return false;
}
}
bool foundFile = false;
// check all files in this directory, return early if a file should be shown
dir.forEachFile([&](auto&& f) {
if (shouldShowFile(f)) {
foundFile = true;
// stop
return false;
}
// continue
return true;
});
if (foundFile) {
return true;
}
// recurse into subdirectories
for (auto subdir : dir.getSubDirectories()) {
if (shouldShowFolder(*subdir, nullptr)) {
return true;
}
}
return false;
}
QVariant FileTreeModel::displayData(const FileTreeItem* item, int column) const
{
switch (column) {
case FileName: {
return item->filename();
}
case ModName: {
return item->mod();
}
case FileType: {
return item->fileType().value_or(QString());
}
case FileSize: {
if (item->isDirectory()) {
return {};
} else {
QString fs;
if (auto n = item->fileSize()) {
fs = localizedByteSize(*n);
}
if (auto n = item->compressedFileSize()) {
return QString("%1 (%2)").arg(fs).arg(localizedByteSize(*n));
} else {
return fs;
}
}
}
case LastModified: {
if (auto d = item->lastModified()) {
if (d.has_value() && d.value().isValid()) {
return QLocale::system().toString(d.value(), QLocale::ShortFormat);
}
}
return {};
}
default: {
return {};
}
}
}
std::wstring FileTreeModel::makeModName(const MOShared::FileEntry& file,
int originID) const
{
static const std::wstring Unmanaged = UnmanagedModName().toStdWString();
const auto& origin = m_core.directoryStructure()->getOriginByID(originID);
if (origin.getID() == 0) {
return Unmanaged;
}
std::wstring name = origin.getName();
const auto& archive = file.getArchive();
if (!archive.name().empty()) {
name += L" (" + archive.name() + L")";
}
return name;
}
QString FileTreeModel::makeTooltip(const FileTreeItem& item) const
{
auto nowrap = [&](auto&& s) {
return "<p style=\"white-space: pre; margin: 0; padding: 0;\">" + s + "</p>";
};
auto line = [&](auto&& caption, auto&& value) {
if (value.isEmpty()) {
return nowrap("<b>" + caption + ":</b>\n");
} else {
return nowrap("<b>" + caption + ":</b> " + value.toHtmlEscaped()) + "\n";
}
};
if (item.isDirectory()) {
return line(tr("Directory"), item.filename()) +
line(tr("Virtual path"), item.virtualPath());
}
static const QString ListStart = "<ul style=\""
"margin-left: 20px; "
"margin-top: 0; "
"margin-bottom: 0; "
"padding: 0; "
"-qt-list-indent: 0;"
"\">";
static const QString ListEnd = "</ul>";
QString s = line(tr("Virtual path"), item.virtualPath()) +
line(tr("Real path"), item.realPath()) + line(tr("From"), item.mod());
const auto file = m_core.directoryStructure()->searchFile(
item.dataRelativeFilePath().toStdWString(), nullptr);
if (file) {
const auto alternatives = file->getAlternatives();
QStringList list;
for (auto&& alt : alternatives) {
const auto& origin = m_core.directoryStructure()->getOriginByID(alt.originID());
list.push_back(QString::fromStdWString(origin.getName()));
}
if (list.size() == 1) {
s += line(tr("Also in"), list[0]);
} else if (list.size() >= 2) {
s += line(tr("Also in"), QString()) + ListStart;
for (auto&& alt : list) {
s += "<li>" + alt + "</li>";
}
s += ListEnd;
}
}
return s;
}
QVariant FileTreeModel::makeIcon(const FileTreeItem& item,
const QModelIndex& index) const
{
if (item.isDirectory()) {
return m_iconFetcher.genericDirectoryIcon();
}
auto v = m_iconFetcher.icon(item.realPath());
if (!v.isNull()) {
return v;
}
m_iconPending.push_back(index);
m_iconPendingTimer.start(std::chrono::milliseconds(1));
return m_iconFetcher.genericFileIcon();
}
void FileTreeModel::updatePendingIcons()
{
std::vector<QModelIndex> v(std::move(m_iconPending));
m_iconPending.clear();
for (auto&& index : v) {
emit dataChanged(index, index, {Qt::DecorationRole});
}
if (m_iconPending.empty()) {
m_iconPendingTimer.stop();
}
}
void FileTreeModel::removePendingIcons(const QModelIndex& parent, int first, int last)
{
auto itor = m_iconPending.begin();
while (itor != m_iconPending.end()) {
if (itor->parent() == parent) {
if (itor->row() >= first && itor->row() <= last) {
itor = m_iconPending.erase(itor);
continue;
}
}
++itor;
}
}
|