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
|
/*
Copyright (C) 2012 Sebastian Herbord. All rights reserved.
This file is part of Mod Organizer.
Mod Organizer 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.
Mod Organizer 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 Mod Organizer. If not, see <http://www.gnu.org/licenses/>.
*/
#include "modlistsortproxy.h"
#include "messagedialog.h"
#include "modinfo.h"
#include "modlistbypriorityproxy.h"
#include "modlistdropinfo.h"
#include "organizercore.h"
#include "profile.h"
#include "qtgroupingproxy.h"
#include <QApplication>
#include <QCheckBox>
#include <QDebug>
#include <QMenu>
#include <QMimeData>
#include <QTreeView>
#include <QWidgetAction>
#include <log.h>
using namespace MOBase;
ModListSortProxy::ModListSortProxy(Profile* profile, OrganizerCore* organizer)
: QSortFilterProxyModel(organizer), m_Organizer(organizer), m_Profile(profile),
m_FilterActive(false), m_FilterMode(FilterAnd),
m_FilterSeparators(SeparatorFilter)
{
setDynamicSortFilter(true); // this seems to work without dynamicsortfilter
// but I don't know why. This should be necessary
}
void ModListSortProxy::setProfile(Profile* profile)
{
m_Profile = profile;
}
void ModListSortProxy::updateFilterActive()
{
m_FilterActive = (!m_Criteria.empty() || !m_Filter.isEmpty());
emit filterActive(m_FilterActive);
}
void ModListSortProxy::setCriteria(const std::vector<Criteria>& criteria)
{
// avoid refreshing the filter unless we are checking all mods for update.
const bool changed = (criteria != m_Criteria);
const bool isForUpdates =
(!criteria.empty() && criteria[0].id == CategoryFactory::UpdateAvailable);
if (changed || isForUpdates) {
m_Criteria = criteria;
updateFilterActive();
invalidateFilter();
emit filterInvalidated();
}
}
unsigned long ModListSortProxy::flagsId(const std::vector<ModInfo::EFlag>& flags) const
{
unsigned long result = 0;
for (ModInfo::EFlag flag : flags) {
if ((flag != ModInfo::FLAG_FOREIGN) && (flag != ModInfo::FLAG_OVERWRITE)) {
result += 1 << (int)flag;
}
}
return result;
}
unsigned long ModListSortProxy::conflictFlagsId(
const std::vector<ModInfo::EConflictFlag>& flags) const
{
unsigned long result = 0;
for (ModInfo::EConflictFlag flag : flags) {
if ((flag != ModInfo::FLAG_OVERWRITE_CONFLICT)) {
result += 1 << (int)flag;
}
}
return result;
}
bool ModListSortProxy::lessThan(const QModelIndex& left, const QModelIndex& right) const
{
if (sourceModel()->hasChildren(left) || sourceModel()->hasChildren(right)) {
// when sorting by priority, we do not want to use the parent lessThan because
// it uses the display role which can be inconsistent (e.g. for backups)
if (sortColumn() != ModList::COL_PRIORITY) {
return QSortFilterProxyModel::lessThan(left, right);
} else if (qobject_cast<QtGroupingProxy*>(sourceModel())) {
// if the underlying proxy is a QtGroupingProxy we need to rely on
// Qt::DisplayRole because the other roles are not correctly handled
// by that kind of proxy
return left.data(Qt::DisplayRole).toInt() < right.data(Qt::DisplayRole).toInt();
}
}
bool lOk, rOk;
int leftIndex = left.data(ModList::IndexRole).toInt(&lOk);
int rightIndex = right.data(ModList::IndexRole).toInt(&rOk);
if (!lOk || !rOk) {
return false;
}
ModInfo::Ptr leftMod = ModInfo::getByIndex(leftIndex);
ModInfo::Ptr rightMod = ModInfo::getByIndex(rightIndex);
bool lt = left.data(ModList::PriorityRole).toInt() <
right.data(ModList::PriorityRole).toInt();
switch (left.column()) {
case ModList::COL_FLAGS: {
std::vector<ModInfo::EFlag> leftFlags = leftMod->getFlags();
std::vector<ModInfo::EFlag> rightFlags = rightMod->getFlags();
if (leftFlags.size() != rightFlags.size()) {
lt = leftFlags.size() < rightFlags.size();
} else {
lt = flagsId(leftFlags) < flagsId(rightFlags);
}
} break;
case ModList::COL_CONFLICTFLAGS: {
std::vector<ModInfo::EConflictFlag> leftFlags = leftMod->getConflictFlags();
std::vector<ModInfo::EConflictFlag> rightFlags = rightMod->getConflictFlags();
if (leftFlags.size() != rightFlags.size()) {
lt = leftFlags.size() < rightFlags.size();
} else {
lt = conflictFlagsId(leftFlags) < conflictFlagsId(rightFlags);
}
} break;
case ModList::COL_CONTENT: {
const auto& lContents = leftMod->getContents();
const auto& rContents = rightMod->getContents();
unsigned int lValue = 0;
unsigned int rValue = 0;
m_Organizer->modDataContents().forEachContentIn(
lContents, [&lValue](auto const& content) {
lValue += 2U << static_cast<unsigned int>(content.id());
});
m_Organizer->modDataContents().forEachContentIn(
rContents, [&rValue](auto const& content) {
rValue += 2U << static_cast<unsigned int>(content.id());
});
lt = lValue < rValue;
} break;
case ModList::COL_NAME: {
int comp = QString::compare(leftMod->name(), rightMod->name(), Qt::CaseInsensitive);
if (comp != 0)
lt = comp < 0;
} break;
case ModList::COL_CATEGORY: {
if (leftMod->primaryCategory() != rightMod->primaryCategory()) {
if (leftMod->primaryCategory() < 0)
lt = false;
else if (rightMod->primaryCategory() < 0)
lt = true;
else {
try {
CategoryFactory& categories = CategoryFactory::instance();
QString leftCatName = categories.getCategoryName(
categories.getCategoryIndex(leftMod->primaryCategory()));
QString rightCatName = categories.getCategoryName(
categories.getCategoryIndex(rightMod->primaryCategory()));
lt = leftCatName < rightCatName;
} catch (const std::exception& e) {
log::error("failed to compare categories: {}", e.what());
}
}
}
} break;
case ModList::COL_MODID: {
if (leftMod->nexusId() != rightMod->nexusId())
lt = leftMod->nexusId() < rightMod->nexusId();
} break;
case ModList::COL_VERSION: {
if (leftMod->version() != rightMod->version())
lt = leftMod->version() < rightMod->version();
} break;
case ModList::COL_INSTALLTIME: {
QDateTime leftTime = left.data().toDateTime();
QDateTime rightTime = right.data().toDateTime();
if (leftTime != rightTime)
return leftTime < rightTime;
} break;
case ModList::COL_GAME: {
if (leftMod->gameName() != rightMod->gameName()) {
lt = leftMod->gameName() < rightMod->gameName();
} else {
int comp =
QString::compare(leftMod->name(), rightMod->name(), Qt::CaseInsensitive);
if (comp != 0)
lt = comp < 0;
}
} break;
case ModList::COL_NOTES: {
QString leftComments = leftMod->comments();
QString rightComments = rightMod->comments();
if (leftComments != rightComments) {
if (leftComments.isEmpty()) {
lt = sortOrder() == Qt::DescendingOrder;
} else if (rightComments.isEmpty()) {
lt = sortOrder() == Qt::AscendingOrder;
} else {
lt = leftComments < rightComments;
}
}
} break;
case ModList::COL_PRIORITY: {
if (leftMod->isBackup() != rightMod->isBackup()) {
lt = leftMod->isBackup();
} else if (leftMod->isOverwrite() != rightMod->isOverwrite()) {
lt = rightMod->isOverwrite();
}
} break;
default: {
log::warn("Sorting is not defined for column {}", left.column());
} break;
}
return lt;
}
void ModListSortProxy::updateFilter(const QString& filter)
{
m_Filter = filter;
updateFilterActive();
invalidateFilter();
emit filterInvalidated();
}
bool ModListSortProxy::hasConflictFlag(
const std::vector<ModInfo::EConflictFlag>& flags) const
{
for (ModInfo::EConflictFlag flag : flags) {
if ((flag == ModInfo::FLAG_CONFLICT_MIXED) ||
(flag == ModInfo::FLAG_CONFLICT_OVERWRITE) ||
(flag == ModInfo::FLAG_CONFLICT_OVERWRITTEN) ||
(flag == ModInfo::FLAG_CONFLICT_REDUNDANT) ||
(flag == ModInfo::FLAG_ARCHIVE_CONFLICT_OVERWRITE) ||
(flag == ModInfo::FLAG_ARCHIVE_CONFLICT_OVERWRITTEN) ||
(flag == ModInfo::FLAG_ARCHIVE_CONFLICT_MIXED) ||
(flag == ModInfo::FLAG_ARCHIVE_LOOSE_CONFLICT_OVERWRITE) ||
(flag == ModInfo::FLAG_ARCHIVE_LOOSE_CONFLICT_OVERWRITTEN)) {
return true;
}
}
return false;
}
bool ModListSortProxy::filterMatchesModAnd(ModInfo::Ptr info, bool enabled) const
{
for (auto&& c : m_Criteria) {
if (!criteriaMatchMod(info, enabled, c)) {
return false;
}
}
return true;
}
bool ModListSortProxy::filterMatchesModOr(ModInfo::Ptr info, bool enabled) const
{
for (auto&& c : m_Criteria) {
if (criteriaMatchMod(info, enabled, c)) {
return true;
}
}
if (!m_Criteria.empty()) {
// nothing matched
return false;
}
return true;
}
bool ModListSortProxy::optionsMatchMod(ModInfo::Ptr info, bool) const
{
return true;
}
bool ModListSortProxy::criteriaMatchMod(ModInfo::Ptr info, bool enabled,
const Criteria& c) const
{
bool b = false;
switch (c.type) {
case TypeSpecial: // fall-through
case TypeCategory: {
b = categoryMatchesMod(info, enabled, c.id);
break;
}
case TypeContent: {
b = contentMatchesMod(info, enabled, c.id);
break;
}
default: {
log::error("bad criteria type {}", c.type);
break;
}
}
if (c.inverse) {
b = !b;
}
return b;
}
bool ModListSortProxy::categoryMatchesMod(ModInfo::Ptr info, bool enabled,
int category) const
{
bool b = false;
switch (category) {
case CategoryFactory::Checked: {
b = (enabled || info->alwaysEnabled());
break;
}
case CategoryFactory::UpdateAvailable: {
b = (info->updateAvailable() || info->downgradeAvailable());
break;
}
case CategoryFactory::HasCategory: {
b = !info->getCategories().empty();
break;
}
case CategoryFactory::Conflict: {
b = (hasConflictFlag(info->getConflictFlags()));
break;
}
case CategoryFactory::HasHiddenFiles: {
b = (info->hasFlag(ModInfo::FLAG_HIDDEN_FILES));
break;
}
case CategoryFactory::Endorsed: {
b = (info->endorsedState() == EndorsedState::ENDORSED_TRUE);
break;
}
case CategoryFactory::Backup: {
b = (info->hasFlag(ModInfo::FLAG_BACKUP));
break;
}
case CategoryFactory::Managed: {
b = (!info->hasFlag(ModInfo::FLAG_FOREIGN));
break;
}
case CategoryFactory::HasGameData: {
b = !info->hasFlag(ModInfo::FLAG_INVALID);
break;
}
case CategoryFactory::HasNexusID: {
// never show these
if (info->hasFlag(ModInfo::FLAG_FOREIGN) || info->hasFlag(ModInfo::FLAG_BACKUP) ||
info->hasFlag(ModInfo::FLAG_OVERWRITE)) {
return false;
}
b = (info->nexusId() > 0);
break;
}
case CategoryFactory::Tracked: {
b = (info->trackedState() == TrackedState::TRACKED_TRUE);
break;
}
default: {
b = (info->categorySet(category));
break;
}
}
return b;
}
bool ModListSortProxy::contentMatchesMod(ModInfo::Ptr info, bool enabled,
int content) const
{
return info->hasContent(content);
}
bool ModListSortProxy::filterMatchesMod(ModInfo::Ptr info, bool enabled) const
{
// don't check if there are no filters selected
if (!m_FilterActive) {
return true;
}
// special case for separators
if (info->hasFlag(ModInfo::FLAG_SEPARATOR)) {
switch (m_FilterSeparators) {
case SeparatorFilter: {
// filter normally
break;
}
case SeparatorShow: {
// force visible
return true;
}
case SeparatorHide: {
// force hide
return false;
}
}
}
if (!m_Filter.isEmpty()) {
bool display = false;
QString filterCopy = QString(m_Filter);
filterCopy.replace("||", ";").replace("OR", ";").replace("|", ";");
QStringList ORList = filterCopy.split(";", Qt::SkipEmptyParts);
bool segmentGood = true;
// split in ORSegments that internally use AND logic
for (auto& ORSegment : ORList) {
QStringList ANDKeywords = ORSegment.split(" ", Qt::SkipEmptyParts);
segmentGood = true;
bool foundKeyword = false;
// check each word in the segment for match, each word needs to be matched but it
// doesn't matter where.
for (auto& currentKeyword : ANDKeywords) {
foundKeyword = false;
// search keyword in name
if (m_EnabledColumns[ModList::COL_NAME] &&
info->name().contains(currentKeyword, Qt::CaseInsensitive)) {
foundKeyword = true;
}
// Search by notes
if (!foundKeyword && m_EnabledColumns[ModList::COL_NOTES] &&
(info->notes().contains(currentKeyword, Qt::CaseInsensitive) ||
info->comments().contains(currentKeyword, Qt::CaseInsensitive))) {
foundKeyword = true;
}
// Search by categories
if (!foundKeyword && m_EnabledColumns[ModList::COL_CATEGORY]) {
for (auto category : info->categories()) {
if (category.contains(currentKeyword, Qt::CaseInsensitive)) {
foundKeyword = true;
break;
}
}
}
// Search by Nexus ID
if (!foundKeyword && m_EnabledColumns[ModList::COL_MODID]) {
bool ok;
int filterID = currentKeyword.toInt(&ok);
if (ok) {
int modID = info->nexusId();
while (modID > 0) {
if (modID == filterID) {
foundKeyword = true;
break;
}
modID = (int)(modID / 10);
}
}
}
if (!foundKeyword) {
// currentKeword is missing from everything, AND fails and we need to check
// next ORsegment
segmentGood = false;
break;
}
} // for ANDKeywords loop
if (segmentGood) {
// the last AND loop didn't break so the ORSegments is true so mod matches
// filter
display = true;
break;
}
} // for ORList loop
if (!display) {
return false;
}
} // if (!m_CurrentFilter.isEmpty())
if (m_FilterMode == FilterAnd) {
return filterMatchesModAnd(info, enabled);
} else {
return filterMatchesModOr(info, enabled);
}
}
void ModListSortProxy::setColumnVisible(int column, bool visible)
{
m_EnabledColumns[column] = visible;
}
void ModListSortProxy::setOptions(ModListSortProxy::FilterMode mode,
SeparatorsMode separators)
{
if (m_FilterMode != mode || separators != m_FilterSeparators) {
m_FilterMode = mode;
m_FilterSeparators = separators;
invalidateFilter();
emit filterInvalidated();
}
}
bool ModListSortProxy::filterAcceptsRow(int source_row, const QModelIndex& parent) const
{
if (m_Profile == nullptr) {
return false;
}
if (source_row >= static_cast<int>(m_Profile->numMods())) {
log::warn("invalid row index: {}", source_row);
return false;
}
QModelIndex idx = sourceModel()->index(source_row, 0, parent);
if (!idx.isValid()) {
log::debug("invalid mod index");
return false;
}
unsigned int index = ULONG_MAX;
{
bool ok = false;
index = idx.data(ModList::IndexRole).toInt(&ok);
if (!ok) {
index = ULONG_MAX;
}
}
if (sourceModel()->hasChildren(idx)) {
// we need to check the separator itself first
if (index < ModInfo::getNumMods() && ModInfo::getByIndex(index)->isSeparator()) {
if (filterMatchesMod(ModInfo::getByIndex(index), false)) {
return true;
}
}
for (int i = 0; i < sourceModel()->rowCount(idx); ++i) {
if (filterAcceptsRow(i, idx)) {
return true;
}
}
return false;
} else {
bool modEnabled =
idx.sibling(source_row, 0).data(Qt::CheckStateRole).toInt() == Qt::Checked;
return filterMatchesMod(ModInfo::getByIndex(index), modEnabled);
}
}
bool ModListSortProxy::sourceIsByPriorityProxy() const
{
return dynamic_cast<ModListByPriorityProxy*>(sourceModel()) != nullptr;
}
bool ModListSortProxy::canDropMimeData(const QMimeData* data, Qt::DropAction action,
int row, int column,
const QModelIndex& parent) const
{
ModListDropInfo dropInfo(data, *m_Organizer);
if (!dropInfo.isLocalFileDrop() && sortColumn() != ModList::COL_PRIORITY) {
return false;
}
// disable drop install with group proxy, except the one for collapsible separator
// - it would be nice to be able to "install to category" or something like that but
// it's a bit more complicated since the drop position is based on the category, so
// just disabling for now
if (dropInfo.isDownloadDrop()) {
// maybe there is a cleaner way?
if (qobject_cast<QtGroupingProxy*>(sourceModel())) {
return false;
}
}
// see dropMimeData for details
if (sortOrder() == Qt::DescendingOrder && row != -1 && !sourceIsByPriorityProxy()) {
--row;
}
return QSortFilterProxyModel::canDropMimeData(data, action, row, column, parent);
}
bool ModListSortProxy::dropMimeData(const QMimeData* data, Qt::DropAction action,
int row, int column, const QModelIndex& parent)
{
ModListDropInfo dropInfo(data, *m_Organizer);
if (!dropInfo.isLocalFileDrop() && sortColumn() != ModList::COL_PRIORITY) {
QWidget* wid = qApp->activeWindow()->findChild<QTreeView*>("modList");
MessageDialog::showMessage(
tr("Drag&Drop is only supported when sorting by priority"), wid);
return false;
}
if (row == -1 && column == -1) {
return sourceModel()->dropMimeData(data, action, -1, -1, mapToSource(parent));
}
// in the regular model, when dropping between rows, the row-value passed to
// the sourceModel is inconsistent between ascending and descending ordering
//
// we want to fix that, but we cannot do it for the by-priority proxy because
// it messes up with non top-level items, so we simply forward the row and the
// by-priority proxy will fix the row for us
if (sortOrder() == Qt::DescendingOrder && row != -1 && !sourceIsByPriorityProxy()) {
--row;
}
return QSortFilterProxyModel::dropMimeData(data, action, row, column, parent);
}
void ModListSortProxy::setSourceModel(QAbstractItemModel* sourceModel)
{
QSortFilterProxyModel::setSourceModel(sourceModel);
QAbstractProxyModel* proxy = qobject_cast<QAbstractProxyModel*>(sourceModel);
if (proxy != nullptr) {
sourceModel = proxy->sourceModel();
}
if (sourceModel) {
connect(sourceModel, SIGNAL(aboutToChangeData()), this, SLOT(aboutToChangeData()),
Qt::UniqueConnection);
connect(sourceModel, SIGNAL(postDataChanged()), this, SLOT(postDataChanged()),
Qt::UniqueConnection);
}
}
void ModListSortProxy::aboutToChangeData()
{
// having a filter active when dataChanged is called caused a crash
// (at least with some Qt versions)
// this may be related to the fact that the item being edited may disappear from the
// view as a result of the edit
m_PreChangeCriteria = m_Criteria;
setCriteria({});
}
void ModListSortProxy::postDataChanged()
{
// if the filter is re-activated right away the editor can't be deleted but becomes
// invisible or at least the view continues to think it's being edited. As a result no
// new editor can be opened
QTimer::singleShot(10, [this]() {
setCriteria(m_PreChangeCriteria);
m_PreChangeCriteria.clear();
});
}
|