Added optional mods dialog

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97
2023-08-24 12:44:11 +03:00
parent ea384d59fb
commit 2990c5d0c9
9 changed files with 382 additions and 20 deletions

View File

@ -0,0 +1,143 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (c) 2023 Trial97 <alexandru.tripon97@gmail.com>
*
* This program 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, version 3.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
#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)
{
ui->setupUi(this);
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);
}
OptionalModDialog::~OptionalModDialog()
{
delete ui;
}

View File

@ -0,0 +1,76 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (c) 2023 Trial97 <alexandru.tripon97@gmail.com>
*
* This program 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, version 3.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <QAbstractListModel>
#include <QDialog>
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<QString, bool> m_selected;
};
class OptionalModDialog : public QDialog {
Q_OBJECT
public:
OptionalModDialog(QWidget* parent, QStringList mods);
~OptionalModDialog() override;
QStringList getResult() { return listModel->getResult(); }
private:
Ui::OptionalModDialog* ui;
OptionalModListModel* listModel;
};

View File

@ -0,0 +1,105 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>OptionalModDialog</class>
<widget class="QDialog" name="OptionalModDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>550</width>
<height>310</height>
</rect>
</property>
<property name="windowTitle">
<string>Select Mods To Install</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="1" column="0">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="font">
<font>
<pointsize>11</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Select optional mods to install.</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="font">
<font>
<italic>true</italic>
</font>
</property>
<property name="text">
<string>Note: All files will be downloaded but the unselected mods will be disabled.</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="ModListView" name="treeView"/>
</item>
</layout>
</item>
<item row="2" column="0">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="cancelButton">
<property name="enabled">
<bool>true</bool>
</property>
<property name="text">
<string>Cancel</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="clearAllButton">
<property name="text">
<string>Clear All</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="selectAllButton">
<property name="text">
<string>Select All</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="installButton">
<property name="text">
<string>Install</string>
</property>
<property name="default">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>ModListView</class>
<extends>QTreeView</extends>
<header>ui/widgets/ModListView.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

View File

@ -38,7 +38,7 @@
#include <QAbstractListModel>
#include <QDialog>
#include "modplatform/atlauncher/ATLPackIndex.h"
#include "modplatform/atlauncher/ATLPackManifest.h"
#include "net/NetJob.h"
namespace Ui {