From 993d522e15e43ab4e628483edbf0b16829291ff7 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Wed, 26 Jan 2022 19:14:33 +0800 Subject: [PATCH] add line ending settings for configs --- src/core/core.pri | 1 + src/core/coreconfig.cpp | 17 ++++++++ src/core/coreconfig.h | 7 ++++ src/core/editorconfig.cpp | 38 ----------------- src/core/editorconfig.h | 3 -- src/core/global.cpp | 3 ++ src/core/global.h | 38 +++++++++++++++++ .../notebookbackend/localnotebookbackend.cpp | 41 ++++++++++++++++++- src/data/core/vnotex.json | 4 +- .../dialogs/settings/notemanagementpage.cpp | 30 ++++++++++++++ .../dialogs/settings/notemanagementpage.h | 3 ++ src/widgets/notebooknodeexplorer.cpp | 6 ++- 12 files changed, 146 insertions(+), 45 deletions(-) create mode 100644 src/core/global.cpp diff --git a/src/core/core.pri b/src/core/core.pri index 6c0eb8eb..a4a0690a 100644 --- a/src/core/core.pri +++ b/src/core/core.pri @@ -17,6 +17,7 @@ SOURCES += \ $$PWD/editorconfig.cpp \ $$PWD/externalfile.cpp \ $$PWD/file.cpp \ + $$PWD/global.cpp \ $$PWD/historyitem.cpp \ $$PWD/historymgr.cpp \ $$PWD/htmltemplatehelper.cpp \ diff --git a/src/core/coreconfig.cpp b/src/core/coreconfig.cpp index c9f37231..801faf50 100644 --- a/src/core/coreconfig.cpp +++ b/src/core/coreconfig.cpp @@ -67,6 +67,12 @@ void CoreConfig::init(const QJsonObject &p_app, } m_perNotebookHistoryEnabled = READBOOL(QStringLiteral("per_notebook_history")); + + + { + auto lineEnding = READSTR(QStringLiteral("line_ending")); + m_lineEnding = stringToLineEndingPolicy(lineEnding); + } } QJsonObject CoreConfig::toJson() const @@ -82,6 +88,7 @@ QJsonObject CoreConfig::toJson() const obj[QStringLiteral("check_for_updates_on_start")] = m_checkForUpdatesOnStartEnabled; obj[QStringLiteral("history_max_count")] = m_historyMaxCount; obj[QStringLiteral("per_notebook_history")] = m_perNotebookHistoryEnabled; + obj[QStringLiteral("line_ending")] = lineEndingPolicyToString(m_lineEnding); return obj; } @@ -224,3 +231,13 @@ const QString &CoreConfig::getShortcutLeaderKey() const { return m_shortcutLeaderKey; } + +LineEndingPolicy CoreConfig::getLineEndingPolicy() const +{ + return m_lineEnding; +} + +void CoreConfig::setLineEndingPolicy(LineEndingPolicy p_ending) +{ + updateConfig(m_lineEnding, p_ending, this); +} diff --git a/src/core/coreconfig.h b/src/core/coreconfig.h index f75f8d3b..c2bb9cb5 100644 --- a/src/core/coreconfig.h +++ b/src/core/coreconfig.h @@ -7,6 +7,8 @@ #include #include +#include "global.h" + namespace vnotex { class CoreConfig : public IConfig @@ -110,6 +112,9 @@ namespace vnotex const QString &getShortcutLeaderKey() const; + LineEndingPolicy getLineEndingPolicy() const; + void setLineEndingPolicy(LineEndingPolicy p_ending); + private: friend class MainConfig; @@ -150,6 +155,8 @@ namespace vnotex // Whether store history in each notebook. bool m_perNotebookHistoryEnabled = false; + LineEndingPolicy m_lineEnding = LineEndingPolicy::LF; + static QStringList s_availableLocales; }; } // ns vnotex diff --git a/src/core/editorconfig.cpp b/src/core/editorconfig.cpp index 4453ff46..c1c06803 100644 --- a/src/core/editorconfig.cpp +++ b/src/core/editorconfig.cpp @@ -221,44 +221,6 @@ EditorConfig::AutoSavePolicy EditorConfig::stringToAutoSavePolicy(const QString } } -QString EditorConfig::lineEndingPolicyToString(LineEndingPolicy p_ending) const -{ - switch (p_ending) { - case LineEndingPolicy::Platform: - return QStringLiteral("platform"); - - case LineEndingPolicy::File: - return QStringLiteral("file"); - - case LineEndingPolicy::LF: - return QStringLiteral("lf"); - - case LineEndingPolicy::CRLF: - return QStringLiteral("crlf"); - - case LineEndingPolicy::CR: - return QStringLiteral("cr"); - } - - return QStringLiteral("platform"); -} - -LineEndingPolicy EditorConfig::stringToLineEndingPolicy(const QString &p_str) const -{ - auto ending = p_str.toLower(); - if (ending == QStringLiteral("file")) { - return LineEndingPolicy::File; - } else if (ending == QStringLiteral("lf")) { - return LineEndingPolicy::LF; - } else if (ending == QStringLiteral("crlf")) { - return LineEndingPolicy::CRLF; - } else if (ending == QStringLiteral("cr")) { - return LineEndingPolicy::CR; - } else { - return LineEndingPolicy::Platform; - } -} - EditorConfig::AutoSavePolicy EditorConfig::getAutoSavePolicy() const { return m_autoSavePolicy; diff --git a/src/core/editorconfig.h b/src/core/editorconfig.h index 62b39906..405377a0 100644 --- a/src/core/editorconfig.h +++ b/src/core/editorconfig.h @@ -149,9 +149,6 @@ namespace vnotex QString autoSavePolicyToString(AutoSavePolicy p_policy) const; AutoSavePolicy stringToAutoSavePolicy(const QString &p_str) const; - QString lineEndingPolicyToString(LineEndingPolicy p_ending) const; - LineEndingPolicy stringToLineEndingPolicy(const QString &p_str) const; - void loadImageHost(const QJsonObject &p_app, const QJsonObject &p_user); QJsonObject saveImageHost() const; diff --git a/src/core/global.cpp b/src/core/global.cpp new file mode 100644 index 00000000..63242691 --- /dev/null +++ b/src/core/global.cpp @@ -0,0 +1,3 @@ +#include "global.h" + +using namespace vnotex; diff --git a/src/core/global.h b/src/core/global.h index b3d1ef95..ccb5d013 100644 --- a/src/core/global.h +++ b/src/core/global.h @@ -140,6 +140,44 @@ namespace vnotex CR }; + inline QString lineEndingPolicyToString(LineEndingPolicy p_ending) + { + switch (p_ending) { + case LineEndingPolicy::Platform: + return QStringLiteral("platform"); + + case LineEndingPolicy::File: + return QStringLiteral("file"); + + case LineEndingPolicy::LF: + return QStringLiteral("lf"); + + case LineEndingPolicy::CRLF: + return QStringLiteral("crlf"); + + case LineEndingPolicy::CR: + return QStringLiteral("cr"); + } + + return QStringLiteral("platform"); + } + + inline LineEndingPolicy stringToLineEndingPolicy(const QString &p_str) + { + auto ending = p_str.toLower(); + if (ending == QStringLiteral("file")) { + return LineEndingPolicy::File; + } else if (ending == QStringLiteral("lf")) { + return LineEndingPolicy::LF; + } else if (ending == QStringLiteral("crlf")) { + return LineEndingPolicy::CRLF; + } else if (ending == QStringLiteral("cr")) { + return LineEndingPolicy::CR; + } else { + return LineEndingPolicy::Platform; + } + } + enum Role { // Qt::UserRole = 0x0100 diff --git a/src/core/notebookbackend/localnotebookbackend.cpp b/src/core/notebookbackend/localnotebookbackend.cpp index 096f7148..0818f41b 100644 --- a/src/core/notebookbackend/localnotebookbackend.cpp +++ b/src/core/notebookbackend/localnotebookbackend.cpp @@ -6,9 +6,13 @@ #include #include +#include + #include -#include "exception.h" #include +#include "exception.h" +#include "coreconfig.h" +#include "configmgr.h" using namespace vnotex; @@ -66,7 +70,40 @@ void LocalNotebookBackend::writeFile(const QString &p_filePath, const QString &p void LocalNotebookBackend::writeFile(const QString &p_filePath, const QJsonObject &p_jobj) { - writeFile(p_filePath, QJsonDocument(p_jobj).toJson()); + const auto &coreConfig = ConfigMgr::getInst().getCoreConfig(); + auto data = QJsonDocument(p_jobj).toJson(); + vte::LineEnding before = vte::LineEnding::LF; + QString text; + switch (coreConfig.getLineEndingPolicy()) { + case LineEndingPolicy::Platform: +#if defined(Q_OS_WIN) + text = QString::fromUtf8(data); + vte::TextUtils::transformLineEnding(text, before, vte::LineEnding::CRLF); +#endif + break; + + case LineEndingPolicy::File: + // Not supported. + Q_FALLTHROUGH(); + case LineEndingPolicy::LF: + break; + + case LineEndingPolicy::CRLF: + text = QString::fromUtf8(data); + vte::TextUtils::transformLineEnding(text, before, vte::LineEnding::CRLF); + break; + + case LineEndingPolicy::CR: + text = QString::fromUtf8(data); + vte::TextUtils::transformLineEnding(text, before, vte::LineEnding::CR); + break; + } + + if (!text.isEmpty()) { + writeFile(p_filePath, text); + } else { + writeFile(p_filePath, data); + } } QString LocalNotebookBackend::readTextFile(const QString &p_filePath) diff --git a/src/data/core/vnotex.json b/src/data/core/vnotex.json index 979bac95..24d87b1c 100644 --- a/src/data/core/vnotex.json +++ b/src/data/core/vnotex.json @@ -77,7 +77,9 @@ "check_for_updates_on_start" : true, "//comment" : "Max count of the history items for each notebook and session config", "history_max_count" : 100, - "per_notebook_history" : false + "per_notebook_history" : false, + "//comment" : "Line ending policy for config files, platform/lf/crlf/cr", + "line_ending" : "lf" }, "editor" : { "core": { diff --git a/src/widgets/dialogs/settings/notemanagementpage.cpp b/src/widgets/dialogs/settings/notemanagementpage.cpp index dafda62c..6f96829a 100644 --- a/src/widgets/dialogs/settings/notemanagementpage.cpp +++ b/src/widgets/dialogs/settings/notemanagementpage.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -30,6 +31,22 @@ void NoteManagementPage::setupUI() connect(m_perNotebookHistoryCheckBox, &QCheckBox::stateChanged, this, &NoteManagementPage::pageIsChanged); } + + { + m_lineEndingComboBox = WidgetsFactory::createComboBox(this); + m_lineEndingComboBox->setToolTip(tr("Line ending used to write configuration files")); + + m_lineEndingComboBox->addItem(tr("Follow Platform"), (int)LineEndingPolicy::Platform); + m_lineEndingComboBox->addItem(tr("LF (Linux/macOS)"), (int)LineEndingPolicy::LF); + m_lineEndingComboBox->addItem(tr("CR LF (Windows)"), (int)LineEndingPolicy::CRLF); + m_lineEndingComboBox->addItem(tr("CR"), (int)LineEndingPolicy::CR); + + const QString label(tr("Line ending:")); + mainLayout->addRow(label, m_lineEndingComboBox); + addSearchItem(label, m_lineEndingComboBox->toolTip(), m_lineEndingComboBox); + connect(m_lineEndingComboBox, QOverload::of(&QComboBox::currentIndexChanged), + this, &NoteManagementPage::pageIsChanged); + } } void NoteManagementPage::loadInternal() @@ -37,6 +54,14 @@ void NoteManagementPage::loadInternal() const auto &coreConfig = ConfigMgr::getInst().getCoreConfig(); m_perNotebookHistoryCheckBox->setChecked(coreConfig.isPerNotebookHistoryEnabled()); + + { + int idx = m_lineEndingComboBox->findData(static_cast(coreConfig.getLineEndingPolicy())); + if (idx == -1) { + idx = 0; + } + m_lineEndingComboBox->setCurrentIndex(idx); + } } bool NoteManagementPage::saveInternal() @@ -45,6 +70,11 @@ bool NoteManagementPage::saveInternal() coreConfig.setPerNotebookHistoryEnabled(m_perNotebookHistoryCheckBox->isChecked()); + { + auto ending = m_lineEndingComboBox->currentData().toInt(); + coreConfig.setLineEndingPolicy(static_cast(ending)); + } + return true; } diff --git a/src/widgets/dialogs/settings/notemanagementpage.h b/src/widgets/dialogs/settings/notemanagementpage.h index 1e20dafa..b7afcb3b 100644 --- a/src/widgets/dialogs/settings/notemanagementpage.h +++ b/src/widgets/dialogs/settings/notemanagementpage.h @@ -4,6 +4,7 @@ #include "settingspage.h" class QCheckBox; +class QComboBox; namespace vnotex { @@ -24,6 +25,8 @@ namespace vnotex void setupUI(); QCheckBox *m_perNotebookHistoryCheckBox = nullptr; + + QComboBox *m_lineEndingComboBox = nullptr; }; } diff --git a/src/widgets/notebooknodeexplorer.cpp b/src/widgets/notebooknodeexplorer.cpp index 13b40fca..daa5c1e2 100644 --- a/src/widgets/notebooknodeexplorer.cpp +++ b/src/widgets/notebooknodeexplorer.cpp @@ -2302,7 +2302,11 @@ void NotebookNodeExplorer::updateSlaveExplorer() return; } - masterNode = getCurrentMasterNode(); + auto data = getItemNodeData(item); + if (data.isNode()) { + masterNode = data.getNode(); + Q_ASSERT(masterNode->isContainer()); + } } else { // Root node. masterNode = m_notebook ? m_notebook->getRootNode().data() : nullptr;