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
|
#include "modinfodialogconflictsmodels.h"
#include "modinfodialog.h"
#include <utility.h>
using MOBase::naturalCompare;
ConflictItem::ConflictItem(QString before, QString relativeName, QString after,
MOShared::FileIndex index, QString fileName,
bool hasAltOrigins, QString altOrigin, bool archive)
: m_before(std::move(before)), m_relativeName(std::move(relativeName)),
m_after(std::move(after)), m_index(index), m_fileName(std::move(fileName)),
m_hasAltOrigins(hasAltOrigins), m_altOrigin(std::move(altOrigin)),
m_isArchive(archive)
{}
const QString& ConflictItem::before() const
{
return m_before;
}
const QString& ConflictItem::relativeName() const
{
return m_relativeName;
}
const QString& ConflictItem::after() const
{
return m_after;
}
const QString& ConflictItem::fileName() const
{
return m_fileName;
}
const QString& ConflictItem::altOrigin() const
{
return m_altOrigin;
}
bool ConflictItem::hasAlts() const
{
return m_hasAltOrigins;
}
bool ConflictItem::isArchive() const
{
return m_isArchive;
}
MOShared::FileIndex ConflictItem::fileIndex() const
{
return m_index;
}
bool ConflictItem::canHide() const
{
return canHideFile(isArchive(), fileName());
}
bool ConflictItem::canUnhide() const
{
return canUnhideFile(isArchive(), fileName());
}
bool ConflictItem::canRun() const
{
return canRunFile(isArchive(), fileName());
}
bool ConflictItem::canOpen() const
{
return canOpenFile(isArchive(), fileName());
}
bool ConflictItem::canPreview(PluginContainer& pluginContainer) const
{
return canPreviewFile(pluginContainer, isArchive(), fileName());
}
bool ConflictItem::canExplore() const
{
return canExploreFile(isArchive(), fileName());
}
ConflictListModel::ConflictListModel(QTreeView* tree, std::vector<Column> columns)
: m_tree(tree), m_columns(std::move(columns)), m_sortColumn(-1),
m_sortOrder(Qt::AscendingOrder)
{
m_tree->setModel(this);
}
void ConflictListModel::clear()
{
beginResetModel();
m_items.clear();
endResetModel();
}
void ConflictListModel::reserve(std::size_t s)
{
m_items.reserve(s);
}
QModelIndex ConflictListModel::index(int row, int col, const QModelIndex&) const
{
return createIndex(row, col);
}
QModelIndex ConflictListModel::parent(const QModelIndex&) const
{
return {};
}
int ConflictListModel::rowCount(const QModelIndex& parent) const
{
if (parent.isValid()) {
return 0;
}
return static_cast<int>(m_items.size());
}
int ConflictListModel::columnCount(const QModelIndex&) const
{
return static_cast<int>(m_columns.size());
}
const ConflictItem* ConflictListModel::itemFromIndex(const QModelIndex& index) const
{
const auto row = index.row();
if (row < 0) {
return nullptr;
}
const auto i = static_cast<std::size_t>(row);
if (i >= m_items.size()) {
return nullptr;
}
return &m_items[i];
}
QModelIndex ConflictListModel::indexFromItem(const ConflictItem* item, int col)
{
for (std::size_t i = 0; i < m_items.size(); ++i) {
if (&m_items[i] == item) {
return createIndex(static_cast<int>(i), col);
}
}
return {};
}
QVariant ConflictListModel::data(const QModelIndex& index, int role) const
{
if (role == Qt::DisplayRole || role == Qt::FontRole) {
const ConflictItem* item = itemFromIndex(index);
if (!item) {
return {};
}
const auto col = index.column();
if (col < 0) {
return {};
}
const auto c = static_cast<std::size_t>(col);
if (c >= m_columns.size()) {
return {};
}
if (role == Qt::DisplayRole) {
return (item->*m_columns[c].getText)();
} else if (role == Qt::FontRole) {
if (item->isArchive()) {
QFont f = m_tree->font();
f.setItalic(true);
return f;
}
}
}
return {};
}
QVariant ConflictListModel::headerData(int col, Qt::Orientation, int role) const
{
if (role == Qt::DisplayRole) {
if (col < 0) {
return {};
}
const auto i = static_cast<std::size_t>(col);
if (i >= m_columns.size()) {
return {};
}
return m_columns[i].caption;
}
return {};
}
void ConflictListModel::sort(int colIndex, Qt::SortOrder order)
{
m_sortColumn = colIndex;
m_sortOrder = order;
emit layoutAboutToBeChanged({}, QAbstractItemModel::VerticalSortHint);
const auto oldList = persistentIndexList();
std::vector<std::pair<const ConflictItem*, 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()});
}
doSort();
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 ConflictListModel::add(ConflictItem item)
{
m_items.emplace_back(std::move(item));
}
void ConflictListModel::finished()
{
beginResetModel();
endResetModel();
sort(m_sortColumn, m_sortOrder);
}
const ConflictItem* ConflictListModel::getItem(std::size_t row) const
{
if (row >= m_items.size()) {
return nullptr;
}
return &m_items[row];
}
void ConflictListModel::doSort()
{
if (m_items.empty()) {
return;
}
if (m_sortColumn < 0) {
return;
}
const auto c = static_cast<std::size_t>(m_sortColumn);
if (c >= m_columns.size()) {
return;
}
const auto& col = m_columns[c];
// avoids branching on sort order while sorting
auto sortAsc = [&](const auto& a, const auto& b) {
return (naturalCompare((a.*col.getText)(), (b.*col.getText)()) < 0);
};
auto sortDesc = [&](const auto& a, const auto& b) {
return (naturalCompare((a.*col.getText)(), (b.*col.getText)()) > 0);
};
if (m_sortOrder == Qt::AscendingOrder) {
std::sort(m_items.begin(), m_items.end(), sortAsc);
} else {
std::sort(m_items.begin(), m_items.end(), sortDesc);
}
}
OverwriteConflictListModel::OverwriteConflictListModel(QTreeView* tree)
: ConflictListModel(tree, {{tr("File"), &ConflictItem::relativeName},
{tr("Overwritten Mods"), &ConflictItem::before}})
{}
OverwrittenConflictListModel::OverwrittenConflictListModel(QTreeView* tree)
: ConflictListModel(tree, {{tr("File"), &ConflictItem::relativeName},
{tr("Providing Mod"), &ConflictItem::after}})
{}
NoConflictListModel::NoConflictListModel(QTreeView* tree)
: ConflictListModel(tree, {{tr("File"), &ConflictItem::relativeName}})
{}
AdvancedConflictListModel::AdvancedConflictListModel(QTreeView* tree)
: ConflictListModel(tree, {{tr("Overwrites"), &ConflictItem::before},
{tr("File"), &ConflictItem::relativeName},
{tr("Overwritten By"), &ConflictItem::after}})
{}
|