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
|
/*
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 "categoriesdialog.h"
#include "categories.h"
#include "categoryimportdialog.h"
#include "messagedialog.h"
#include "nexusinterface.h"
#include "settings.h"
#include "ui_categoriesdialog.h"
#include "utility.h"
#include <QItemDelegate>
#include <QLineEdit>
#include <QMenu>
#include <QRegularExpressionValidator>
class NewIDValidator : public QIntValidator
{
public:
NewIDValidator(const std::set<int>& ids) : m_UsedIDs(ids) {}
virtual State validate(QString& input, int& pos) const
{
State intRes = QIntValidator::validate(input, pos);
if (intRes == Acceptable) {
bool ok = false;
int id = input.toInt(&ok);
if (m_UsedIDs.find(id) != m_UsedIDs.end()) {
return QValidator::Intermediate;
}
}
return intRes;
}
private:
const std::set<int>& m_UsedIDs;
};
class ExistingIDValidator : public QIntValidator
{
public:
ExistingIDValidator(const std::set<int>& ids) : m_UsedIDs(ids) {}
virtual State validate(QString& input, int& pos) const
{
State intRes = QIntValidator::validate(input, pos);
if (intRes == Acceptable) {
bool ok = false;
int id = input.toInt(&ok);
if ((id == 0) || (m_UsedIDs.find(id) != m_UsedIDs.end())) {
return QValidator::Acceptable;
} else {
return QValidator::Intermediate;
}
} else {
return intRes;
}
}
private:
const std::set<int>& m_UsedIDs;
};
class ValidatingDelegate : public QItemDelegate
{
public:
ValidatingDelegate(QObject* parent, QValidator* validator)
: QItemDelegate(parent), m_Validator(validator)
{}
QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem&,
const QModelIndex&) const
{
QLineEdit* edit = new QLineEdit(parent);
edit->setValidator(m_Validator);
return edit;
}
virtual void setModelData(QWidget* editor, QAbstractItemModel* model,
const QModelIndex& index) const
{
QLineEdit* edit = qobject_cast<QLineEdit*>(editor);
int pos = 0;
QString editText = edit->text();
if (m_Validator->validate(editText, pos) == QValidator::Acceptable) {
// Set ID and Parent ID as ints so sorting works as expected
if (index.column() == 0 || index.column() == 2) {
model->setData(index, editText.toInt());
return;
}
QItemDelegate::setModelData(editor, model, index);
}
}
private:
QValidator* m_Validator;
};
CategoriesDialog::CategoriesDialog(QWidget* parent)
: TutorableDialog("Categories", parent), ui(new Ui::CategoriesDialog)
{
ui->setupUi(this);
fillTable();
connect(ui->categoriesTable, SIGNAL(cellChanged(int, int)), this,
SLOT(cellChanged(int, int)));
if (Settings::instance().nexus().categoryMappings()) {
connect(ui->nexusRefresh, SIGNAL(clicked()), this, SLOT(nexusRefresh_clicked()));
connect(ui->nexusImportButton, SIGNAL(clicked()), this,
SLOT(nexusImport_clicked()));
ui->nexusCategoryList->setDisabled(false);
} else {
ui->nexusCategoryList->setDisabled(true);
}
}
CategoriesDialog::~CategoriesDialog()
{
delete ui;
}
int CategoriesDialog::exec()
{
GeometrySaver gs(Settings::instance(), this);
return QDialog::exec();
}
void CategoriesDialog::cellChanged(int row, int)
{
int currentID = ui->categoriesTable->item(row, 0)->text().toInt();
if (currentID > m_HighestID) {
m_HighestID = currentID;
}
}
void CategoriesDialog::commitChanges()
{
CategoryFactory& categories = CategoryFactory::instance();
categories.reset();
for (int i = 0; i < ui->categoriesTable->rowCount(); ++i) {
int index = ui->categoriesTable->verticalHeader()->logicalIndex(i);
QVariantList nexusData;
if (ui->categoriesTable->item(index, 3) != nullptr)
nexusData = ui->categoriesTable->item(index, 3)->data(Qt::UserRole).toList();
std::vector<CategoryFactory::NexusCategory> nexusCats;
for (auto nexusCat : nexusData) {
nexusCats.push_back(CategoryFactory::NexusCategory(
nexusCat.toList()[0].toString(), nexusCat.toList()[1].toInt()));
}
categories.addCategory(ui->categoriesTable->item(index, 0)->text().toInt(),
ui->categoriesTable->item(index, 1)->text(), nexusCats,
ui->categoriesTable->item(index, 2)->text().toInt());
}
categories.setParents();
std::vector<CategoryFactory::NexusCategory> nexusCats;
for (int i = 0; i < ui->nexusCategoryList->count(); ++i) {
nexusCats.push_back(CategoryFactory::NexusCategory(
ui->nexusCategoryList->item(i)->data(Qt::DisplayRole).toString(),
ui->nexusCategoryList->item(i)->data(Qt::UserRole).toInt()));
}
categories.setNexusCategories(nexusCats);
categories.saveCategories();
}
void CategoriesDialog::refreshIDs()
{
m_HighestID = 0;
for (int i = 0; i < ui->categoriesTable->rowCount(); ++i) {
int id = ui->categoriesTable->item(i, 0)->text().toInt();
if (id > m_HighestID) {
m_HighestID = id;
}
m_IDs.insert(id);
}
}
void CategoriesDialog::fillTable()
{
CategoryFactory& categories = CategoryFactory::instance();
QTableWidget* table = ui->categoriesTable;
QListWidget* list = ui->nexusCategoryList;
table->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed);
table->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
table->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Fixed);
table->horizontalHeader()->setSectionResizeMode(3, QHeaderView::Stretch);
table->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
table->verticalHeader()->setVisible(false);
table->setItemDelegateForColumn(
0, new ValidatingDelegate(this, new NewIDValidator(m_IDs)));
table->setItemDelegateForColumn(
2, new ValidatingDelegate(this, new ExistingIDValidator(m_IDs)));
table->setItemDelegateForColumn(
3, new ValidatingDelegate(this,
new QRegularExpressionValidator(
QRegularExpression("([0-9]+)?(,[0-9]+)*"), this)));
int row = 0;
for (const auto& category : categories.m_Categories) {
if (category.ID() == 0) {
--row;
continue;
}
++row;
table->insertRow(row);
QScopedPointer<QTableWidgetItem> idItem(new QTableWidgetItem());
idItem->setData(Qt::DisplayRole, category.ID());
QScopedPointer<QTableWidgetItem> nameItem(new QTableWidgetItem(category.name()));
QScopedPointer<QTableWidgetItem> parentIDItem(new QTableWidgetItem());
parentIDItem->setData(Qt::DisplayRole, category.parentID());
QScopedPointer<QTableWidgetItem> nexusCatItem(new QTableWidgetItem());
table->setItem(row, 0, idItem.take());
table->setItem(row, 1, nameItem.take());
table->setItem(row, 2, parentIDItem.take());
table->setItem(row, 3, nexusCatItem.take());
}
for (const auto& nexusCat : categories.m_NexusMap) {
QScopedPointer<QListWidgetItem> nexusItem(new QListWidgetItem());
nexusItem->setData(Qt::DisplayRole, nexusCat.second.name());
nexusItem->setData(Qt::UserRole, nexusCat.second.ID());
list->addItem(nexusItem.take());
auto item = table->item(categories.resolveNexusID(nexusCat.first) - 1, 3);
if (item != nullptr) {
auto itemData = item->data(Qt::UserRole).toList();
QVariantList newData;
newData.append(nexusCat.second.name());
newData.append(nexusCat.second.ID());
itemData.insert(itemData.length(), newData);
QStringList names;
for (auto cat : itemData) {
names.append(cat.toList()[0].toString());
}
item->setData(Qt::UserRole, itemData);
item->setData(Qt::DisplayRole, names.join(", "));
}
}
table->setSortingEnabled(true);
refreshIDs();
}
void CategoriesDialog::addCategory_clicked()
{
int row = m_ContextRow >= 0 ? m_ContextRow : 0;
ui->categoriesTable->setSortingEnabled(false);
ui->categoriesTable->insertRow(row);
QScopedPointer<QTableWidgetItem> idItem(new QTableWidgetItem());
idItem->setData(Qt::DisplayRole, ++m_HighestID);
QScopedPointer<QTableWidgetItem> parentIDItem(new QTableWidgetItem());
parentIDItem->setData(Qt::DisplayRole, 0);
ui->categoriesTable->setItem(row, 0, idItem.take());
ui->categoriesTable->setItem(row, 1, new QTableWidgetItem("new"));
ui->categoriesTable->setItem(row, 2, parentIDItem.take());
ui->categoriesTable->setItem(row, 3, new QTableWidgetItem(""));
ui->categoriesTable->setSortingEnabled(true);
}
void CategoriesDialog::removeCategory_clicked()
{
if (m_ContextRow >= 0)
ui->categoriesTable->removeRow(m_ContextRow);
}
void CategoriesDialog::removeNexusMap_clicked()
{
if (m_ContextRow >= 0) {
ui->categoriesTable->item(m_ContextRow, 3)->setData(Qt::UserRole, QVariantList());
ui->categoriesTable->item(m_ContextRow, 3)->setData(Qt::DisplayRole, QString());
}
}
void CategoriesDialog::nexusRefresh_clicked()
{
CategoryFactory::instance().refreshNexusCategories(this);
}
void CategoriesDialog::nexusImport_clicked()
{
auto importDialog = CategoryImportDialog(this);
if (importDialog.exec() && importDialog.strategy()) {
refreshIDs();
QTableWidget* table = ui->categoriesTable;
QListWidget* list = ui->nexusCategoryList;
if (importDialog.strategy() == CategoryImportDialog::Overwrite) {
table->setRowCount(0);
m_HighestID = 0;
}
int row = 0;
table->setSortingEnabled(false);
for (int i = 0; i < list->count(); ++i) {
QString name = list->item(i)->data(Qt::DisplayRole).toString();
int nexusID = list->item(i)->data(Qt::UserRole).toInt();
QStringList nexusLabel;
QVariantList nexusData;
nexusLabel.append(name);
QVariantList data;
data.append(QVariant(name));
data.append(QVariant(nexusID));
nexusData.insert(nexusData.size(), data);
QScopedPointer<QTableWidgetItem> nexusCatItem(
new QTableWidgetItem(nexusLabel.join(", ")));
nexusCatItem->setData(Qt::UserRole, nexusData);
if (!table->findItems(name, Qt::MatchExactly).size()) {
row = table->rowCount();
table->insertRow(table->rowCount());
QScopedPointer<QTableWidgetItem> idItem(new QTableWidgetItem());
idItem->setData(Qt::DisplayRole, ++m_HighestID);
QScopedPointer<QTableWidgetItem> nameItem(new QTableWidgetItem(name));
QScopedPointer<QTableWidgetItem> parentIDItem(new QTableWidgetItem());
parentIDItem->setData(Qt::DisplayRole, 0); // No parent
table->setItem(row, 0, idItem.take());
table->setItem(row, 1, nameItem.take());
table->setItem(row, 2, parentIDItem.take());
if (importDialog.assign()) {
table->setItem(row, 3, nexusCatItem.take());
}
} else {
for (auto item : table->findItems(name, Qt::MatchContains | Qt::MatchWrap)) {
if (item->column() == 1 && item->text() == name && importDialog.remap()) {
table->setItem(item->row(), 3, nexusCatItem.take());
} else if (importDialog.remap()) {
QScopedPointer<QTableWidgetItem> blankItem(new QTableWidgetItem());
blankItem->setData(Qt::UserRole, QVariantList());
table->setItem(item->row(), 3, blankItem.get());
}
}
}
}
table->setSortingEnabled(true);
refreshIDs();
}
}
void CategoriesDialog::nxmGameInfoAvailable(QString gameName, QVariant,
QVariant resultData, int)
{
QVariantMap result = resultData.toMap();
QVariantList categories = result["categories"].toList();
CategoryFactory& catFactory = CategoryFactory::instance();
QListWidget* list = ui->nexusCategoryList;
list->clear();
for (const auto& category : categories) {
auto catMap = category.toMap();
QScopedPointer<QListWidgetItem> nexusItem(new QListWidgetItem());
nexusItem->setData(Qt::DisplayRole, catMap["name"].toString());
nexusItem->setData(Qt::UserRole, catMap["category_id"].toInt());
list->addItem(nexusItem.take());
}
}
void CategoriesDialog::nxmRequestFailed(QString, int, int, QVariant, int, int errorCode,
const QString& errorMessage)
{
MessageDialog::showMessage(
tr("Error %1: Request to Nexus failed: %2").arg(errorCode).arg(errorMessage),
this);
}
void CategoriesDialog::on_categoriesTable_customContextMenuRequested(const QPoint& pos)
{
m_ContextRow = ui->categoriesTable->rowAt(pos.y());
QMenu menu;
menu.addAction(tr("Add"), this, SLOT(addCategory_clicked()));
menu.addAction(tr("Remove"), this, SLOT(removeCategory_clicked()));
if (Settings::instance().nexus().categoryMappings()) {
menu.addAction(tr("Remove Nexus Mapping(s)"), this, SLOT(removeNexusMap_clicked()));
}
menu.exec(ui->categoriesTable->mapToGlobal(pos));
}
|