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

Support for Sessions #554

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 10 additions & 1 deletion src/NotepadNext/ApplicationSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,18 @@ ApplicationSetting<type> name{#group "/" #name, default};\
}


Settings::Settings(QObject *parent)
: QSettings{parent}
{
}

Settings::Settings(const QString &path, QSettings::Format format)
: QSettings(path, format)
{
}

ApplicationSettings::ApplicationSettings(QObject *parent)
: QSettings{parent}
: Settings{parent}
{
}

Expand Down
34 changes: 31 additions & 3 deletions src/NotepadNext/ApplicationSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <QSettings>
#include <QString>

#include "SessionManager.h"

template<typename T>
class ApplicationSetting
Expand All @@ -45,12 +46,30 @@ class ApplicationSetting
inline T getDefault() const { return mCallable ? mCallable() : mDefault ; }
inline const char * const key() const { return mKey; }

private:
protected:
const char * const mKey;
const T mDefault;
std::function<T()> mCallable;
};

template<typename T>
class SessionSetting : public ApplicationSetting<T>
{
};

// This macro should be used on any Qt class which uses session settings so they could be saved/reloaded
#define USE_SESSION_SETTINGS \
public slots: \
void onSettingsLoaded(const Settings &settings); \
void onSettingsSaved(Settings &settings); \
\
protected: \
template<typename T> \
void initSettingsListener(T *This, SessionManager *manager) \
{ \
connect(manager, &SessionManager::onSessionSaved, This, &T::onSettingsSaved); \
connect(manager, &SessionManager::onSessionLoaded, This, &T::onSettingsLoaded); \
}

#define DEFINE_SETTING(name, lname, type)\
public:\
Expand All @@ -61,12 +80,13 @@ Q_SIGNAL\
void lname##Changed(type lname);\


class ApplicationSettings : public QSettings
class Settings : public QSettings
{
Q_OBJECT

public:
explicit ApplicationSettings(QObject *parent = nullptr);
explicit Settings(QObject *parent = nullptr);
Settings(const QString &path, QSettings::Format format);

public:
template <typename T>
Expand All @@ -81,7 +101,15 @@ class ApplicationSettings : public QSettings
void set(const ApplicationSetting<T> &setting, const T &value)
{ setValue(QLatin1String(setting.key()), value); }

};

class ApplicationSettings: public Settings
{
Q_OBJECT

public:
explicit ApplicationSettings(QObject *parent = nullptr);

DEFINE_SETTING(ShowMenuBar, showMenuBar, bool)
DEFINE_SETTING(ShowToolBar, showToolBar, bool)
DEFINE_SETTING(ShowTabBar, showTabBar, bool)
Expand Down
8 changes: 4 additions & 4 deletions src/NotepadNext/NotepadNext.pro
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ SOURCES += \
QRegexSearch.cpp \
QuickFindWidget.cpp \
RangeAllocator.cpp \
RecentFilesListManager.cpp \
RecentFilesListMenuBuilder.cpp \
RecentListManager.cpp \
RecentListMenuBuilder.cpp \
RtfConverter.cpp \
SciIFaceTable.cpp \
ScintillaCommenter.cpp \
Expand Down Expand Up @@ -169,8 +169,8 @@ HEADERS += \
QRegexSearch.h \
QuickFindWidget.h \
RangeAllocator.h \
RecentFilesListManager.h \
RecentFilesListMenuBuilder.h \
RecentListManager.h \
RecentListMenuBuilder.h \
RtfConverter.h \
SciIFaceTable.h \
ScintillaCommenter.h \
Expand Down
18 changes: 12 additions & 6 deletions src/NotepadNext/NotepadNextApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include "MainWindow.h"
#include "NotepadNextApplication.h"
#include "RecentFilesListManager.h"
#include "RecentListManager.h"
#include "EditorManager.h"
#include "LuaExtension.h"
#include "DebugManager.h"
Expand All @@ -44,6 +44,9 @@
#include <Windows.h>
#endif

#define RECENT_SESSIONS_KEY "App/RecentSessionsList"
#define RECENT_FILES_KEY "App/RecentFilesList"

const SingleApplication::Options opts = SingleApplication::ExcludeAppPath | SingleApplication::ExcludeAppVersion | SingleApplication::SecondaryNotification;

template <>
Expand Down Expand Up @@ -125,7 +128,8 @@ bool NotepadNextApplication::init()

luaState = new LuaState();

recentFilesListManager = new RecentFilesListManager(this);
recentFilesListManager = new RecentListManager(this);
recentSessionsListManager = new RecentListManager(this);
editorManager = new EditorManager(settings, this);
sessionManager = new SessionManager(this);

Expand Down Expand Up @@ -216,7 +220,7 @@ bool NotepadNextApplication::init()
if (settings->restorePreviousSession()) {
qInfo("Restoring previous session");

sessionManager->loadSession(window);
sessionManager->loadDefaultSession(window);
}

openFiles(parser.positionalArguments());
Expand Down Expand Up @@ -490,12 +494,14 @@ void NotepadNextApplication::openFiles(const QStringList &files)

void NotepadNextApplication::loadSettings()
{
recentFilesListManager->setFileList(getSettings()->value("App/RecentFilesList").toStringList());
recentFilesListManager->setFileList(getSettings()->value(RECENT_FILES_KEY).toStringList());
recentSessionsListManager->setFileList(getSettings()->value(RECENT_SESSIONS_KEY).toStringList());
}

void NotepadNextApplication::saveSettings()
{
getSettings()->setValue("App/RecentFilesList", recentFilesListManager->fileList());
getSettings()->setValue(RECENT_FILES_KEY, recentFilesListManager->fileList());
getSettings()->setValue(RECENT_SESSIONS_KEY, recentSessionsListManager->fileList());
}

MainWindow *NotepadNextApplication::createNewWindow()
Expand All @@ -517,7 +523,7 @@ MainWindow *NotepadNextApplication::createNewWindow()
}
}

getSessionManager()->saveSession(window);
getSessionManager()->saveCurrentSession(window);
});

return window;
Expand Down
8 changes: 5 additions & 3 deletions src/NotepadNext/NotepadNextApplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
class MainWindow;
class LuaState;
class EditorManager;
class RecentFilesListManager;
class RecentListManager;
class ScintillaNext;
class SessionManager;
class TranslationManager;
Expand All @@ -46,7 +46,8 @@ class NotepadNextApplication : public SingleApplication

bool init();

RecentFilesListManager *getRecentFilesListManager() const { return recentFilesListManager; }
RecentListManager *getRecentFilesListManager() const { return recentFilesListManager; }
RecentListManager *getRecentSessionsListManager() const { return recentSessionsListManager; }
EditorManager *getEditorManager() const { return editorManager; }
SessionManager *getSessionManager() const;
TranslationManager *getTranslationManager() const { return translationManager; };
Expand Down Expand Up @@ -80,7 +81,8 @@ private slots:
void loadSettings();

EditorManager *editorManager;
RecentFilesListManager *recentFilesListManager;
RecentListManager *recentFilesListManager;
RecentListManager *recentSessionsListManager;
ApplicationSettings *settings;
SessionManager *sessionManager;
TranslationManager *translationManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,52 +17,52 @@
*/


#include "RecentFilesListManager.h"
#include "RecentListManager.h"

RecentFilesListManager::RecentFilesListManager(QObject *parent) :
QObject(parent)
RecentListManager::RecentListManager(QObject *parent) :
QObject(parent), limit(10)
{
}

void RecentFilesListManager::addFile(const QString &filePath)
void RecentListManager::addFile(const QString &filePath)
{
qInfo(Q_FUNC_INFO);

// Attempt to remove it first to make sure it is not added twice
removeFile(filePath);

// Set a limit on how many can be in the list
if (recentFiles.size() >= 10) {
if (recentFiles.size() >= limit) {
recentFiles.removeLast();
}

recentFiles.prepend(filePath);
}

void RecentFilesListManager::removeFile(const QString &filePath)
void RecentListManager::removeFile(const QString &filePath)
{
recentFiles.removeOne(filePath);
}

void RecentFilesListManager::clear()
void RecentListManager::clear()
{
// Clear the file list
recentFiles.clear();
}

QString RecentFilesListManager::mostRecentFile() const
QString RecentListManager::mostRecentFile() const
{
Q_ASSERT(!recentFiles.empty());

return recentFiles.first();
}

QStringList RecentFilesListManager::fileList() const
QStringList RecentListManager::fileList() const
{
return recentFiles;
}

void RecentFilesListManager::setFileList(const QStringList &list)
void RecentListManager::setFileList(const QStringList &list)
{
clear();
recentFiles.append(list);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
*/


#ifndef RECENTFILESLISTMANAGER_H
#define RECENTFILESLISTMANAGER_H
#ifndef RECENTLISTMANAGER_H
#define RECENTLISTMANAGER_H

#include <QObject>
#include <QList>

class RecentFilesListManager : public QObject
class RecentListManager : public QObject
{
Q_OBJECT

public:
explicit RecentFilesListManager(QObject *parent = Q_NULLPTR);
explicit RecentListManager(QObject *parent = Q_NULLPTR);

QString mostRecentFile() const;
QStringList fileList() const;
Expand All @@ -43,6 +43,7 @@ public slots:

private:
QStringList recentFiles;
int limit;
};

#endif // RECENTFILESLISTMANAGER_H
#endif // RECENTLISTMANAGER_H
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,42 @@
*/


#include "RecentFilesListMenuBuilder.h"
#include "RecentListMenuBuilder.h"

#include <QAction>
#include <QMenu>
#include <QDir>

RecentFilesListMenuBuilder::RecentFilesListMenuBuilder(RecentFilesListManager *manager) :
RecentListMenuBuilder::RecentListMenuBuilder(RecentListManager *manager, int limit) :
QObject(manager),
manager(manager)
manager(manager),
limit(limit)
{
}

void RecentFilesListMenuBuilder::populateMenu(QMenu *menu)
void RecentListMenuBuilder::populateMenu(QMenu *menu)
{
int i = 0;

while (menu->actions().size() > limit) {
delete menu->actions().takeLast();
}

QList<QAction *> recentFileListActions;
for (const QString &file : manager->fileList()) {
++i;
QAction *action = new QAction(QString("%1%2: %3").arg(i < 10 ? "&" : "").arg(i).arg(QDir::toNativeSeparators(file)), menu);

action->setData(file);
connect(action, &QAction::triggered, this, &RecentFilesListMenuBuilder::recentFileActionTriggered);
connect(action, &QAction::triggered, this, &RecentListMenuBuilder::recentFileActionTriggered);

recentFileListActions.append(action);
}

menu->addActions(recentFileListActions);
}

void RecentFilesListMenuBuilder::recentFileActionTriggered()
void RecentListMenuBuilder::recentFileActionTriggered()
{
qInfo(Q_FUNC_INFO);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@
*/


#ifndef RECENTFILESLISTMENUBUILDER_H
#define RECENTFILESLISTMENUBUILDER_H
#ifndef RECENTLISTMENUBUILDER_H
#define RECENTLISTMENUBUILDER_H

#include "RecentFilesListManager.h"
#include "RecentListManager.h"
#include <QObject>

class QMenu;

class RecentFilesListMenuBuilder : public QObject
class RecentListMenuBuilder : public QObject
{
Q_OBJECT

public:
explicit RecentFilesListMenuBuilder(RecentFilesListManager *manager);
explicit RecentListMenuBuilder(RecentListManager *manager, int limit);
void populateMenu(QMenu *menu);

signals:
Expand All @@ -40,7 +40,8 @@ private slots:
void recentFileActionTriggered();

private:
RecentFilesListManager *manager;
RecentListManager *manager;
int limit;
};

#endif // RECENTFILESLISTMENUBUILDER_H
Loading
Loading