MdEditor: fix zoomPage() bug after setting style sheet

This commit is contained in:
Le Tan 2018-07-25 06:39:31 +08:00
parent 2b01a4ccd0
commit ba0660de28
4 changed files with 44 additions and 18 deletions

View File

@ -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

View File

@ -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<HighlightingStyle> &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()));
}

View File

@ -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;

View File

@ -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)