Skip to content

Commit

Permalink
Add new dialog for category import
Browse files Browse the repository at this point in the history
- Merge or replace strategies
- Nexus mappings are optional
- If mappings are applied, remapping is optional
  • Loading branch information
Silarn committed Sep 22, 2023
1 parent d2e48ed commit 7c900bb
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 25 deletions.
67 changes: 42 additions & 25 deletions src/categoriesdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ 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"
Expand Down Expand Up @@ -292,43 +293,59 @@ void CategoriesDialog::nexusRefresh_clicked()

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) {
auto importDialog = CategoryImportDialog(this);
if (importDialog.exec() && importDialog.strategy()) {
refreshIDs();
QTableWidget* table = ui->categoriesTable;
QListWidget* list = ui->nexusCategoryList;

table->setRowCount(0);
refreshIDs();
if (importDialog.strategy() == CategoryImportDialog::Overwrite) {
table->setRowCount(0);
m_HighestID = 0;
}
int row = 0;
for (int i = 0; i < list->count(); ++i) {
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(list->item(i)->data(Qt::DisplayRole).toString()));
QString name = list->item(i)->data(Qt::DisplayRole).toString();
int nexusID = list->item(i)->data(Qt::UserRole).toInt();
QStringList nexusLabel;
QVariantList nexusData;
nexusLabel.append(list->item(i)->data(Qt::DisplayRole).toString());
nexusLabel.append(name);
QVariantList data;
data.append(QVariant(list->item(i)->data(Qt::DisplayRole).toString()));
data.append(QVariant(list->item(i)->data(Qt::UserRole).toInt()));
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);
QScopedPointer<QTableWidgetItem> parentIDItem(new QTableWidgetItem());
parentIDItem->setData(Qt::DisplayRole, 0);
if (!table->findItems(name, Qt::MatchExactly).size()) {
row = table->rowCount();
table->insertRow(table->rowCount());
// table->setVerticalHeaderItem(row, 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());
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();
}
Expand Down
75 changes: 75 additions & 0 deletions src/categoryimportdialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "categoryimportdialog.h"
#include "ui_categoryimportdialog.h"

#include "organizercore.h"

using namespace MOBase;

CategoryImportDialog::CategoryImportDialog(QWidget* parent)
: QDialog(parent), ui(new Ui::CategoryImportDialog)
{
ui->setupUi(this);
connect(ui->buttonBox, &QDialogButtonBox::accepted, this,
&CategoryImportDialog::accepted);
connect(ui->buttonBox, &QDialogButtonBox::rejected, this,
&CategoryImportDialog::rejected);
connect(ui->strategyGroup, &QButtonGroup::buttonClicked, this,
&CategoryImportDialog::on_strategyClicked);
connect(ui->assignOption, &QCheckBox::clicked, this,
&CategoryImportDialog::on_assignOptionClicked);
}

void CategoryImportDialog::accepted()
{
accept();
}

void CategoryImportDialog::rejected()
{
reject();
}

CategoryImportDialog::~CategoryImportDialog()
{
delete ui;
}

CategoryImportDialog::ImportStrategy CategoryImportDialog::strategy()
{
if (ui->mergeOption->isChecked()) {
return ImportStrategy::Merge;
} else if (ui->replaceOption->isChecked()) {
return ImportStrategy::Overwrite;
}
return ImportStrategy::None;
}

bool CategoryImportDialog::assign()
{
return ui->assignOption->isChecked();
}

bool CategoryImportDialog::remap()
{
return ui->remapOption->isChecked();
}

void CategoryImportDialog::on_strategyClicked(QAbstractButton* button)
{
if (button == ui->replaceOption) {
ui->remapOption->setChecked(false);
ui->remapOption->setDisabled(true);
} else {
ui->remapOption->setEnabled(true);
}
}

void CategoryImportDialog::on_assignOptionClicked(bool checked)
{
if (checked && strategy() == ImportStrategy::Merge) {
ui->remapOption->setEnabled(true);
} else {
ui->remapOption->setChecked(false);
ui->remapOption->setDisabled(true);
}
}
44 changes: 44 additions & 0 deletions src/categoryimportdialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef CATEGORYIMPORTDIALOG_H
#define CATEGORYIMPORTDIALOG_H

#include <QDialog>

namespace Ui
{
class CategoryImportDialog;
}

/**
* @brief Dialog that allows users to configure mod categories
**/
class CategoryImportDialog : public QDialog
{
Q_OBJECT

public:
enum ImportStrategy
{
None,
Overwrite,
Merge
};

public:
explicit CategoryImportDialog(QWidget* parent = 0);
~CategoryImportDialog();

ImportStrategy strategy();
bool assign();
bool remap();

public slots:
void accepted();
void rejected();
void on_strategyClicked(QAbstractButton* button);
void on_assignOptionClicked(bool clicked);

private:
Ui::CategoryImportDialog* ui;
};

#endif // CATEGORYIMPORTDIALOG_H

0 comments on commit 7c900bb

Please sign in to comment.