From ba0660de2881f6c734dfdc3cfb0c41b8a08c8145 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Wed, 25 Jul 2018 06:39:31 +0800 Subject: [PATCH] MdEditor: fix zoomPage() bug after setting style sheet --- src/vlinenumberarea.h | 2 ++ src/vmdeditor.cpp | 55 +++++++++++++++++++++++++++++-------------- src/vmdeditor.h | 4 ++++ src/vtextedit.cpp | 1 + 4 files changed, 44 insertions(+), 18 deletions(-) diff --git a/src/vlinenumberarea.h b/src/vlinenumberarea.h index 8d5d25ef..55155bab 100644 --- a/src/vlinenumberarea.h +++ b/src/vlinenumberarea.h @@ -91,5 +91,7 @@ inline void VLineNumberArea::setForegroundColor(const QColor &p_color) inline void VLineNumberArea::setDigitWidth(int p_width) { m_digitWidth = p_width; + m_blockCount = -1; + calculateWidth(); } #endif // VLINENUMBERAREA_H diff --git a/src/vmdeditor.cpp b/src/vmdeditor.cpp index a0526df9..eb969ad3 100644 --- a/src/vmdeditor.cpp +++ b/src/vmdeditor.cpp @@ -126,17 +126,9 @@ void VMdEditor::updateFontAndPalette() // setTextColor(palette.color(QPalette::Text)); // Only this could override the font-family set of QWidget in QSS. - setStyleSheet(QString("VMdEditor, VLineNumberArea {" - "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())); + setFontAndPaletteByStyleSheet(font, palette); - updateLineNumberAreaWidth(fontMetrics()); + updateLineNumberAreaWidth(QFontMetrics(font)); } void VMdEditor::beginEdit() @@ -284,7 +276,6 @@ void VMdEditor::makeBlockVisible(const QTextBlock &p_block) } while (y + rectHeight > height && vbar->value() < vbar->maximum()) { - moved = true; vbar->setValue(vbar->value() + vbar->singleStep()); rt = layout->blockBoundingRect(p_block); rectHeight = (int)rt.height(); @@ -1207,17 +1198,20 @@ void VMdEditor::wheelEvent(QWheelEvent *p_event) void VMdEditor::zoomPage(bool p_zoomIn, int p_range) { - int delta; const int minSize = 2; - if (p_zoomIn) { - delta = p_range; - zoomIn(p_range); - } else { - delta = -p_range; - zoomOut(p_range); + int delta = p_zoomIn ? p_range : -p_range; + + // zoomIn() and zoomOut() does not work if we set stylesheet of VMdEditor. + int ptSz = font().pointSize() + delta; + if (ptSz < minSize) { + ptSz = minSize; } + setFontPointSizeByStyleSheet(ptSz); + + emit m_object->statusMessage(QObject::tr("Set base font point size %1").arg(ptSz)); + m_zoomDelta += delta; QVector &styles = m_pegHighlighter->getStyles(); @@ -1384,3 +1378,28 @@ void VMdEditor::setEditTab(VEditTab *p_editTab) { m_editTab = p_editTab; } + +void VMdEditor::setFontPointSizeByStyleSheet(int p_ptSize) +{ + QFont ft = font(); + ft.setPointSize(p_ptSize); + + setFontAndPaletteByStyleSheet(ft, palette()); + + ensurePolished(); + + updateLineNumberAreaWidth(QFontMetrics(ft)); +} + +void VMdEditor::setFontAndPaletteByStyleSheet(const QFont &p_font, const QPalette &p_palette) +{ + setStyleSheet(QString("VMdEditor, VLineNumberArea {" + "font-family: \"%1\";" + "font-size: %2pt;" + "color: %3;" + "background-color: %4; }") + .arg(p_font.family()) + .arg(p_font.pointSize()) + .arg(p_palette.color(QPalette::Text).name()) + .arg(p_palette.color(QPalette::Base).name())); +} diff --git a/src/vmdeditor.h b/src/vmdeditor.h index c73cfbfb..1ddded16 100644 --- a/src/vmdeditor.h +++ b/src/vmdeditor.h @@ -256,6 +256,10 @@ private: void insertImageLink(const QString &p_text, const QString &p_url); + void setFontPointSizeByStyleSheet(int p_ptSize); + + void setFontAndPaletteByStyleSheet(const QFont &p_font, const QPalette &p_palette); + PegMarkdownHighlighter *m_pegHighlighter; VCodeBlockHighlightHelper *m_cbHighlighter; diff --git a/src/vtextedit.cpp b/src/vtextedit.cpp index 8362e845..306b586f 100644 --- a/src/vtextedit.cpp +++ b/src/vtextedit.cpp @@ -446,6 +446,7 @@ void VTextEdit::setDisplayScaleFactor(qreal p_factor) void VTextEdit::updateLineNumberAreaWidth(const QFontMetrics &p_metrics) { m_lineNumberArea->setDigitWidth(p_metrics.width(QLatin1Char('8'))); + updateLineNumberAreaMargin(); } void VTextEdit::dragMoveEvent(QDragMoveEvent *p_event)