Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/NotepadNext/ApplicationSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ CREATE_SETTING(App, RestorePreviousSession, restorePreviousSession, bool, false)
CREATE_SETTING(App, RestoreUnsavedFiles, restoreUnsavedFiles, bool, false)
CREATE_SETTING(App, RestoreTempFiles, restoreTempFiles, bool, false)

CREATE_SETTING(App, DefaultDirectoryBehavior, defaultDirectoryBehavior, ApplicationSettings::DefaultDirectoryBehaviorEnum, ApplicationSettings::FollowCurrentDocument)
CREATE_SETTING(App, DefaultDirectory, defaultDirectory, QString, QString())

CREATE_SETTING(App, Translation, translation, QString, QStringLiteral(""))

CREATE_SETTING(Editor, ShowWhitespace, showWhitespace, bool, false);
Expand Down
13 changes: 11 additions & 2 deletions src/NotepadNext/ApplicationSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <QObject>
#include <QSettings>
#include <QString>
#include <QMetaEnum>


template<typename T>
Expand Down Expand Up @@ -68,7 +69,13 @@ class ApplicationSettings : public QSettings
public:
explicit ApplicationSettings(QObject *parent = nullptr);

public:
enum DefaultDirectoryBehaviorEnum {
FollowCurrentDocument,
RememberLastUsed,
HardCoded
};
Q_ENUM(DefaultDirectoryBehaviorEnum)

template <typename T>
T get(const char *key, const T &defaultValue) const
{ return value(QLatin1String(key), defaultValue).template value<T>(); }
Expand All @@ -81,7 +88,6 @@ class ApplicationSettings : public QSettings
void set(const ApplicationSetting<T> &setting, const T &value)
{ setValue(QLatin1String(setting.key()), value); }

public:
DEFINE_SETTING(ShowMenuBar, showMenuBar, bool)
DEFINE_SETTING(ShowToolBar, showToolBar, bool)
DEFINE_SETTING(ShowTabBar, showTabBar, bool)
Expand All @@ -97,6 +103,9 @@ class ApplicationSettings : public QSettings
DEFINE_SETTING(RestoreUnsavedFiles, restoreUnsavedFiles, bool)
DEFINE_SETTING(RestoreTempFiles, restoreTempFiles, bool)

DEFINE_SETTING(DefaultDirectoryBehavior, defaultDirectoryBehavior, DefaultDirectoryBehaviorEnum)
DEFINE_SETTING(DefaultDirectory, defaultDirectory, QString)

DEFINE_SETTING(Translation, translation, QString)

DEFINE_SETTING(ShowWhitespace, showWhitespace, bool);
Expand Down
59 changes: 59 additions & 0 deletions src/NotepadNext/DefaultDirectoryManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* This file is part of Notepad Next.
* Copyright 2025 Justin Dailey
*
* Notepad Next is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Notepad Next is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Notepad Next. If not, see <https://www.gnu.org/licenses/>.
*/

#include "DefaultDirectoryManager.h"
#include "ApplicationSettings.h"

DefaultDirectoryManager::DefaultDirectoryManager(MainWindow *window, ApplicationSettings *settings, QObject *parent)
: QObject{parent}, window(window), settings(settings)
{
connect(window, &MainWindow::editorActivated, this, &DefaultDirectoryManager::editorActivated);
connect(window, &MainWindow::fileDialogAccepted, this, &DefaultDirectoryManager::fileDialogAccepted);
}

QString DefaultDirectoryManager::getDefaultDirectory() const
{
ApplicationSettings::DefaultDirectoryBehaviorEnum e = settings->defaultDirectoryBehavior();

if (e == ApplicationSettings::FollowCurrentDocument){
const ScintillaNext *editor = window->currentEditor();
return editor->isFile() ? editor->getPath() : settings->defaultDirectory();
}
else if (e == ApplicationSettings::RememberLastUsed) {
return settings->defaultDirectory();
}
else if (e == ApplicationSettings::HardCoded) {
return settings->defaultDirectory();
}

return QString();
}

void DefaultDirectoryManager::editorActivated(ScintillaNext *editor)
{
if (settings->defaultDirectoryBehavior() == ApplicationSettings::FollowCurrentDocument && editor->isFile()) {
settings->setDefaultDirectory(editor->getPath());
}
}

void DefaultDirectoryManager::fileDialogAccepted(const QString &filePath)
{
if (settings->defaultDirectoryBehavior() == ApplicationSettings::RememberLastUsed && !filePath.isEmpty()) {
settings->setDefaultDirectory(QFileInfo(filePath).absolutePath());
}
}
41 changes: 41 additions & 0 deletions src/NotepadNext/DefaultDirectoryManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* This file is part of Notepad Next.
* Copyright 2025 Justin Dailey
*
* Notepad Next is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Notepad Next is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Notepad Next. If not, see <https://www.gnu.org/licenses/>.
*/

#pragma once

#include "MainWindow.h"
#include "ApplicationSettings.h"

#include <QObject>

class DefaultDirectoryManager : public QObject
{
Q_OBJECT
public:
explicit DefaultDirectoryManager(MainWindow *window, ApplicationSettings *settings, QObject *parent = nullptr);

QString getDefaultDirectory() const;

public slots:
void editorActivated(ScintillaNext *editor);
void fileDialogAccepted(const QString &filePath);

private:
MainWindow *window;
ApplicationSettings *settings;
};
2 changes: 2 additions & 0 deletions src/NotepadNext/NotepadNext.pro
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ SOURCES += \
ComboBoxDelegate.cpp \
Converter.cpp \
DebugManager.cpp \
DefaultDirectoryManager.cpp \
DockedEditor.cpp \
EditorHexViewerTableModel.cpp \
EditorManager.cpp \
Expand Down Expand Up @@ -147,6 +148,7 @@ HEADERS += \
ComboBoxDelegate.h \
Converter.h \
DebugManager.h \
DefaultDirectoryManager.h \
DockedEditor.h \
DockedEditorTitleBar.h \
EditorHexViewerTableModel.h \
Expand Down
59 changes: 22 additions & 37 deletions src/NotepadNext/dialogs/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "MainWindow.h"
#include "BookMarkDecorator.h"
#include "DefaultDirectoryManager.h"
#include "MarkerAppDecorator.h"
#include "URLFinder.h"
#include "SessionManager.h"
Expand Down Expand Up @@ -104,6 +105,8 @@ MainWindow::MainWindow(NotepadNextApplication *app) :

qInfo("setupUi Completed");

defaultDirectoryManager = new DefaultDirectoryManager(this, app->getSettings(), this);

connect(this, &MainWindow::aboutToClose, this, &MainWindow::saveSettings);

// Create and set up the connections to the docked editor
Expand Down Expand Up @@ -1057,8 +1060,8 @@ void MainWindow::openFileList(const QStringList &fileNames)
if (fileNames.size() == 0)
return;

QList<ScintillaNext *> openedEditors;
ScintillaNext *initialEditor = getInitialEditor();
const ScintillaNext *mostRecentEditor = Q_NULLPTR;

for (const QString &filePath : fileNames) {
qInfo("%s", qUtf8Printable(filePath));
Expand Down Expand Up @@ -1089,13 +1092,13 @@ void MainWindow::openFileList(const QStringList &fileNames)
}

if (editor) {
mostRecentEditor = editor;
openedEditors.append(editor);
}
}

// If any were successful, switch to the last one
if (mostRecentEditor) {
dockedEditor->switchToEditor(mostRecentEditor);
if (!openedEditors.empty()) {
dockedEditor->switchToEditor(openedEditors.last());
}

if (initialEditor) {
Expand Down Expand Up @@ -1136,16 +1139,12 @@ bool MainWindow::checkEditorsBeforeClose(const QVector<ScintillaNext *> &editors

void MainWindow::openFileDialog()
{
QString dialogDir;
const QString filter = app->getFileDialogFilter();
const ScintillaNext *editor = currentEditor();

// Use the path if possible
if (editor->isFile()) {
dialogDir = editor->getPath();
}
QStringList fileNames = FileDialogHelpers::getOpenFileNames(this, QString(), defaultDirectoryManager->getDefaultDirectory(), filter);

QStringList fileNames = FileDialogHelpers::getOpenFileNames(this, QString(), dialogDir, filter);
if (!fileNames.empty())
emit fileDialogAccepted(fileNames.last());

openFileList(fileNames);
}
Expand All @@ -1157,15 +1156,7 @@ void MainWindow::openFile(const QString &filePath)

void MainWindow::openFolderAsWorkspaceDialog()
{
QString dialogDir;
const ScintillaNext *editor = currentEditor();

// Use the path if possible
if (editor->isFile()) {
dialogDir = editor->getPath();
}

QString dir = QFileDialog::getExistingDirectory(this, tr("Open Folder as Workspace"), dialogDir, QFileDialog::ShowDirsOnly);
QString dir = QFileDialog::getExistingDirectory(this, tr("Open Folder as Workspace"), defaultDirectoryManager->getDefaultDirectory(), QFileDialog::ShowDirsOnly);

setFolderAsWorkspacePath(dir);
}
Expand Down Expand Up @@ -1335,22 +1326,18 @@ bool MainWindow::saveFile(ScintillaNext *editor)

bool MainWindow::saveCurrentFileAsDialog()
{
QString dialogDir;
const QString filter = app->getFileDialogFilter();
ScintillaNext *editor = currentEditor();

// Use the file path if possible
if (editor->isFile()) {
dialogDir = editor->getFilePath();
}

QString selectedFilter = app->getFileDialogFilterForLanguage(editor->languageName);
QString fileName = FileDialogHelpers::getSaveFileName(this, QString(), dialogDir, filter, &selectedFilter);
QString fileName = FileDialogHelpers::getSaveFileName(this, QString(), defaultDirectoryManager->getDefaultDirectory(), filter, &selectedFilter);

if (fileName.size() == 0) {
return false;
}

emit fileDialogAccepted(fileName);

// TODO: distinguish between the above case (i.e. the user cancels the dialog) and a failure
// calling editor->saveAs() as it might fail.

Expand Down Expand Up @@ -1379,22 +1366,18 @@ bool MainWindow::saveFileAs(ScintillaNext *editor, const QString &fileName)

bool MainWindow::saveCopyAsDialog()
{
QString dialogDir;
const QString filter = app->getFileDialogFilter();
const ScintillaNext* editor = currentEditor();

// Use the file path if possible
if (editor->isFile()) {
dialogDir = editor->getFilePath();
}
const QString languageName = currentEditor()->languageName;

QString selectedFilter = app->getFileDialogFilterForLanguage(editor->languageName);
QString fileName = FileDialogHelpers::getSaveFileName(this, tr("Save a Copy As"), dialogDir, filter, &selectedFilter);
QString selectedFilter = app->getFileDialogFilterForLanguage(languageName);
const QString fileName = FileDialogHelpers::getSaveFileName(this, tr("Save a Copy As"), defaultDirectoryManager->getDefaultDirectory(), filter, &selectedFilter);

if (fileName.size() == 0) {
return false;
}

emit fileDialogAccepted(fileName);

return saveCopyAs(fileName);
}

Expand Down Expand Up @@ -1463,12 +1446,14 @@ void MainWindow::renameFile()
if (editor->isFile()) {
const QString filter = app->getFileDialogFilter();
QString selectedFilter = app->getFileDialogFilterForLanguage(editor->languageName);
QString fileName = FileDialogHelpers::getSaveFileName(this, tr("Rename"), editor->getFilePath(), filter, &selectedFilter);
QString fileName = FileDialogHelpers::getSaveFileName(this, tr("Rename"), defaultDirectoryManager->getDefaultDirectory(), filter, &selectedFilter);

if (fileName.isEmpty()) {
return;
}

emit fileDialogAccepted(fileName);

// TODO
// The new fileName might be to one of the existing editors.
//auto otherEditor = app->getEditorByFilePath(fileName);
Expand Down
3 changes: 3 additions & 0 deletions src/NotepadNext/dialogs/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Settings;
class QuickFindWidget;
class ZoomEventWatcher;
class Converter;
class DefaultDirectoryManager;

class MainWindow : public QMainWindow
{
Expand Down Expand Up @@ -129,6 +130,7 @@ public slots:
signals:
void editorActivated(ScintillaNext *editor);
void aboutToClose();
void fileDialogAccepted(const QString &filePath);

protected:
void closeEvent(QCloseEvent *event) override;
Expand Down Expand Up @@ -168,6 +170,7 @@ private slots:
//NppImporter *npp;

MacroManager macroManager;
DefaultDirectoryManager *defaultDirectoryManager;

ZoomEventWatcher *zoomEventWatcher;
int zoomLevel = 0;
Expand Down
Loading