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
38 changes: 21 additions & 17 deletions src/NotepadNext/ScintillaNext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "uchardet.h"
#include <cinttypes>

#include <QDebug>
#include <QDir>
#include <QMouseEvent>
#include <QSaveFile>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -369,22 +367,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 Expand Up @@ -611,6 +614,7 @@ void ScintillaNext::setFileInfo(const QString &filePath)
bufferType = ScintillaNext::File;

updateTimestamp();
emit renamed();
}

void ScintillaNext::detachFileInfo(const QString &newName)
Expand Down
4 changes: 2 additions & 2 deletions src/NotepadNext/ScintillaNext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -122,7 +121,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
6 changes: 3 additions & 3 deletions src/NotepadNext/SessionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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));

Expand Down Expand Up @@ -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);
Expand Down
79 changes: 72 additions & 7 deletions src/NotepadNext/dialogs/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,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, [=]() {
Expand All @@ -285,7 +285,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()));
}
});

Expand Down Expand Up @@ -930,7 +931,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);
Expand Down Expand Up @@ -1011,7 +1012,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);
Expand All @@ -1031,7 +1033,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);
Expand Down Expand Up @@ -1357,14 +1360,48 @@ 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));

return reply == QMessageBox::Yes;
}

void MainWindow::closeByPath(const QString &path, bool isDirectory)
{
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)
{
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();
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.


Expand Down Expand Up @@ -2025,6 +2062,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);
Expand Down
23 changes: 23 additions & 0 deletions src/NotepadNext/dialogs/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ 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);

void closeByPath(const QString &path, bool isDirectory);

public slots:
void newFile();

Expand Down Expand Up @@ -125,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();
Expand Down Expand Up @@ -173,4 +182,18 @@ private slots:
int contextMenuPos = 0;
};

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

#endif // MAINWINDOW_H
Loading
Loading