Skip to content

Commit

Permalink
Improvements after review
Browse files Browse the repository at this point in the history
  • Loading branch information
SimLV committed May 8, 2024
1 parent fe945df commit 58dbd4c
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 45 deletions.
23 changes: 14 additions & 9 deletions src/NotepadNext/ScintillaNext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,22 +369,27 @@ 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;
}

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) {
Expand Down
3 changes: 2 additions & 1 deletion src/NotepadNext/ScintillaNext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
37 changes: 31 additions & 6 deletions src/NotepadNext/dialogs/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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;
}

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 <b>%1</b> 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 <b>%1</b>?").arg(filePath));
Expand Down
20 changes: 20 additions & 0 deletions src/NotepadNext/dialogs/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ class MainWindow : public QMainWindow
QVector<ScintillaNext *> editors() const;
DockedEditor *getDockedEditor() const { return dockedEditor; }

template<typename Func>
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();

Expand Down Expand Up @@ -171,4 +179,16 @@ private slots:
int contextMenuPos = 0;
};

template<typename Func>
void MainWindow::forEachEditorByPath(const QString &path, Func callback)
{
for(auto &&editor : editors())
{
if (editor->getFilePath() == path)
{
callback(editor);
}
}
}

#endif // MAINWINDOW_H
68 changes: 39 additions & 29 deletions src/NotepadNext/docks/FolderAsWorkspaceDock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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 <b>%1</b>?").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());
}
}
}
1 change: 1 addition & 0 deletions src/NotepadNext/docks/FolderAsWorkspaceDock.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions src/NotepadNext/docks/FolderAsWorkspaceDock.ui
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<string>File Menu</string>
</property>
<addaction name="actionRename"/>
<addaction name="actionMoveToTrash"/>
<addaction name="actionDelete"/>
</widget>
</item>
Expand All @@ -42,6 +43,7 @@
<string>Folder Menu</string>
</property>
<addaction name="actionRename"/>
<addaction name="actionMoveToTrash"/>
<addaction name="actionDelete"/>
<addaction name="separator"/>
<addaction name="actionNewFolder"/>
Expand Down Expand Up @@ -95,6 +97,11 @@
<string>&amp;Delete</string>
</property>
</action>
<action name="actionMoveToTrash">
<property name="text">
<string>Move to &amp;Trash</string>
</property>
</action>
</widget>
<resources>
<include location="../resources.qrc"/>
Expand Down

0 comments on commit 58dbd4c

Please sign in to comment.