Merge pull request #1383 from TheKodeToad/rename-groups

Rename groups
This commit is contained in:
Tayou
2023-10-15 23:38:27 +02:00
committed by GitHub
6 changed files with 173 additions and 97 deletions

View File

@ -514,10 +514,10 @@ void MainWindow::showInstanceContextMenu(const QPoint& pos)
} else {
auto group = view->groupNameAt(pos);
QAction* actionVoid = new QAction(BuildConfig.LAUNCHER_DISPLAYNAME, this);
QAction* actionVoid = new QAction(group.isNull() ? BuildConfig.LAUNCHER_DISPLAYNAME : group, this);
actionVoid->setEnabled(false);
QAction* actionCreateInstance = new QAction(tr("Create instance"), this);
QAction* actionCreateInstance = new QAction(tr("&Create instance"), this);
actionCreateInstance->setToolTip(ui->actionAddInstance->toolTip());
if (!group.isNull()) {
QVariantMap instance_action_data;
@ -531,12 +531,13 @@ void MainWindow::showInstanceContextMenu(const QPoint& pos)
actions.prepend(actionVoid);
actions.append(actionCreateInstance);
if (!group.isNull()) {
QAction* actionDeleteGroup = new QAction(tr("Delete group '%1'").arg(group), this);
QVariantMap delete_group_action_data;
delete_group_action_data["group"] = group;
actionDeleteGroup->setData(delete_group_action_data);
connect(actionDeleteGroup, SIGNAL(triggered(bool)), SLOT(deleteGroup()));
QAction* actionDeleteGroup = new QAction(tr("&Delete group"), this);
connect(actionDeleteGroup, &QAction::triggered, this, [this, group] { deleteGroup(group); });
actions.append(actionDeleteGroup);
QAction* actionRenameGroup = new QAction(tr("&Rename group"), this);
connect(actionRenameGroup, &QAction::triggered, this, [this, group] { renameGroup(group); });
actions.append(actionRenameGroup);
}
}
QMenu myMenu;
@ -1128,40 +1129,49 @@ void MainWindow::on_actionChangeInstGroup_triggered()
if (!m_selectedInstance)
return;
bool ok = false;
InstanceId instId = m_selectedInstance->id();
QString name(APPLICATION->instances()->getInstanceGroup(instId));
auto groups = APPLICATION->instances()->getGroups();
groups.insert(0, "");
groups.sort(Qt::CaseInsensitive);
int foo = groups.indexOf(name);
QString src(APPLICATION->instances()->getInstanceGroup(instId));
QStringList groups = APPLICATION->instances()->getGroups();
groups.prepend("");
int index = groups.indexOf(src);
bool ok = false;
QString dst = QInputDialog::getItem(this, tr("Group name"), tr("Enter a new group name."), groups, index, true, &ok);
dst = dst.simplified();
name = QInputDialog::getItem(this, tr("Group name"), tr("Enter a new group name."), groups, foo, true, &ok);
name = name.simplified();
if (ok) {
APPLICATION->instances()->setInstanceGroup(instId, name);
APPLICATION->instances()->setInstanceGroup(instId, dst);
}
}
void MainWindow::deleteGroup()
void MainWindow::deleteGroup(QString group)
{
QObject* obj = sender();
if (!obj)
Q_ASSERT(!group.isEmpty());
const int reply = QMessageBox::question(this, tr("Delete group"), tr("Are you sure you want to delete the group '%1'?").arg(group),
QMessageBox::Yes | QMessageBox::No);
if (reply == QMessageBox::Yes)
APPLICATION->instances()->deleteGroup(group);
}
void MainWindow::renameGroup(QString group)
{
Q_ASSERT(!group.isEmpty());
QString name = QInputDialog::getText(this, tr("Rename group"), tr("Enter a new group name."), QLineEdit::Normal, group);
name = name.simplified();
if (name.isNull() || name == group)
return;
QAction* action = qobject_cast<QAction*>(obj);
if (!action)
const bool empty = name.isEmpty();
const bool duplicate = APPLICATION->instances()->getGroups().contains(name, Qt::CaseInsensitive) && group.toLower() != name.toLower();
if (empty || duplicate) {
QMessageBox::warning(this, tr("Cannot rename group"), empty ? tr("Cannot set empty name.") : tr("Group already exists. :/"));
return;
auto map = action->data().toMap();
if (!map.contains("group"))
return;
QString groupName = map["group"].toString();
if (!groupName.isEmpty()) {
auto reply = QMessageBox::question(this, tr("Delete group"), tr("Are you sure you want to delete the group %1?").arg(groupName),
QMessageBox::Yes | QMessageBox::No);
if (reply == QMessageBox::Yes) {
APPLICATION->instances()->deleteGroup(groupName);
}
}
APPLICATION->instances()->renameGroup(group, name);
}
void MainWindow::undoTrashInstance()

View File

@ -148,7 +148,8 @@ class MainWindow : public QMainWindow {
void on_actionDeleteInstance_triggered();
void deleteGroup();
void deleteGroup(QString group);
void renameGroup(QString group);
void undoTrashInstance();
inline void on_actionExportInstance_triggered() { on_actionExportInstanceZip_triggered(); }

View File

@ -2,6 +2,7 @@
/*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
* 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
@ -61,22 +62,14 @@ CopyInstanceDialog::CopyInstanceDialog(InstancePtr original, QWidget* parent)
ui->iconButton->setIcon(APPLICATION->icons()->getIcon(InstIconKey));
ui->instNameTextBox->setText(original->name());
ui->instNameTextBox->setFocus();
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
auto groupList = APPLICATION->instances()->getGroups();
QSet<QString> groups(groupList.begin(), groupList.end());
groupList = QStringList(groups.values());
#else
auto groups = APPLICATION->instances()->getGroups().toSet();
auto groupList = QStringList(groups.toList());
#endif
groupList.sort(Qt::CaseInsensitive);
groupList.removeOne("");
groupList.push_front("");
ui->groupBox->addItems(groupList);
int index = groupList.indexOf(APPLICATION->instances()->getInstanceGroup(m_original->id()));
if (index == -1) {
QStringList groups = APPLICATION->instances()->getGroups();
groups.prepend("");
ui->groupBox->addItems(groups);
int index = groups.indexOf(APPLICATION->instances()->getInstanceGroup(m_original->id()));
if (index == -1)
index = 0;
}
ui->groupBox->setCurrentIndex(index);
ui->groupBox->lineEdit()->setPlaceholderText(tr("No group"));
ui->copySavesCheckbox->setChecked(m_selectedOptions.isCopySavesEnabled());

View File

@ -2,6 +2,7 @@
/*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
* 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
@ -75,23 +76,14 @@ NewInstanceDialog::NewInstanceDialog(const QString& initialGroup,
InstIconKey = "default";
ui->iconButton->setIcon(APPLICATION->icons()->getIcon(InstIconKey));
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
auto groupList = APPLICATION->instances()->getGroups();
auto groups = QSet<QString>(groupList.begin(), groupList.end());
groupList = groups.values();
#else
auto groups = APPLICATION->instances()->getGroups().toSet();
auto groupList = QStringList(groups.toList());
#endif
groupList.sort(Qt::CaseInsensitive);
groupList.removeOne("");
groupList.push_front(initialGroup);
groupList.push_front("");
ui->groupBox->addItems(groupList);
int index = groupList.indexOf(initialGroup);
QStringList groups = APPLICATION->instances()->getGroups();
groups.prepend("");
int index = groups.indexOf(initialGroup);
if (index == -1) {
index = 0;
index = 1;
groups.insert(index, initialGroup);
}
ui->groupBox->addItems(groups);
ui->groupBox->setCurrentIndex(index);
ui->groupBox->lineEdit()->setPlaceholderText(tr("No group"));