Only call on interactive uses

Signed-off-by: Yihe Li <winmikedows@hotmail.com>
This commit is contained in:
Yihe Li 2025-03-27 06:53:57 +08:00
parent ea44c2465c
commit 8fea37b8b7
No known key found for this signature in database
4 changed files with 22 additions and 7 deletions

View File

@ -388,6 +388,17 @@ void BaseInstance::setName(QString val)
emit propertiesChanged(this); emit propertiesChanged(this);
} }
bool BaseInstance::syncInstanceDirName() const
{
auto oldRoot = instanceRoot();
auto newRoot = FS::PathCombine(QFileInfo(oldRoot).dir().absolutePath(), name());
if (oldRoot == newRoot)
return true;
if (!QFile::rename(oldRoot, newRoot))
return false;
return true;
}
QString BaseInstance::name() const QString BaseInstance::name() const
{ {
return m_settings->get("name").toString(); return m_settings->get("name").toString();

View File

@ -126,6 +126,9 @@ class BaseInstance : public QObject, public std::enable_shared_from_this<BaseIns
QString name() const; QString name() const;
void setName(QString val); void setName(QString val);
/// Sync name and rename instance dir accordingly; returns true if successful
bool syncInstanceDirName() const;
/// Value used for instance window titles /// Value used for instance window titles
QString windowTitle() const; QString windowTitle() const;

View File

@ -93,8 +93,7 @@ bool askToUpdateInstanceDirName(InstancePtr instance, QWidget* parent)
return false; return false;
// Now we can confirm that a renaming is happening // Now we can confirm that a renaming is happening
auto ret = QFile::rename(oldRoot, newRoot); if (!instance->syncInstanceDirName()) {
if (!ret) {
QMessageBox::warning(parent, QObject::tr("Cannot rename instance"), QMessageBox::warning(parent, QObject::tr("Cannot rename instance"),
QObject::tr("An error occurred when performing the following renaming operation: <br/>" QObject::tr("An error occurred when performing the following renaming operation: <br/>"
" - Old instance root: %1<br/>" " - Old instance root: %1<br/>"

View File

@ -1428,6 +1428,13 @@ void MainWindow::on_actionExportInstanceFlamePack_triggered()
void MainWindow::on_actionRenameInstance_triggered() void MainWindow::on_actionRenameInstance_triggered()
{ {
if (m_selectedInstance) { if (m_selectedInstance) {
connect(view->itemDelegate(), &QAbstractItemDelegate::closeEditor, this, [this] {
if (askToUpdateInstanceDirName(m_selectedInstance, this)) {
auto newID = m_selectedInstance->name();
refreshInstances();
setSelectedInstanceById(newID);
}
}, Qt::SingleShotConnection);
view->edit(view->currentIndex()); view->edit(view->currentIndex());
} }
} }
@ -1693,11 +1700,6 @@ void MainWindow::instanceDataChanged(const QModelIndex& topLeft, const QModelInd
QItemSelection test(topLeft, bottomRight); QItemSelection test(topLeft, bottomRight);
if (test.contains(current)) { if (test.contains(current)) {
instanceChanged(current, current); instanceChanged(current, current);
if (m_selectedInstance && askToUpdateInstanceDirName(m_selectedInstance, this)) {
auto newID = m_selectedInstance->name();
refreshInstances();
setSelectedInstanceById(newID);
}
} }
} }