diff options
| author | Jeremy Rimpo <jeremy.rimpo@servermonkey.com> | 2023-09-25 17:39:41 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-25 17:39:41 -0500 |
| commit | 79d457f598bcfa7a8d094a87033a31f370c9c4c1 (patch) | |
| tree | 3674e6b1223ac104422ce77b06d020c50ad31cc0 /src/categoriesdialog.cpp | |
| parent | 70509275405d2a5c5e94743786f03b17ef2051f2 (diff) | |
| parent | 74668b9363d877d055a5e8b8c7d001ba448dc832 (diff) | |
Merge pull request #1880 from ModOrganizer2/qt6-category
WIP: Category Revamp
Diffstat (limited to 'src/categoriesdialog.cpp')
| -rw-r--r-- | src/categoriesdialog.cpp | 208 |
1 files changed, 170 insertions, 38 deletions
diff --git a/src/categoriesdialog.cpp b/src/categoriesdialog.cpp index 99a931ef..ff226286 100644 --- a/src/categoriesdialog.cpp +++ b/src/categoriesdialog.cpp @@ -19,6 +19,9 @@ 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" @@ -109,6 +112,14 @@ CategoriesDialog::CategoriesDialog(QWidget* parent) 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() @@ -136,21 +147,31 @@ void CategoriesDialog::commitChanges() categories.reset(); for (int i = 0; i < ui->categoriesTable->rowCount(); ++i) { - int index = ui->categoriesTable->verticalHeader()->logicalIndex(i); - QString nexusIDString = ui->categoriesTable->item(index, 2)->text(); - QStringList nexusIDStringList = nexusIDString.split(',', Qt::SkipEmptyParts); - std::vector<int> nexusIDs; - for (QStringList::iterator iter = nexusIDStringList.begin(); - iter != nexusIDStringList.end(); ++iter) { - nexusIDs.push_back(iter->toInt()); + int index = ui->categoriesTable->verticalHeader()->logicalIndex(i); + QVariantList 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(), nexusIDs, - ui->categoriesTable->item(index, 3)->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(); } @@ -170,57 +191,66 @@ void CategoriesDialog::fillTable() { CategoryFactory& categories = CategoryFactory::instance(); QTableWidget* table = ui->categoriesTable; + QListWidget* list = ui->nexusCategoryList; -#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) table->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed); table->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch); table->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Fixed); - table->horizontalHeader()->setSectionResizeMode(3, QHeaderView::Fixed); + table->horizontalHeader()->setSectionResizeMode(3, QHeaderView::Stretch); table->verticalHeader()->setSectionsMovable(true); table->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed); -#else - table->horizontalHeader()->setResizeMode(0, QHeaderView::Fixed); - table->horizontalHeader()->setResizeMode(1, QHeaderView::Stretch); - table->horizontalHeader()->setResizeMode(2, QHeaderView::Fixed); - table->horizontalHeader()->setResizeMode(3, QHeaderView::Fixed); - table->verticalHeader()->setMovable(true); - table->verticalHeader()->setResizeMode(QHeaderView::Fixed); -#endif - table->setItemDelegateForColumn( 0, new ValidatingDelegate(this, new NewIDValidator(m_IDs))); table->setItemDelegateForColumn( - 2, new ValidatingDelegate(this, + 2, new ValidatingDelegate(this, new ExistingIDValidator(m_IDs))); + table->setItemDelegateForColumn( + 3, new ValidatingDelegate(this, new QRegularExpressionValidator( QRegularExpression("([0-9]+)?(,[0-9]+)*"), this))); - table->setItemDelegateForColumn( - 3, new ValidatingDelegate(this, new ExistingIDValidator(m_IDs))); int row = 0; - for (std::vector<CategoryFactory::Category>::const_iterator iter = - categories.m_Categories.begin(); - iter != categories.m_Categories.end(); ++iter, ++row) { - const CategoryFactory::Category& category = *iter; - if (category.m_ID == 0) { + for (const auto& category : categories.m_Categories) { + if (category.ID() == 0) { --row; continue; } + ++row; table->insertRow(row); // table->setVerticalHeaderItem(row, new QTableWidgetItem(" ")); QScopedPointer<QTableWidgetItem> idItem(new QTableWidgetItem()); - idItem->setData(Qt::DisplayRole, category.m_ID); + idItem->setData(Qt::DisplayRole, category.ID()); - QScopedPointer<QTableWidgetItem> nameItem(new QTableWidgetItem(category.m_Name)); - QScopedPointer<QTableWidgetItem> nexusIDItem( - new QTableWidgetItem(MOBase::VectorJoin(category.m_NexusIDs, ","))); + QScopedPointer<QTableWidgetItem> nameItem(new QTableWidgetItem(category.name())); QScopedPointer<QTableWidgetItem> parentIDItem(new QTableWidgetItem()); - parentIDItem->setData(Qt::DisplayRole, category.m_ParentID); + 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, nexusIDItem.take()); - table->setItem(row, 3, parentIDItem.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(", ")); + } } refreshIDs(); @@ -234,13 +264,112 @@ void CategoriesDialog::addCategory_clicked() ui->categoriesTable->setItem(row, 0, new QTableWidgetItem(QString::number(++m_HighestID))); ui->categoriesTable->setItem(row, 1, new QTableWidgetItem("new")); - ui->categoriesTable->setItem(row, 2, new QTableWidgetItem("")); - ui->categoriesTable->setItem(row, 3, new QTableWidgetItem("0")); + ui->categoriesTable->setItem(row, 2, new QTableWidgetItem("0")); + ui->categoriesTable->setItem(row, 3, new QTableWidgetItem("")); } void CategoriesDialog::removeCategory_clicked() { - ui->categoriesTable->removeRow(m_ContextRow); + 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; + 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()); + // table->setVerticalHeaderItem(row, new QTableWidgetItem(" ")); + + 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()); + } + } + } + } + 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) @@ -249,6 +378,9 @@ void CategoriesDialog::on_categoriesTable_customContextMenuRequested(const QPoin 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)); } |
