Editor: support specifying font via settings to override style

This commit is contained in:
Le Tan 2018-11-02 19:54:23 +08:00
parent ee6059ff7c
commit a21a1e723a
17 changed files with 176 additions and 13 deletions

View File

@ -15,7 +15,8 @@
extern VConfigManager *g_config; extern VConfigManager *g_config;
VSettingsDialog::VSettingsDialog(QWidget *p_parent) VSettingsDialog::VSettingsDialog(QWidget *p_parent)
: QDialog(p_parent) : QDialog(p_parent),
m_needUpdateEditorFont(false)
{ {
m_tabList = new QListWidget(this); m_tabList = new QListWidget(this);
m_tabList->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred); m_tabList->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
@ -61,7 +62,7 @@ VSettingsDialog::VSettingsDialog(QWidget *p_parent)
// Add tabs. // Add tabs.
addTab(new VGeneralTab(), tr("General")); addTab(new VGeneralTab(), tr("General"));
addTab(new VLookTab(), tr("Appearance")); 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 VNoteManagementTab(), tr("Note Management"));
addTab(new VMarkdownTab(), tr("Markdown")); addTab(new VMarkdownTab(), tr("Markdown"));
addTab(new VMiscTab(), tr("Misc")); addTab(new VMiscTab(), tr("Misc"));
@ -633,8 +634,9 @@ bool VLookTab::saveToolBarIconSize()
return true; return true;
} }
VReadEditTab::VReadEditTab(QWidget *p_parent) VReadEditTab::VReadEditTab(VSettingsDialog *p_dlg, QWidget *p_parent)
: QWidget(p_parent) : QWidget(p_parent),
m_settingsDlg(p_dlg)
{ {
m_readBox = new QGroupBox(tr("Read Mode (For Markdown Only)")); m_readBox = new QGroupBox(tr("Read Mode (For Markdown Only)"));
m_editBox = new QGroupBox(tr("Edit Mode")); 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 = new QCheckBox(tr("Smart input method in Vim mode"));
m_smartIM->setToolTip(tr("Disable input method when leaving Insert mode 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. // Editor zoom delta.
m_editorZoomDeltaSpin = new QSpinBox(); m_editorZoomDeltaSpin = new QSpinBox();
m_editorZoomDeltaSpin->setToolTip(tr("Set the zoom delta of the editor font")); 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->addRow(tr("Key mode:"), m_keyModeCB);
editLayout->addWidget(m_smartIM); editLayout->addWidget(m_smartIM);
editLayout->addRow(tr("Editor zoom delta:"), m_editorZoomDeltaSpin); editLayout->addRow(tr("Editor zoom delta:"), m_editorZoomDeltaSpin);
editLayout->addRow(editorFontLayout);
m_editBox->setLayout(editLayout); m_editBox->setLayout(editLayout);
m_smartIM->hide(); m_smartIM->hide();
@ -748,6 +763,10 @@ bool VReadEditTab::loadConfiguration()
return false; return false;
} }
if (!loadEditorFontFamily()) {
return false;
}
if (!loadKeyMode()) { if (!loadKeyMode()) {
return false; return false;
} }
@ -777,6 +796,10 @@ bool VReadEditTab::saveConfiguration()
return false; return false;
} }
if (!saveEditorFontFamily()) {
return false;
}
if (!saveKeyMode()) { if (!saveKeyMode()) {
return false; return false;
} }
@ -826,6 +849,32 @@ bool VReadEditTab::saveEditorZoomDelta()
return true; 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() bool VReadEditTab::loadFlashAnchor()
{ {
m_flashAnchor->setChecked(g_config->getEnableFlashAnchor()); m_flashAnchor->setChecked(g_config->getEnableFlashAnchor());

View File

@ -16,6 +16,9 @@ class QStackedLayout;
class QListWidget; class QListWidget;
class QPlainTextEdit; class QPlainTextEdit;
class QVBoxLayout; class QVBoxLayout;
class QFontComboBox;
class VSettingsDialog;
class VGeneralTab : public QWidget class VGeneralTab : public QWidget
{ {
@ -87,7 +90,8 @@ class VReadEditTab : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit VReadEditTab(QWidget *p_parent = 0); explicit VReadEditTab(VSettingsDialog *p_dlg, QWidget *p_parent = 0);
bool loadConfiguration(); bool loadConfiguration();
bool saveConfiguration(); bool saveConfiguration();
@ -112,6 +116,11 @@ private:
bool loadEditorZoomDelta(); bool loadEditorZoomDelta();
bool saveEditorZoomDelta(); bool saveEditorZoomDelta();
bool loadEditorFontFamily();
bool saveEditorFontFamily();
VSettingsDialog *m_settingsDlg;
// Web zoom factor. // Web zoom factor.
QCheckBox *m_customWebZoom; QCheckBox *m_customWebZoom;
QDoubleSpinBox *m_webZoomFactorSpin; QDoubleSpinBox *m_webZoomFactorSpin;
@ -134,6 +143,10 @@ private:
// Editor zoom delta. // Editor zoom delta.
QSpinBox *m_editorZoomDeltaSpin; QSpinBox *m_editorZoomDeltaSpin;
// Editor font family.
QCheckBox *m_customEditorFont;
QFontComboBox *m_editorFontFamilyCB;
QGroupBox *m_readBox; QGroupBox *m_readBox;
QGroupBox *m_editBox; QGroupBox *m_editBox;
}; };
@ -255,6 +268,9 @@ class VSettingsDialog : public QDialog
public: public:
explicit VSettingsDialog(QWidget *p_parent = 0); explicit VSettingsDialog(QWidget *p_parent = 0);
void setNeedUpdateEditorFont(bool p_need);
bool getNeedUpdateEditorFont() const;
private slots: private slots:
void saveConfiguration(); void saveConfiguration();
@ -276,6 +292,17 @@ private:
// Reset the layout. // Reset the layout.
QPushButton *m_resetLayoutBtn; 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 #endif // VSETTINGSDIALOG_H

View File

@ -161,6 +161,7 @@ tooltip_fg=@master_fg
; Toolbar. ; Toolbar.
toolbar_bg=@main_bg toolbar_bg=@main_bg
toolbar_separator_bg=@separator_bg toolbar_separator_bg=@separator_bg
toolbar_extension_bg=@base_fg
toolbutton_hover_bg=@hover_bg toolbutton_hover_bg=@hover_bg
toolbutton_pressed_bg=@pressed_bg toolbutton_pressed_bg=@pressed_bg
toolbutton_checked_bg=@selected_bg toolbutton_checked_bg=@selected_bg

View File

@ -146,6 +146,10 @@ QToolButton::menu-arrow {
width: $16px; width: $16px;
height: $16px; height: $16px;
} }
QToolBarExtension {
background: @toolbar_extension_bg;
}
/* End QToolButton*/ /* End QToolButton*/
/* DockWidget */ /* DockWidget */

View File

@ -159,6 +159,7 @@ tooltip_fg=@master_fg
; Toolbar. ; Toolbar.
toolbar_bg=@main_bg toolbar_bg=@main_bg
toolbar_separator_bg=@separator_bg toolbar_separator_bg=@separator_bg
toolbar_extension_bg=@base_fg
toolbutton_hover_bg=@hover_bg toolbutton_hover_bg=@hover_bg
toolbutton_pressed_bg=@pressed_bg toolbutton_pressed_bg=@pressed_bg
toolbutton_checked_bg=@selected_bg toolbutton_checked_bg=@selected_bg

View File

@ -146,6 +146,10 @@ QToolButton::menu-arrow {
width: $16px; width: $16px;
height: $16px; height: $16px;
} }
QToolBarExtension {
background: @toolbar_extension_bg;
}
/* End QToolButton*/ /* End QToolButton*/
/* DockWidget */ /* DockWidget */

View File

@ -310,6 +310,9 @@ enable_extra_buffer=true
; 2 - always ; 2 - always
auto_scroll_cursor_line=1 auto_scroll_cursor_line=1
; Editor font family to override the value set by the style
editor_font_family=
[export] [export]
; Path of the wkhtmltopdf tool ; Path of the wkhtmltopdf tool
wkhtmltopdf=wkhtmltopdf wkhtmltopdf=wkhtmltopdf

View File

@ -365,6 +365,8 @@ void VConfigManager::initEditorConfigs()
m_enableExtraBuffer = getConfigFromSettings("editor", "enable_extra_buffer").toBool(); m_enableExtraBuffer = getConfigFromSettings("editor", "enable_extra_buffer").toBool();
m_autoScrollCursorLine = getConfigFromSettings("editor", "auto_scroll_cursor_line").toInt(); m_autoScrollCursorLine = getConfigFromSettings("editor", "auto_scroll_cursor_line").toInt();
m_editorFontFamily = getConfigFromSettings("editor", "editor_font_family").toString();
} }
void VConfigManager::initSettings() void VConfigManager::initSettings()

View File

@ -118,7 +118,7 @@ public:
// Reset the layout. // Reset the layout.
void resetLayoutConfigurations(); void resetLayoutConfigurations();
const QFont &getMdEditFont() const; QFont getMdEditFont() const;
const QPalette &getMdEditPalette() const; const QPalette &getMdEditPalette() const;
@ -599,6 +599,9 @@ public:
int getAutoScrollCursorLine() const; int getAutoScrollCursorLine() const;
void setAutoScrollCursorLine(int p_mode); void setAutoScrollCursorLine(int p_mode);
const QString &getEditorFontFamily() const;
void setEditorFontFamily(const QString &p_font);
private: private:
// Look up a config from user and default settings. // Look up a config from user and default settings.
QVariant getConfigFromSettings(const QString &section, const QString &key) const; QVariant getConfigFromSettings(const QString &section, const QString &key) const;
@ -1072,6 +1075,9 @@ private:
// 2 - always // 2 - always
int m_autoScrollCursorLine; 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. // The name of the config file in each directory.
static const QString c_dirConfigFile; static const QString c_dirConfigFile;
@ -1126,9 +1132,15 @@ private:
}; };
inline const QFont &VConfigManager::getMdEditFont() const inline QFont VConfigManager::getMdEditFont() const
{ {
if (m_editorFontFamily.isEmpty()) {
return mdEditFont; return mdEditFont;
} else {
QFont font(mdEditFont);
font.setFamily(m_editorFontFamily);
return font;
}
} }
inline const QPalette &VConfigManager::getMdEditPalette() const 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); 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 #endif // VCONFIGMANAGER_H

View File

@ -624,6 +624,17 @@ QVector<VEditTabInfo> VEditArea::getAllTabsInfo() const
return tabs; 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 VEditArea::windowIndex(const VEditWindow *p_window) const
{ {
int nrWin = splitter->count(); int nrWin = splitter->count();

View File

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

View File

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

View File

@ -181,6 +181,10 @@ public:
virtual void insertCompletion(const QString &p_prefix, const QString &p_completion); virtual void insertCompletion(const QString &p_prefix, const QString &p_completion);
QFont getFont() const;
virtual void updateFontAndPalette() = 0;
// Wrapper functions for QPlainTextEdit/QTextEdit. // Wrapper functions for QPlainTextEdit/QTextEdit.
// Ends with W to distinguish it from the original interfaces. // Ends with W to distinguish it from the original interfaces.
public: public:
@ -235,8 +239,6 @@ public:
protected: protected:
void init(); void init();
virtual void updateFontAndPalette() = 0;
// Update m_config according to VConfigManager. // Update m_config according to VConfigManager.
void updateEditConfig(); void updateEditConfig();

View File

@ -3,6 +3,7 @@
#include <QPrinter> #include <QPrinter>
#include <QPrintDialog> #include <QPrintDialog>
#include <QPainter> #include <QPainter>
#include "vmainwindow.h" #include "vmainwindow.h"
#include "vdirectorytree.h" #include "vdirectorytree.h"
#include "vnote.h" #include "vnote.h"
@ -49,6 +50,7 @@
#include "vexplorer.h" #include "vexplorer.h"
#include "vlistue.h" #include "vlistue.h"
#include "vtagexplorer.h" #include "vtagexplorer.h"
#include "vmdeditor.h"
extern VConfigManager *g_config; extern VConfigManager *g_config;
@ -2429,7 +2431,11 @@ void VMainWindow::openFindDialog()
void VMainWindow::viewSettings() void VMainWindow::viewSettings()
{ {
VSettingsDialog settingsDialog(this); VSettingsDialog settingsDialog(this);
settingsDialog.exec(); if (settingsDialog.exec()) {
if (settingsDialog.getNeedUpdateEditorFont()) {
updateFontOfAllTabs();
}
}
} }
void VMainWindow::closeCurrentFile() void VMainWindow::closeCurrentFile()
@ -3438,3 +3444,14 @@ void VMainWindow::restartVNote()
{ {
QCoreApplication::exit(RESTART_EXIT_CODE); 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();
}
}
}

View File

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

View File

@ -131,6 +131,7 @@ VMdEditor::VMdEditor(VFile *p_file,
void VMdEditor::updateFontAndPalette() void VMdEditor::updateFontAndPalette()
{ {
QFont font(g_config->getMdEditFont()); QFont font(g_config->getMdEditFont());
font.setPointSize(font.pointSize() + m_zoomDelta);
setFont(font); setFont(font);
const QPalette &palette = g_config->getMdEditPalette(); 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) void VMdEditor::zoomPage(bool p_zoomIn, int p_range)
{ {
if (p_range == 0) {
return;
}
const int minSize = 2; const int minSize = 2;
int delta = p_zoomIn ? p_range : -p_range; int delta = p_zoomIn ? p_range : -p_range;

View File

@ -81,6 +81,8 @@ public:
void updateHeaderSequenceByConfigChange(); void updateHeaderSequenceByConfigChange();
void updateFontAndPalette() Q_DECL_OVERRIDE;
public slots: public slots:
bool jumpTitle(bool p_forward, int p_relativeLevel, int p_repeat) Q_DECL_OVERRIDE; 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); void requestHtmlToText(const QString &p_html, int p_id, int p_timeStamp);
protected: protected:
void updateFontAndPalette() Q_DECL_OVERRIDE;
void contextMenuEvent(QContextMenuEvent *p_event) Q_DECL_OVERRIDE; void contextMenuEvent(QContextMenuEvent *p_event) Q_DECL_OVERRIDE;
// Used to implement dragging mouse with Ctrl and left button pressed to scroll. // Used to implement dragging mouse with Ctrl and left button pressed to scroll.