This repository has been archived by the owner on May 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTreeModModel.cpp
502 lines (411 loc) · 12.9 KB
/
TreeModModel.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
#include "TreeModModel.h"
#include <QtWidgets>
TreeModModel::TreeModModel(SettingsInterface* settingsInterface, OpenMWConfigInterface* configInterface, QObject *parent)
: QAbstractItemModel(parent)
{
settings = settingsInterface;
config = configInterface;
QVector<QVariant> rootData;
rootData << tr("Index") << tr("Mod") << tr("Folder") << tr("Enabled");
rootItem = new TreeModItem(rootData);
loadDataFromJson();
}
TreeModModel::~TreeModModel()
{
saveDataToJson();
saveDataToConfig();
delete rootItem;
}
int TreeModModel::columnCount(const QModelIndex & /* parent */) const
{
return rootItem->columnCount();
}
QVariant TreeModModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::TextColorRole)
{
foreach (const QModelIndex& conflict, currentConflicts)
{
if (conflict == index.sibling(index.row(),0))
return QVariant(QColor(255, 0, 0));
}
}
if (role == Qt::DisplayRole || role == Qt::EditRole)
{
if (index.column() != TreeModItem::COLUMN_ENABLED)
{
return getItem(index)->data(index.column());
}
}
else if (role == Qt::CheckStateRole && index.column() == TreeModItem::COLUMN_ENABLED)
{
return getItem(index)->data(index.column()).toBool() ? Qt::Checked : Qt::Unchecked;
}
return QVariant();
}
Qt::ItemFlags TreeModModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::ItemIsDropEnabled;
QFlags<Qt::ItemFlag> flag = QAbstractItemModel::flags(index);
flag.setFlag(Qt::ItemIsSelectable, true);
flag.setFlag(Qt::ItemIsEditable, true);
if (index.column() == TreeModItem::COLUMN_INDEX)
{
flag.setFlag(Qt::ItemIsEditable, false);
flag.setFlag(Qt::ItemIsDragEnabled, true);
}
else if (index.column() == TreeModItem::COLUMN_ENABLED)
{
flag.setFlag(Qt::ItemIsEditable, false);
flag.setFlag(Qt::ItemIsUserCheckable, true);
}
return flag;
}
TreeModItem *TreeModModel::getItem(const QModelIndex &index) const
{
if (index.isValid()) {
TreeModItem *item = static_cast<TreeModItem*>(index.internalPointer());
if (item)
return item;
}
return rootItem;
}
QVariant TreeModModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
return rootItem->data(section);
return QVariant();
}
QModelIndex TreeModModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
TreeModItem *parentItem = getItem(parent);
TreeModItem *childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
}
bool TreeModModel::insertColumns(int position, int columns, const QModelIndex &parent)
{
bool success;
beginInsertColumns(parent, position, position + columns - 1);
success = rootItem->insertColumns(position, columns);
endInsertColumns();
return success;
}
bool TreeModModel::insertRows(int position, int rows, const QModelIndex &parent)
{
TreeModItem *parentItem = getItem(parent);
bool success;
beginInsertRows(parent, position, position + rows - 1);
success = parentItem->insertChildren(position, rows, rootItem->columnCount());
endInsertRows();
// Redo indexing
recalculateIndexes(parentItem, position);
return success;
}
QModelIndex TreeModModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
TreeModItem *childItem = getItem(index);
TreeModItem *parentItem = childItem->parent();
if (parentItem == rootItem)
return QModelIndex();
return createIndex(parentItem->childNumber(), 0, parentItem);
}
QModelIndex TreeModModel::getIndexForFolder(const QString& folder)
{
QModelIndexList matches = this->match(this->index(0, TreeModItem::COLUMN_FOLDER), Qt::DisplayRole, QVariant::fromValue(folder), 1, Qt::MatchCaseSensitive | Qt::MatchRecursive);
if (matches.count() > 0)
return matches.first();
return QModelIndex();
}
bool TreeModModel::removeColumns(int position, int columns, const QModelIndex &parent)
{
bool success;
beginRemoveColumns(parent, position, position + columns - 1);
success = rootItem->removeColumns(position, columns);
endRemoveColumns();
if (rootItem->columnCount() == 0)
removeRows(0, rowCount());
return success;
}
bool TreeModModel::removeRows(int position, int rows, const QModelIndex &parent)
{
TreeModItem* parentItem = getItem(parent);
bool success = true;
for (int r = 0; r < rows; r++)
{
QString folder = parent.child(position+r, TreeModItem::COLUMN_FOLDER).data().toString();
foreach(const QString& key, folderConflicts.keys())
{
if (folderConflicts[key].contains(folder))
folderConflicts.remove(folder);
if (folderConflicts[key].isEmpty())
folderConflicts.remove(key);
}
}
// if (index.column() == TreeModItem::COLUMN_FOLDER)
// {
// QString oldFolder = parent.child(row)
// QString newFolder = value.toString();
// // Remove all references to the old folder.
// foreach(const QString& key, folderConflicts)
// {
// if (folderConflicts[key].contains(oldFolder))
// folderConflicts.remove(oldFolder);
// if (folderConflicts[key].isEmpty())
// folderConflicts.remove(key);
// }
// }
beginRemoveRows(parent, position, position + rows - 1);
success = parentItem->removeChildren(position, rows);
endRemoveRows();
// Redo indexing
recalculateIndexes(parentItem, position);
// Clear conflicts; another selection is going to come right after.
currentConflicts.clear();
return success;
}
int TreeModModel::rowCount(const QModelIndex &parent) const
{
return getItem(parent)->childCount();
}
bool TreeModModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (role == Qt::CheckStateRole && index.column() == TreeModItem::COLUMN_ENABLED)
{
return getItem(index)->setData(index.column(), value.toBool() ? Qt::Checked : Qt::Unchecked);
}
if (role != Qt::EditRole)
return false;
if (index.column() == TreeModItem::COLUMN_FOLDER)
{
QString oldFolder = index.data().toString();
QString newFolder = value.toString();
// Remove all references to the old folder.
foreach(const QString& key, folderConflicts.keys())
{
if (folderConflicts[key].contains(oldFolder))
folderConflicts.remove(oldFolder);
if (folderConflicts[key].isEmpty())
folderConflicts.remove(key);
}
// Go through all files and add them to the conflict map.
if (QDir(newFolder).exists())
{
QDirIterator it(newFolder, QStringList(), QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext())
{
QString foundPath = it.next();
QString relativePath = foundPath.right(foundPath.length() - newFolder.length() - 1);
folderConflicts[relativePath].push_back(newFolder);
}
}
}
bool result = false;
if (index.column() == TreeModItem::COLUMN_INDEX)
result = getItem(index)->setData(index.column(), index.row());
else
result = getItem(index)->setData(index.column(), value);
if (result)
emit dataChanged(index, index);
return result;
}
bool TreeModModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
{
if (role != Qt::EditRole || orientation != Qt::Horizontal)
return false;
bool result = rootItem->setData(section, value);
if (result)
emit headerDataChanged(orientation, section, section);
return result;
}
void TreeModModel::addMods(const QJsonArray& modsArray, const QModelIndex& parent)
{
foreach( QJsonValue mod, modsArray )
{
QJsonObject modTable = mod.toObject();
QVector<QVariant> data;
data << 0 << modTable["name"].toString() << modTable["folder"].toString() << modTable["enabled"].toBool();
int newRow = this->rowCount(parent);
this->insertRow(newRow, parent);
for (int column = 0; column < data.size(); column++)
{
this->setData(this->index(newRow, column, parent), data[column]);
}
QJsonValue subModsV = modTable["mods"];
if (!subModsV.isUndefined())
{
QJsonArray subModsArray = subModsV.toArray();
addMods(subModsArray, this->index(newRow, 0, parent));
}
}
}
void TreeModModel::loadDataFromJson()
{
QJsonArray modsArray = settings->getJsonDoc().object()["mods"].toArray();
addMods(modsArray, QModelIndex());
}
void TreeModModel::saveDataToJson()
{
settings->setModJson(rootItem);
}
void TreeModModel::saveDataToConfig()
{
QVector<QVariant>& dataVect = config->getByKey("data");
dataVect.clear();
rootItem->serialize(dataVect);
}
void TreeModModel::recalculateIndexes(TreeModItem* parent, int startAt)
{
for (int i = startAt; i < parent->childCount(); i++)
{
parent->child(i)->setData(TreeModItem::COLUMN_INDEX, i);
}
}
QStringList TreeModModel::mimeTypes() const
{
QStringList types;
types << "application/openmwmm.text.data";
return types;
}
QMimeData* TreeModModel::mimeData(const QModelIndexList &indexes) const
{
QMimeData *mimeData = new QMimeData();
QByteArray encodedData;
QDataStream stream(&encodedData, QIODevice::WriteOnly);
stream.setVersion(QDataStream::Qt_5_7);
// Serialize item.
stream << indexes.length();
foreach (const QModelIndex &index, indexes) {
if (index.isValid()) {
this->getItem(index)->serialize(stream);
}
}
mimeData->setData("application/openmwmm.text.data", encodedData);
return mimeData;
}
void TreeModModel::importFromDataStream(QDataStream& stream, const QModelIndex& parent, int row)
{
if (!this->insertRow(row, parent))
return;
for (int column = 0; column < this->columnCount(); column++)
{
QVariant columnData;
stream >> columnData;
QModelIndex child = this->index(row, column, parent);
if (column == TreeModItem::COLUMN_INDEX)
this->setData(child, row);
else if (column == TreeModItem::COLUMN_ENABLED)
this->setData(child, columnData, Qt::CheckStateRole);
else
this->setData(child, columnData);
}
// Handle children.
QVariant childrenCount;
stream >> childrenCount;
for (int child = 0; child < childrenCount.toInt(); child++)
importFromDataStream(stream, this->index(row, 0, parent), child);
}
bool TreeModModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
{
if (!canDropMimeData(data, action, row, column, parent))
return false;
if (action == Qt::IgnoreAction)
return true;
if (data->hasFormat("application/openmwmm.text.data"))
{
QByteArray encodedData = data->data("application/openmwmm.text.data");
QDataStream stream(&encodedData, QIODevice::ReadOnly);
int sourceIndexCount;
stream >> sourceIndexCount;
for (int i = 0; i < sourceIndexCount; i++)
importFromDataStream(stream, parent, row+i);
return true;
}
return false;
}
bool TreeModModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const
{
Q_UNUSED(action);
Q_UNUSED(row);
Q_UNUSED(column);
Q_UNUSED(parent);
if (data->hasFormat("application/openmwmm.text.data"))
return true;
return false;
}
Qt::DropActions TreeModModel::supportedDragActions() const
{
return supportedDropActions();
}
Qt::DropActions TreeModModel::supportedDropActions() const
{
return Qt::MoveAction;
}
//! TODO: Optimize this.
void TreeModModel::updateConflictSelection(const QItemSelection& selected, const QItemSelection& deselected)
{
Q_UNUSED(deselected);
if (selected == currentSelection)
return;
currentSelection = selected;
// Clear conflicts.
QModelIndexList selectionCopy = currentConflicts;
currentConflicts.clear();
foreach(const QModelIndex& index, selectionCopy)
this->dataChanged(index.sibling(index.row(), 0), index.sibling(index.row(), TreeModItem::COLUMN_COUNT), QVector<int>() << Qt::TextColorRole);
// We don't care what happens if the selection is empty.
if (selected.empty())
return;
QString baseFolder;
QModelIndex thisIndex;
foreach(const QModelIndex& index, selected.indexes())
{
if (index.column() == TreeModItem::COLUMN_FOLDER)
{
thisIndex = index;
baseFolder = index.data().toString();
break;
}
}
if (baseFolder.isEmpty())
{
qDebug("Warning: Could not locate folder for selection.");
return;
}
QDirIterator it(baseFolder, QStringList(), QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext())
{
QString foundPath = it.next();
QString relativePath = foundPath.right(foundPath.length() - baseFolder.length() - 1);
if (folderConflicts.contains(relativePath) && folderConflicts[relativePath].size() > 1)
{
foreach (const QString& conflictingFolder, folderConflicts[relativePath])
{
if (conflictingFolder == baseFolder)
continue;
QModelIndex conflictingIndex = getIndexForFolder(conflictingFolder);
if (!conflictingIndex.isValid())
{
qDebug() << "Warning: Could not find index for conflict for '" + conflictingFolder + "'";
continue;
}
while (conflictingIndex.isValid())
{
currentConflicts.push_back(conflictingIndex.sibling(conflictingIndex.row(), 0));
this->dataChanged(conflictingIndex.sibling(conflictingIndex.row(), 0), conflictingIndex.sibling(conflictingIndex.row(), TreeModItem::COLUMN_COUNT), QVector<int>() << Qt::TextColorRole);
QModelIndex p = conflictingIndex.parent();
conflictingIndex = p.sibling(p.row(), conflictingIndex.column());
}
}
}
}
}