From 479335dfe08466ce77f769e550c7f68a10b522a5 Mon Sep 17 00:00:00 2001 From: TheKodeToad Date: Sun, 27 Aug 2023 00:49:21 +0100 Subject: [PATCH] Rewrite optional mod dialog Signed-off-by: TheKodeToad --- .../pages/modplatform/OptionalModDialog.cpp | 146 ++++------------- .../ui/pages/modplatform/OptionalModDialog.h | 41 +---- .../ui/pages/modplatform/OptionalModDialog.ui | 148 +++++++++--------- 3 files changed, 113 insertions(+), 222 deletions(-) diff --git a/launcher/ui/pages/modplatform/OptionalModDialog.cpp b/launcher/ui/pages/modplatform/OptionalModDialog.cpp index 620de4f74..fc1c8b3cb 100644 --- a/launcher/ui/pages/modplatform/OptionalModDialog.cpp +++ b/launcher/ui/pages/modplatform/OptionalModDialog.cpp @@ -19,125 +19,45 @@ #include "OptionalModDialog.h" #include "ui_OptionalModDialog.h" -OptionalModListModel::OptionalModListModel(QWidget* parent, QStringList mods) : QAbstractListModel(parent), m_mods(mods) {} - -QStringList OptionalModListModel::getResult() -{ - QStringList result; - for (const auto& mod : m_mods) { - if (m_selected.value(mod, false)) { - result << mod; - } - } - return result; -} - -int OptionalModListModel::rowCount(const QModelIndex& parent) const -{ - return parent.isValid() ? 0 : m_mods.size(); -} - -int OptionalModListModel::columnCount(const QModelIndex& parent) const -{ - // Enabled, Name - return parent.isValid() ? 0 : 2; -} - -QVariant OptionalModListModel::data(const QModelIndex& index, int role) const -{ - auto row = index.row(); - auto mod = m_mods.at(row); - - if (role == Qt::DisplayRole && index.column() == NameColumn) { - return mod; - } else if (role == Qt::CheckStateRole && index.column() == EnabledColumn) { - return m_selected.value(mod, false) ? Qt::Checked : Qt::Unchecked; - } - - return {}; -} - -bool OptionalModListModel::setData(const QModelIndex& index, [[maybe_unused]] const QVariant& value, int role) -{ - if (role == Qt::CheckStateRole) { - auto row = index.row(); - auto mod = m_mods.at(row); - - toggleMod(mod, row); - return true; - } - - return false; -} - -QVariant OptionalModListModel::headerData(int section, Qt::Orientation orientation, int role) const -{ - if (role == Qt::DisplayRole && orientation == Qt::Horizontal) { - switch (section) { - case EnabledColumn: - return QString(); - case NameColumn: - return QString("Name"); - } - } - - return {}; -} - -Qt::ItemFlags OptionalModListModel::flags(const QModelIndex& index) const -{ - auto flags = QAbstractListModel::flags(index); - if (index.isValid() && index.column() == EnabledColumn) { - flags |= Qt::ItemIsUserCheckable; - } - return flags; -} - -void OptionalModListModel::toggleAll(bool enabled) -{ - for (const auto& mod : m_mods) { - m_selected[mod] = enabled; - } - - emit dataChanged(OptionalModListModel::index(0, EnabledColumn), OptionalModListModel::index(m_mods.size() - 1, EnabledColumn)); -} - -void OptionalModListModel::toggleMod(QString mod, int index) -{ - auto enable = !m_selected.value(mod, false); - - setMod(mod, index, enable); -} - -void OptionalModListModel::setMod(QString mod, int index, bool enable, bool shouldEmit) -{ - if (m_selected.value(mod, false) == enable) - return; - - m_selected[mod] = enable; - - if (shouldEmit) { - emit dataChanged(OptionalModListModel::index(index, EnabledColumn), OptionalModListModel::index(index, EnabledColumn)); - } -} - -OptionalModDialog::OptionalModDialog(QWidget* parent, QStringList mods) : QDialog(parent), ui(new Ui::OptionalModDialog) +OptionalModDialog::OptionalModDialog(QWidget* parent, const QStringList& mods) : QDialog(parent), ui(new Ui::OptionalModDialog) { ui->setupUi(this); + for (const QString& mod : mods) { + auto item = new QListWidgetItem(mod, ui->list); + item->setFlags(item->flags() | Qt::ItemIsUserCheckable); + item->setCheckState(Qt::Unchecked); + item->setData(Qt::UserRole, mod); + } - listModel = new OptionalModListModel(this, mods); - ui->treeView->setModel(listModel); - - ui->treeView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - ui->treeView->header()->setSectionResizeMode(OptionalModListModel::NameColumn, QHeaderView::Stretch); - - connect(ui->selectAllButton, &QPushButton::clicked, listModel, &OptionalModListModel::selectAll); - connect(ui->clearAllButton, &QPushButton::clicked, listModel, &OptionalModListModel::clearAll); - connect(ui->installButton, &QPushButton::clicked, this, &QDialog::accept); - connect(ui->cancelButton, &QPushButton::clicked, this, &QDialog::reject); + connect(ui->selectAllButton, &QPushButton::clicked, ui->list, [this] { + for (int i = 0; i < ui->list->count(); i++) + ui->list->item(i)->setCheckState(Qt::Checked); + }); + connect(ui->clearAllButton, &QPushButton::clicked, ui->list, [this] { + for (int i = 0; i < ui->list->count(); i++) + ui->list->item(i)->setCheckState(Qt::Unchecked); + }); + connect(ui->list, &QListWidget::itemActivated, [](QListWidgetItem* item) { + if (item->checkState() == Qt::Checked) + item->setCheckState(Qt::Unchecked); + else + item->setCheckState(Qt::Checked); + }); } OptionalModDialog::~OptionalModDialog() { delete ui; } + +QStringList OptionalModDialog::getResult() +{ + QStringList result; + result.reserve(ui->list->count()); + for (int i = 0; i < ui->list->count(); i++) { + auto item = ui->list->item(i); + if (item->checkState() == Qt::Checked) + result.append(item->data(Qt::UserRole).toString()); + } + return result; +} diff --git a/launcher/ui/pages/modplatform/OptionalModDialog.h b/launcher/ui/pages/modplatform/OptionalModDialog.h index 4e22d0e46..1897c1fca 100644 --- a/launcher/ui/pages/modplatform/OptionalModDialog.h +++ b/launcher/ui/pages/modplatform/OptionalModDialog.h @@ -25,52 +25,15 @@ namespace Ui { class OptionalModDialog; } -class OptionalModListModel : public QAbstractListModel { - Q_OBJECT - public: - enum Columns { - EnabledColumn = 0, - NameColumn, - }; - - OptionalModListModel(QWidget* parent, QStringList mods); - - QStringList getResult(); - - int rowCount(const QModelIndex& parent) const override; - int columnCount(const QModelIndex& parent) const override; - - QVariant data(const QModelIndex& index, int role) const override; - bool setData(const QModelIndex& index, const QVariant& value, int role) override; - QVariant headerData(int section, Qt::Orientation orientation, int role) const override; - - Qt::ItemFlags flags(const QModelIndex& index) const override; - - public slots: - void selectAll() { toggleAll(true); } - void clearAll() { toggleAll(false); }; - void toggleAll(bool enabled); - - private: - void toggleMod(QString mod, int index); - void setMod(QString mod, int index, bool enable, bool shouldEmit = true); - - private: - QStringList m_mods; - QHash m_selected; -}; - class OptionalModDialog : public QDialog { Q_OBJECT public: - OptionalModDialog(QWidget* parent, QStringList mods); + OptionalModDialog(QWidget* parent, const QStringList& mods); ~OptionalModDialog() override; - QStringList getResult() { return listModel->getResult(); } + QStringList getResult(); private: Ui::OptionalModDialog* ui; - - OptionalModListModel* listModel; }; diff --git a/launcher/ui/pages/modplatform/OptionalModDialog.ui b/launcher/ui/pages/modplatform/OptionalModDialog.ui index 76d1da89d..0b809d2cb 100644 --- a/launcher/ui/pages/modplatform/OptionalModDialog.ui +++ b/launcher/ui/pages/modplatform/OptionalModDialog.ui @@ -11,81 +11,63 @@ - Select Mods To Install + Select Optional Mods - - - - 11 - 75 - true - + + + Qt::IgnoreAction - - Select optional mods to install. - - - Qt::AlignCenter - - - - - - - - true - - - - Note: All files will be downloaded but the unselected mods will be disabled. - - - Qt::AlignCenter - - - - - - - - - - - - - + true - - Cancel - - - - Clear All - - + + + + + Select All + + + + + + + Deselect All + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Unchecked mods will be disabled. + + + + - - - Select All - - - - - - - Install - - - true + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok @@ -93,13 +75,39 @@ - - - ModListView - QTreeView -
ui/widgets/ModListView.h
-
-
- + + + buttonBox + accepted() + OptionalModDialog + accept() + + + 274 + 284 + + + 274 + 154 + + + + + buttonBox + rejected() + OptionalModDialog + reject() + + + 274 + 284 + + + 274 + 154 + + + +