From 54a71ae9772c4587b8dd08e7fb81f0f66e332591 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Sun, 29 Aug 2021 10:33:01 +0800 Subject: [PATCH] add switch for per-notebook history --- src/core/configmgr.cpp | 2 +- src/core/coreconfig.cpp | 13 +++++ src/core/coreconfig.h | 6 +++ src/core/historymgr.cpp | 29 +++++----- src/core/historymgr.h | 2 +- src/data/core/vnotex.json | 3 +- src/data/extra/themes/moonlight/interface.qss | 4 +- src/data/extra/themes/pure/interface.qss | 4 +- .../dialogs/settings/notemanagementpage.cpp | 54 +++++++++++++++++++ .../dialogs/settings/notemanagementpage.h | 30 +++++++++++ .../dialogs/settings/settingsdialog.cpp | 7 +++ src/widgets/widgets.pri | 2 + 12 files changed, 135 insertions(+), 21 deletions(-) create mode 100644 src/widgets/dialogs/settings/notemanagementpage.cpp create mode 100644 src/widgets/dialogs/settings/notemanagementpage.h diff --git a/src/core/configmgr.cpp b/src/core/configmgr.cpp index 30e0d7c9..5cca0ec8 100644 --- a/src/core/configmgr.cpp +++ b/src/core/configmgr.cpp @@ -24,7 +24,7 @@ using namespace vnotex; #ifndef QT_NO_DEBUG - #define VX_DEBUG_WEB + // #define VX_DEBUG_WEB #endif const QString ConfigMgr::c_orgName = "VNote"; diff --git a/src/core/coreconfig.cpp b/src/core/coreconfig.cpp index c6b94b93..1a5b2808 100644 --- a/src/core/coreconfig.cpp +++ b/src/core/coreconfig.cpp @@ -63,6 +63,8 @@ void CoreConfig::init(const QJsonObject &p_app, if (m_historyMaxCount < 0) { m_historyMaxCount = 100; } + + m_perNotebookHistoryEnabled = READBOOL(QStringLiteral("per_notebook_history")); } QJsonObject CoreConfig::toJson() const @@ -76,6 +78,7 @@ QJsonObject CoreConfig::toJson() const obj[QStringLiteral("recover_last_session_on_start")] = m_recoverLastSessionOnStartEnabled; obj[QStringLiteral("check_for_updates_on_start")] = m_checkForUpdatesOnStartEnabled; obj[QStringLiteral("history_max_count")] = m_historyMaxCount; + obj[QStringLiteral("per_notebook_history")] = m_perNotebookHistoryEnabled; return obj; } @@ -203,3 +206,13 @@ int CoreConfig::getHistoryMaxCount() const { return m_historyMaxCount; } + +bool CoreConfig::isPerNotebookHistoryEnabled() const +{ + return m_perNotebookHistoryEnabled; +} + +void CoreConfig::setPerNotebookHistoryEnabled(bool p_enabled) +{ + updateConfig(m_perNotebookHistoryEnabled, p_enabled, this); +} diff --git a/src/core/coreconfig.h b/src/core/coreconfig.h index 1bb80a2a..c5ff4d80 100644 --- a/src/core/coreconfig.h +++ b/src/core/coreconfig.h @@ -104,6 +104,9 @@ namespace vnotex int getHistoryMaxCount() const; + bool isPerNotebookHistoryEnabled() const; + void setPerNotebookHistoryEnabled(bool p_enabled); + private: friend class MainConfig; @@ -138,6 +141,9 @@ namespace vnotex // Max count of the history items for each notebook and session config. int m_historyMaxCount = 100; + // Whether store history in each notebook. + bool m_perNotebookHistoryEnabled = true; + static QStringList s_availableLocales; }; } // ns vnotex diff --git a/src/core/historymgr.cpp b/src/core/historymgr.cpp index d3dada70..754f9568 100644 --- a/src/core/historymgr.cpp +++ b/src/core/historymgr.cpp @@ -25,12 +25,9 @@ bool HistoryItemFull::operator<(const HistoryItemFull &p_other) const } -int HistoryMgr::s_maxHistoryCount = 100; - HistoryMgr::HistoryMgr() + : m_perNotebookHistoryEnabled(ConfigMgr::getInst().getCoreConfig().isPerNotebookHistoryEnabled()) { - s_maxHistoryCount = ConfigMgr::getInst().getCoreConfig().getHistoryMaxCount(); - connect(&VNoteX::getInst().getNotebookMgr(), &NotebookMgr::notebooksUpdated, this, &HistoryMgr::loadHistory); @@ -57,7 +54,7 @@ void HistoryMgr::loadHistory() } // Load from notebooks. - { + if (m_perNotebookHistoryEnabled) { const auto ¬ebooks = VNoteX::getInst().getNotebookMgr().getNotebooks(); for (const auto &nb : notebooks) { const auto &history = nb->getHistory(); @@ -98,13 +95,14 @@ void HistoryMgr::add(const QString &p_path, bool p_readOnly, Notebook *p_notebook) { - if (p_path.isEmpty() || s_maxHistoryCount == 0) { + const int maxHistoryCount = ConfigMgr::getInst().getCoreConfig().getHistoryMaxCount(); + if (p_path.isEmpty() || maxHistoryCount == 0) { return; } HistoryItem item(p_path, p_lineNumber, QDateTime::currentDateTimeUtc()); - if (p_notebook) { + if (p_notebook && m_perNotebookHistoryEnabled) { p_notebook->addHistory(item); } else { auto &sessionConfig = ConfigMgr::getInst().getSessionConfig(); @@ -145,8 +143,8 @@ void HistoryMgr::add(const QString &p_path, file.m_mode = p_mode; file.m_readOnly = p_readOnly; - if (m_lastClosedFiles.size() > s_maxHistoryCount) { - m_lastClosedFiles.remove(0, m_lastClosedFiles.size() - s_maxHistoryCount); + if (m_lastClosedFiles.size() > maxHistoryCount) { + m_lastClosedFiles.remove(0, m_lastClosedFiles.size() - maxHistoryCount); } } @@ -165,8 +163,9 @@ void HistoryMgr::insertHistoryItem(QVector &p_history, const Histor p_history.append(p_item); - if (p_history.size() > s_maxHistoryCount) { - p_history.remove(0, p_history.size() - s_maxHistoryCount); + const int maxHistoryCount = ConfigMgr::getInst().getCoreConfig().getHistoryMaxCount(); + if (p_history.size() > maxHistoryCount) { + p_history.remove(0, p_history.size() - maxHistoryCount); } } @@ -174,9 +173,11 @@ void HistoryMgr::clear() { ConfigMgr::getInst().getSessionConfig().clearHistory(); - const auto ¬ebooks = VNoteX::getInst().getNotebookMgr().getNotebooks(); - for (const auto &nb : notebooks) { - nb->clearHistory(); + if (m_perNotebookHistoryEnabled) { + const auto ¬ebooks = VNoteX::getInst().getNotebookMgr().getNotebooks(); + for (const auto &nb : notebooks) { + nb->clearHistory(); + } } loadHistory(); diff --git a/src/core/historymgr.h b/src/core/historymgr.h index 73e810d5..30375fec 100644 --- a/src/core/historymgr.h +++ b/src/core/historymgr.h @@ -74,7 +74,7 @@ namespace vnotex QVector m_lastClosedFiles; - static int s_maxHistoryCount; + const bool m_perNotebookHistoryEnabled = false; }; } diff --git a/src/data/core/vnotex.json b/src/data/core/vnotex.json index 81ab7ac3..d853f7ce 100644 --- a/src/data/core/vnotex.json +++ b/src/data/core/vnotex.json @@ -74,7 +74,8 @@ "recover_last_session_on_start" : true, "check_for_updates_on_start" : true, "//comment" : "Max count of the history items for each notebook and session config", - "history_max_count" : 100 + "history_max_count" : 100, + "per_notebook_history" : true }, "editor" : { "core": { diff --git a/src/data/extra/themes/moonlight/interface.qss b/src/data/extra/themes/moonlight/interface.qss index a02ec2e0..4b873f81 100644 --- a/src/data/extra/themes/moonlight/interface.qss +++ b/src/data/extra/themes/moonlight/interface.qss @@ -516,7 +516,7 @@ QMainWindow > QTabBar::tab:right { border-right: 3px solid transparent; border-bottom: none; margin: 0px; - padding: -10px 2px 5px 2px; + padding: 6px 2px -10px 2px; } /* Tabified QDockWidget */ @@ -524,7 +524,7 @@ QMainWindow > QTabBar::tab:left { border-left: 3px solid transparent; border-bottom: none; margin: 0px; - padding: -10px 2px 5px 2px; + padding: -10px 2px 6px 2px; } QTabBar::tab:hover { diff --git a/src/data/extra/themes/pure/interface.qss b/src/data/extra/themes/pure/interface.qss index a02ec2e0..4b873f81 100644 --- a/src/data/extra/themes/pure/interface.qss +++ b/src/data/extra/themes/pure/interface.qss @@ -516,7 +516,7 @@ QMainWindow > QTabBar::tab:right { border-right: 3px solid transparent; border-bottom: none; margin: 0px; - padding: -10px 2px 5px 2px; + padding: 6px 2px -10px 2px; } /* Tabified QDockWidget */ @@ -524,7 +524,7 @@ QMainWindow > QTabBar::tab:left { border-left: 3px solid transparent; border-bottom: none; margin: 0px; - padding: -10px 2px 5px 2px; + padding: -10px 2px 6px 2px; } QTabBar::tab:hover { diff --git a/src/widgets/dialogs/settings/notemanagementpage.cpp b/src/widgets/dialogs/settings/notemanagementpage.cpp new file mode 100644 index 00000000..dafda62c --- /dev/null +++ b/src/widgets/dialogs/settings/notemanagementpage.cpp @@ -0,0 +1,54 @@ +#include "notemanagementpage.h" + +#include +#include + +#include +#include +#include +#include +#include + +using namespace vnotex; + +NoteManagementPage::NoteManagementPage(QWidget *p_parent) + : SettingsPage(p_parent) +{ + setupUI(); +} + +void NoteManagementPage::setupUI() +{ + auto mainLayout = WidgetsFactory::createFormLayout(this); + + { + const QString label(tr("Per-Notebook access history")); + m_perNotebookHistoryCheckBox = WidgetsFactory::createCheckBox(label, this); + m_perNotebookHistoryCheckBox->setToolTip(tr("Store note access history in its notebook")); + mainLayout->addRow(m_perNotebookHistoryCheckBox); + addSearchItem(label, m_perNotebookHistoryCheckBox->toolTip(), m_perNotebookHistoryCheckBox); + connect(m_perNotebookHistoryCheckBox, &QCheckBox::stateChanged, + this, &NoteManagementPage::pageIsChanged); + } +} + +void NoteManagementPage::loadInternal() +{ + const auto &coreConfig = ConfigMgr::getInst().getCoreConfig(); + + m_perNotebookHistoryCheckBox->setChecked(coreConfig.isPerNotebookHistoryEnabled()); +} + +bool NoteManagementPage::saveInternal() +{ + auto &coreConfig = ConfigMgr::getInst().getCoreConfig(); + + coreConfig.setPerNotebookHistoryEnabled(m_perNotebookHistoryCheckBox->isChecked()); + + return true; +} + +QString NoteManagementPage::title() const +{ + return tr("Note Management"); +} diff --git a/src/widgets/dialogs/settings/notemanagementpage.h b/src/widgets/dialogs/settings/notemanagementpage.h new file mode 100644 index 00000000..1e20dafa --- /dev/null +++ b/src/widgets/dialogs/settings/notemanagementpage.h @@ -0,0 +1,30 @@ +#ifndef NOTEMANAGEMENTPAGE_H +#define NOTEMANAGEMENTPAGE_H + +#include "settingspage.h" + +class QCheckBox; + +namespace vnotex +{ + class NoteManagementPage : public SettingsPage + { + Q_OBJECT + public: + explicit NoteManagementPage(QWidget *p_parent = nullptr); + + QString title() const Q_DECL_OVERRIDE; + + protected: + void loadInternal() Q_DECL_OVERRIDE; + + bool saveInternal() Q_DECL_OVERRIDE; + + private: + void setupUI(); + + QCheckBox *m_perNotebookHistoryCheckBox = nullptr; + }; +} + +#endif // NOTEMANAGEMENTPAGE_H diff --git a/src/widgets/dialogs/settings/settingsdialog.cpp b/src/widgets/dialogs/settings/settingsdialog.cpp index edd12087..b46890b0 100644 --- a/src/widgets/dialogs/settings/settingsdialog.cpp +++ b/src/widgets/dialogs/settings/settingsdialog.cpp @@ -19,6 +19,7 @@ #include "themepage.h" #include "imagehostpage.h" #include "vipage.h" +#include "notemanagementpage.h" using namespace vnotex; @@ -90,6 +91,12 @@ void SettingsDialog::setupPages() addPage(page); } + // Note Management. + { + auto page = new NoteManagementPage(this); + addPage(page); + } + // Appearance. { auto page = new AppearancePage(this); diff --git a/src/widgets/widgets.pri b/src/widgets/widgets.pri index 11e8b3ef..114e02d8 100644 --- a/src/widgets/widgets.pri +++ b/src/widgets/widgets.pri @@ -23,6 +23,7 @@ SOURCES += \ $$PWD/dialogs/settings/markdowneditorpage.cpp \ $$PWD/dialogs/settings/miscpage.cpp \ $$PWD/dialogs/settings/newimagehostdialog.cpp \ + $$PWD/dialogs/settings/notemanagementpage.cpp \ $$PWD/dialogs/settings/quickaccesspage.cpp \ $$PWD/dialogs/settings/settingspage.cpp \ $$PWD/dialogs/settings/settingsdialog.cpp \ @@ -138,6 +139,7 @@ HEADERS += \ $$PWD/dialogs/settings/markdowneditorpage.h \ $$PWD/dialogs/settings/miscpage.h \ $$PWD/dialogs/settings/newimagehostdialog.h \ + $$PWD/dialogs/settings/notemanagementpage.h \ $$PWD/dialogs/settings/quickaccesspage.h \ $$PWD/dialogs/settings/settingspage.h \ $$PWD/dialogs/settings/settingsdialog.h \