Просмотр исходного кода

MdEditor: fix the font style issue

Font family may be constrained by QWidget style in qss file.
Le Tan 7 лет назад
Родитель
Сommit
0e724635b3
5 измененных файлов с 36 добавлено и 7 удалено
  1. 4 4
      src/vconfigmanager.h
  2. 6 0
      src/vlinenumberarea.h
  3. 19 3
      src/vmdeditor.cpp
  4. 5 0
      src/vtextedit.cpp
  5. 2 0
      src/vtextedit.h

+ 4 - 4
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<HighlightingStyle> 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;
 }

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

+ 19 - 3
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()

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

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