From 882066cd98562f29f97da096b1a21f0eb32f9611 Mon Sep 17 00:00:00 2001 From: Sim Date: Tue, 7 May 2024 19:23:31 +0300 Subject: [PATCH 1/9] Workspace operations: Rename --- .../docks/FolderAsWorkspaceDock.cpp | 40 ++++++++++++ src/NotepadNext/docks/FolderAsWorkspaceDock.h | 10 +++ .../docks/FolderAsWorkspaceDock.ui | 64 ++++++++++++++++++- 3 files changed, 113 insertions(+), 1 deletion(-) diff --git a/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp b/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp index cb8ff936e..78c7572c1 100644 --- a/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp +++ b/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp @@ -32,6 +32,7 @@ FolderAsWorkspaceDock::FolderAsWorkspaceDock(QWidget *parent) : { ui->setupUi(this); + model->setReadOnly(false); ui->treeView->setModel(model); ui->treeView->header()->hideSection(1); ui->treeView->header()->hideSection(2); @@ -42,6 +43,7 @@ FolderAsWorkspaceDock::FolderAsWorkspaceDock(QWidget *parent) : emit fileDoubleClicked(model->filePath(index)); } }); + connect(ui->treeView, &QTreeView::customContextMenuRequested, this, &FolderAsWorkspaceDock::onCustomContextMenu); ApplicationSettings settings; setRootPath(settings.get(rootPathSetting)); @@ -65,3 +67,41 @@ QString FolderAsWorkspaceDock::rootPath() const { return model->rootPath(); } + +void FolderAsWorkspaceDock::onCustomContextMenu(const QPoint &point) +{ + QModelIndex index = ui->treeView->indexAt(point); + if (!index.isValid()) { + lastSelectedItem = model->index(0, 0); + ui->menuEmpty->exec(ui->treeView->viewport()->mapToGlobal(point)); + return; + } + lastSelectedItem = index; + if (model->isDir(index)) { + ui->menuDirectory->exec(ui->treeView->viewport()->mapToGlobal(point)); + } + else { + ui->menuFile->exec(ui->treeView->viewport()->mapToGlobal(point)); + } +} + +void FolderAsWorkspaceDock::on_actionNewFile_triggered() +{ + qInfo(Q_FUNC_INFO); +} + +void FolderAsWorkspaceDock::on_actionNewFolder_triggered() +{ + qInfo(Q_FUNC_INFO); +} + +void FolderAsWorkspaceDock::on_actionRename_triggered() +{ + ui->treeView->setCurrentIndex(lastSelectedItem); + ui->treeView->edit(lastSelectedItem); +} + +void FolderAsWorkspaceDock::on_actionDelete_triggered() +{ + qInfo(Q_FUNC_INFO); +} diff --git a/src/NotepadNext/docks/FolderAsWorkspaceDock.h b/src/NotepadNext/docks/FolderAsWorkspaceDock.h index 085254905..c50fe4721 100644 --- a/src/NotepadNext/docks/FolderAsWorkspaceDock.h +++ b/src/NotepadNext/docks/FolderAsWorkspaceDock.h @@ -21,6 +21,7 @@ #define FOLDERASWORKSPACEDOCK_H #include +#include namespace Ui { class FolderAsWorkspaceDock; @@ -42,10 +43,19 @@ class FolderAsWorkspaceDock : public QDockWidget signals: void fileDoubleClicked(const QString &filePath); +private slots: + void on_actionNewFile_triggered(); + void on_actionNewFolder_triggered(); + void on_actionRename_triggered(); + void on_actionDelete_triggered(); + + void onCustomContextMenu(const QPoint &point); + private: Ui::FolderAsWorkspaceDock *ui; QFileSystemModel *model; + QModelIndex lastSelectedItem; }; #endif // FOLDERASWORKSPACEDOCK_H diff --git a/src/NotepadNext/docks/FolderAsWorkspaceDock.ui b/src/NotepadNext/docks/FolderAsWorkspaceDock.ui index 3515d5620..6f76f2332 100644 --- a/src/NotepadNext/docks/FolderAsWorkspaceDock.ui +++ b/src/NotepadNext/docks/FolderAsWorkspaceDock.ui @@ -27,11 +27,47 @@ 0 + + + + File Menu + + + + + + + + + Folder Menu + + + + + + + + + + + + Space Menu + + + + + + + Qt::CustomContextMenu + QFrame::NoFrame + + QAbstractItemView::EditKeyPressed + false @@ -39,7 +75,33 @@ + + + + :/icons/newfile.png:/icons/newfile.png + + + &New File + + + + + New &Folder + + + + + &Rename + + + + + &Delete + + - + + + From 08a04c94f1a2db8c5ea6438a3d60dac98e9e7c1b Mon Sep 17 00:00:00 2001 From: Sim Date: Tue, 7 May 2024 20:57:49 +0300 Subject: [PATCH 2/9] Workspace operations: Delete, New Folder, Save Here --- .../docks/FolderAsWorkspaceDock.cpp | 72 ++++++++++++++++--- src/NotepadNext/docks/FolderAsWorkspaceDock.h | 7 +- .../docks/FolderAsWorkspaceDock.ui | 12 ++-- 3 files changed, 72 insertions(+), 19 deletions(-) diff --git a/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp b/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp index 78c7572c1..acf00cdbd 100644 --- a/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp +++ b/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp @@ -18,18 +18,24 @@ #include "FolderAsWorkspaceDock.h" + +#include +#include + #include "ApplicationSettings.h" +#include "MainWindow.h" #include "ui_FolderAsWorkspaceDock.h" -#include +QString NEW_DIR_TEMPLATE("dir_%1"); ApplicationSetting rootPathSetting{"FolderAsWorkspace/RootPath"}; -FolderAsWorkspaceDock::FolderAsWorkspaceDock(QWidget *parent) : +FolderAsWorkspaceDock::FolderAsWorkspaceDock(MainWindow *parent) : QDockWidget(parent), - ui(new Ui::FolderAsWorkspaceDock), - model(new QFileSystemModel(this)) + window(parent) { + ui = new Ui::FolderAsWorkspaceDock; + model = new QFileSystemModel(this); ui->setupUi(this); model->setReadOnly(false); @@ -72,7 +78,7 @@ void FolderAsWorkspaceDock::onCustomContextMenu(const QPoint &point) { QModelIndex index = ui->treeView->indexAt(point); if (!index.isValid()) { - lastSelectedItem = model->index(0, 0); + lastSelectedItem = model->index(rootPath()); ui->menuEmpty->exec(ui->treeView->viewport()->mapToGlobal(point)); return; } @@ -85,14 +91,45 @@ void FolderAsWorkspaceDock::onCustomContextMenu(const QPoint &point) } } -void FolderAsWorkspaceDock::on_actionNewFile_triggered() +void FolderAsWorkspaceDock::on_actionSaveHere_triggered() { - qInfo(Q_FUNC_INFO); + QDir parentDir(model->filePath(lastSelectedItem)); + auto doc = window->currentEditor(); + QString dstName(parentDir.absoluteFilePath(doc->getName())); + + if (doc->saveAs(dstName) != QFileDevice::NoError) + { + qWarning("Unable to save %s", dstName.toUtf8().constData()); + } + else + { + auto newItem = model->index(dstName); + if (!doc->isFile()) { + doc->setFileInfo(dstName); + } + ui->treeView->setCurrentIndex(newItem); + ui->treeView->edit(newItem); + } } void FolderAsWorkspaceDock::on_actionNewFolder_triggered() { - qInfo(Q_FUNC_INFO); + QDir parentDir(model->filePath(lastSelectedItem)); + int i = 1; + + for (;parentDir.exists(NEW_DIR_TEMPLATE.arg(i)); i++) { + // Intentional + } + QString dstName = NEW_DIR_TEMPLATE.arg(i); + + auto newItem = model->mkdir(lastSelectedItem, dstName); + if (!newItem.isValid()) { + qWarning("Unable to create %s", dstName.toUtf8().constData()); + } + else { + ui->treeView->setCurrentIndex(newItem); + ui->treeView->edit(newItem); + } } void FolderAsWorkspaceDock::on_actionRename_triggered() @@ -103,5 +140,22 @@ void FolderAsWorkspaceDock::on_actionRename_triggered() void FolderAsWorkspaceDock::on_actionDelete_triggered() { - qInfo(Q_FUNC_INFO); + bool ret; + QString path(model->filePath(lastSelectedItem)); + QMessageBox::StandardButton reply = QMessageBox::question(this, tr("Delete Item"), + tr("Are you sure you want to delete %1?").arg(path)); + + if (reply == QMessageBox::Yes) + { + if (model->isDir(lastSelectedItem)) { + ret = model->rmdir(lastSelectedItem); + } + else { + ret = model->remove(lastSelectedItem); + } + if (!ret) + { + qWarning("Unable to delete %s", path.toUtf8().constData()); + } + } } diff --git a/src/NotepadNext/docks/FolderAsWorkspaceDock.h b/src/NotepadNext/docks/FolderAsWorkspaceDock.h index c50fe4721..2b47a93f3 100644 --- a/src/NotepadNext/docks/FolderAsWorkspaceDock.h +++ b/src/NotepadNext/docks/FolderAsWorkspaceDock.h @@ -28,13 +28,14 @@ class FolderAsWorkspaceDock; } class QFileSystemModel; +class MainWindow; class FolderAsWorkspaceDock : public QDockWidget { Q_OBJECT public: - explicit FolderAsWorkspaceDock(QWidget *parent = nullptr); + explicit FolderAsWorkspaceDock(MainWindow *parent = nullptr); ~FolderAsWorkspaceDock(); void setRootPath(const QString dir); @@ -44,7 +45,7 @@ class FolderAsWorkspaceDock : public QDockWidget void fileDoubleClicked(const QString &filePath); private slots: - void on_actionNewFile_triggered(); + void on_actionSaveHere_triggered(); void on_actionNewFolder_triggered(); void on_actionRename_triggered(); void on_actionDelete_triggered(); @@ -54,7 +55,9 @@ private slots: private: Ui::FolderAsWorkspaceDock *ui; + MainWindow *window; QFileSystemModel *model; + QModelIndex lastSelectedItem; }; diff --git a/src/NotepadNext/docks/FolderAsWorkspaceDock.ui b/src/NotepadNext/docks/FolderAsWorkspaceDock.ui index 6f76f2332..d0fa22728 100644 --- a/src/NotepadNext/docks/FolderAsWorkspaceDock.ui +++ b/src/NotepadNext/docks/FolderAsWorkspaceDock.ui @@ -45,7 +45,7 @@ - + @@ -54,7 +54,7 @@ Space Menu - + @@ -75,13 +75,9 @@ - - - - :/icons/newfile.png:/icons/newfile.png - + - &New File + &Save Here From 45fcfcbfce20803d9f135b1cbb7d2bbcc7f24134 Mon Sep 17 00:00:00 2001 From: Sim Date: Tue, 7 May 2024 22:41:09 +0300 Subject: [PATCH 3/9] Rename editor after renaming a file --- src/NotepadNext/docks/FolderAsWorkspaceDock.cpp | 14 ++++++++++++++ src/NotepadNext/docks/FolderAsWorkspaceDock.h | 1 + 2 files changed, 15 insertions(+) diff --git a/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp b/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp index acf00cdbd..c0af74a50 100644 --- a/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp +++ b/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp @@ -50,6 +50,7 @@ FolderAsWorkspaceDock::FolderAsWorkspaceDock(MainWindow *parent) : } }); connect(ui->treeView, &QTreeView::customContextMenuRequested, this, &FolderAsWorkspaceDock::onCustomContextMenu); + connect(model, &QFileSystemModel::fileRenamed, this, &FolderAsWorkspaceDock::onFileRenamed); ApplicationSettings settings; setRootPath(settings.get(rootPathSetting)); @@ -138,6 +139,19 @@ void FolderAsWorkspaceDock::on_actionRename_triggered() ui->treeView->edit(lastSelectedItem); } +void FolderAsWorkspaceDock::onFileRenamed(const QString &path, const QString &oldName, const QString &newName) +{ + QDir dir(path); + QString fileName = dir.absoluteFilePath(oldName); + for(auto &&editor : window->editors()) + { + if (editor->isFile() && (editor->getFilePath() == fileName)) + { + editor->setName(newName); + } + } +} + void FolderAsWorkspaceDock::on_actionDelete_triggered() { bool ret; diff --git a/src/NotepadNext/docks/FolderAsWorkspaceDock.h b/src/NotepadNext/docks/FolderAsWorkspaceDock.h index 2b47a93f3..3def21d0f 100644 --- a/src/NotepadNext/docks/FolderAsWorkspaceDock.h +++ b/src/NotepadNext/docks/FolderAsWorkspaceDock.h @@ -51,6 +51,7 @@ private slots: void on_actionDelete_triggered(); void onCustomContextMenu(const QPoint &point); + void onFileRenamed(const QString &path, const QString &oldName, const QString &newName); private: Ui::FolderAsWorkspaceDock *ui; From fe945df747ae30c7fd131147d785fc9d5614d46e Mon Sep 17 00:00:00 2001 From: Sim Date: Tue, 7 May 2024 22:46:06 +0300 Subject: [PATCH 4/9] 'Delete' removes folders correctly --- src/NotepadNext/docks/FolderAsWorkspaceDock.cpp | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp b/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp index c0af74a50..fedc9075a 100644 --- a/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp +++ b/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp @@ -154,20 +154,13 @@ void FolderAsWorkspaceDock::onFileRenamed(const QString &path, const QString &ol void FolderAsWorkspaceDock::on_actionDelete_triggered() { - bool ret; QString path(model->filePath(lastSelectedItem)); QMessageBox::StandardButton reply = QMessageBox::question(this, tr("Delete Item"), tr("Are you sure you want to delete %1?").arg(path)); if (reply == QMessageBox::Yes) { - if (model->isDir(lastSelectedItem)) { - ret = model->rmdir(lastSelectedItem); - } - else { - ret = model->remove(lastSelectedItem); - } - if (!ret) + if (!model->remove(lastSelectedItem)) { qWarning("Unable to delete %s", path.toUtf8().constData()); } From 58dbd4ce5e4abee6344fec8e01dbef756cdea032 Mon Sep 17 00:00:00 2001 From: Sim Date: Wed, 8 May 2024 09:29:32 +0300 Subject: [PATCH 5/9] Improvements after review --- src/NotepadNext/ScintillaNext.cpp | 23 ++++--- src/NotepadNext/ScintillaNext.h | 3 +- src/NotepadNext/dialogs/MainWindow.cpp | 37 ++++++++-- src/NotepadNext/dialogs/MainWindow.h | 20 ++++++ .../docks/FolderAsWorkspaceDock.cpp | 68 +++++++++++-------- src/NotepadNext/docks/FolderAsWorkspaceDock.h | 1 + .../docks/FolderAsWorkspaceDock.ui | 7 ++ 7 files changed, 114 insertions(+), 45 deletions(-) diff --git a/src/NotepadNext/ScintillaNext.cpp b/src/NotepadNext/ScintillaNext.cpp index 48058d35a..b2bf46f06 100644 --- a/src/NotepadNext/ScintillaNext.cpp +++ b/src/NotepadNext/ScintillaNext.cpp @@ -369,15 +369,7 @@ bool ScintillaNext::rename(const QString &newFilePath) QFile::remove(oldPath); // Everything worked fine, so update the buffer's info - setFileInfo(newFilePath); - setSavePoint(); - - // If this was a temporary file, make sure it is not any more - setTemporary(false); - - emit saved(); - - emit renamed(); + renameEditorPath(newFilePath); return true; } @@ -385,6 +377,19 @@ bool ScintillaNext::rename(const QString &newFilePath) return false; } +void ScintillaNext::renameEditorPath(const QString &newFilePath) +{ + setFileInfo(newFilePath); + setSavePoint(); + + // If this was a temporary file, make sure it is not any more + setTemporary(false); + + emit saved(); + + emit renamed(); +} + ScintillaNext::FileStateChange ScintillaNext::checkFileForStateChange() { if (bufferType == BufferType::New) { diff --git a/src/NotepadNext/ScintillaNext.h b/src/NotepadNext/ScintillaNext.h index 0eab58078..e53fc4b72 100644 --- a/src/NotepadNext/ScintillaNext.h +++ b/src/NotepadNext/ScintillaNext.h @@ -122,7 +122,8 @@ public slots: void reload(); QFileDevice::FileError saveAs(const QString &newFilePath); QFileDevice::FileError saveCopyAs(const QString &filePath); - bool rename(const QString &newFilePath); + bool rename(const QString &newFilePath); // update FS then update representation + void renameEditorPath(const QString &newFilePath); // update representation only ScintillaNext::FileStateChange checkFileForStateChange(); bool moveToTrash(); diff --git a/src/NotepadNext/dialogs/MainWindow.cpp b/src/NotepadNext/dialogs/MainWindow.cpp index 38c4e7924..8899cab57 100644 --- a/src/NotepadNext/dialogs/MainWindow.cpp +++ b/src/NotepadNext/dialogs/MainWindow.cpp @@ -1332,19 +1332,44 @@ void MainWindow::moveCurrentFileToTrash() moveFileToTrash(editor); } +bool MainWindow::askMoveToTrash(const QString &path) +{ + auto reply = QMessageBox::question(this, tr("Delete File"), tr("Are you sure you want to move %1 to the trash?").arg(path)); + + if (reply == QMessageBox::Yes) { + return true; + } + return false; +} + +bool MainWindow::askDeletePermanent(const QString &path) +{ + auto reply = QMessageBox::question(this, tr("Delete File"), tr("Are you sure you want to delete %1?").arg(path)); + + if (reply == QMessageBox::Yes) { + return true; + } + return false; +} + +void MainWindow::closeByPath(const QString &path) +{ + forEachEditorByPath(path, [=](ScintillaNext* editor) { + closeFile(editor); + }); + // Since the file no longer exists, specifically remove it from the recent files list + app->getRecentFilesListManager()->removeFile(path); +} + void MainWindow::moveFileToTrash(ScintillaNext *editor) { Q_ASSERT(editor->isFile()); const QString filePath = editor->getFilePath(); - auto reply = QMessageBox::question(this, tr("Delete File"), tr("Are you sure you want to move %1 to the trash?").arg(filePath)); - if (reply == QMessageBox::Yes) { + if (askMoveToTrash(filePath)) { if (editor->moveToTrash()) { - closeCurrentFile(); - - // Since the file no longer exists, specifically remove it from the recent files list - app->getRecentFilesListManager()->removeFile(editor->getFilePath()); + closeByPath(filePath); } else { QMessageBox::warning(this, tr("Error Deleting File"), tr("Something went wrong deleting %1?").arg(filePath)); diff --git a/src/NotepadNext/dialogs/MainWindow.h b/src/NotepadNext/dialogs/MainWindow.h index bd73ad6a3..b38fbdc9c 100644 --- a/src/NotepadNext/dialogs/MainWindow.h +++ b/src/NotepadNext/dialogs/MainWindow.h @@ -58,6 +58,14 @@ class MainWindow : public QMainWindow QVector editors() const; DockedEditor *getDockedEditor() const { return dockedEditor; } + template + void forEachEditorByPath(const QString &path, Func callback); + + bool askMoveToTrash(const QString &path); + bool askDeletePermanent(const QString &path); + + void closeByPath(const QString &path); + public slots: void newFile(); @@ -171,4 +179,16 @@ private slots: int contextMenuPos = 0; }; +template +void MainWindow::forEachEditorByPath(const QString &path, Func callback) +{ + for(auto &&editor : editors()) + { + if (editor->getFilePath() == path) + { + callback(editor); + } + } +} + #endif // MAINWINDOW_H diff --git a/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp b/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp index fedc9075a..a6790501c 100644 --- a/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp +++ b/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp @@ -95,22 +95,20 @@ void FolderAsWorkspaceDock::onCustomContextMenu(const QPoint &point) void FolderAsWorkspaceDock::on_actionSaveHere_triggered() { QDir parentDir(model->filePath(lastSelectedItem)); - auto doc = window->currentEditor(); - QString dstName(parentDir.absoluteFilePath(doc->getName())); + auto editor = window->currentEditor(); + QString dstName(parentDir.absoluteFilePath(editor->getName())); - if (doc->saveAs(dstName) != QFileDevice::NoError) - { - qWarning("Unable to save %s", dstName.toUtf8().constData()); - } - else - { + if (editor->saveAs(dstName) == QFileDevice::NoError) { auto newItem = model->index(dstName); - if (!doc->isFile()) { - doc->setFileInfo(dstName); + if (!editor->isFile()) { + editor->setFileInfo(dstName); } ui->treeView->setCurrentIndex(newItem); ui->treeView->edit(newItem); } + else { + qWarning("Unable to save %s", dstName.toUtf8().constData()); + } } void FolderAsWorkspaceDock::on_actionNewFolder_triggered() @@ -124,13 +122,13 @@ void FolderAsWorkspaceDock::on_actionNewFolder_triggered() QString dstName = NEW_DIR_TEMPLATE.arg(i); auto newItem = model->mkdir(lastSelectedItem, dstName); - if (!newItem.isValid()) { - qWarning("Unable to create %s", dstName.toUtf8().constData()); - } - else { + if (newItem.isValid()) { ui->treeView->setCurrentIndex(newItem); ui->treeView->edit(newItem); } + else { + qWarning("Unable to create %s", dstName.toUtf8().constData()); + } } void FolderAsWorkspaceDock::on_actionRename_triggered() @@ -139,30 +137,42 @@ void FolderAsWorkspaceDock::on_actionRename_triggered() ui->treeView->edit(lastSelectedItem); } -void FolderAsWorkspaceDock::onFileRenamed(const QString &path, const QString &oldName, const QString &newName) +void FolderAsWorkspaceDock::onFileRenamed(const QString &parentPath, const QString &oldName, const QString &newName) { - QDir dir(path); - QString fileName = dir.absoluteFilePath(oldName); - for(auto &&editor : window->editors()) - { - if (editor->isFile() && (editor->getFilePath() == fileName)) - { - editor->setName(newName); - } - } + QDir parentDir(parentPath); + QString oldPath = parentDir.absoluteFilePath(oldName); + QString newPath = parentDir.absoluteFilePath(newName); + + window->forEachEditorByPath(oldPath, [=](ScintillaNext* editor) { + editor->renameEditorPath(newPath); + }); } void FolderAsWorkspaceDock::on_actionDelete_triggered() { QString path(model->filePath(lastSelectedItem)); - QMessageBox::StandardButton reply = QMessageBox::question(this, tr("Delete Item"), - tr("Are you sure you want to delete %1?").arg(path)); - if (reply == QMessageBox::Yes) + if (window->askDeletePermanent(path)) { - if (!model->remove(lastSelectedItem)) - { + if (model->remove(lastSelectedItem)) { + window->closeByPath(path); + } + else { qWarning("Unable to delete %s", path.toUtf8().constData()); } } } + +void FolderAsWorkspaceDock::on_actionMoveToTrash_triggered() +{ + QString path(model->filePath(lastSelectedItem)); + + if (window->askMoveToTrash(path)) { + if (QFile::moveToTrash(path)) { + window->closeByPath(path); + } + else { + qWarning("Unable to remove %s", path.toUtf8().constData()); + } + } +} diff --git a/src/NotepadNext/docks/FolderAsWorkspaceDock.h b/src/NotepadNext/docks/FolderAsWorkspaceDock.h index 3def21d0f..684950f00 100644 --- a/src/NotepadNext/docks/FolderAsWorkspaceDock.h +++ b/src/NotepadNext/docks/FolderAsWorkspaceDock.h @@ -49,6 +49,7 @@ private slots: void on_actionNewFolder_triggered(); void on_actionRename_triggered(); void on_actionDelete_triggered(); + void on_actionMoveToTrash_triggered(); void onCustomContextMenu(const QPoint &point); void onFileRenamed(const QString &path, const QString &oldName, const QString &newName); diff --git a/src/NotepadNext/docks/FolderAsWorkspaceDock.ui b/src/NotepadNext/docks/FolderAsWorkspaceDock.ui index d0fa22728..148aa288a 100644 --- a/src/NotepadNext/docks/FolderAsWorkspaceDock.ui +++ b/src/NotepadNext/docks/FolderAsWorkspaceDock.ui @@ -33,6 +33,7 @@ File Menu + @@ -42,6 +43,7 @@ Folder Menu + @@ -95,6 +97,11 @@ &Delete + + + Move to &Trash + + From 135b88044661b2a5439d86afc993dfc26837f223 Mon Sep 17 00:00:00 2001 From: Sim Date: Wed, 8 May 2024 21:09:18 +0300 Subject: [PATCH 6/9] `newDirTemplate` as FolderAsWorkspaceDock.cpp --- src/NotepadNext/docks/FolderAsWorkspaceDock.cpp | 6 +++--- src/NotepadNext/docks/FolderAsWorkspaceDock.h | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp b/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp index a6790501c..91ba770e1 100644 --- a/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp +++ b/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp @@ -26,7 +26,6 @@ #include "MainWindow.h" #include "ui_FolderAsWorkspaceDock.h" -QString NEW_DIR_TEMPLATE("dir_%1"); ApplicationSetting rootPathSetting{"FolderAsWorkspace/RootPath"}; @@ -34,6 +33,7 @@ FolderAsWorkspaceDock::FolderAsWorkspaceDock(MainWindow *parent) : QDockWidget(parent), window(parent) { + newDirTemplate = tr("dir_%1"); ui = new Ui::FolderAsWorkspaceDock; model = new QFileSystemModel(this); ui->setupUi(this); @@ -116,10 +116,10 @@ void FolderAsWorkspaceDock::on_actionNewFolder_triggered() QDir parentDir(model->filePath(lastSelectedItem)); int i = 1; - for (;parentDir.exists(NEW_DIR_TEMPLATE.arg(i)); i++) { + for (;parentDir.exists(newDirTemplate.arg(i)); i++) { // Intentional } - QString dstName = NEW_DIR_TEMPLATE.arg(i); + QString dstName = newDirTemplate.arg(i); auto newItem = model->mkdir(lastSelectedItem, dstName); if (newItem.isValid()) { diff --git a/src/NotepadNext/docks/FolderAsWorkspaceDock.h b/src/NotepadNext/docks/FolderAsWorkspaceDock.h index 684950f00..ade5e71ef 100644 --- a/src/NotepadNext/docks/FolderAsWorkspaceDock.h +++ b/src/NotepadNext/docks/FolderAsWorkspaceDock.h @@ -61,6 +61,8 @@ private slots: QFileSystemModel *model; QModelIndex lastSelectedItem; + + QString newDirTemplate; }; #endif // FOLDERASWORKSPACEDOCK_H From 799ad736bb02add6251cb5ddb7257e47e83e401b Mon Sep 17 00:00:00 2001 From: dail8859 Date: Wed, 8 May 2024 22:46:37 -0400 Subject: [PATCH 7/9] Cleanup round 1 --- src/NotepadNext/dialogs/MainWindow.cpp | 20 ++---- src/NotepadNext/dialogs/MainWindow.h | 3 +- .../docks/FolderAsWorkspaceDock.cpp | 61 ++++++++----------- src/NotepadNext/docks/FolderAsWorkspaceDock.h | 3 - .../docks/FolderAsWorkspaceDock.ui | 13 ++-- 5 files changed, 38 insertions(+), 62 deletions(-) diff --git a/src/NotepadNext/dialogs/MainWindow.cpp b/src/NotepadNext/dialogs/MainWindow.cpp index 8899cab57..bec24178c 100644 --- a/src/NotepadNext/dialogs/MainWindow.cpp +++ b/src/NotepadNext/dialogs/MainWindow.cpp @@ -1336,20 +1336,7 @@ bool MainWindow::askMoveToTrash(const QString &path) { auto reply = QMessageBox::question(this, tr("Delete File"), tr("Are you sure you want to move %1 to the trash?").arg(path)); - if (reply == QMessageBox::Yes) { - return true; - } - return false; -} - -bool MainWindow::askDeletePermanent(const QString &path) -{ - auto reply = QMessageBox::question(this, tr("Delete File"), tr("Are you sure you want to delete %1?").arg(path)); - - if (reply == QMessageBox::Yes) { - return true; - } - return false; + return reply == QMessageBox::Yes; } void MainWindow::closeByPath(const QString &path) @@ -1369,7 +1356,10 @@ void MainWindow::moveFileToTrash(ScintillaNext *editor) if (askMoveToTrash(filePath)) { if (editor->moveToTrash()) { - closeByPath(filePath); + closeCurrentFile(); + + // Since the file no longer exists, specifically remove it from the recent files list + app->getRecentFilesListManager()->removeFile(editor->getFilePath()); } else { QMessageBox::warning(this, tr("Error Deleting File"), tr("Something went wrong deleting %1?").arg(filePath)); diff --git a/src/NotepadNext/dialogs/MainWindow.h b/src/NotepadNext/dialogs/MainWindow.h index b38fbdc9c..72d4862e2 100644 --- a/src/NotepadNext/dialogs/MainWindow.h +++ b/src/NotepadNext/dialogs/MainWindow.h @@ -62,7 +62,6 @@ class MainWindow : public QMainWindow void forEachEditorByPath(const QString &path, Func callback); bool askMoveToTrash(const QString &path); - bool askDeletePermanent(const QString &path); void closeByPath(const QString &path); @@ -182,8 +181,10 @@ private slots: template void MainWindow::forEachEditorByPath(const QString &path, Func callback) { + qInfo() << path; for(auto &&editor : editors()) { + qInfo() << editor->getFilePath(); if (editor->getFilePath() == path) { callback(editor); diff --git a/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp b/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp index 91ba770e1..17f60bbf3 100644 --- a/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp +++ b/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp @@ -31,11 +31,10 @@ ApplicationSetting rootPathSetting{"FolderAsWorkspace/RootPath"}; FolderAsWorkspaceDock::FolderAsWorkspaceDock(MainWindow *parent) : QDockWidget(parent), - window(parent) + ui(new Ui::FolderAsWorkspaceDock), + window(parent), + model(new QFileSystemModel(this)) { - newDirTemplate = tr("dir_%1"); - ui = new Ui::FolderAsWorkspaceDock; - model = new QFileSystemModel(this); ui->setupUi(this); model->setReadOnly(false); @@ -49,6 +48,7 @@ FolderAsWorkspaceDock::FolderAsWorkspaceDock(MainWindow *parent) : emit fileDoubleClicked(model->filePath(index)); } }); + connect(ui->treeView, &QTreeView::customContextMenuRequested, this, &FolderAsWorkspaceDock::onCustomContextMenu); connect(model, &QFileSystemModel::fileRenamed, this, &FolderAsWorkspaceDock::onFileRenamed); @@ -78,12 +78,17 @@ QString FolderAsWorkspaceDock::rootPath() const void FolderAsWorkspaceDock::onCustomContextMenu(const QPoint &point) { QModelIndex index = ui->treeView->indexAt(point); + + // Nothing was selected if (!index.isValid()) { lastSelectedItem = model->index(rootPath()); ui->menuEmpty->exec(ui->treeView->viewport()->mapToGlobal(point)); return; } + lastSelectedItem = index; + + // Decide which menu to show if it is a directory or not if (model->isDir(index)) { ui->menuDirectory->exec(ui->treeView->viewport()->mapToGlobal(point)); } @@ -96,38 +101,39 @@ void FolderAsWorkspaceDock::on_actionSaveHere_triggered() { QDir parentDir(model->filePath(lastSelectedItem)); auto editor = window->currentEditor(); - QString dstName(parentDir.absoluteFilePath(editor->getName())); + QString destinationName(parentDir.absoluteFilePath(editor->getName())); - if (editor->saveAs(dstName) == QFileDevice::NoError) { - auto newItem = model->index(dstName); - if (!editor->isFile()) { - editor->setFileInfo(dstName); - } + if (window->saveFileAs(editor, destinationName)) { + auto newItem = model->index(destinationName); ui->treeView->setCurrentIndex(newItem); ui->treeView->edit(newItem); } else { - qWarning("Unable to save %s", dstName.toUtf8().constData()); + qWarning("Unable to save %s", qUtf8Printable(destinationName)); } } void FolderAsWorkspaceDock::on_actionNewFolder_triggered() { - QDir parentDir(model->filePath(lastSelectedItem)); + QDir parentDirectory(model->filePath(lastSelectedItem)); + QString destinationName; int i = 1; - for (;parentDir.exists(newDirTemplate.arg(i)); i++) { - // Intentional - } - QString dstName = newDirTemplate.arg(i); + // Find the next valid folder name + do { + destinationName = tr("New Folder %1").arg(i); + ++i; + } while (parentDirectory.exists(destinationName)); + + // Try to create it + auto newItem = model->mkdir(lastSelectedItem, destinationName); - auto newItem = model->mkdir(lastSelectedItem, dstName); if (newItem.isValid()) { ui->treeView->setCurrentIndex(newItem); ui->treeView->edit(newItem); } else { - qWarning("Unable to create %s", dstName.toUtf8().constData()); + qWarning("Unable to create %s", qUtf8Printable(destinationName)); } } @@ -144,23 +150,8 @@ void FolderAsWorkspaceDock::onFileRenamed(const QString &parentPath, const QStri QString newPath = parentDir.absoluteFilePath(newName); window->forEachEditorByPath(oldPath, [=](ScintillaNext* editor) { - editor->renameEditorPath(newPath); - }); -} - -void FolderAsWorkspaceDock::on_actionDelete_triggered() -{ - QString path(model->filePath(lastSelectedItem)); - - if (window->askDeletePermanent(path)) - { - if (model->remove(lastSelectedItem)) { - window->closeByPath(path); - } - else { - qWarning("Unable to delete %s", path.toUtf8().constData()); - } - } + editor->renameEditorPath(newPath); + }); } void FolderAsWorkspaceDock::on_actionMoveToTrash_triggered() diff --git a/src/NotepadNext/docks/FolderAsWorkspaceDock.h b/src/NotepadNext/docks/FolderAsWorkspaceDock.h index ade5e71ef..e7f9a4d1f 100644 --- a/src/NotepadNext/docks/FolderAsWorkspaceDock.h +++ b/src/NotepadNext/docks/FolderAsWorkspaceDock.h @@ -48,7 +48,6 @@ private slots: void on_actionSaveHere_triggered(); void on_actionNewFolder_triggered(); void on_actionRename_triggered(); - void on_actionDelete_triggered(); void on_actionMoveToTrash_triggered(); void onCustomContextMenu(const QPoint &point); @@ -61,8 +60,6 @@ private slots: QFileSystemModel *model; QModelIndex lastSelectedItem; - - QString newDirTemplate; }; #endif // FOLDERASWORKSPACEDOCK_H diff --git a/src/NotepadNext/docks/FolderAsWorkspaceDock.ui b/src/NotepadNext/docks/FolderAsWorkspaceDock.ui index 148aa288a..d74c6de06 100644 --- a/src/NotepadNext/docks/FolderAsWorkspaceDock.ui +++ b/src/NotepadNext/docks/FolderAsWorkspaceDock.ui @@ -34,7 +34,6 @@ - @@ -44,7 +43,6 @@ - @@ -79,7 +77,7 @@ - &Save Here + &Save Current File Here @@ -92,12 +90,11 @@ &Rename - - - &Delete - - + + + :/icons/bin_closed.png:/icons/bin_closed.png + Move to &Trash From c936ab432c4f581219e91a1b84a7740d13f1d8e7 Mon Sep 17 00:00:00 2001 From: Sim Date: Fri, 10 May 2024 22:22:27 +0300 Subject: [PATCH 8/9] Rename at Folder as Workspace View should update all opened Editors + getPath dropped from ScintillaNext + getFilePath now uses Qt directory separators --- src/NotepadNext/ScintillaNext.cpp | 15 ++++--- src/NotepadNext/ScintillaNext.h | 1 - src/NotepadNext/SessionManager.cpp | 6 +-- src/NotepadNext/dialogs/MainWindow.cpp | 41 ++++++++++++++++--- src/NotepadNext/dialogs/MainWindow.h | 6 ++- .../docks/FolderAsWorkspaceDock.cpp | 13 +----- src/NotepadNext/docks/FolderAsWorkspaceDock.h | 1 - 7 files changed, 51 insertions(+), 32 deletions(-) diff --git a/src/NotepadNext/ScintillaNext.cpp b/src/NotepadNext/ScintillaNext.cpp index b2bf46f06..d905d4375 100644 --- a/src/NotepadNext/ScintillaNext.cpp +++ b/src/NotepadNext/ScintillaNext.cpp @@ -23,6 +23,7 @@ #include "uchardet.h" #include +#include #include #include #include @@ -92,6 +93,7 @@ ScintillaNext *ScintillaNext::fromFile(const QString &filePath, bool tryToCreate return Q_NULLPTR; } + qInfo() << Q_FUNC_INFO << filePath; editor->setFileInfo(filePath); return editor; @@ -231,18 +233,14 @@ QFileInfo ScintillaNext::getFileInfo() const return fileInfo; } -QString ScintillaNext::getPath() const -{ - Q_ASSERT(isFile()); - - return QDir::toNativeSeparators(fileInfo.canonicalPath()); -} - QString ScintillaNext::getFilePath() const { Q_ASSERT(isFile()); - return QDir::toNativeSeparators(fileInfo.canonicalFilePath()); + // All files should be absolute for now + // There is an unexpected behaviour because QFileInfo is refresh info from a disk sometimes + // So canonicalFilePath would return empty string if there is no actual file anymore + return fileInfo.filePath(); } void ScintillaNext::setFoldMarkers(const QString &type) @@ -616,6 +614,7 @@ void ScintillaNext::setFileInfo(const QString &filePath) bufferType = ScintillaNext::File; updateTimestamp(); + emit renamed(); } void ScintillaNext::detachFileInfo(const QString &newName) diff --git a/src/NotepadNext/ScintillaNext.h b/src/NotepadNext/ScintillaNext.h index e53fc4b72..35f9b0399 100644 --- a/src/NotepadNext/ScintillaNext.h +++ b/src/NotepadNext/ScintillaNext.h @@ -84,7 +84,6 @@ class ScintillaNext : public ScintillaEdit QString getName() const { return name; } void setName(const QString &name); - QString getPath() const; QString getFilePath() const; // NOTE: this is dangerous and should only be used in very rare situations diff --git a/src/NotepadNext/SessionManager.cpp b/src/NotepadNext/SessionManager.cpp index 9249fcb27..b561cd879 100644 --- a/src/NotepadNext/SessionManager.cpp +++ b/src/NotepadNext/SessionManager.cpp @@ -245,7 +245,7 @@ bool SessionManager::willFileGetStoredInSession(ScintillaNext *editor) const void SessionManager::storeFileDetails(ScintillaNext *editor, QSettings &settings) { settings.setValue("Type", "File"); - settings.setValue("FilePath", editor->getFilePath()); + settings.setValue("FilePath", QDir::toNativeSeparators(editor->getFilePath())); storeEditorViewDetails(editor, settings); } @@ -254,7 +254,7 @@ ScintillaNext* SessionManager::loadFileDetails(QSettings &settings) { qInfo(Q_FUNC_INFO); - const QString filePath = settings.value("FilePath").toString(); + const QString filePath = QDir::fromNativeSeparators(settings.value("FilePath").toString()); qDebug("Session file: \"%s\"", qUtf8Printable(filePath)); @@ -284,7 +284,7 @@ void SessionManager::storeUnsavedFileDetails(ScintillaNext *editor, QSettings &s const QString sessionFileName = RandomSessionFileName(); settings.setValue("Type", "UnsavedFile"); - settings.setValue("FilePath", editor->getFilePath()); + settings.setValue("FilePath", QDir::toNativeSeparators(editor->getFilePath())); settings.setValue("SessionFileName", sessionFileName); storeEditorViewDetails(editor, settings); diff --git a/src/NotepadNext/dialogs/MainWindow.cpp b/src/NotepadNext/dialogs/MainWindow.cpp index bec24178c..ad4c87e94 100644 --- a/src/NotepadNext/dialogs/MainWindow.cpp +++ b/src/NotepadNext/dialogs/MainWindow.cpp @@ -274,7 +274,7 @@ MainWindow::MainWindow(NotepadNextApplication *app) : connect(ui->actionCopyFullPath, &QAction::triggered, this, [=]() { auto editor = currentEditor(); if (editor->isFile()) { - QApplication::clipboard()->setText(editor->getFilePath()); + QApplication::clipboard()->setText(QDir::toNativeSeparators(editor->getFilePath())); } }); connect(ui->actionCopyFileName, &QAction::triggered, this, [=]() { @@ -283,7 +283,8 @@ MainWindow::MainWindow(NotepadNextApplication *app) : connect(ui->actionCopyFileDirectory, &QAction::triggered, this, [=]() { auto editor = currentEditor(); if (editor->isFile()) { - QApplication::clipboard()->setText(editor->getPath()); + QFileInfo currentFile(editor->getFilePath()); + QApplication::clipboard()->setText(QDir::toNativeSeparators(currentFile.dir().absolutePath())); } }); @@ -905,7 +906,7 @@ void MainWindow::openFileList(const QStringList &fileNames) const ScintillaNext *mostRecentEditor = Q_NULLPTR; for (const QString &filePath : fileNames) { - qInfo("%s", qUtf8Printable(filePath)); + qInfo() << Q_FUNC_INFO << filePath; // Search currently open editors to see if it is already open ScintillaNext *editor = app->getEditorManager()->getEditorByFilePath(filePath); @@ -986,7 +987,8 @@ void MainWindow::openFileDialog() // Use the path if possible if (editor->isFile()) { - dialogDir = editor->getPath(); + QFileInfo currentFile(editor->getFilePath()); + dialogDir = currentFile.dir().absolutePath(); } QStringList fileNames = FileDialogHelpers::getOpenFileNames(this, QString(), dialogDir, filter); @@ -1006,7 +1008,8 @@ void MainWindow::openFolderAsWorkspaceDialog() // Use the path if possible if (editor->isFile()) { - dialogDir = editor->getPath(); + QFileInfo currentFile(editor->getFilePath()); + dialogDir = currentFile.dir().absolutePath(); } QString dir = QFileDialog::getExistingDirectory(this, tr("Open Folder as Workspace"), dialogDir, QFileDialog::ShowDirsOnly); @@ -1988,6 +1991,34 @@ void MainWindow::dropEvent(QDropEvent *event) } } +void MainWindow::onNodeRenamed(const QString &parentPath, const QString &oldName, const QString &newName) +{ + qInfo(Q_FUNC_INFO); + + QDir parentDir(parentPath); + QString oldPath = parentDir.absoluteFilePath(oldName); + QString newPath = parentDir.absoluteFilePath(newName); + + QFileInfo fileInfo(newPath); + if (fileInfo.isDir()) { + for(auto &&editor : editors()) { + qInfo() << "" << editor->getFilePath(); + if (editor->isFile() && editor->getFilePath().startsWith(oldPath)) + { + QString newFilePath = newPath + editor->getFilePath().mid(oldPath.length()); + qInfo() << "->" << newFilePath; + editor->setFileInfo(newFilePath); + } + } + } + else { + forEachEditorByPath(oldPath, [=](ScintillaNext* editor) { + qInfo() << Q_FUNC_INFO << editor->getFilePath(); + editor->setFileInfo(newPath); + }); + } +} + void MainWindow::tabBarRightClicked(ScintillaNext *editor) { qInfo(Q_FUNC_INFO); diff --git a/src/NotepadNext/dialogs/MainWindow.h b/src/NotepadNext/dialogs/MainWindow.h index 72d4862e2..fd0280cc4 100644 --- a/src/NotepadNext/dialogs/MainWindow.h +++ b/src/NotepadNext/dialogs/MainWindow.h @@ -132,6 +132,8 @@ public slots: void switchToEditor(const ScintillaNext *editor); + void onNodeRenamed(const QString &parentPath, const QString &oldName, const QString &newName); + signals: void editorActivated(ScintillaNext *editor); void aboutToClose(); @@ -181,10 +183,10 @@ private slots: template void MainWindow::forEachEditorByPath(const QString &path, Func callback) { - qInfo() << path; + qDebug() << Q_FUNC_INFO << path; for(auto &&editor : editors()) { - qInfo() << editor->getFilePath(); + qDebug() << editor->getFilePath(); if (editor->getFilePath() == path) { callback(editor); diff --git a/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp b/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp index 17f60bbf3..e55a98cf0 100644 --- a/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp +++ b/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp @@ -50,7 +50,7 @@ FolderAsWorkspaceDock::FolderAsWorkspaceDock(MainWindow *parent) : }); connect(ui->treeView, &QTreeView::customContextMenuRequested, this, &FolderAsWorkspaceDock::onCustomContextMenu); - connect(model, &QFileSystemModel::fileRenamed, this, &FolderAsWorkspaceDock::onFileRenamed); + connect(model, &QFileSystemModel::fileRenamed, window, &MainWindow::onNodeRenamed); ApplicationSettings settings; setRootPath(settings.get(rootPathSetting)); @@ -143,17 +143,6 @@ void FolderAsWorkspaceDock::on_actionRename_triggered() ui->treeView->edit(lastSelectedItem); } -void FolderAsWorkspaceDock::onFileRenamed(const QString &parentPath, const QString &oldName, const QString &newName) -{ - QDir parentDir(parentPath); - QString oldPath = parentDir.absoluteFilePath(oldName); - QString newPath = parentDir.absoluteFilePath(newName); - - window->forEachEditorByPath(oldPath, [=](ScintillaNext* editor) { - editor->renameEditorPath(newPath); - }); -} - void FolderAsWorkspaceDock::on_actionMoveToTrash_triggered() { QString path(model->filePath(lastSelectedItem)); diff --git a/src/NotepadNext/docks/FolderAsWorkspaceDock.h b/src/NotepadNext/docks/FolderAsWorkspaceDock.h index e7f9a4d1f..a65792ccd 100644 --- a/src/NotepadNext/docks/FolderAsWorkspaceDock.h +++ b/src/NotepadNext/docks/FolderAsWorkspaceDock.h @@ -51,7 +51,6 @@ private slots: void on_actionMoveToTrash_triggered(); void onCustomContextMenu(const QPoint &point); - void onFileRenamed(const QString &path, const QString &oldName, const QString &newName); private: Ui::FolderAsWorkspaceDock *ui; From 5885f4666fb7e52b991815d2f16f4e442febdaee Mon Sep 17 00:00:00 2001 From: Sim Date: Sun, 12 May 2024 10:27:12 +0300 Subject: [PATCH 9/9] Delete at Folder as Workspace View should work now. --- src/NotepadNext/dialogs/MainWindow.cpp | 31 +++++++++++++++---- src/NotepadNext/dialogs/MainWindow.h | 2 +- .../docks/FolderAsWorkspaceDock.cpp | 4 ++- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/NotepadNext/dialogs/MainWindow.cpp b/src/NotepadNext/dialogs/MainWindow.cpp index ad4c87e94..551bd3b07 100644 --- a/src/NotepadNext/dialogs/MainWindow.cpp +++ b/src/NotepadNext/dialogs/MainWindow.cpp @@ -1342,13 +1342,32 @@ bool MainWindow::askMoveToTrash(const QString &path) return reply == QMessageBox::Yes; } -void MainWindow::closeByPath(const QString &path) +void MainWindow::closeByPath(const QString &path, bool isDirectory) { - forEachEditorByPath(path, [=](ScintillaNext* editor) { - closeFile(editor); - }); - // Since the file no longer exists, specifically remove it from the recent files list - app->getRecentFilesListManager()->removeFile(path); + qInfo(Q_FUNC_INFO); + + if (isDirectory) { + for (ScintillaNext *editor: editors()) { + if (editor->isFile() && editor->getFilePath().startsWith(path)) { + qInfo() << "->" << editor->getFilePath(); + + app->getRecentFilesListManager()->removeFile(path); + if (editor->isSavedToDisk()) { + editor->close(); + } + else { + editor->detachFileInfo(editor->getName()); + } + } + } + } + else { + forEachEditorByPath(path, [=](ScintillaNext* editor) { + closeFile(editor); + }); + // Since the file no longer exists, specifically remove it from the recent files list + app->getRecentFilesListManager()->removeFile(path); + } } void MainWindow::moveFileToTrash(ScintillaNext *editor) diff --git a/src/NotepadNext/dialogs/MainWindow.h b/src/NotepadNext/dialogs/MainWindow.h index fd0280cc4..57bc2cedb 100644 --- a/src/NotepadNext/dialogs/MainWindow.h +++ b/src/NotepadNext/dialogs/MainWindow.h @@ -63,7 +63,7 @@ class MainWindow : public QMainWindow bool askMoveToTrash(const QString &path); - void closeByPath(const QString &path); + void closeByPath(const QString &path, bool isDirectory); public slots: void newFile(); diff --git a/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp b/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp index e55a98cf0..c74b4dcfc 100644 --- a/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp +++ b/src/NotepadNext/docks/FolderAsWorkspaceDock.cpp @@ -148,8 +148,10 @@ void FolderAsWorkspaceDock::on_actionMoveToTrash_triggered() QString path(model->filePath(lastSelectedItem)); if (window->askMoveToTrash(path)) { + QFileInfo fileInfo(path); + bool isDirectory = fileInfo.isDir(); if (QFile::moveToTrash(path)) { - window->closeByPath(path); + window->closeByPath(path, isDirectory); } else { qWarning("Unable to remove %s", path.toUtf8().constData());