Use custom signals to record previous name

Signed-off-by: Yihe Li <winmikedows@hotmail.com>
This commit is contained in:
Yihe Li 2025-03-27 17:13:38 +08:00
parent 11a0dbf810
commit a2e44c0ef7
No known key found for this signature in database
8 changed files with 18 additions and 44 deletions

View File

@ -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()) {

View File

@ -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);

View File

@ -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();

View File

@ -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);

View File

@ -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)

View File

@ -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);

View File

@ -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);
}
}

View File

@ -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();
};