mirror of
https://github.com/PrismLauncher/PrismLauncher.git
synced 2025-04-30 06:34:27 +02:00
Use custom signals to record previous name
Signed-off-by: Yihe Li <winmikedows@hotmail.com>
This commit is contained in:
parent
11a0dbf810
commit
a2e44c0ef7
@ -46,21 +46,22 @@
|
|||||||
#include "InstanceList.h"
|
#include "InstanceList.h"
|
||||||
#include "ui/dialogs/CustomMessageBox.h"
|
#include "ui/dialogs/CustomMessageBox.h"
|
||||||
|
|
||||||
QString askToUpdateInstanceDirName(InstancePtr instance, QWidget* parent)
|
QString askToUpdateInstanceDirName(InstancePtr instance, const QString& oldName, const QString& newName, QWidget* parent)
|
||||||
{
|
{
|
||||||
|
if (oldName == newName)
|
||||||
|
return QString();
|
||||||
|
|
||||||
QString renamingMode = APPLICATION->settings()->get("InstRenamingMode").toString();
|
QString renamingMode = APPLICATION->settings()->get("InstRenamingMode").toString();
|
||||||
if (renamingMode == "MetadataOnly")
|
if (renamingMode == "MetadataOnly")
|
||||||
return QString();
|
return QString();
|
||||||
|
|
||||||
auto oldRoot = instance->instanceRoot();
|
auto oldRoot = instance->instanceRoot();
|
||||||
auto oldName = QFileInfo(oldRoot).baseName();
|
auto newDirName = FS::DirNameFromString(newName, QFileInfo(oldRoot).dir().absolutePath());
|
||||||
if (oldName == FS::RemoveInvalidFilenameChars(instance->name(), '-'))
|
auto newRoot = FS::PathCombine(QFileInfo(oldRoot).dir().absolutePath(), newDirName);
|
||||||
return QString();
|
|
||||||
|
|
||||||
auto newName = FS::DirNameFromString(instance->name(), QFileInfo(oldRoot).dir().absolutePath());
|
|
||||||
auto newRoot = FS::PathCombine(QFileInfo(oldRoot).dir().absolutePath(), newName);
|
|
||||||
if (oldRoot == newRoot)
|
if (oldRoot == newRoot)
|
||||||
return QString();
|
return QString();
|
||||||
|
if (oldRoot == FS::PathCombine(QFileInfo(oldRoot).dir().absolutePath(), newName))
|
||||||
|
return QString();
|
||||||
|
|
||||||
// Check for conflict
|
// Check for conflict
|
||||||
if (QDir(newRoot).exists()) {
|
if (QDir(newRoot).exists()) {
|
||||||
|
@ -40,7 +40,7 @@
|
|||||||
#include "BaseInstance.h"
|
#include "BaseInstance.h"
|
||||||
|
|
||||||
/// Update instanceRoot to make it sync with name/id; return newRoot if a directory rename happened
|
/// Update instanceRoot to make it sync with name/id; return newRoot if a directory rename happened
|
||||||
QString askToUpdateInstanceDirName(InstancePtr instance, QWidget* parent);
|
QString askToUpdateInstanceDirName(InstancePtr instance, const QString& oldName, const QString& newName, QWidget* parent);
|
||||||
|
|
||||||
/// Check if there are linked instances, and display a warning; return true if the operation should proceed
|
/// Check if there are linked instances, and display a warning; return true if the operation should proceed
|
||||||
bool checkLinkedInstances(const QString& id, QWidget* parent, const QString& verb);
|
bool checkLinkedInstances(const QString& id, QWidget* parent, const QString& verb);
|
||||||
|
@ -583,18 +583,6 @@ InstancePtr InstanceList::getInstanceById(QString instId) const
|
|||||||
return InstancePtr();
|
return InstancePtr();
|
||||||
}
|
}
|
||||||
|
|
||||||
InstancePtr InstanceList::getInstanceByRoot(QString instanceRoot) const
|
|
||||||
{
|
|
||||||
if (instanceRoot.isEmpty())
|
|
||||||
return InstancePtr();
|
|
||||||
for (auto& inst : m_instances) {
|
|
||||||
if (inst->instanceRoot() == instanceRoot) {
|
|
||||||
return inst;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return InstancePtr();
|
|
||||||
}
|
|
||||||
|
|
||||||
InstancePtr InstanceList::getInstanceByManagedName(const QString& managed_name) const
|
InstancePtr InstanceList::getInstanceByManagedName(const QString& managed_name) const
|
||||||
{
|
{
|
||||||
if (managed_name.isEmpty())
|
if (managed_name.isEmpty())
|
||||||
@ -613,11 +601,6 @@ QModelIndex InstanceList::getInstanceIndexById(const QString& id) const
|
|||||||
return index(getInstIndex(getInstanceById(id).get()));
|
return index(getInstIndex(getInstanceById(id).get()));
|
||||||
}
|
}
|
||||||
|
|
||||||
QModelIndex InstanceList::getInstanceIndexByRoot(const QString& instanceRoot) const
|
|
||||||
{
|
|
||||||
return index(getInstIndex(getInstanceByRoot(instanceRoot).get()));
|
|
||||||
}
|
|
||||||
|
|
||||||
int InstanceList::getInstIndex(BaseInstance* inst) const
|
int InstanceList::getInstIndex(BaseInstance* inst) const
|
||||||
{
|
{
|
||||||
int count = m_instances.count();
|
int count = m_instances.count();
|
||||||
|
@ -99,11 +99,9 @@ class InstanceList : public QAbstractListModel {
|
|||||||
|
|
||||||
/* O(n) */
|
/* O(n) */
|
||||||
InstancePtr getInstanceById(QString id) const;
|
InstancePtr getInstanceById(QString id) const;
|
||||||
InstancePtr getInstanceByRoot(QString instanceRoot) const;
|
|
||||||
/* O(n) */
|
/* O(n) */
|
||||||
InstancePtr getInstanceByManagedName(const QString& managed_name) const;
|
InstancePtr getInstanceByManagedName(const QString& managed_name) const;
|
||||||
QModelIndex getInstanceIndexById(const QString& id) const;
|
QModelIndex getInstanceIndexById(const QString& id) const;
|
||||||
QModelIndex getInstanceIndexByRoot(const QString& instanceRoot) const;
|
|
||||||
QStringList getGroups();
|
QStringList getGroups();
|
||||||
bool isGroupCollapsed(const QString& groupName);
|
bool isGroupCollapsed(const QString& groupName);
|
||||||
|
|
||||||
|
@ -289,14 +289,15 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWi
|
|||||||
|
|
||||||
view->setSelectionMode(QAbstractItemView::SingleSelection);
|
view->setSelectionMode(QAbstractItemView::SingleSelection);
|
||||||
// FIXME: leaks ListViewDelegate
|
// FIXME: leaks ListViewDelegate
|
||||||
view->setItemDelegate(new ListViewDelegate(this));
|
auto delegate = new ListViewDelegate(this);
|
||||||
|
view->setItemDelegate(delegate);
|
||||||
view->setFrameShape(QFrame::NoFrame);
|
view->setFrameShape(QFrame::NoFrame);
|
||||||
// do not show ugly blue border on the mac
|
// do not show ugly blue border on the mac
|
||||||
view->setAttribute(Qt::WA_MacShowFocusRect, false);
|
view->setAttribute(Qt::WA_MacShowFocusRect, false);
|
||||||
connect(view->itemDelegate(), &QAbstractItemDelegate::closeEditor, this, [this] {
|
connect(delegate, &ListViewDelegate::textChanged, this, [this](QString before, QString after) {
|
||||||
if (auto newRoot = askToUpdateInstanceDirName(m_selectedInstance, this); !newRoot.isEmpty()) {
|
if (auto newRoot = askToUpdateInstanceDirName(m_selectedInstance, before, after, this); !newRoot.isEmpty()) {
|
||||||
refreshInstances();
|
refreshInstances();
|
||||||
setSelectedInstanceByRoot(newRoot);
|
setSelectedInstanceById(QFileInfo(newRoot).fileName());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -1138,18 +1139,6 @@ void MainWindow::setSelectedInstanceById(const QString& id)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::setSelectedInstanceByRoot(const QString& instanceRoot)
|
|
||||||
{
|
|
||||||
if (instanceRoot.isNull())
|
|
||||||
return;
|
|
||||||
const QModelIndex index = APPLICATION->instances()->getInstanceIndexByRoot(instanceRoot);
|
|
||||||
if (index.isValid()) {
|
|
||||||
QModelIndex selectionIndex = proxymodel->mapFromSource(index);
|
|
||||||
view->selectionModel()->setCurrentIndex(selectionIndex, QItemSelectionModel::ClearAndSelect);
|
|
||||||
updateStatusCenter();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainWindow::on_actionChangeInstGroup_triggered()
|
void MainWindow::on_actionChangeInstGroup_triggered()
|
||||||
{
|
{
|
||||||
if (!m_selectedInstance)
|
if (!m_selectedInstance)
|
||||||
|
@ -224,7 +224,6 @@ class MainWindow : public QMainWindow {
|
|||||||
void setCatBackground(bool enabled);
|
void setCatBackground(bool enabled);
|
||||||
void updateInstanceToolIcon(QString new_icon);
|
void updateInstanceToolIcon(QString new_icon);
|
||||||
void setSelectedInstanceById(const QString& id);
|
void setSelectedInstanceById(const QString& id);
|
||||||
void setSelectedInstanceByRoot(const QString& instanceRoot);
|
|
||||||
void updateStatusCenter();
|
void updateStatusCenter();
|
||||||
void setInstanceActionsEnabled(bool enabled);
|
void setInstanceActionsEnabled(bool enabled);
|
||||||
|
|
||||||
|
@ -397,6 +397,7 @@ void ListViewDelegate::setModelData(QWidget* editor, QAbstractItemModel* model,
|
|||||||
// Prevent instance names longer than 128 chars
|
// Prevent instance names longer than 128 chars
|
||||||
text.truncate(128);
|
text.truncate(128);
|
||||||
if (text.size() != 0) {
|
if (text.size() != 0) {
|
||||||
|
emit textChanged(model->data(index).toString(), text);
|
||||||
model->setData(index, text);
|
model->setData(index, text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,6 +33,9 @@ class ListViewDelegate : public QStyledItemDelegate {
|
|||||||
void setEditorData(QWidget* editor, const QModelIndex& index) const override;
|
void setEditorData(QWidget* editor, const QModelIndex& index) const override;
|
||||||
void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override;
|
void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override;
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void textChanged(QString before, QString after) const;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void editingDone();
|
void editingDone();
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user