Browse Source

Editor: support specifying font via settings to override style

Le Tan 7 years ago
parent
commit
a21a1e723a

+ 53 - 4
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());

+ 28 - 1
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

+ 1 - 0
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

+ 4 - 0
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 */

+ 1 - 0
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

+ 4 - 0
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 */

+ 3 - 0
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

+ 2 - 0
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()

+ 31 - 3
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 &section, 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

+ 11 - 0
src/veditarea.cpp

@@ -624,6 +624,17 @@ QVector<VEditTabInfo> VEditArea::getAllTabsInfo() const
     return tabs;
 }
 
+QVector<VEditTab *> VEditArea::getAllTabs() const
+{
+    QVector<VEditTab *> 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();

+ 2 - 0
src/veditarea.h

@@ -54,6 +54,8 @@ public:
     // Return VEditTabInfo of all edit tabs.
     QVector<VEditTabInfo> getAllTabsInfo() const;
 
+    QVector<VEditTab *> getAllTabs() const;
+
     // Return the count of VEditWindow.
     int windowCount() const;
 

+ 5 - 0
src/veditor.cpp

@@ -1741,3 +1741,8 @@ void VEditor::scrollCursorLineIfNecessary()
         makeBlockVisible(cursor.block());
     }
 }
+
+QFont VEditor::getFont() const
+{
+    return m_editor->font();
+}

+ 4 - 2
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();
 

+ 18 - 1
src/vmainwindow.cpp

@@ -3,6 +3,7 @@
 #include <QPrinter>
 #include <QPrintDialog>
 #include <QPainter>
+
 #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<VEditTab *> tabs = m_editArea->getAllTabs();
+    for (auto tab : tabs) {
+        const VMdTab *mdTab = dynamic_cast<const VMdTab *>(tab);
+        if (mdTab && mdTab->getEditor()) {
+            mdTab->getEditor()->updateFontAndPalette();
+        }
+    }
+}

+ 2 - 0
src/vmainwindow.h

@@ -319,6 +319,8 @@ private:
 
     void showNotebookPanel();
 
+    void updateFontOfAllTabs();
+
     // Captain mode functions.
 
     // Popup the attachment list if it is enabled.

+ 5 - 0
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;

+ 2 - 2
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.