-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathmodinfodialogesps.cpp
More file actions
371 lines (292 loc) · 7.95 KB
/
modinfodialogesps.cpp
File metadata and controls
371 lines (292 loc) · 7.95 KB
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
#include "modinfodialogesps.h"
#include "modinfodialog.h"
#include "settings.h"
#include "ui_modinfodialog.h"
#include <log.h>
#include <report.h>
using namespace MOBase;
class ESPItem
{
public:
ESPItem(QString rootPath, QString relativePath)
: m_rootPath(std::move(rootPath)), m_active(false)
{
if (relativePath.contains('/') || relativePath.contains('\\')) {
m_inactivePath = relativePath;
} else {
m_activePath = relativePath;
m_active = true;
}
pathChanged();
}
const QString& rootPath() const { return m_rootPath; }
const QString& relativePath() const
{
if (m_active) {
return m_activePath;
} else {
return m_inactivePath;
}
}
const QString& filename() const { return m_filename; }
const QString& activePath() const { return m_activePath; }
const QString& inactivePath() const { return m_inactivePath; }
const QFileInfo& fileInfo() const { return m_fileInfo; }
bool isActive() const { return m_active; }
bool activate(const QString& newName)
{
QDir root(m_rootPath);
if (root.rename(m_inactivePath, newName)) {
m_active = true;
m_activePath = newName;
if (QFileInfo(m_inactivePath).fileName() != newName) {
// file was renamed
m_inactivePath = QFileInfo(m_inactivePath).path() + QDir::separator() + newName;
}
pathChanged();
return true;
}
return false;
}
bool deactivate(const QString& newName)
{
QDir root(m_rootPath);
if (root.rename(m_activePath, newName)) {
m_active = false;
m_inactivePath = newName;
pathChanged();
return true;
}
return false;
}
private:
QString m_rootPath;
QString m_activePath;
QString m_inactivePath;
QString m_filename;
QFileInfo m_fileInfo;
bool m_active;
void pathChanged()
{
m_fileInfo.setFile(m_rootPath + QDir::separator() + relativePath());
m_filename = m_fileInfo.fileName();
}
};
class ESPListModel : public QAbstractItemModel
{
public:
void clear()
{
beginResetModel();
m_esps.clear();
endResetModel();
}
QModelIndex index(int row, int col, const QModelIndex& = {}) const override
{
return createIndex(row, col);
}
QModelIndex parent(const QModelIndex&) const override { return {}; }
int rowCount(const QModelIndex& = {}) const override
{
return static_cast<int>(m_esps.size());
}
int columnCount(const QModelIndex& = {}) const override { return 1; }
QVariant data(const QModelIndex& index, int role) const override
{
if (role == Qt::DisplayRole) {
if (auto* esp = getESP(index)) {
return esp->filename();
}
}
return {};
}
void add(ESPItem esp) { m_esps.emplace_back(std::move(esp)); }
void addOne(ESPItem esp)
{
const auto i = m_esps.size();
beginInsertRows({}, static_cast<int>(i), static_cast<int>(i));
add(std::move(esp));
endInsertRows();
}
bool removeRows(int row, int count, const QModelIndex& = {}) override
{
if (row < 0) {
return false;
}
const auto start = static_cast<std::size_t>(row);
if (start >= m_esps.size()) {
return false;
}
const auto end = std::min(start + static_cast<std::size_t>(count), m_esps.size());
beginRemoveRows({}, static_cast<int>(start), static_cast<int>(end));
m_esps.erase(m_esps.begin() + start, m_esps.begin() + end);
endRemoveRows();
return true;
}
void finished()
{
beginResetModel();
std::sort(m_esps.begin(), m_esps.end(), [](const auto& a, const auto& b) {
return (naturalCompare(a.filename(), b.filename()) < 0);
});
endResetModel();
}
const ESPItem* getESP(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_esps.size()) {
return nullptr;
}
return &m_esps[i];
}
ESPItem* getESP(const QModelIndex& index)
{
return const_cast<ESPItem*>(std::as_const(*this).getESP(index));
}
private:
std::deque<ESPItem> m_esps;
};
ESPsTab::ESPsTab(ModInfoDialogTabContext cx)
: ModInfoDialogTab(std::move(cx)), m_inactiveModel(new ESPListModel),
m_activeModel(new ESPListModel)
{
ui->inactiveESPList->setModel(m_inactiveModel);
ui->activeESPList->setModel(m_activeModel);
QObject::connect(ui->activateESP, &QToolButton::clicked, [&] {
onActivate();
});
QObject::connect(ui->deactivateESP, &QToolButton::clicked, [&] {
onDeactivate();
});
}
void ESPsTab::clear()
{
m_inactiveModel->clear();
m_activeModel->clear();
setHasData(false);
}
bool ESPsTab::feedFile(const QString& rootPath, const QString& fullPath)
{
static const QString extensions[] = {".esp", ".esm", ".esl"};
for (const auto& e : extensions) {
if (fullPath.endsWith(e, Qt::CaseInsensitive)) {
ESPItem esp(rootPath, fullPath.mid(rootPath.length() + 1));
if (esp.isActive()) {
m_activeModel->add(std::move(esp));
} else {
m_inactiveModel->add(std::move(esp));
}
return true;
}
}
return false;
}
void ESPsTab::update()
{
m_inactiveModel->finished();
m_activeModel->finished();
setHasData(m_inactiveModel->rowCount() > 0 || m_activeModel->rowCount() > 0);
}
void ESPsTab::saveState(Settings& s)
{
s.geometry().saveState(ui->ESPsSplitter);
}
void ESPsTab::restoreState(const Settings& s)
{
s.geometry().restoreState(ui->ESPsSplitter);
}
void ESPsTab::onActivate()
{
const auto index = ui->inactiveESPList->currentIndex();
if (!index.isValid()) {
return;
}
auto* esp = m_inactiveModel->getESP(index);
if (!esp) {
return;
}
if (esp->isActive()) {
log::warn("ESPsTab::onActive(): item is already active");
return;
}
QDir root(esp->rootPath());
const QFileInfo file(esp->fileInfo());
QString newName = file.fileName();
while (root.exists(newName)) {
bool okClicked = false;
newName = QInputDialog::getText(
parentWidget(), QObject::tr("File Exists"),
QObject::tr("A file with that name exists, please enter a new one"),
QLineEdit::Normal, file.fileName(), &okClicked);
if (!okClicked) {
return;
}
if (newName.isEmpty()) {
newName = file.fileName();
}
}
if (esp->activate(newName)) {
// copy esp, original will be destroyed
auto copy = *esp;
m_inactiveModel->removeRow(index.row());
m_activeModel->addOne(std::move(copy));
selectRow(ui->inactiveESPList, index.row());
} else {
reportError(QObject::tr("Failed to move file"));
}
}
void ESPsTab::onDeactivate()
{
const auto index = ui->activeESPList->currentIndex();
if (!index.isValid()) {
return;
}
auto* esp = m_activeModel->getESP(index);
if (!esp) {
return;
}
if (!esp->isActive()) {
log::warn("ESPsTab::onDeactivate(): item is already inactive");
return;
}
QDir root(esp->rootPath());
// if we moved the file from optional to active in this session, we move the
// file back to where it came from. Otherwise, it is moved to the new folder
// "optional"
QString newName = esp->inactivePath();
if (newName.isEmpty()) {
if (!root.exists("optional")) {
if (!root.mkdir("optional")) {
reportError(QObject::tr("Failed to create directory \"optional\""));
return;
}
}
newName = QString("optional") + QDir::separator() + esp->fileInfo().fileName();
}
if (esp->deactivate(newName)) {
// copy esp, original will be destroyed
auto copy = *esp;
m_activeModel->removeRow(index.row());
m_inactiveModel->addOne(std::move(copy));
selectRow(ui->activeESPList, index.row());
} else {
reportError(QObject::tr("Failed to move file"));
}
}
void ESPsTab::selectRow(QListView* list, int row)
{
const auto* model = list->model();
const auto count = model->rowCount();
if (count == 0) {
return;
}
if (row >= count) {
list->setCurrentIndex(model->index(count - 1, 0));
} else {
list->setCurrentIndex(model->index(row, 0));
}
}