From e06ce877108e1a390bd27f412d5174f870f24725 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Tue, 15 Jan 2019 20:41:22 +0800 Subject: [PATCH] add config enable_smart_table to control whether format tables automatically --- src/resources/vnote.ini | 3 +++ src/vconfigmanager.cpp | 2 ++ src/vconfigmanager.h | 21 +++++++++++++++++++++ src/vmainwindow.cpp | 11 +++++++++++ src/vtablehelper.cpp | 7 ++++++- 5 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/resources/vnote.ini b/src/resources/vnote.ini index bc8ca41d..9c554f3c 100644 --- a/src/resources/vnote.ini +++ b/src/resources/vnote.ini @@ -324,6 +324,9 @@ auto_scroll_cursor_line=1 ; Editor font family to override the value set by the style editor_font_family= +; Whether enable smart table +enable_smart_table=true + [export] ; Path of the wkhtmltopdf tool wkhtmltopdf=wkhtmltopdf diff --git a/src/vconfigmanager.cpp b/src/vconfigmanager.cpp index e3f85c2b..57f415b7 100644 --- a/src/vconfigmanager.cpp +++ b/src/vconfigmanager.cpp @@ -371,6 +371,8 @@ void VConfigManager::initEditorConfigs() m_autoScrollCursorLine = getConfigFromSettings(section, "auto_scroll_cursor_line").toInt(); m_editorFontFamily = getConfigFromSettings(section, "editor_font_family").toString(); + + m_enableSmartTable = getConfigFromSettings(section, "enable_smart_table").toBool(); } void VConfigManager::initMarkdownConfigs() diff --git a/src/vconfigmanager.h b/src/vconfigmanager.h index 8a561397..6a2f3284 100644 --- a/src/vconfigmanager.h +++ b/src/vconfigmanager.h @@ -628,6 +628,9 @@ public: bool getPrependDotInRelativePath() const; void setPrependDotInRelativePath(bool p_enabled); + bool getEnableSmartTable() const; + void setEnableSmartTable(bool p_enabled); + private: void initEditorConfigs(); @@ -1112,6 +1115,9 @@ private: // Whether prepend a dot in the relative path of images and attachments. bool m_prependDotInRelativePath; + // Whether enable smart table. + bool m_enableSmartTable; + // The name of the config file in each directory. static const QString c_dirConfigFile; @@ -2898,4 +2904,19 @@ inline void VConfigManager::setPrependDotInRelativePath(bool p_enabled) m_prependDotInRelativePath = p_enabled; setConfigToSettings("markdown", "prepend_dot_in_relative_path", m_prependDotInRelativePath); } + +inline bool VConfigManager::getEnableSmartTable() const +{ + return m_enableSmartTable; +} + +inline void VConfigManager::setEnableSmartTable(bool p_enabled) +{ + if (m_enableSmartTable == p_enabled) { + return; + } + + m_enableSmartTable = p_enabled; + setConfigToSettings("editor", "enable_smart_table", m_enableSmartTable); +} #endif // VCONFIGMANAGER_H diff --git a/src/vmainwindow.cpp b/src/vmainwindow.cpp index b9be214e..02cd9bce 100644 --- a/src/vmainwindow.cpp +++ b/src/vmainwindow.cpp @@ -1322,6 +1322,17 @@ void VMainWindow::initEditMenu() tabAct->setChecked(g_config->getEnableTabHighlight()); initAutoScrollCursorLineMenu(editMenu); + + // Smart table. + QAction *smartTableAct = new QAction(tr("Smart Table"), this); + smartTableAct->setToolTip(tr("Format table automatically")); + smartTableAct->setCheckable(true); + connect(smartTableAct, &QAction::triggered, + this, [](bool p_checked) { + g_config->setEnableSmartTable(p_checked); + }); + editMenu->addAction(smartTableAct); + smartTableAct->setChecked(g_config->getEnableSmartTable()); } void VMainWindow::initDockWindows() diff --git a/src/vtablehelper.cpp b/src/vtablehelper.cpp index 0e5a7505..6e060736 100644 --- a/src/vtablehelper.cpp +++ b/src/vtablehelper.cpp @@ -1,6 +1,9 @@ #include "vtablehelper.h" #include "veditor.h" +#include "vconfigmanager.h" + +extern VConfigManager *g_config; VTableHelper::VTableHelper(VEditor *p_editor, QObject *p_parent) : QObject(p_parent), @@ -10,7 +13,9 @@ VTableHelper::VTableHelper(VEditor *p_editor, QObject *p_parent) void VTableHelper::updateTableBlocks(const QVector &p_blocks) { - if (m_editor->isReadOnlyW() || !m_editor->isModified()) { + if (m_editor->isReadOnlyW() || + !m_editor->isModified() || + !g_config->getEnableSmartTable()) { return; }