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

project-plugin: Allow renaming files

parent de7bbd68
......@@ -12,10 +12,12 @@
#include <QFile>
#include <QFileInfo>
#include <QIcon>
#include <QMessageBox>
#include <QMimeDatabase>
#include <QThread>
#include <KIconUtils>
#include <KLocalizedString>
#include <KTextEditor/Document>
......@@ -131,3 +133,28 @@ QIcon *KateProjectItem::icon() const
return m_icon;
}
void KateProjectItem::setData(const QVariant &value, int role)
{
if (role == Qt::EditRole) {
auto newFileName = value.toString();
if (newFileName.isEmpty())
return;
auto oldFileName = data(Qt::DisplayRole).toString();
auto oldName = data(Qt::UserRole).toString();
QString newName = oldName;
newName.replace(oldFileName, newFileName);
if (!QFile::rename(oldName, newName)) {
QMessageBox::critical(nullptr, i18n("Error"), i18n("File name already exists"));
return;
}
// change internal path
setData(oldName, Qt::UserRole);
setData(oldName, Qt::ToolTipRole);
}
QStandardItem::setData(value, role);
}
......@@ -53,6 +53,7 @@ public:
* @return data for role
*/
QVariant data(int role = Qt::UserRole + 1) const override;
void setData(const QVariant &value, int role) override;
/**
* We want case-insensitive sorting and directories first!
......
......@@ -28,6 +28,8 @@
#include <QProcess>
#include <QStandardPaths>
#include "kateprojectviewtree.h"
KateProjectTreeViewContextMenu::KateProjectTreeViewContextMenu()
{
}
......@@ -61,7 +63,7 @@ static bool isGit(const QString &filename)
return isGit;
}
void KateProjectTreeViewContextMenu::exec(const QString &filename, const QPoint &pos, QWidget *parent)
void KateProjectTreeViewContextMenu::exec(const QString &filename, const QModelIndex &index, const QPoint &pos, QWidget *parent)
{
/**
* Create context menu
......@@ -124,6 +126,8 @@ void KateProjectTreeViewContextMenu::exec(const QString &filename, const QPoint
job->start();
};
auto rename = menu.addAction(QIcon::fromTheme(QStringLiteral("edit-rename")), i18n("&Rename"));
/**
* run menu and handle the triggered action
*/
......@@ -141,6 +145,8 @@ void KateProjectTreeViewContextMenu::exec(const QString &filename, const QPoint
QDialog *dlg = new KPropertiesDialog(fileItem);
dlg->setAttribute(Qt::WA_DeleteOnClose);
dlg->show();
} else if (action == rename) {
static_cast<KateProjectViewTree *>(parent)->edit(index);
} else {
// One of the git actions was triggered
}
......
......@@ -12,6 +12,7 @@
#include <QString>
class QWidget;
class QModelIndex;
class KateProjectTreeViewContextMenu
{
......@@ -32,7 +33,7 @@ public:
* our project.
* @return project
*/
void exec(const QString &filename, const QPoint &pos, QWidget *parent);
void exec(const QString &filename, const QModelIndex &index, const QPoint &pos, QWidget *parent);
protected:
};
......
......@@ -159,7 +159,7 @@ void KateProjectViewTree::contextMenuEvent(QContextMenuEvent *event)
}
KateProjectTreeViewContextMenu menu;
menu.exec(filePath, viewport()->mapToGlobal(event->pos()), this);
menu.exec(filePath, index, viewport()->mapToGlobal(event->pos()), this);
event->accept();
}
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