cleaned up and improved GameOptions Model & Page

- added array support
- cleaned up logic
- ran clang-format
- added description & default value columns
- added basic editing support (bools only) - no saving

Co-authored-by: TheLastRar <TheLastRar@users.noreply.github.com>
Signed-off-by: Tayou <tayou@gmx.net>
This commit is contained in:
Tayou 2023-02-27 17:30:30 +01:00 committed by Tayou
parent 564567a568
commit 511076d3ba
No known key found for this signature in database
GPG Key ID: 02CA43C1CB6E9887
5 changed files with 587 additions and 236 deletions

View File

@ -1,129 +1,400 @@
#include "GameOptions.h" // SPDX-License-Identifier: GPL-3.0-only
#include <QDebug> /*
#include <QSaveFile> * Prism Launcher - Minecraft Launcher
#include "FileSystem.h" * Copyright (C) 2023 Tayou <tayou@gmx.net>
* Copyright (C) 2023 TheLastRar <TheLastRar@users.noreply.github.com>
namespace { *
bool load(const QString& path, std::vector<GameOptionItem>& contents, int& version) * 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
contents.clear(); * the Free Software Foundation, version 3.
QFile file(path); *
if (!file.open(QFile::ReadOnly)) { * This program is distributed in the hope that it will be useful,
qWarning() << "Failed to read options file."; * but WITHOUT ANY WARRANTY; without even the implied warranty of
return false; * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
} * GNU General Public License for more details.
version = 0; *
while (!file.atEnd()) { * You should have received a copy of the GNU General Public License
auto line = file.readLine(); * along with this program. If not, see <https://www.gnu.org/licenses/>.
if (line.endsWith('\n')) { *
line.chop(1); * This file incorporates work covered by the following copyright and
} * permission notice:
auto separatorIndex = line.indexOf(':'); *
if (separatorIndex == -1) { * Copyright 2013-2021 MultiMC Contributors
continue; *
} * Licensed under the Apache License, Version 2.0 (the "License");
auto key = QString::fromUtf8(line.data(), separatorIndex); * you may not use this file except in compliance with the License.
auto value = QString::fromUtf8(line.data() + separatorIndex + 1, line.size() - 1 - separatorIndex); * You may obtain a copy of the License at
qDebug() << "!!" << key << "!!"; *
if (key == "version") { * http://www.apache.org/licenses/LICENSE-2.0
version = value.toInt(); *
continue; * Unless required by applicable law or agreed to in writing, software
} * distributed under the License is distributed on an "AS IS" BASIS,
contents.emplace_back(GameOptionItem{ key, value }); * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
} * See the License for the specific language governing permissions and
qDebug() << "Loaded" << path << "with version:" << version; * limitations under the License.
return true; */
}
bool save(const QString& path, std::vector<GameOptionItem>& mapping, int version) #include <QDebug>
{ #include <QSaveFile>
QSaveFile out(path); #include "FileSystem.h"
if (!out.open(QIODevice::WriteOnly)) {
return false; #include "FileSystem.h"
} #include "GameOptions.h"
if (version != 0) {
QString versionLine = QString("version:%1\n").arg(version); static Qt::CheckState boolToState(bool b)
out.write(versionLine.toUtf8()); {
} return b ? Qt::Checked : Qt::Unchecked;
auto iter = mapping.begin(); };
while (iter != mapping.end()) {
out.write(iter->key.toUtf8()); namespace {
out.write(":"); bool load(const QString& path, std::vector<GameOptionItem>& contents, int& version)
out.write(iter->value.toUtf8()); {
out.write("\n"); contents.clear();
iter++; QFile file(path);
} if (!file.open(QFile::ReadOnly)) {
return out.commit(); qWarning() << "Failed to read options file.";
} return false;
} // namespace }
GameOptions::GameOptions(const QString& path) : path(path) version = 0;
{ while (!file.atEnd()) {
reload(); // This should be handled by toml++ or some other toml parser rather than this manual parsing
} QString line = QString::fromUtf8(file.readLine());
if (line.endsWith('\n')) {
QVariant GameOptions::headerData(int section, Qt::Orientation orientation, int role) const line.chop(1);
{ }
if (role != Qt::DisplayRole) { if (line.endsWith('\r')) {
return QAbstractListModel::headerData(section, orientation, role); line.chop(1);
} }
switch (section) { GameOptionItem item = GameOptionItem();
case 0:
return tr("Key"); auto parts = line.split(':');
case 1:
return tr("Value"); item.key = parts[0];
default: item.value = parts[1];
return QVariant(); item.type = OptionType::String;
} qDebug() << "Reading Game Options Key:" << item.key;
}
if (item.key == "version") {
QVariant GameOptions::data(const QModelIndex& index, int role) const version = item.value.toInt();
{ continue;
if (!index.isValid()) };
return QVariant();
bool isInt = false;
int row = index.row(); bool isFloat = false;
int column = index.column(); item.intValue = item.value.toInt(&isInt);
item.floatValue = item.value.toFloat(&isFloat);
if (row < 0 || row >= int(contents.size())) if (isInt) {
return QVariant(); item.type = OptionType::Int;
// qDebug() << "The value" << value << "is a int";
switch (role) { } else if (isFloat) {
case Qt::DisplayRole: item.type = OptionType::Float;
if (column == 0) { // qDebug() << "The value" << value << "is a float";
return contents[row].key; } else if (item.value == "true" || item.value == "false") {
} else { item.boolValue = item.value == "true" ? true : false;
return contents[row].value; item.type = OptionType::Bool;
} qDebug() << "The value" << item.value << "is a bool";
default: } else if (item.value.endsWith("]") && item.value.startsWith("[")) {
return QVariant(); qDebug() << "The value" << item.value << "is an array";
} for (QString part : item.value.mid(1, item.value.size() - 2).split(",")) {
return QVariant(); GameOptionChildItem child{ part, static_cast<int>(contents.size()) };
} qDebug() << "Array has entry" << part;
item.children.append(child);
int GameOptions::rowCount(const QModelIndex&) const }
{ }
return contents.size(); contents.emplace_back(item);
} }
qDebug() << "Loaded" << path << "with version:" << version;
int GameOptions::columnCount(const QModelIndex&) const return true;
{ }
return 2; bool save(const QString& path, std::vector<GameOptionItem>& mapping, int version)
} {
QSaveFile out(path);
bool GameOptions::isLoaded() const if (!out.open(QIODevice::WriteOnly)) {
{ return false;
return loaded; }
} if (version != 0) {
QString versionLine = QString("version:%1\n").arg(version);
bool GameOptions::reload() out.write(versionLine.toUtf8());
{ }
beginResetModel(); auto iter = mapping.begin();
loaded = load(path, contents, version); while (iter != mapping.end()) {
endResetModel(); out.write(iter->key.toUtf8());
return loaded; out.write(":");
} out.write(iter->value.toUtf8());
out.write("\n");
bool GameOptions::save() iter++;
{ }
return ::save(path, contents, version); return out.commit();
} }
} // namespace
GameOptions::GameOptions(const QString& path) : path(path)
{
reload();
}
QVariant GameOptions::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole) {
return QAbstractItemModel::headerData(section, orientation, role);
}
switch (section) {
case 0:
return tr("Key");
case 1:
return tr("Description");
case 2:
return tr("Value");
case 3:
return tr("Default Value");
default:
return QVariant();
}
}
bool GameOptions::setData(const QModelIndex& index, const QVariant& value, int role)
{
auto row = index.row();
auto column = (Column)index.column();
if (column == Column::Value) {
switch (contents[row].type) {
case OptionType::String: {
contents[row].value = value.toString();
}
case OptionType::Int: {
contents[row].intValue = value.toInt();
}
case OptionType::Bool: {
contents[row].boolValue = value.toBool();
}
case OptionType::Float: {
contents[row].floatValue = value.toFloat();
}
}
}
return true;
}
Qt::ItemFlags GameOptions::flags(const QModelIndex& index) const
{
Qt::ItemFlags flags = QAbstractItemModel::flags(index);
if (!index.isValid())
return flags;
Column column = (Column)index.column();
if (column == Column::Key) {
return flags;
}
if (index.parent().isValid()) {
return flags;
}
if (contents[index.row()].children.count() > 0) {
return flags;
}
flags = flags | Qt::ItemFlag::ItemIsEditable;
if (column == Column::Value || column == Column::DefaultValue) {
flags = flags | Qt::ItemFlag::ItemIsUserCheckable;
}
if (column == Column::DefaultValue) {
flags = flags & ~Qt::ItemFlag::ItemIsEnabled;
}
return flags;
}
QVariant GameOptions::data(const QModelIndex& index, int role) const
{
if (!index.isValid())
return QVariant();
int row = index.row();
Column column = (Column)index.column();
if (row < 0 || row >= int(contents.size()))
return QVariant();
if (index.parent().isValid()) {
switch (role) {
case Qt::DisplayRole: {
if (column == Column::Value) {
GameOptionChildItem* item = static_cast<GameOptionChildItem*>(index.internalPointer());
return item->value;
} else {
return QVariant();
}
}
default: {
return QVariant();
}
}
}
switch (role) {
case Qt::DisplayRole: {
switch (column) {
case Column::Key: {
return contents[row].key;
}
case Column::Description: {
return "Description goes here!";
}
case Column::Value: {
switch (contents[row].type) {
case OptionType::String: {
return contents[row].value;
}
case OptionType::Int: {
return contents[row].intValue;
}
case OptionType::Bool: {
return contents[row].boolValue;
}
case OptionType::Float: {
return contents[row].floatValue;
}
case OptionType::KeyBind: {
return contents[row].value;
}
default: {
return QVariant();
}
}
}
case Column::DefaultValue: {
switch (contents[row].type) {
case OptionType::String: {
return contents[row].value;
}
case OptionType::Int: {
return contents[row].intValue;
}
case OptionType::Bool: {
return contents[row].boolValue;
}
case OptionType::Float: {
return contents[row].floatValue;
}
case OptionType::KeyBind: {
return contents[row].value;
}
default: {
return QVariant();
}
}
}
default: {
return QVariant();
}
}
}
case Qt::CheckStateRole: {
switch (column) {
case Column::Value: {
if (contents[row].type == OptionType::Bool) {
return boolToState(contents[row].boolValue);
} else {
return QVariant();
}
}
case Column::DefaultValue: {
return boolToState(contents[row].boolValue);
} else {
return QVariant();
}
}
default: {
return QVariant();
}
}
}
default: {
return QVariant();
}
}
return QVariant();
}
QModelIndex GameOptions::index(int row, int column, const QModelIndex& parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
if (parent.isValid()) {
if (parent.parent().isValid())
return QModelIndex();
GameOptionItem* item = static_cast<GameOptionItem*>(parent.internalPointer());
return createIndex(row, column, &item->children[row]);
} else {
return createIndex(row, column, &contents[row]);
}
}
QModelIndex GameOptions::parent(const QModelIndex& index) const
{
if (!index.isValid())
return QModelIndex();
const void* childItem = index.internalPointer();
// Determine where childItem points to
if (childItem >= &contents[0] && childItem <= &contents.back()) {
// Parent is root/contents
return QModelIndex();
} else {
GameOptionChildItem* child = static_cast<GameOptionChildItem*>(index.internalPointer());
return createIndex(child->parentRow, 0, &contents[child->parentRow]);
}
}
int GameOptions::rowCount(const QModelIndex& parent) const
{
if (!parent.isValid()) {
return static_cast<int>(contents.size());
} else {
if (parent.column() > 0)
return 0;
// Our tree model is only one layer deep
// If we have parent, we can't go deeper
if (parent.parent().isValid())
return 0;
GameOptionItem* item = static_cast<GameOptionItem*>(parent.internalPointer());
return item->children.count();
}
}
int GameOptions::columnCount(const QModelIndex& parent) const
{
// Our tree model is only one layer deep
// If we have parent, we can't go deeper
if (parent.parent().isValid())
return 0;
return 4;
}
bool GameOptions::isLoaded() const
{
return loaded;
}
bool GameOptions::reload()
{
beginResetModel();
loaded = load(path, contents, version);
endResetModel();
return loaded;
}
bool GameOptions::save()
{
return ::save(path, contents, version);
}

View File

@ -1,32 +1,85 @@
#pragma once // SPDX-License-Identifier: GPL-3.0-only
/*
#include <QAbstractListModel> * Prism Launcher - Minecraft Launcher
#include <QString> * Copyright (C) 2023 Tayou <tayou@gmx.net>
#include <map> * Copyright (C) 2023 TheLastRar <TheLastRar@users.noreply.github.com>
*
struct GameOptionItem { * This program is free software: you can redistribute it and/or modify
QString key; * it under the terms of the GNU General Public License as published by
QString value; * the Free Software Foundation, version 3.
}; *
* This program is distributed in the hope that it will be useful,
class GameOptions : public QAbstractListModel { * but WITHOUT ANY WARRANTY; without even the implied warranty of
Q_OBJECT * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
public: * GNU General Public License for more details.
explicit GameOptions(const QString& path); *
virtual ~GameOptions() = default; * You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
int rowCount(const QModelIndex& parent = QModelIndex()) const override; *
int columnCount(const QModelIndex& parent) const override; * This file incorporates work covered by the following copyright and
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; * permission notice:
QVariant headerData(int section, Qt::Orientation orientation, int role) const override; *
* Copyright 2013-2021 MultiMC Contributors
bool isLoaded() const; *
bool reload(); * Licensed under the Apache License, Version 2.0 (the "License");
bool save(); * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
private: *
std::vector<GameOptionItem> contents; * http://www.apache.org/licenses/LICENSE-2.0
bool loaded = false; *
QString path; * Unless required by applicable law or agreed to in writing, software
int version = 0; * distributed under the License is distributed on an "AS IS" BASIS,
}; * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <QAbstractListModel>
#include <QString>
#include <map>
enum class OptionType { String, Int, Float, Bool, KeyBind };
struct GameOptionChildItem {
QString value;
int parentRow;
};
struct GameOptionItem {
QString key;
bool boolValue;
int intValue;
float floatValue;
QString value;
OptionType type;
QList<GameOptionChildItem> children;
};
class GameOptions : public QAbstractItemModel {
Q_OBJECT
public:
enum class Column { Key, Description, Value, DefaultValue };
explicit GameOptions(const QString& path);
virtual ~GameOptions() = default;
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
int columnCount(const QModelIndex& parent) const override;
Qt::ItemFlags flags(const QModelIndex& index) const override;
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex& index) const override;
bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override;
bool isLoaded() const;
bool reload();
bool save();
private:
std::vector<GameOptionItem> contents;
bool loaded = false;
QString path;
int version = 0;
};

View File

@ -1,74 +1,96 @@
// SPDX-License-Identifier: GPL-3.0-only // SPDX-License-Identifier: GPL-3.0-only
/* /*
* Prism Launcher - Minecraft Launcher * Prism Launcher - Minecraft Launcher
* Copyright (c) 2022 Jamie Mansfield <jmansfield@cadixdev.org> * Copyright (c) 2022 Jamie Mansfield <jmansfield@cadixdev.org>
* * Copyright (C) 2023 Tayou <tayou@gmx.net>
* This program is free software: you can redistribute it and/or modify * Copyright (C) 2023 TheLastRar <TheLastRar@users.noreply.github.com>
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, version 3. * 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
* This program is distributed in the hope that it will be useful, * the Free Software Foundation, version 3.
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * This program is distributed in the hope that it will be useful,
* GNU General Public License for more details. * but WITHOUT ANY WARRANTY; without even the implied warranty of
* * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* You should have received a copy of the GNU General Public License * GNU General Public License for more details.
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* * You should have received a copy of the GNU General Public License
* This file incorporates work covered by the following copyright and * along with this program. If not, see <https://www.gnu.org/licenses/>.
* permission notice: *
* * This file incorporates work covered by the following copyright and
* Copyright 2013-2021 MultiMC Contributors * permission notice:
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Copyright 2013-2021 MultiMC Contributors
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* http://www.apache.org/licenses/LICENSE-2.0 * You may obtain a copy of the License at
* *
* Unless required by applicable law or agreed to in writing, software * http://www.apache.org/licenses/LICENSE-2.0
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * Unless required by applicable law or agreed to in writing, software
* See the License for the specific language governing permissions and * distributed under the License is distributed on an "AS IS" BASIS,
* limitations under the License. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/ * See the License for the specific language governing permissions and
* limitations under the License.
#include "GameOptionsPage.h" */
#include "minecraft/MinecraftInstance.h"
#include "minecraft/gameoptions/GameOptions.h" #include "GameOptionsPage.h"
#include "ui_GameOptionsPage.h" #include "minecraft/MinecraftInstance.h"
#include "minecraft/gameoptions/GameOptions.h"
GameOptionsPage::GameOptionsPage(MinecraftInstance* inst, QWidget* parent) : QWidget(parent), ui(new Ui::GameOptionsPage) #include "ui_GameOptionsPage.h"
{
ui->setupUi(this); GameOptionsPage::GameOptionsPage(MinecraftInstance* inst, QWidget* parent) : QWidget(parent), ui(new Ui::GameOptionsPage)
ui->tabWidget->tabBar()->hide(); {
m_model = inst->gameOptionsModel(); ui->setupUi(this);
ui->optionsView->setModel(m_model.get()); ui->tabWidget->tabBar()->hide();
auto head = ui->optionsView->header(); m_model = inst->gameOptionsModel();
if (head->count()) { ui->optionsView->setModel(m_model.get());
head->setSectionResizeMode(0, QHeaderView::ResizeToContents); auto head = ui->optionsView->header();
for (int i = 1; i < head->count(); i++) { head->setDefaultSectionSize(250);
head->setSectionResizeMode(i, QHeaderView::Stretch); if (head->count()) {
} for (int i = 1; i < head->count(); i++) {
} head->setSectionResizeMode(i, QHeaderView::Stretch);
} }
head->setSectionResizeMode(head->count() -1, QHeaderView::Stretch);
GameOptionsPage::~GameOptionsPage() }
{ connect(ui->optionsView, &QTreeView::doubleClicked, this, &GameOptionsPage::OptionDoubleClicked);
// m_model->save(); }
}
GameOptionsPage::~GameOptionsPage()
void GameOptionsPage::openedImpl() {
{ // m_model->save();
// m_model->observe(); }
}
void GameOptionsPage::openedImpl()
void GameOptionsPage::closedImpl() {
{ // m_model->observe();
// m_model->unobserve(); }
}
void GameOptionsPage::closedImpl()
void GameOptionsPage::retranslate() {
{ // m_model->unobserve();
ui->retranslateUi(this); }
}
void GameOptionsPage::retranslate()
{
ui->retranslateUi(this);
}
// QTreeView's double click checks if the cell clicked on has children
// but a typical tree model would only have children in the first column
// Workaround this by calling expand ourself
void GameOptionsPage::OptionDoubleClicked(const QModelIndex& index)
{
if (!index.isValid() || index.column() == 0)
return;
const QModelIndex firstColumn = ui->optionsView->model()->index(index.row(), 0, index.parent());
if (!ui->optionsView->model()->hasChildren(firstColumn))
return;
if (ui->optionsView->isExpanded(firstColumn))
ui->optionsView->collapse(firstColumn);
else
ui->optionsView->expand(firstColumn);
}

View File

@ -2,6 +2,8 @@
/* /*
* Prism Launcher - Minecraft Launcher * Prism Launcher - Minecraft Launcher
* Copyright (c) 2022 Jamie Mansfield <jmansfield@cadixdev.org> * Copyright (c) 2022 Jamie Mansfield <jmansfield@cadixdev.org>
* Copyright (C) 2023 Tayou <tayou@gmx.net>
* Copyright (C) 2023 TheLastRar <TheLastRar@users.noreply.github.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -67,4 +69,7 @@ class GameOptionsPage : public QWidget, public BasePage {
private: // data private: // data
Ui::GameOptionsPage* ui = nullptr; Ui::GameOptionsPage* ui = nullptr;
std::shared_ptr<GameOptions> m_model; std::shared_ptr<GameOptions> m_model;
private Q_SLOTS:
void OptionDoubleClicked(const QModelIndex& index);
}; };

View File

@ -66,7 +66,7 @@
</size> </size>
</property> </property>
<property name="rootIsDecorated"> <property name="rootIsDecorated">
<bool>false</bool> <bool>true</bool>
</property> </property>
<attribute name="headerStretchLastSection"> <attribute name="headerStretchLastSection">
<bool>false</bool> <bool>false</bool>