diff --git a/launcher/InstanceDirUpdate.cpp b/launcher/InstanceDirUpdate.cpp index 49010e6c9..ac6dddc37 100644 --- a/launcher/InstanceDirUpdate.cpp +++ b/launcher/InstanceDirUpdate.cpp @@ -46,21 +46,22 @@ #include "InstanceList.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(); if (renamingMode == "MetadataOnly") return QString(); auto oldRoot = instance->instanceRoot(); - auto oldName = QFileInfo(oldRoot).baseName(); - if (oldName == FS::RemoveInvalidFilenameChars(instance->name(), '-')) - return QString(); - - auto newName = FS::DirNameFromString(instance->name(), QFileInfo(oldRoot).dir().absolutePath()); - auto newRoot = FS::PathCombine(QFileInfo(oldRoot).dir().absolutePath(), newName); + auto newDirName = FS::DirNameFromString(newName, QFileInfo(oldRoot).dir().absolutePath()); + auto newRoot = FS::PathCombine(QFileInfo(oldRoot).dir().absolutePath(), newDirName); if (oldRoot == newRoot) return QString(); + if (oldRoot == FS::PathCombine(QFileInfo(oldRoot).dir().absolutePath(), newName)) + return QString(); // Check for conflict if (QDir(newRoot).exists()) { diff --git a/launcher/InstanceDirUpdate.h b/launcher/InstanceDirUpdate.h index 354eba465..94dd3b933 100644 --- a/launcher/InstanceDirUpdate.h +++ b/launcher/InstanceDirUpdate.h @@ -40,7 +40,7 @@ #include "BaseInstance.h" /// 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 bool checkLinkedInstances(const QString& id, QWidget* parent, const QString& verb); diff --git a/launcher/InstanceList.cpp b/launcher/InstanceList.cpp index a4f694e2e..918fa1073 100644 --- a/launcher/InstanceList.cpp +++ b/launcher/InstanceList.cpp @@ -583,18 +583,6 @@ InstancePtr InstanceList::getInstanceById(QString instId) const 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 { if (managed_name.isEmpty()) @@ -613,11 +601,6 @@ QModelIndex InstanceList::getInstanceIndexById(const QString& id) const 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 count = m_instances.count(); diff --git a/launcher/InstanceList.h b/launcher/InstanceList.h index dace9e5cf..c85fe55c7 100644 --- a/launcher/InstanceList.h +++ b/launcher/InstanceList.h @@ -99,11 +99,9 @@ class InstanceList : public QAbstractListModel { /* O(n) */ InstancePtr getInstanceById(QString id) const; - InstancePtr getInstanceByRoot(QString instanceRoot) const; /* O(n) */ InstancePtr getInstanceByManagedName(const QString& managed_name) const; QModelIndex getInstanceIndexById(const QString& id) const; - QModelIndex getInstanceIndexByRoot(const QString& instanceRoot) const; QStringList getGroups(); bool isGroupCollapsed(const QString& groupName); diff --git a/launcher/ui/MainWindow.cpp b/launcher/ui/MainWindow.cpp index 7e9c72da7..6c5f86d0a 100644 --- a/launcher/ui/MainWindow.cpp +++ b/launcher/ui/MainWindow.cpp @@ -289,14 +289,15 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWi view->setSelectionMode(QAbstractItemView::SingleSelection); // FIXME: leaks ListViewDelegate - view->setItemDelegate(new ListViewDelegate(this)); + auto delegate = new ListViewDelegate(this); + view->setItemDelegate(delegate); view->setFrameShape(QFrame::NoFrame); // do not show ugly blue border on the mac view->setAttribute(Qt::WA_MacShowFocusRect, false); - connect(view->itemDelegate(), &QAbstractItemDelegate::closeEditor, this, [this] { - if (auto newRoot = askToUpdateInstanceDirName(m_selectedInstance, this); !newRoot.isEmpty()) { + connect(delegate, &ListViewDelegate::textChanged, this, [this](QString before, QString after) { + if (auto newRoot = askToUpdateInstanceDirName(m_selectedInstance, before, after, this); !newRoot.isEmpty()) { 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() { if (!m_selectedInstance) diff --git a/launcher/ui/MainWindow.h b/launcher/ui/MainWindow.h index 02e9bb31e..0e692eda7 100644 --- a/launcher/ui/MainWindow.h +++ b/launcher/ui/MainWindow.h @@ -224,7 +224,6 @@ class MainWindow : public QMainWindow { void setCatBackground(bool enabled); void updateInstanceToolIcon(QString new_icon); void setSelectedInstanceById(const QString& id); - void setSelectedInstanceByRoot(const QString& instanceRoot); void updateStatusCenter(); void setInstanceActionsEnabled(bool enabled); diff --git a/launcher/ui/instanceview/InstanceDelegate.cpp b/launcher/ui/instanceview/InstanceDelegate.cpp index d947163bc..c7115801e 100644 --- a/launcher/ui/instanceview/InstanceDelegate.cpp +++ b/launcher/ui/instanceview/InstanceDelegate.cpp @@ -397,6 +397,7 @@ void ListViewDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, // Prevent instance names longer than 128 chars text.truncate(128); if (text.size() != 0) { + emit textChanged(model->data(index).toString(), text); model->setData(index, text); } } diff --git a/launcher/ui/instanceview/InstanceDelegate.h b/launcher/ui/instanceview/InstanceDelegate.h index 69dd32ba7..98ff9a2fc 100644 --- a/launcher/ui/instanceview/InstanceDelegate.h +++ b/launcher/ui/instanceview/InstanceDelegate.h @@ -33,6 +33,9 @@ class ListViewDelegate : public QStyledItemDelegate { void setEditorData(QWidget* editor, 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: void editingDone(); };