Skip to content

Commit

Permalink
Several updates
Browse files Browse the repository at this point in the history
* No longer cause an error when deleting a category that's being used
* Add a dialog when installing a mod with no Nexus mapping (if not disabled)
* Allow disabling Nexus category mapping in Settings (Nexus tab)
* Add context option to remove nexus mappings in the category editor
* Some clang style fixes
  • Loading branch information
Silarn committed Sep 19, 2023
1 parent 131eb44 commit 2444096
Show file tree
Hide file tree
Showing 13 changed files with 1,213 additions and 834 deletions.
2 changes: 2 additions & 0 deletions src/categories.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ void CategoryFactory::saveCategories()
nexusMapFile.write(line);
}
nexusMapFile.close();

emit categoriesSaved();
}

unsigned int
Expand Down
3 changes: 3 additions & 0 deletions src/categories.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ class CategoryFactory : public QObject {
*/
static QString nexusMappingFilePath();

signals:
void categoriesSaved();

private:
explicit CategoryFactory();

Expand Down
117 changes: 69 additions & 48 deletions src/categoriesdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>.

#include "categoriesdialog.h"
#include "categories.h"
#include "messagedialog.h"
#include "nexusinterface.h"
#include "settings.h"
#include "ui_categoriesdialog.h"
#include "utility.h"
#include "nexusinterface.h"
#include "messagedialog.h"
#include <QItemDelegate>
#include <QLineEdit>
#include <QMenu>
Expand Down Expand Up @@ -104,16 +104,22 @@ class ValidatingDelegate : public QItemDelegate
QValidator* m_Validator;
};


CategoriesDialog::CategoriesDialog(PluginContainer* pluginContainer, QWidget* parent)
: TutorableDialog("Categories", parent), ui(new Ui::CategoriesDialog), m_PluginContainer(pluginContainer)
: TutorableDialog("Categories", parent), ui(new Ui::CategoriesDialog),
m_PluginContainer(pluginContainer)
{
ui->setupUi(this);
fillTable();
connect(ui->categoriesTable, SIGNAL(cellChanged(int,int)), this,
SLOT(cellChanged(int,int)));
connect(ui->nexusRefresh, SIGNAL(clicked()), this, SLOT(nexusRefresh_clicked()));
connect(ui->nexusImportButton, SIGNAL(clicked()), this, SLOT(nexusImport_clicked()));
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()
Expand Down Expand Up @@ -142,29 +148,26 @@ void CategoriesDialog::commitChanges()

for (int i = 0; i < ui->categoriesTable->rowCount(); ++i) {
int index = ui->categoriesTable->verticalHeader()->logicalIndex(i);
QVariantList nexusData = ui->categoriesTable->item(index, 3)->data(Qt::UserRole).toList();
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()));
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->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()
)
);
nexusCats.push_back(CategoryFactory::NexusCategory(
ui->nexusCategoryList->item(i)->data(Qt::DisplayRole).toString(),
ui->nexusCategoryList->item(i)->data(Qt::UserRole).toInt()));
}

categories->setNexusCategories(nexusCats);
Expand All @@ -187,21 +190,27 @@ void CategoriesDialog::refreshIDs()
void CategoriesDialog::fillTable()
{
CategoryFactory* categories = CategoryFactory::instance();
QTableWidget* table = ui->categoriesTable;
QListWidget* list = ui->nexusCategoryList;
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()->setSectionsMovable(true);
table->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
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)));
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 (std::vector<CategoryFactory::Category>::const_iterator iter = categories->m_Categories.begin();
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) {
Expand All @@ -225,13 +234,12 @@ void CategoriesDialog::fillTable()
table->setItem(row, 3, nexusCatItem.take());
}

for (auto nexusCat : categories->m_NexusMap)
{
for (auto nexusCat : categories->m_NexusMap) {
QScopedPointer<QListWidgetItem> nexusItem(new QListWidgetItem());
nexusItem->setData(Qt::DisplayRole, nexusCat.second.m_Name);
nexusItem->setData(Qt::UserRole, nexusCat.second.m_ID);
list->addItem(nexusItem.take());
auto item = table->item(categories->resolveNexusID(nexusCat.first)-1, 3);
auto item = table->item(categories->resolveNexusID(nexusCat.first) - 1, 3);
if (item != nullptr) {
auto itemData = item->data(Qt::UserRole).toList();
QVariantList newData;
Expand Down Expand Up @@ -267,22 +275,29 @@ void CategoriesDialog::removeCategory_clicked()
ui->categoriesTable->removeRow(m_ContextRow);
}

void CategoriesDialog::removeNexusMap_clicked()
{
ui->categoriesTable->item(m_ContextRow, 3)->setData(Qt::UserRole, QVariantList());
ui->categoriesTable->item(m_ContextRow, 3)->setData(Qt::DisplayRole, QString());
// ui->categoriesTable->update();
}

void CategoriesDialog::nexusRefresh_clicked()
{
NexusInterface &nexus = NexusInterface::instance();
NexusInterface& nexus = NexusInterface::instance();
nexus.setPluginContainer(m_PluginContainer);
nexus.requestGameInfo(Settings::instance().game().plugin()->gameShortName(), this, QVariant(), QString());
nexus.requestGameInfo(Settings::instance().game().plugin()->gameShortName(), this,
QVariant(), QString());
}


void CategoriesDialog::nexusImport_clicked()
{
if (QMessageBox::question(nullptr, tr("Import Nexus Categories?"),
tr("This will overwrite your existing categories with the loaded Nexus categories."),
QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
tr("This will overwrite your existing categories with the "
"loaded Nexus categories."),
QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
QTableWidget* table = ui->categoriesTable;
QListWidget* list = ui->nexusCategoryList;
QListWidget* list = ui->nexusCategoryList;

table->setRowCount(0);
refreshIDs();
Expand All @@ -295,15 +310,17 @@ void CategoriesDialog::nexusImport_clicked()
QScopedPointer<QTableWidgetItem> idItem(new QTableWidgetItem());
idItem->setData(Qt::DisplayRole, ++m_HighestID);

QScopedPointer<QTableWidgetItem> nameItem(new QTableWidgetItem(list->item(i)->data(Qt::DisplayRole).toString()));
QScopedPointer<QTableWidgetItem> nameItem(
new QTableWidgetItem(list->item(i)->data(Qt::DisplayRole).toString()));
QStringList nexusLabel;
QVariantList nexusData;
nexusLabel.append(list->item(i)->data(Qt::DisplayRole).toString());
QVariantList data;
data.append(QVariant(list->item(i)->data(Qt::DisplayRole).toString()));
data.append(QVariant(list->item(i)->data(Qt::UserRole).toInt()));
nexusData.insert(nexusData.size(), data);
QScopedPointer<QTableWidgetItem> nexusCatItem(new QTableWidgetItem(nexusLabel.join(", ")));
QScopedPointer<QTableWidgetItem> nexusCatItem(
new QTableWidgetItem(nexusLabel.join(", ")));
nexusCatItem->setData(Qt::UserRole, nexusData);
QScopedPointer<QTableWidgetItem> parentIDItem(new QTableWidgetItem());
parentIDItem->setData(Qt::DisplayRole, 0);
Expand All @@ -317,13 +334,13 @@ void CategoriesDialog::nexusImport_clicked()
}
}


void CategoriesDialog::nxmGameInfoAvailable(QString gameName, QVariant, QVariant resultData, int)
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;
QVariantMap result = resultData.toMap();
QVariantList categories = result["categories"].toList();
CategoryFactory* catFactory = CategoryFactory::instance();
QListWidget* list = ui->nexusCategoryList;
list->clear();
for (auto category : categories) {
auto catMap = category.toMap();
Expand All @@ -334,19 +351,23 @@ void CategoriesDialog::nxmGameInfoAvailable(QString gameName, QVariant, QVariant
}
}


void CategoriesDialog::nxmRequestFailed(QString, int, int, QVariant, int, int errorCode, const QString& errorMessage)
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);
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));
}
7 changes: 4 additions & 3 deletions src/categoriesdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>.
#ifndef CATEGORIESDIALOG_H
#define CATEGORIESDIALOG_H

#include "tutorabledialog.h"
#include "categories.h"
#include "plugincontainer.h"
#include "tutorabledialog.h"
#include <set>

namespace Ui
Expand Down Expand Up @@ -54,7 +54,8 @@ class CategoriesDialog : public MOBase::TutorableDialog
public slots:

void nxmGameInfoAvailable(QString gameName, QVariant, QVariant resultData, int);
void nxmRequestFailed(QString, int, int, QVariant, int, int errorCode, const QString& errorMessage);
void nxmRequestFailed(QString, int, int, QVariant, int, int errorCode,
const QString& errorMessage);

signals:
void refreshNexusCategories();
Expand All @@ -64,6 +65,7 @@ private slots:
void on_categoriesTable_customContextMenuRequested(const QPoint& pos);
void addCategory_clicked();
void removeCategory_clicked();
void removeNexusMap_clicked();
void nexusRefresh_clicked();
void nexusImport_clicked();
void cellChanged(int row, int column);
Expand All @@ -80,7 +82,6 @@ private slots:
int m_HighestID;
std::set<int> m_IDs;
std::vector<CategoryFactory::NexusCategory> m_NexusCategories;

};

#endif // CATEGORIESDIALOG_H
28 changes: 25 additions & 3 deletions src/installationmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ InstallationResult InstallationManager::install(const QString& fileName,
QString gameName = "";
QString version = "";
QString newestVersion = "";
int category = 0;
int categoryID = 0;
int fileCategoryID = 1;
QString repository = "Nexus";
Expand All @@ -661,9 +662,30 @@ InstallationResult InstallationManager::install(const QString& fileName,

version = metaFile.value("version", "").toString();
newestVersion = metaFile.value("newestVersion", "").toString();
unsigned int categoryIndex = CategoryFactory::instance()->resolveNexusID(
metaFile.value("category", 0).toInt());
categoryID = CategoryFactory::instance()->getCategoryID(categoryIndex);
category = metaFile.value("category", 0).toInt();
unsigned int categoryIndex = CategoryFactory::instance()->resolveNexusID(category);
if (category != 0 && categoryIndex == 0U &&
Settings::instance().nexus().categoryMappings()) {
QMessageBox nexusQuery;
nexusQuery.setText(tr(
"This Nexus category has not yet been mapped. Do you wish to proceed without "
"setting a category, proceed and disable automatic Nexus mappings, or stop "
"and configure your category mappings?"));
nexusQuery.addButton(tr("&Proceed"), QMessageBox::YesRole);
nexusQuery.addButton(tr("&Disable"), QMessageBox::AcceptRole);
nexusQuery.addButton(tr("&Stop && Configure"), QMessageBox::DestructiveRole);
auto ret = nexusQuery.exec();
switch (ret) {
case 1:
Settings::instance().nexus().setCategoryMappings(false);
case 0:
break;
case 2:
return MOBase::IPluginInstaller::RESULT_CATEGORYREQUESTED;
}
} else {
categoryID = CategoryFactory::instance()->getCategoryID(categoryIndex);
}
repository = metaFile.value("repository", "").toString();
fileCategoryID = metaFile.value("fileCategory", 1).toInt();
}
Expand Down
Loading

0 comments on commit 2444096

Please sign in to comment.