Custom environment variables UI and implementation

Signed-off-by: TheKodeToad <TheKodeToad@proton.me>
This commit is contained in:
TheKodeToad
2023-07-12 18:10:13 +01:00
parent 9499066375
commit 997a3709d4
31 changed files with 6722 additions and 486 deletions

View File

@ -0,0 +1,64 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2023 TheKodeToad <TheKodeToad@proton.me>
*
* 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 <QTabBar>
#include <QVBoxLayout>
#include "EnvironmentVariablesPage.h"
EnvironmentVariablesPage::EnvironmentVariablesPage(QWidget* parent) : QWidget(parent)
{
auto verticalLayout = new QVBoxLayout(this);
verticalLayout->setContentsMargins(0, 0, 0, 0);
variables = new EnvironmentVariables(this);
variables->setContentsMargins(6, 6, 6, 6);
verticalLayout->addWidget(variables);
variables->initialize(false, true, false, APPLICATION->settings()->get("Env").toMap());
}
QString EnvironmentVariablesPage::displayName() const
{
return tr("Environment Variables");
}
QIcon EnvironmentVariablesPage::icon() const
{
return APPLICATION->getThemedIcon("environment-variables");
}
QString EnvironmentVariablesPage::id() const
{
return "environment-variables";
}
QString EnvironmentVariablesPage::helpPage() const
{
return "Environment-variables";
}
bool EnvironmentVariablesPage::apply()
{
APPLICATION->settings()->set("Env", variables->value());
return true;
}
void EnvironmentVariablesPage::retranslate()
{
variables->retranslate();
}

View File

@ -0,0 +1,42 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2023 TheKodeToad <TheKodeToad@proton.me>
*
* 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 <Application.h>
#include "ui/pages/BasePage.h"
#include "ui/widgets/EnvironmentVariables.h"
class EnvironmentVariablesPage : public QWidget, public BasePage {
Q_OBJECT
public:
explicit EnvironmentVariablesPage(QWidget* parent = nullptr);
QString displayName() const override;
QIcon icon() const override;
QString id() const override;
QString helpPage() const override;
bool apply() override;
void retranslate() override;
private:
EnvironmentVariables* variables;
};

View File

@ -84,6 +84,9 @@ void InstanceSettingsPage::globalSettingsButtonClicked(bool)
case 2:
APPLICATION->ShowGlobalSettings(this, "custom-commands");
return;
case 3:
APPLICATION->ShowGlobalSettings(this, "environment-variables");
return;
default:
APPLICATION->ShowGlobalSettings(this, "minecraft-settings");
return;
@ -217,6 +220,11 @@ void InstanceSettingsPage::applySettings()
m_settings->reset("PostExitCommand");
}
// Environment Variables
m_settings->set("UseEnv", ui->environmentVariables->checked());
m_settings->set("OverrideEnv", ui->environmentVariables->override());
m_settings->set("Env", ui->environmentVariables->value());
// Workarounds
bool workarounds = ui->nativeWorkaroundsGroupBox->isChecked();
m_settings->set("OverrideNativeWorkarounds", workarounds);
@ -345,6 +353,14 @@ void InstanceSettingsPage::loadSettings()
m_settings->get("PostExitCommand").toString()
);
// Environment variables
ui->environmentVariables->initialize(
true,
m_settings->get("UseEnv").toBool(),
m_settings->get("OverrideEnv").toBool(),
m_settings->get("Env").toMap()
);
// Workarounds
ui->nativeWorkaroundsGroupBox->setChecked(m_settings->get("OverrideNativeWorkarounds").toBool());
ui->useNativeGLFWCheck->setChecked(m_settings->get("UseNativeGLFW").toBool());
@ -492,6 +508,7 @@ void InstanceSettingsPage::retranslate()
{
ui->retranslateUi(this);
ui->customCommands->retranslate(); // TODO: why is this seperate from the others?
ui->environmentVariables->retranslate();
}
void InstanceSettingsPage::updateThresholds()

View File

@ -35,13 +35,10 @@
</item>
<item>
<widget class="QTabWidget" name="settingsTabs">
<property name="tabShape">
<enum>QTabWidget::Rounded</enum>
</property>
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="minecraftTab">
<widget class="QWidget" name="minecraftPage">
<attribute name="title">
<string notr="true">Java</string>
</attribute>
@ -246,7 +243,7 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="javaTab">
<widget class="QWidget" name="javaPage">
<attribute name="title">
<string>Game windows</string>
</attribute>
@ -406,7 +403,7 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="tab">
<widget class="QWidget" name="customCommandsPage">
<attribute name="title">
<string>Custom commands</string>
</attribute>
@ -416,6 +413,16 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="environmentVariablesPage">
<attribute name="title">
<string>Environment variables</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_16">
<item>
<widget class="EnvironmentVariables" name="environmentVariables" native="true"/>
</item>
</layout>
</widget>
<widget class="QWidget" name="workaroundsPage">
<attribute name="title">
<string>Workarounds</string>
@ -669,6 +676,12 @@
<header>ui/widgets/CustomCommands.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>EnvironmentVariables</class>
<extends>QWidget</extends>
<header>ui/widgets/EnvironmentVariables.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>openGlobalJavaSettingsButton</tabstop>

View File

@ -0,0 +1,138 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2023 TheKodeToad <TheKodeToad@proton.me>
*
* 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 <QKeyEvent>
#include "Application.h"
#include "EnvironmentVariables.h"
#include "ui/dialogs/CustomMessageBox.h"
#include "ui_EnvironmentVariables.h"
EnvironmentVariables::EnvironmentVariables(QWidget* parent) : QWidget(parent), ui(new Ui::EnvironmentVariables)
{
ui->setupUi(this);
ui->list->installEventFilter(this);
ui->list->sortItems(0, Qt::AscendingOrder);
ui->list->setSortingEnabled(true);
ui->list->header()->resizeSections(QHeaderView::Interactive);
ui->list->header()->resizeSection(0, 200);
connect(ui->add, &QPushButton::clicked, this, [this] {
auto item = new QTreeWidgetItem(ui->list);
item->setText(0, "ENV_VAR");
item->setText(1, "value");
item->setFlags(item->flags() | Qt::ItemIsEditable);
ui->list->addTopLevelItem(item);
ui->list->selectionModel()->select(ui->list->model()->index(ui->list->indexOfTopLevelItem(item), 0),
QItemSelectionModel::ClearAndSelect | QItemSelectionModel::SelectionFlag::Rows);
ui->list->editItem(item);
});
connect(ui->remove, &QPushButton::clicked, this, [this] {
for (QTreeWidgetItem* item : ui->list->selectedItems())
ui->list->takeTopLevelItem(ui->list->indexOfTopLevelItem(item));
});
connect(ui->clear, &QPushButton::clicked, this, [this] { ui->list->clear(); });
connect(ui->globalOverride, &QCheckBox::clicked, this, [this](bool state) {
if (!state)
return;
auto global = APPLICATION->settings()->get("Env").toMap();
if (global.isEmpty())
return;
auto response = CustomMessageBox::selectable(
this, tr("Reset"),
tr("You have chosen to ignore global settings.\n\nWould you like to clear the current variables and copy "
"the global variables over?"),
QMessageBox::Question, QMessageBox::Yes | QMessageBox::No, QMessageBox::No)
->exec();
if (response == QMessageBox::Yes)
initialize(true, checked(), override(), global);
});
}
EnvironmentVariables::~EnvironmentVariables()
{
delete ui;
}
void EnvironmentVariables::initialize(bool instance, bool checked, bool override, const QMap<QString, QVariant>& value)
{
// update widgets to settings
ui->groupBox->setCheckable(instance);
ui->groupBox->setChecked(checked);
ui->globalOverride->setVisible(instance);
ui->globalOverride->setChecked(override);
// populate
ui->list->clear();
for (auto iter = value.begin(); iter != value.end(); iter++) {
auto item = new QTreeWidgetItem(ui->list);
item->setText(0, iter.key());
item->setText(1, iter.value().toString());
item->setFlags(item->flags() | Qt::ItemIsEditable);
ui->list->addTopLevelItem(item);
}
}
bool EnvironmentVariables::eventFilter(QObject* watched, QEvent* event)
{
if (watched == ui->list && event->type() == QEvent::KeyPress) {
const QKeyEvent* keyEvent = (QKeyEvent*)event;
if (keyEvent->key() == Qt::Key_Delete) {
emit ui->remove->clicked();
return true;
}
}
return QObject::eventFilter(watched, event);
}
void EnvironmentVariables::retranslate()
{
ui->retranslateUi(this);
}
bool EnvironmentVariables::checked() const
{
if (!ui->groupBox->isCheckable())
return true;
return ui->groupBox->isChecked();
}
bool EnvironmentVariables::override() const
{
if (!ui->globalOverride->isVisible())
return false;
return ui->globalOverride->isChecked();
}
QMap<QString, QVariant> EnvironmentVariables::value() const
{
QMap<QString, QVariant> result;
QTreeWidgetItem* item = ui->list->topLevelItem(0);
for (int i = 1; item != nullptr; item = ui->list->topLevelItem(i++))
result[item->text(0)] = item->text(1);
return result;
}

View File

@ -0,0 +1,44 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2023 TheKodeToad <TheKodeToad@proton.me>
*
* 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 <QMap>
#include <QWidget>
namespace Ui {
class EnvironmentVariables;
}
class EnvironmentVariables : public QWidget {
Q_OBJECT
public:
explicit EnvironmentVariables(QWidget* state = nullptr);
~EnvironmentVariables() override;
void initialize(bool instance, bool checked, bool override, const QMap<QString, QVariant>& value);
bool eventFilter(QObject* watched, QEvent* event) override;
void retranslate();
bool checked() const;
bool override() const;
QMap<QString, QVariant> value() const;
private:
Ui::EnvironmentVariables* ui;
};

View File

@ -0,0 +1,122 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>EnvironmentVariables</class>
<widget class="QWidget" name="EnvironmentVariables">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>565</width>
<height>410</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>&amp;Environment Variables</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QCheckBox" name="globalOverride">
<property name="text">
<string>&amp;Ignore global settings</string>
</property>
</widget>
</item>
<item>
<widget class="QTreeWidget" name="list">
<property name="alternatingRowColors">
<bool>true</bool>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::ExtendedSelection</enum>
</property>
<property name="rootIsDecorated">
<bool>false</bool>
</property>
<property name="itemsExpandable">
<bool>false</bool>
</property>
<property name="animated">
<bool>true</bool>
</property>
<property name="expandsOnDoubleClick">
<bool>false</bool>
</property>
<column>
<property name="text">
<string>Name</string>
</property>
</column>
<column>
<property name="text">
<string>Value</string>
</property>
</column>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="buttons">
<item>
<widget class="QPushButton" name="add">
<property name="text">
<string>&amp;Add</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="remove">
<property name="text">
<string>&amp;Remove</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="clear">
<property name="text">
<string>&amp;Clear</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>