From 0e724635b3b7eedb9bda2bdd56d1264957484c94 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Fri, 13 Jul 2018 22:29:24 +0800 Subject: [PATCH] MdEditor: fix the font style issue Font family may be constrained by QWidget style in qss file. --- src/vconfigmanager.h | 8 ++++---- src/vlinenumberarea.h | 6 ++++++ src/vmdeditor.cpp | 22 +++++++++++++++++++--- src/vtextedit.cpp | 5 +++++ src/vtextedit.h | 2 ++ 5 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/vconfigmanager.h b/src/vconfigmanager.h index fe2d5418..4aa4c34c 100644 --- a/src/vconfigmanager.h +++ b/src/vconfigmanager.h @@ -95,9 +95,9 @@ public: // Reset the layout. void resetLayoutConfigurations(); - QFont getMdEditFont() const; + const QFont &getMdEditFont() const; - QPalette getMdEditPalette() const; + const QPalette &getMdEditPalette() const; QVector getMdHighlightingStyles() const; @@ -984,12 +984,12 @@ private: }; -inline QFont VConfigManager::getMdEditFont() const +inline const QFont &VConfigManager::getMdEditFont() const { return mdEditFont; } -inline QPalette VConfigManager::getMdEditPalette() const +inline const QPalette &VConfigManager::getMdEditPalette() const { return mdEditPalette; } diff --git a/src/vlinenumberarea.h b/src/vlinenumberarea.h index 32df4204..8d5d25ef 100644 --- a/src/vlinenumberarea.h +++ b/src/vlinenumberarea.h @@ -42,6 +42,8 @@ public: return QSize(calculateWidth(), 0); } + void setDigitWidth(int p_width); + int calculateWidth() const; const QColor &getBackgroundColor() const; @@ -86,4 +88,8 @@ inline void VLineNumberArea::setForegroundColor(const QColor &p_color) m_foregroundColor = p_color; } +inline void VLineNumberArea::setDigitWidth(int p_width) +{ + m_digitWidth = p_width; +} #endif // VLINENUMBERAREA_H diff --git a/src/vmdeditor.cpp b/src/vmdeditor.cpp index 9b0eefb4..92c14790 100644 --- a/src/vmdeditor.cpp +++ b/src/vmdeditor.cpp @@ -111,11 +111,27 @@ VMdEditor::VMdEditor(VFile *p_file, void VMdEditor::updateFontAndPalette() { - setFont(g_config->getMdEditFont()); - setPalette(g_config->getMdEditPalette()); + QFont font(g_config->getMdEditFont()); + setFont(font); + + const QPalette &palette = g_config->getMdEditPalette(); + setPalette(palette); // setPalette() won't change the foreground. - setTextColor(g_config->getMdEditPalette().color(QPalette::Text)); + setTextColor(palette.color(QPalette::Text)); + + // Only this could override the font-family set of QWidget in QSS. + // May be need to reset all the stylesheet? + setStyleSheet(QString("font-family: \"%1\";" + "font-size: %2pt;" + "color: %3;" + "background-color: %4;") + .arg(font.family()) + .arg(font.pointSize()) + .arg(palette.color(QPalette::Text).name()) + .arg(palette.color(QPalette::Base).name())); + + updateLineNumberAreaWidth(fontMetrics()); } void VMdEditor::beginEdit() diff --git a/src/vtextedit.cpp b/src/vtextedit.cpp index 4de52a9d..4fad8650 100644 --- a/src/vtextedit.cpp +++ b/src/vtextedit.cpp @@ -442,3 +442,8 @@ void VTextEdit::setDisplayScaleFactor(qreal p_factor) : m_defaultCursorWidth); getLayout()->setCursorWidth(m_defaultCursorWidth); } + +void VTextEdit::updateLineNumberAreaWidth(const QFontMetrics &p_metrics) +{ + m_lineNumberArea->setDigitWidth(p_metrics.width(QLatin1Char('8'))); +} diff --git a/src/vtextedit.h b/src/vtextedit.h index e7ea94ff..0506a3dd 100644 --- a/src/vtextedit.h +++ b/src/vtextedit.h @@ -81,6 +81,8 @@ protected: // Return the Y offset of the content via the scrollbar. int contentOffsetY() const; + void updateLineNumberAreaWidth(const QFontMetrics &p_metrics); + private slots: // Update viewport margin to hold the line number area. void updateLineNumberAreaMargin();