Commit 1448b3c3 authored by Krzysztof Stokop's avatar Krzysztof Stokop
Browse files

Cleaning kateprojecttreeviewcontextmenu.cpp and project menu "delete...

Cleaning kateprojecttreeviewcontextmenu.cpp and project menu "delete (file/directory)" introduction (not finished)
parent e93a1d28
......@@ -18,16 +18,24 @@
#include <KNS3/KMoreToolsMenuFactory>
#include <KPropertiesDialog>
#include <QAction>
#include <QApplication>
#include <QClipboard>
#include <QDir>
#include <QFileInfo>
#include <QIcon>
#include <QInputDialog>
#include <QMenu>
#include <QMessageBox>
#include <QMimeDatabase>
#include <QMimeType>
#include <QStandardPaths>
#include <KToolInvocation>
#include <ktexteditor/editor.h>
#include <ktexteditor/application.h>
static QString getName()
{
......@@ -63,6 +71,9 @@ void KateProjectTreeViewContextMenu::exec(const QString &filename, const QModelI
*/
QAction *copyAction = menu.addAction(QIcon::fromTheme(QStringLiteral("edit-copy")), i18n("Copy File Path"));
/**
* Open terminal here
*/
QAction* terminal = menu.addAction(QIcon::fromTheme(QStringLiteral("utilities-terminal")), i18n("Open Terminal here"));
/**
......@@ -87,6 +98,11 @@ void KateProjectTreeViewContextMenu::exec(const QString &filename, const QModelI
* Open Containing folder
*/
auto openContaingFolderAction = menu.addAction(QIcon::fromTheme(QStringLiteral("document-open-folder")), i18n("&Open Containing Folder"));
/**
* File delete dialog
*/
QAction* fileDelete = menu.addAction(QIcon::fromTheme(QStringLiteral("delete")), i18n("Delete"));
/**
* File Properties Dialog
......@@ -119,7 +135,7 @@ void KateProjectTreeViewContextMenu::exec(const QString &filename, const QModelI
job->start();
};
auto terminalLambda = [](const QString &filename)
auto handleOpenTerminalHere = [](const QString &filename)
{
QFileInfo checkFile(filename);
if (QUrl::fromLocalFile(filename).isLocalFile() && checkFile.isFile())
......@@ -127,6 +143,48 @@ void KateProjectTreeViewContextMenu::exec(const QString &filename, const QModelI
if (QUrl::fromLocalFile(filename).isLocalFile() && checkFile.isDir())
KToolInvocation::invokeTerminal(QString(), {}, filename);
};
auto handleDeleteFile = [parent](const QString &path)
{
QFileInfo fileInfo(path);
//message box
const QString title = i18n("Confirm deleting: %1", path);
const QString text = i18n("Do you want to delete: %1 ?", path);
if (QMessageBox::Yes == QMessageBox::question(parent, title, text, QMessageBox::No | QMessageBox::Yes, QMessageBox::Yes))
{
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()
}
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;
}
}
}
}
//to do - >refresh project tree
};
// we can ATM only handle file renames
QAction *rename = nullptr;
......@@ -137,51 +195,77 @@ void KateProjectTreeViewContextMenu::exec(const QString &filename, const QModelI
/**
* run menu and handle the triggered action
*/
if (QAction *const action = menu.exec(pos)) {
if (action == copyAction) {
if (QAction *const action = menu.exec(pos))
{
if (action == copyAction)
{
QApplication::clipboard()->setText(filename);
} else if (action->parentWidget() == openWithMenu) {
}
else if (action == terminal)
{
// handle "open terminal here"
handleOpenTerminalHere(filename);
}
else if (action->parentWidget() == openWithMenu)
{
// handle "open with"
handleOpenWith(action, filename);
}
else if (action == terminal) {
// handle "open terminal here"
terminalLambda(filename);
}else if (action == openContaingFolderAction) {
else if (action == openContaingFolderAction)
{
KIO::highlightInFileManager({QUrl::fromLocalFile(filename)});
} else if (action == filePropertiesAction) {
}
else if (action == fileDelete)
{
handleDeleteFile(filename);
}
else if (action == filePropertiesAction)
{
// code copied and adapted from frameworks/kio/src/filewidgets/knewfilemenu.cpp
KFileItem fileItem(QUrl::fromLocalFile(filename));
QDialog *dlg = new KPropertiesDialog(fileItem);
dlg->setAttribute(Qt::WA_DeleteOnClose);
dlg->show();
} else if (rename && action == rename) {
}
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) {
if (!item)
{
return;
}
item->setData(QVariant::fromValue(parent->project()), KateProjectItem::ProjectRole);
/** start the edit */
parent->edit(index);
} else if (action == fileHistory) {
}
else if (action == fileHistory)
{
showFileHistory(index.data(Qt::UserRole).toString());
} else if (addFile && action == addFile) {
}
else if (addFile && action == addFile)
{
QString name = getName();
if (!name.isEmpty()) {
if (!name.isEmpty())
{
parent->addFile(index, name);
}
} else if (addFolder && action == addFolder) {
}
else if (addFolder && action == addFolder)
{
QString name = getName();
if (!name.isEmpty()) {
if (!name.isEmpty())
{
parent->addDirectory(index, name);
}
} else {
}
else
{
// One of the git actions was triggered
}
}
......
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