Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context Menu operations on Folder as Workspace tree #556

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
20 changes: 5 additions & 15 deletions src/NotepadNext/dialogs/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <b>%1</b> 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 <b>delete</b> <b>%1</b>?").arg(path));

if (reply == QMessageBox::Yes) {
return true;
}
return false;
return reply == QMessageBox::Yes;
}

void MainWindow::closeByPath(const QString &path)
Expand All @@ -1369,7 +1356,10 @@ void MainWindow::moveFileToTrash(ScintillaNext *editor)

if (askMoveToTrash(filePath)) {
if (editor->moveToTrash()) {
closeByPath(filePath);
closeCurrentFile();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you closing current file? One could open one file and delete another one.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right! Good catch! 😄 This is what I get for trying to modify it on little sleep.


// 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 <b>%1</b>?").arg(filePath));
Expand Down
3 changes: 2 additions & 1 deletion src/NotepadNext/dialogs/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -182,8 +181,10 @@ private slots:
template<typename Func>
void MainWindow::forEachEditorByPath(const QString &path, Func callback)
{
qInfo() << path;
for(auto &&editor : editors())
{
qInfo() << editor->getFilePath();
if (editor->getFilePath() == path)
{
callback(editor);
Expand Down
61 changes: 26 additions & 35 deletions src/NotepadNext/docks/FolderAsWorkspaceDock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ ApplicationSetting<QString> 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);
Expand All @@ -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);

Expand Down Expand Up @@ -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));
}
Expand All @@ -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));
}
}

Expand All @@ -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()
Expand Down
3 changes: 0 additions & 3 deletions src/NotepadNext/docks/FolderAsWorkspaceDock.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -61,8 +60,6 @@ private slots:
QFileSystemModel *model;

QModelIndex lastSelectedItem;

QString newDirTemplate;
};

#endif // FOLDERASWORKSPACEDOCK_H
13 changes: 5 additions & 8 deletions src/NotepadNext/docks/FolderAsWorkspaceDock.ui
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
</property>
<addaction name="actionRename"/>
<addaction name="actionMoveToTrash"/>
<addaction name="actionDelete"/>
</widget>
</item>
<item>
Expand All @@ -44,7 +43,6 @@
</property>
<addaction name="actionRename"/>
<addaction name="actionMoveToTrash"/>
<addaction name="actionDelete"/>
<addaction name="separator"/>
<addaction name="actionNewFolder"/>
<addaction name="actionSaveHere"/>
Expand Down Expand Up @@ -79,7 +77,7 @@
</widget>
<action name="actionSaveHere">
<property name="text">
<string>&amp;Save Here</string>
<string>&amp;Save Current File Here</string>
</property>
</action>
<action name="actionNewFolder">
Expand All @@ -92,12 +90,11 @@
<string>&amp;Rename</string>
</property>
</action>
<action name="actionDelete">
<property name="text">
<string>&amp;Delete</string>
</property>
</action>
<action name="actionMoveToTrash">
<property name="icon">
<iconset resource="../resources.qrc">
<normaloff>:/icons/bin_closed.png</normaloff>:/icons/bin_closed.png</iconset>
</property>
<property name="text">
<string>Move to &amp;Trash</string>
</property>
Expand Down