mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
add line ending settings for configs
This commit is contained in:
parent
969db5c3db
commit
993d522e15
@ -17,6 +17,7 @@ SOURCES += \
|
|||||||
$$PWD/editorconfig.cpp \
|
$$PWD/editorconfig.cpp \
|
||||||
$$PWD/externalfile.cpp \
|
$$PWD/externalfile.cpp \
|
||||||
$$PWD/file.cpp \
|
$$PWD/file.cpp \
|
||||||
|
$$PWD/global.cpp \
|
||||||
$$PWD/historyitem.cpp \
|
$$PWD/historyitem.cpp \
|
||||||
$$PWD/historymgr.cpp \
|
$$PWD/historymgr.cpp \
|
||||||
$$PWD/htmltemplatehelper.cpp \
|
$$PWD/htmltemplatehelper.cpp \
|
||||||
|
@ -67,6 +67,12 @@ void CoreConfig::init(const QJsonObject &p_app,
|
|||||||
}
|
}
|
||||||
|
|
||||||
m_perNotebookHistoryEnabled = READBOOL(QStringLiteral("per_notebook_history"));
|
m_perNotebookHistoryEnabled = READBOOL(QStringLiteral("per_notebook_history"));
|
||||||
|
|
||||||
|
|
||||||
|
{
|
||||||
|
auto lineEnding = READSTR(QStringLiteral("line_ending"));
|
||||||
|
m_lineEnding = stringToLineEndingPolicy(lineEnding);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QJsonObject CoreConfig::toJson() const
|
QJsonObject CoreConfig::toJson() const
|
||||||
@ -82,6 +88,7 @@ QJsonObject CoreConfig::toJson() const
|
|||||||
obj[QStringLiteral("check_for_updates_on_start")] = m_checkForUpdatesOnStartEnabled;
|
obj[QStringLiteral("check_for_updates_on_start")] = m_checkForUpdatesOnStartEnabled;
|
||||||
obj[QStringLiteral("history_max_count")] = m_historyMaxCount;
|
obj[QStringLiteral("history_max_count")] = m_historyMaxCount;
|
||||||
obj[QStringLiteral("per_notebook_history")] = m_perNotebookHistoryEnabled;
|
obj[QStringLiteral("per_notebook_history")] = m_perNotebookHistoryEnabled;
|
||||||
|
obj[QStringLiteral("line_ending")] = lineEndingPolicyToString(m_lineEnding);
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -224,3 +231,13 @@ const QString &CoreConfig::getShortcutLeaderKey() const
|
|||||||
{
|
{
|
||||||
return m_shortcutLeaderKey;
|
return m_shortcutLeaderKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LineEndingPolicy CoreConfig::getLineEndingPolicy() const
|
||||||
|
{
|
||||||
|
return m_lineEnding;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CoreConfig::setLineEndingPolicy(LineEndingPolicy p_ending)
|
||||||
|
{
|
||||||
|
updateConfig(m_lineEnding, p_ending, this);
|
||||||
|
}
|
||||||
|
@ -7,6 +7,8 @@
|
|||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
|
|
||||||
|
#include "global.h"
|
||||||
|
|
||||||
namespace vnotex
|
namespace vnotex
|
||||||
{
|
{
|
||||||
class CoreConfig : public IConfig
|
class CoreConfig : public IConfig
|
||||||
@ -110,6 +112,9 @@ namespace vnotex
|
|||||||
|
|
||||||
const QString &getShortcutLeaderKey() const;
|
const QString &getShortcutLeaderKey() const;
|
||||||
|
|
||||||
|
LineEndingPolicy getLineEndingPolicy() const;
|
||||||
|
void setLineEndingPolicy(LineEndingPolicy p_ending);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class MainConfig;
|
friend class MainConfig;
|
||||||
|
|
||||||
@ -150,6 +155,8 @@ namespace vnotex
|
|||||||
// Whether store history in each notebook.
|
// Whether store history in each notebook.
|
||||||
bool m_perNotebookHistoryEnabled = false;
|
bool m_perNotebookHistoryEnabled = false;
|
||||||
|
|
||||||
|
LineEndingPolicy m_lineEnding = LineEndingPolicy::LF;
|
||||||
|
|
||||||
static QStringList s_availableLocales;
|
static QStringList s_availableLocales;
|
||||||
};
|
};
|
||||||
} // ns vnotex
|
} // ns vnotex
|
||||||
|
@ -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
|
EditorConfig::AutoSavePolicy EditorConfig::getAutoSavePolicy() const
|
||||||
{
|
{
|
||||||
return m_autoSavePolicy;
|
return m_autoSavePolicy;
|
||||||
|
@ -149,9 +149,6 @@ namespace vnotex
|
|||||||
QString autoSavePolicyToString(AutoSavePolicy p_policy) const;
|
QString autoSavePolicyToString(AutoSavePolicy p_policy) const;
|
||||||
AutoSavePolicy stringToAutoSavePolicy(const QString &p_str) 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);
|
void loadImageHost(const QJsonObject &p_app, const QJsonObject &p_user);
|
||||||
|
|
||||||
QJsonObject saveImageHost() const;
|
QJsonObject saveImageHost() const;
|
||||||
|
3
src/core/global.cpp
Normal file
3
src/core/global.cpp
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
#include "global.h"
|
||||||
|
|
||||||
|
using namespace vnotex;
|
@ -140,6 +140,44 @@ namespace vnotex
|
|||||||
CR
|
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
|
enum Role
|
||||||
{
|
{
|
||||||
// Qt::UserRole = 0x0100
|
// Qt::UserRole = 0x0100
|
||||||
|
@ -6,9 +6,13 @@
|
|||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
|
|
||||||
|
#include <vtextedit/textutils.h>
|
||||||
|
|
||||||
#include <utils/pathutils.h>
|
#include <utils/pathutils.h>
|
||||||
#include "exception.h"
|
|
||||||
#include <utils/fileutils.h>
|
#include <utils/fileutils.h>
|
||||||
|
#include "exception.h"
|
||||||
|
#include "coreconfig.h"
|
||||||
|
#include "configmgr.h"
|
||||||
|
|
||||||
using namespace vnotex;
|
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)
|
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)
|
QString LocalNotebookBackend::readTextFile(const QString &p_filePath)
|
||||||
|
@ -77,7 +77,9 @@
|
|||||||
"check_for_updates_on_start" : true,
|
"check_for_updates_on_start" : true,
|
||||||
"//comment" : "Max count of the history items for each notebook and session config",
|
"//comment" : "Max count of the history items for each notebook and session config",
|
||||||
"history_max_count" : 100,
|
"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" : {
|
"editor" : {
|
||||||
"core": {
|
"core": {
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <QCheckBox>
|
#include <QCheckBox>
|
||||||
#include <QFormLayout>
|
#include <QFormLayout>
|
||||||
|
#include <QComboBox>
|
||||||
|
|
||||||
#include <widgets/widgetsfactory.h>
|
#include <widgets/widgetsfactory.h>
|
||||||
#include <core/coreconfig.h>
|
#include <core/coreconfig.h>
|
||||||
@ -30,6 +31,22 @@ void NoteManagementPage::setupUI()
|
|||||||
connect(m_perNotebookHistoryCheckBox, &QCheckBox::stateChanged,
|
connect(m_perNotebookHistoryCheckBox, &QCheckBox::stateChanged,
|
||||||
this, &NoteManagementPage::pageIsChanged);
|
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<int>::of(&QComboBox::currentIndexChanged),
|
||||||
|
this, &NoteManagementPage::pageIsChanged);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void NoteManagementPage::loadInternal()
|
void NoteManagementPage::loadInternal()
|
||||||
@ -37,6 +54,14 @@ void NoteManagementPage::loadInternal()
|
|||||||
const auto &coreConfig = ConfigMgr::getInst().getCoreConfig();
|
const auto &coreConfig = ConfigMgr::getInst().getCoreConfig();
|
||||||
|
|
||||||
m_perNotebookHistoryCheckBox->setChecked(coreConfig.isPerNotebookHistoryEnabled());
|
m_perNotebookHistoryCheckBox->setChecked(coreConfig.isPerNotebookHistoryEnabled());
|
||||||
|
|
||||||
|
{
|
||||||
|
int idx = m_lineEndingComboBox->findData(static_cast<int>(coreConfig.getLineEndingPolicy()));
|
||||||
|
if (idx == -1) {
|
||||||
|
idx = 0;
|
||||||
|
}
|
||||||
|
m_lineEndingComboBox->setCurrentIndex(idx);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NoteManagementPage::saveInternal()
|
bool NoteManagementPage::saveInternal()
|
||||||
@ -45,6 +70,11 @@ bool NoteManagementPage::saveInternal()
|
|||||||
|
|
||||||
coreConfig.setPerNotebookHistoryEnabled(m_perNotebookHistoryCheckBox->isChecked());
|
coreConfig.setPerNotebookHistoryEnabled(m_perNotebookHistoryCheckBox->isChecked());
|
||||||
|
|
||||||
|
{
|
||||||
|
auto ending = m_lineEndingComboBox->currentData().toInt();
|
||||||
|
coreConfig.setLineEndingPolicy(static_cast<LineEndingPolicy>(ending));
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include "settingspage.h"
|
#include "settingspage.h"
|
||||||
|
|
||||||
class QCheckBox;
|
class QCheckBox;
|
||||||
|
class QComboBox;
|
||||||
|
|
||||||
namespace vnotex
|
namespace vnotex
|
||||||
{
|
{
|
||||||
@ -24,6 +25,8 @@ namespace vnotex
|
|||||||
void setupUI();
|
void setupUI();
|
||||||
|
|
||||||
QCheckBox *m_perNotebookHistoryCheckBox = nullptr;
|
QCheckBox *m_perNotebookHistoryCheckBox = nullptr;
|
||||||
|
|
||||||
|
QComboBox *m_lineEndingComboBox = nullptr;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2302,7 +2302,11 @@ void NotebookNodeExplorer::updateSlaveExplorer()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
masterNode = getCurrentMasterNode();
|
auto data = getItemNodeData(item);
|
||||||
|
if (data.isNode()) {
|
||||||
|
masterNode = data.getNode();
|
||||||
|
Q_ASSERT(masterNode->isContainer());
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Root node.
|
// Root node.
|
||||||
masterNode = m_notebook ? m_notebook->getRootNode().data() : nullptr;
|
masterNode = m_notebook ? m_notebook->getRootNode().data() : nullptr;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user