From 13d0c60e59f68bdc009a793336c4ba4d63f56431 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Tue, 4 Jun 2019 20:38:10 +0800 Subject: [PATCH] add config table_format_interval for smart table --- src/resources/vnote.ini | 3 +++ src/vconfigmanager.cpp | 2 ++ src/vconfigmanager.h | 10 ++++++++++ src/vmainwindow.cpp | 4 +++- src/vtablehelper.cpp | 35 +++++++++++++++++++++++++++-------- src/vtablehelper.h | 7 +++++++ 6 files changed, 52 insertions(+), 9 deletions(-) diff --git a/src/resources/vnote.ini b/src/resources/vnote.ini index 2be16ecd..39a88dc8 100644 --- a/src/resources/vnote.ini +++ b/src/resources/vnote.ini @@ -333,6 +333,9 @@ editor_font_family= ; Whether enable smart table enable_smart_table=true +; Interval (milliseconds) to format table +table_format_interval=3000 + [export] ; Path of the wkhtmltopdf tool wkhtmltopdf=wkhtmltopdf diff --git a/src/vconfigmanager.cpp b/src/vconfigmanager.cpp index 399db63a..239939b3 100644 --- a/src/vconfigmanager.cpp +++ b/src/vconfigmanager.cpp @@ -377,6 +377,8 @@ void VConfigManager::initEditorConfigs() m_editorFontFamily = getConfigFromSettings(section, "editor_font_family").toString(); m_enableSmartTable = getConfigFromSettings(section, "enable_smart_table").toBool(); + + m_tableFormatIntervalMS = getConfigFromSettings(section, "table_format_interval").toInt(); } void VConfigManager::initMarkdownConfigs() diff --git a/src/vconfigmanager.h b/src/vconfigmanager.h index 3075e1ae..074d340d 100644 --- a/src/vconfigmanager.h +++ b/src/vconfigmanager.h @@ -643,6 +643,8 @@ public: QDateTime getLastStartDateTime() const; void updateLastStartDateTime(); + int getTableFormatInterval() const; + private: void initEditorConfigs(); @@ -1133,6 +1135,9 @@ private: // Whether auto locate to current tab in note list. bool m_syncNoteListToCurrentTab; + // Interval (milliseconds) to format table. + int m_tableFormatIntervalMS; + // The name of the config file in each directory. static const QString c_dirConfigFile; @@ -2959,4 +2964,9 @@ inline void VConfigManager::setSyncNoteListToTab(bool p_enabled) m_syncNoteListToCurrentTab = p_enabled; setConfigToSettings("global", "sync_note_list_to_current_tab", m_syncNoteListToCurrentTab); } + +inline int VConfigManager::getTableFormatInterval() const +{ + return m_tableFormatIntervalMS; +} #endif // VCONFIGMANAGER_H diff --git a/src/vmainwindow.cpp b/src/vmainwindow.cpp index e123e2b1..57326822 100644 --- a/src/vmainwindow.cpp +++ b/src/vmainwindow.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "vmainwindow.h" #include "vdirectorytree.h" @@ -3472,7 +3473,8 @@ void VMainWindow::kickOffStartUpTimer(const QStringList &p_files) } if (g_config->getAllowUserTrack()) { - QTimer::singleShot(30 * 1000, this, SLOT(collectUserStat())); + int interval = (30 + QRandomGenerator::global()->generate() % 60) * 1000; + QTimer::singleShot(interval, this, SLOT(collectUserStat())); } m_syncNoteListToCurrentTab = true; diff --git a/src/vtablehelper.cpp b/src/vtablehelper.cpp index 6e060736..0b104a7b 100644 --- a/src/vtablehelper.cpp +++ b/src/vtablehelper.cpp @@ -1,5 +1,7 @@ #include "vtablehelper.h" +#include + #include "veditor.h" #include "vconfigmanager.h" @@ -9,10 +11,17 @@ VTableHelper::VTableHelper(VEditor *p_editor, QObject *p_parent) : QObject(p_parent), m_editor(p_editor) { + m_timer = new QTimer(this); + m_timer->setSingleShot(true); + m_timer->setInterval(g_config->getTableFormatInterval()); + connect(m_timer, &QTimer::timeout, + this, &VTableHelper::formatTables); } void VTableHelper::updateTableBlocks(const QVector &p_blocks) { + m_timer->stop(); + if (m_editor->isReadOnlyW() || !m_editor->isModified() || !g_config->getEnableSmartTable()) { @@ -24,14 +33,8 @@ void VTableHelper::updateTableBlocks(const QVector &p_blocks) return; } - VTable table(m_editor, p_blocks[idx]); - if (!table.isValid()) { - return; - } - - table.format(); - - table.write(); + m_block = p_blocks[idx]; + m_timer->start(); } int VTableHelper::currentCursorTableBlock(const QVector &p_blocks) const @@ -66,3 +69,19 @@ void VTableHelper::insertTable(int p_nrRow, int p_nrCol, VTable::Alignment p_ali table.write(); } + +void VTableHelper::formatTables() +{ + if (!m_block.isValid()) { + return; + } + + VTable table(m_editor, m_block); + if (!table.isValid()) { + return; + } + + table.format(); + + table.write(); +} diff --git a/src/vtablehelper.h b/src/vtablehelper.h index ddc41484..3e3e3959 100644 --- a/src/vtablehelper.h +++ b/src/vtablehelper.h @@ -2,6 +2,7 @@ #define VTABLEHELPER_H #include +#include #include "markdownhighlighterdata.h" #include "vtable.h" @@ -24,7 +25,13 @@ private: // Return the block index which contains the cursor. int currentCursorTableBlock(const QVector &p_blocks) const; + void formatTables(); + VEditor *m_editor; + + QTimer *m_timer; + + VTableBlock m_block; }; #endif // VTABLEHELPER_H