Commit f9653015 authored by Waqar Ahmed's avatar Waqar Ahmed Committed by Christoph Cullmann
Browse files

Fix file renaming

Call to "emitDataChanged()" was causing the crash maybe. It could be
because there were two "emitDataChanged()", hence it is removed.

This change properly fixes the renaming.
parent 71027932
......@@ -73,6 +73,17 @@ bool KateProject::reload(bool force)
return load(m_globalProject, force);
}
void KateProject::renameFile(const QString &newName, const QString &oldName)
{
auto it = m_file2Item->find(oldName);
if (it == m_file2Item->end()) {
qWarning() << "renameFile() File not found, new: " << newName << "old: " << oldName;
return;
}
(*m_file2Item)[newName] = it.value();
m_file2Item->erase(it);
}
/**
* Read a JSON document from file.
*
......
......@@ -152,6 +152,11 @@ public:
}
}
/**
* rename a file
*/
void renameFile(const QString &newName, const QString &oldName);
/**
* Access to project index.
* May be null.
......
......@@ -6,6 +6,7 @@
*/
#include "kateprojectitem.h"
#include "kateproject.h"
#include <QCoreApplication>
#include <QDir>
......@@ -140,6 +141,15 @@ void KateProjectItem::setData(const QVariant &value, int role)
if (newFileName.isEmpty())
return;
/**
* retrieve the ref to project that we stored
* in KateProjectTreeViewContextMenu
*/
KateProject *project = data(KateProjectItem::ProjectRole).value<KateProject *>();
if (!project) {
return;
}
auto oldFileName = data(Qt::DisplayRole).toString();
auto oldName = data(Qt::UserRole).toString();
QString newName = oldName;
......@@ -154,10 +164,13 @@ void KateProjectItem::setData(const QVariant &value, int role)
return;
}
/**
* Update the file2Item
*/
project->renameFile(newName, oldName);
// change internal path
setData(newName, Qt::UserRole);
emitDataChanged();
}
QStandardItem::setData(value, role);
......
......@@ -33,7 +33,7 @@ public:
/**
* Our defined roles
*/
enum Role { TypeRole = Qt::UserRole + 42 };
enum Role { TypeRole = Qt::UserRole + 42, ProjectRole };
/**
* construct new item with given text
......
......@@ -140,6 +140,18 @@ void KateProjectTreeViewContextMenu::exec(const QString &filename, const QModelI
dlg->setAttribute(Qt::WA_DeleteOnClose);
dlg->show();
} else if (rename && action == rename) {
/**
* hack:
* We store a reference to project in the item so that
* after rename we can update file2Item map properly.
*/
KateProjectItem *item = parent->project()->itemForFile(index.data(Qt::UserRole).toString());
if (!item) {
return;
}
item->setData(QVariant::fromValue(parent->project()), KateProjectItem::ProjectRole);
/** start the edit */
parent->edit(index);
} else if (action == fileHistory) {
showFileHistory(index.data(Qt::UserRole).toString());
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment