From a21a1e723aa415c9e9f3e17db195e6b854cdacab Mon Sep 17 00:00:00 2001 From: Le Tan Date: Fri, 2 Nov 2018 19:54:23 +0800 Subject: [PATCH] Editor: support specifying font via settings to override style --- src/dialog/vsettingsdialog.cpp | 57 +++++++++++++++++-- src/dialog/vsettingsdialog.h | 29 +++++++++- .../themes/v_detorte/v_detorte.palette | 1 + src/resources/themes/v_detorte/v_detorte.qss | 4 ++ .../themes/v_moonlight/v_moonlight.palette | 1 + .../themes/v_moonlight/v_moonlight.qss | 4 ++ src/resources/vnote.ini | 3 + src/vconfigmanager.cpp | 2 + src/vconfigmanager.h | 34 ++++++++++- src/veditarea.cpp | 11 ++++ src/veditarea.h | 2 + src/veditor.cpp | 5 ++ src/veditor.h | 6 +- src/vmainwindow.cpp | 19 ++++++- src/vmainwindow.h | 2 + src/vmdeditor.cpp | 5 ++ src/vmdeditor.h | 4 +- 17 files changed, 176 insertions(+), 13 deletions(-) diff --git a/src/dialog/vsettingsdialog.cpp b/src/dialog/vsettingsdialog.cpp index b499d7d4..2aa8998b 100644 --- a/src/dialog/vsettingsdialog.cpp +++ b/src/dialog/vsettingsdialog.cpp @@ -15,7 +15,8 @@ extern VConfigManager *g_config; VSettingsDialog::VSettingsDialog(QWidget *p_parent) - : QDialog(p_parent) + : QDialog(p_parent), + m_needUpdateEditorFont(false) { m_tabList = new QListWidget(this); m_tabList->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred); @@ -61,7 +62,7 @@ VSettingsDialog::VSettingsDialog(QWidget *p_parent) // Add tabs. addTab(new VGeneralTab(), tr("General")); addTab(new VLookTab(), tr("Appearance")); - addTab(new VReadEditTab(), tr("Read/Edit")); + addTab(new VReadEditTab(this), tr("Read/Edit")); addTab(new VNoteManagementTab(), tr("Note Management")); addTab(new VMarkdownTab(), tr("Markdown")); addTab(new VMiscTab(), tr("Misc")); @@ -633,8 +634,9 @@ bool VLookTab::saveToolBarIconSize() return true; } -VReadEditTab::VReadEditTab(QWidget *p_parent) - : QWidget(p_parent) +VReadEditTab::VReadEditTab(VSettingsDialog *p_dlg, QWidget *p_parent) + : QWidget(p_parent), + m_settingsDlg(p_dlg) { m_readBox = new QGroupBox(tr("Read Mode (For Markdown Only)")); m_editBox = new QGroupBox(tr("Edit Mode")); @@ -685,6 +687,18 @@ VReadEditTab::VReadEditTab(QWidget *p_parent) m_smartIM = new QCheckBox(tr("Smart input method in Vim mode")); m_smartIM->setToolTip(tr("Disable input method when leaving Insert mode in Vim mode")); + // Editor font family. + m_customEditorFont = new QCheckBox(tr("Custom editor font")); + m_customEditorFont->setToolTip(tr("Set the font of editor to override style configuration")); + connect(m_customEditorFont, &QCheckBox::stateChanged, + this, [this](int p_state) { + m_editorFontFamilyCB->setEnabled(p_state == Qt::Checked); + }); + m_editorFontFamilyCB = new QFontComboBox(); + QHBoxLayout *editorFontLayout = new QHBoxLayout(); + editorFontLayout->addWidget(m_customEditorFont); + editorFontLayout->addWidget(m_editorFontFamilyCB); + // Editor zoom delta. m_editorZoomDeltaSpin = new QSpinBox(); m_editorZoomDeltaSpin->setToolTip(tr("Set the zoom delta of the editor font")); @@ -703,6 +717,7 @@ VReadEditTab::VReadEditTab(QWidget *p_parent) editLayout->addRow(tr("Key mode:"), m_keyModeCB); editLayout->addWidget(m_smartIM); editLayout->addRow(tr("Editor zoom delta:"), m_editorZoomDeltaSpin); + editLayout->addRow(editorFontLayout); m_editBox->setLayout(editLayout); m_smartIM->hide(); @@ -748,6 +763,10 @@ bool VReadEditTab::loadConfiguration() return false; } + if (!loadEditorFontFamily()) { + return false; + } + if (!loadKeyMode()) { return false; } @@ -777,6 +796,10 @@ bool VReadEditTab::saveConfiguration() return false; } + if (!saveEditorFontFamily()) { + return false; + } + if (!saveKeyMode()) { return false; } @@ -826,6 +849,32 @@ bool VReadEditTab::saveEditorZoomDelta() return true; } +bool VReadEditTab::loadEditorFontFamily() +{ + const QString &family = g_config->getEditorFontFamily(); + m_customEditorFont->setChecked(!family.isEmpty()); + + m_editorFontFamilyCB->setCurrentFont(g_config->getMdEditFont()); + m_editorFontFamilyCB->setEnabled(m_customEditorFont->isChecked()); + return true; +} + +bool VReadEditTab::saveEditorFontFamily() +{ + QString family; + if (m_customEditorFont->isChecked()) { + QFont font = m_editorFontFamilyCB->currentFont(); + family = font.family(); + } + + if (family != g_config->getEditorFontFamily()) { + g_config->setEditorFontFamily(family); + m_settingsDlg->setNeedUpdateEditorFont(true); + } + + return true; +} + bool VReadEditTab::loadFlashAnchor() { m_flashAnchor->setChecked(g_config->getEnableFlashAnchor()); diff --git a/src/dialog/vsettingsdialog.h b/src/dialog/vsettingsdialog.h index 7bb9eaa9..4540a9db 100644 --- a/src/dialog/vsettingsdialog.h +++ b/src/dialog/vsettingsdialog.h @@ -16,6 +16,9 @@ class QStackedLayout; class QListWidget; class QPlainTextEdit; class QVBoxLayout; +class QFontComboBox; + +class VSettingsDialog; class VGeneralTab : public QWidget { @@ -87,7 +90,8 @@ class VReadEditTab : public QWidget { Q_OBJECT public: - explicit VReadEditTab(QWidget *p_parent = 0); + explicit VReadEditTab(VSettingsDialog *p_dlg, QWidget *p_parent = 0); + bool loadConfiguration(); bool saveConfiguration(); @@ -112,6 +116,11 @@ private: bool loadEditorZoomDelta(); bool saveEditorZoomDelta(); + bool loadEditorFontFamily(); + bool saveEditorFontFamily(); + + VSettingsDialog *m_settingsDlg; + // Web zoom factor. QCheckBox *m_customWebZoom; QDoubleSpinBox *m_webZoomFactorSpin; @@ -134,6 +143,10 @@ private: // Editor zoom delta. QSpinBox *m_editorZoomDeltaSpin; + // Editor font family. + QCheckBox *m_customEditorFont; + QFontComboBox *m_editorFontFamilyCB; + QGroupBox *m_readBox; QGroupBox *m_editBox; }; @@ -255,6 +268,9 @@ class VSettingsDialog : public QDialog public: explicit VSettingsDialog(QWidget *p_parent = 0); + void setNeedUpdateEditorFont(bool p_need); + bool getNeedUpdateEditorFont() const; + private slots: void saveConfiguration(); @@ -276,6 +292,17 @@ private: // Reset the layout. QPushButton *m_resetLayoutBtn; + + bool m_needUpdateEditorFont; }; +inline void VSettingsDialog::setNeedUpdateEditorFont(bool p_need) +{ + m_needUpdateEditorFont = p_need; +} + +inline bool VSettingsDialog::getNeedUpdateEditorFont() const +{ + return m_needUpdateEditorFont; +} #endif // VSETTINGSDIALOG_H diff --git a/src/resources/themes/v_detorte/v_detorte.palette b/src/resources/themes/v_detorte/v_detorte.palette index c5336863..aab18d65 100644 --- a/src/resources/themes/v_detorte/v_detorte.palette +++ b/src/resources/themes/v_detorte/v_detorte.palette @@ -161,6 +161,7 @@ tooltip_fg=@master_fg ; Toolbar. toolbar_bg=@main_bg toolbar_separator_bg=@separator_bg +toolbar_extension_bg=@base_fg toolbutton_hover_bg=@hover_bg toolbutton_pressed_bg=@pressed_bg toolbutton_checked_bg=@selected_bg diff --git a/src/resources/themes/v_detorte/v_detorte.qss b/src/resources/themes/v_detorte/v_detorte.qss index f8c49fbe..b51bd7d0 100644 --- a/src/resources/themes/v_detorte/v_detorte.qss +++ b/src/resources/themes/v_detorte/v_detorte.qss @@ -146,6 +146,10 @@ QToolButton::menu-arrow { width: $16px; height: $16px; } + +QToolBarExtension { + background: @toolbar_extension_bg; +} /* End QToolButton*/ /* DockWidget */ diff --git a/src/resources/themes/v_moonlight/v_moonlight.palette b/src/resources/themes/v_moonlight/v_moonlight.palette index 395491c9..ba36f6bf 100644 --- a/src/resources/themes/v_moonlight/v_moonlight.palette +++ b/src/resources/themes/v_moonlight/v_moonlight.palette @@ -159,6 +159,7 @@ tooltip_fg=@master_fg ; Toolbar. toolbar_bg=@main_bg toolbar_separator_bg=@separator_bg +toolbar_extension_bg=@base_fg toolbutton_hover_bg=@hover_bg toolbutton_pressed_bg=@pressed_bg toolbutton_checked_bg=@selected_bg diff --git a/src/resources/themes/v_moonlight/v_moonlight.qss b/src/resources/themes/v_moonlight/v_moonlight.qss index 418c0771..caa8022a 100644 --- a/src/resources/themes/v_moonlight/v_moonlight.qss +++ b/src/resources/themes/v_moonlight/v_moonlight.qss @@ -146,6 +146,10 @@ QToolButton::menu-arrow { width: $16px; height: $16px; } + +QToolBarExtension { + background: @toolbar_extension_bg; +} /* End QToolButton*/ /* DockWidget */ diff --git a/src/resources/vnote.ini b/src/resources/vnote.ini index 04f6386c..c87161f8 100644 --- a/src/resources/vnote.ini +++ b/src/resources/vnote.ini @@ -310,6 +310,9 @@ enable_extra_buffer=true ; 2 - always auto_scroll_cursor_line=1 +; Editor font family to override the value set by the style +editor_font_family= + [export] ; Path of the wkhtmltopdf tool wkhtmltopdf=wkhtmltopdf diff --git a/src/vconfigmanager.cpp b/src/vconfigmanager.cpp index 60c03805..23587e86 100644 --- a/src/vconfigmanager.cpp +++ b/src/vconfigmanager.cpp @@ -365,6 +365,8 @@ void VConfigManager::initEditorConfigs() m_enableExtraBuffer = getConfigFromSettings("editor", "enable_extra_buffer").toBool(); m_autoScrollCursorLine = getConfigFromSettings("editor", "auto_scroll_cursor_line").toInt(); + + m_editorFontFamily = getConfigFromSettings("editor", "editor_font_family").toString(); } void VConfigManager::initSettings() diff --git a/src/vconfigmanager.h b/src/vconfigmanager.h index 942ea98d..56bef627 100644 --- a/src/vconfigmanager.h +++ b/src/vconfigmanager.h @@ -118,7 +118,7 @@ public: // Reset the layout. void resetLayoutConfigurations(); - const QFont &getMdEditFont() const; + QFont getMdEditFont() const; const QPalette &getMdEditPalette() const; @@ -599,6 +599,9 @@ public: int getAutoScrollCursorLine() const; void setAutoScrollCursorLine(int p_mode); + const QString &getEditorFontFamily() const; + void setEditorFontFamily(const QString &p_font); + private: // Look up a config from user and default settings. QVariant getConfigFromSettings(const QString §ion, const QString &key) const; @@ -1072,6 +1075,9 @@ private: // 2 - always int m_autoScrollCursorLine; + // Editor font family to override the value set by the style + QString m_editorFontFamily; + // The name of the config file in each directory. static const QString c_dirConfigFile; @@ -1126,9 +1132,15 @@ private: }; -inline const QFont &VConfigManager::getMdEditFont() const +inline QFont VConfigManager::getMdEditFont() const { - return mdEditFont; + if (m_editorFontFamily.isEmpty()) { + return mdEditFont; + } else { + QFont font(mdEditFont); + font.setFamily(m_editorFontFamily); + return font; + } } inline const QPalette &VConfigManager::getMdEditPalette() const @@ -2781,4 +2793,20 @@ inline void VConfigManager::setAutoScrollCursorLine(int p_mode) setConfigToSettings("editor", "auto_scroll_cursor_line", m_autoScrollCursorLine); } } + +inline const QString &VConfigManager::getEditorFontFamily() const +{ + return m_editorFontFamily; +} + +inline void VConfigManager::setEditorFontFamily(const QString &p_font) +{ + if (m_editorFontFamily == p_font) { + return; + } + + m_editorFontFamily = p_font; + + setConfigToSettings("editor", "editor_font_family", m_editorFontFamily); +} #endif // VCONFIGMANAGER_H diff --git a/src/veditarea.cpp b/src/veditarea.cpp index c8a240fa..54119b56 100644 --- a/src/veditarea.cpp +++ b/src/veditarea.cpp @@ -624,6 +624,17 @@ QVector VEditArea::getAllTabsInfo() const return tabs; } +QVector VEditArea::getAllTabs() const +{ + QVector tabs; + int nrWin = splitter->count(); + for (int i = 0; i < nrWin; ++i) { + tabs.append(getWindow(i)->getAllTabs()); + } + + return tabs; +} + int VEditArea::windowIndex(const VEditWindow *p_window) const { int nrWin = splitter->count(); diff --git a/src/veditarea.h b/src/veditarea.h index 383e48c8..5424797e 100644 --- a/src/veditarea.h +++ b/src/veditarea.h @@ -54,6 +54,8 @@ public: // Return VEditTabInfo of all edit tabs. QVector getAllTabsInfo() const; + QVector getAllTabs() const; + // Return the count of VEditWindow. int windowCount() const; diff --git a/src/veditor.cpp b/src/veditor.cpp index 4c237ca6..c91c217c 100644 --- a/src/veditor.cpp +++ b/src/veditor.cpp @@ -1741,3 +1741,8 @@ void VEditor::scrollCursorLineIfNecessary() makeBlockVisible(cursor.block()); } } + +QFont VEditor::getFont() const +{ + return m_editor->font(); +} diff --git a/src/veditor.h b/src/veditor.h index 6a2dc874..1e2901b5 100644 --- a/src/veditor.h +++ b/src/veditor.h @@ -181,6 +181,10 @@ public: virtual void insertCompletion(const QString &p_prefix, const QString &p_completion); + QFont getFont() const; + + virtual void updateFontAndPalette() = 0; + // Wrapper functions for QPlainTextEdit/QTextEdit. // Ends with W to distinguish it from the original interfaces. public: @@ -235,8 +239,6 @@ public: protected: void init(); - virtual void updateFontAndPalette() = 0; - // Update m_config according to VConfigManager. void updateEditConfig(); diff --git a/src/vmainwindow.cpp b/src/vmainwindow.cpp index b36c6a54..ec75b05e 100644 --- a/src/vmainwindow.cpp +++ b/src/vmainwindow.cpp @@ -3,6 +3,7 @@ #include #include #include + #include "vmainwindow.h" #include "vdirectorytree.h" #include "vnote.h" @@ -49,6 +50,7 @@ #include "vexplorer.h" #include "vlistue.h" #include "vtagexplorer.h" +#include "vmdeditor.h" extern VConfigManager *g_config; @@ -2429,7 +2431,11 @@ void VMainWindow::openFindDialog() void VMainWindow::viewSettings() { VSettingsDialog settingsDialog(this); - settingsDialog.exec(); + if (settingsDialog.exec()) { + if (settingsDialog.getNeedUpdateEditorFont()) { + updateFontOfAllTabs(); + } + } } void VMainWindow::closeCurrentFile() @@ -3438,3 +3444,14 @@ void VMainWindow::restartVNote() { QCoreApplication::exit(RESTART_EXIT_CODE); } + +void VMainWindow::updateFontOfAllTabs() +{ + QVector tabs = m_editArea->getAllTabs(); + for (auto tab : tabs) { + const VMdTab *mdTab = dynamic_cast(tab); + if (mdTab && mdTab->getEditor()) { + mdTab->getEditor()->updateFontAndPalette(); + } + } +} diff --git a/src/vmainwindow.h b/src/vmainwindow.h index 64e599d4..52e6ce79 100644 --- a/src/vmainwindow.h +++ b/src/vmainwindow.h @@ -319,6 +319,8 @@ private: void showNotebookPanel(); + void updateFontOfAllTabs(); + // Captain mode functions. // Popup the attachment list if it is enabled. diff --git a/src/vmdeditor.cpp b/src/vmdeditor.cpp index e47b1d90..89814993 100644 --- a/src/vmdeditor.cpp +++ b/src/vmdeditor.cpp @@ -131,6 +131,7 @@ VMdEditor::VMdEditor(VFile *p_file, void VMdEditor::updateFontAndPalette() { QFont font(g_config->getMdEditFont()); + font.setPointSize(font.pointSize() + m_zoomDelta); setFont(font); const QPalette &palette = g_config->getMdEditPalette(); @@ -1204,6 +1205,10 @@ void VMdEditor::wheelEvent(QWheelEvent *p_event) void VMdEditor::zoomPage(bool p_zoomIn, int p_range) { + if (p_range == 0) { + return; + } + const int minSize = 2; int delta = p_zoomIn ? p_range : -p_range; diff --git a/src/vmdeditor.h b/src/vmdeditor.h index 14719655..ceb451d2 100644 --- a/src/vmdeditor.h +++ b/src/vmdeditor.h @@ -81,6 +81,8 @@ public: void updateHeaderSequenceByConfigChange(); + void updateFontAndPalette() Q_DECL_OVERRIDE; + public slots: bool jumpTitle(bool p_forward, int p_relativeLevel, int p_repeat) Q_DECL_OVERRIDE; @@ -224,8 +226,6 @@ signals: void requestHtmlToText(const QString &p_html, int p_id, int p_timeStamp); protected: - void updateFontAndPalette() Q_DECL_OVERRIDE; - void contextMenuEvent(QContextMenuEvent *p_event) Q_DECL_OVERRIDE; // Used to implement dragging mouse with Ctrl and left button pressed to scroll.