Commit 78586aca authored by Krzysztof Stokop's avatar Krzysztof Stokop
Browse files

project context menu "delete" option finished, added:

KateProjectViewTree::removeFile() ,
KateProjectViewTree::removeDirectory(),
KateProject::removeFile()
parent 1448b3c3
......@@ -84,6 +84,16 @@ void KateProject::renameFile(const QString &newName, const QString &oldName)
m_file2Item->erase(it);
}
void KateProject::removeFile(const QString &file)
{
auto it = m_file2Item->find(file);
if (it == m_file2Item->end()) {
qWarning() << "removeFile() File not found: " << file;
return;
}
m_file2Item->erase(it);
}
/**
* Read a JSON document from file.
*
......
......@@ -156,6 +156,11 @@ public:
* rename a file
*/
void renameFile(const QString &newName, const QString &oldName);
/**
* remove a file
*/
void removeFile(const QString &file);
/**
* Access to project index.
......
......@@ -144,7 +144,7 @@ void KateProjectTreeViewContextMenu::exec(const QString &filename, const QModelI
KToolInvocation::invokeTerminal(QString(), {}, filename);
};
auto handleDeleteFile = [parent](const QString &path)
auto handleDeleteFile = [parent, index](const QString &path)
{
QFileInfo fileInfo(path);
......@@ -156,34 +156,29 @@ void KateProjectTreeViewContextMenu::exec(const QString &filename, const QModelI
{
if(fileInfo.isDir()) //for dir
{
QDir dir(path);
//has opend files ?
for (auto doc : KTextEditor::Editor::instance()->application()->documents())
if(QUrl(path).isParentOf(doc->url().adjusted(QUrl::RemoveScheme)))
KTextEditor::Editor::instance()->application()->closeDocument(doc);
dir.removeRecursively(); //.moveToTrash()
KTextEditor::Editor::instance()->application()->closeDocument(doc); //KTextEditor::Editor::instance()->application()->documentWillBeDeleted(doc);
parent->removeDirectory(index, path);
}
else //for file
{
QFile file(path);
//if is open, close
for(auto doc : KTextEditor::Editor::instance()->application()->documents())
{
if(doc->url().adjusted(QUrl::RemoveScheme) == QUrl(path).adjusted(QUrl::RemoveScheme))
{
KTextEditor::Editor::instance()->application()->closeDocument(doc);
// KTextEditor::Editor::instance()->application()->documentWillBeDeleted(doc);
file.remove();
// KTextEditor::Editor::instance()->application()->documentDeleted(doc); ???
break;
}
}
parent->removeFile(index, path);
}
}
//to do - >refresh project tree
};
// we can ATM only handle file renames
......
......@@ -163,6 +163,56 @@ void KateProjectViewTree::addDirectory(const QModelIndex &idx, const QString &na
item->sortChildren(0);
}
void KateProjectViewTree::removeFile(const QModelIndex& idx, const QString& fullFilePath)
{
auto proxyModel = static_cast<QSortFilterProxyModel *>(model());
auto index = proxyModel->mapToSource(idx);
auto item = m_project->model()->itemFromIndex(index);
QStandardItem* parent = item->parent();
/**
* Delete file
*/
QFile file(fullFilePath);
file.remove(); //.moveToTrash()
if(parent != nullptr)
{
parent->removeRow(item->row());
parent->sortChildren(0);
}
else
{
m_project->model()->removeRow(item->row());
m_project->model()->sort(0);
}
m_project->removeFile(fullFilePath);
}
void KateProjectViewTree::removeDirectory(const QModelIndex& idx, const QString& fullDirPath)
{
auto proxyModel = static_cast<QSortFilterProxyModel *>(model());
auto index = proxyModel->mapToSource(idx);
auto item = m_project->model()->itemFromIndex(index);
QStandardItem* parent = item->parent();
QDir dir(fullDirPath);
dir.removeRecursively(); //.moveToTrash()
if(parent != nullptr)
{
parent->removeRow(item->row());
parent->sortChildren(0);
}
else
{
m_project->model()->removeRow(item->row());
m_project->model()->sort(0);
}
}
void KateProjectViewTree::slotClicked(const QModelIndex &index)
{
/**
......
......@@ -63,6 +63,16 @@ public:
* Add a new directory
*/
void addDirectory(const QModelIndex &idx, const QString &name);
/**
* remove a file, the function isn't closing document before removing'
*/
void removeFile(const QModelIndex &idx, const QString &fullFilePath);
/**
* remove a directory, the function isn't closing documents before removing
*/
void removeDirectory(const QModelIndex &idx, const QString &fullDirPath);
private Q_SLOTS:
/**
......
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