From 710530bc553ade8c4b8b6be1872f56bdc86b5321 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Mon, 8 Sep 2014 15:58:03 +0200 Subject: [PATCH] Fix too fast zooming in QTextEdit with smooth scrolling events Do not zoom 1pt on every single wheel-event, but instead scale the zoom with the size of the angle delta. Change-Id: Idbe17356c7845ebd0039f655d3e611e71c6f0dd6 Reviewed-by: Shawn Rutledge --- src/widgets/widgets/qplaintextedit.cpp | 31 +++++++++------ src/widgets/widgets/qplaintextedit.h | 1 + src/widgets/widgets/qtextedit.cpp | 31 +++++++++------ src/widgets/widgets/qtextedit.h | 2 + .../widgets/qtextedit/tst_qtextedit.cpp | 38 +++++++++++++++++++ 5 files changed, 79 insertions(+), 24 deletions(-) diff --git a/src/widgets/widgets/qplaintextedit.cpp b/src/widgets/widgets/qplaintextedit.cpp index 3f34a08b5e..3959cb6c3f 100644 --- a/src/widgets/widgets/qplaintextedit.cpp +++ b/src/widgets/widgets/qplaintextedit.cpp @@ -2276,11 +2276,8 @@ void QPlainTextEdit::wheelEvent(QWheelEvent *e) Q_D(QPlainTextEdit); if (!(d->control->textInteractionFlags() & Qt::TextEditable)) { if (e->modifiers() & Qt::ControlModifier) { - const int delta = e->delta(); - if (delta < 0) - zoomOut(); - else if (delta > 0) - zoomIn(); + float delta = e->angleDelta().y() / 120.f; + zoomInF(delta); return; } } @@ -2300,12 +2297,7 @@ void QPlainTextEdit::wheelEvent(QWheelEvent *e) */ void QPlainTextEdit::zoomIn(int range) { - QFont f = font(); - const int newSize = f.pointSize() + range; - if (newSize <= 0) - return; - f.setPointSize(newSize); - setFont(f); + zoomInF(range); } /*! @@ -2321,7 +2313,22 @@ void QPlainTextEdit::zoomIn(int range) */ void QPlainTextEdit::zoomOut(int range) { - zoomIn(-range); + zoomInF(-range); +} + +/*! + \internal +*/ +void QPlainTextEdit::zoomInF(float range) +{ + if (range == 0.f) + return; + QFont f = font(); + const float newSize = f.pointSizeF() + range; + if (newSize <= 0) + return; + f.setPointSizeF(newSize); + setFont(f); } #ifndef QT_NO_CONTEXTMENU diff --git a/src/widgets/widgets/qplaintextedit.h b/src/widgets/widgets/qplaintextedit.h index 54cd3e14ed..f61d9c3aaa 100644 --- a/src/widgets/widgets/qplaintextedit.h +++ b/src/widgets/widgets/qplaintextedit.h @@ -271,6 +271,7 @@ protected: QRectF blockBoundingGeometry(const QTextBlock &block) const; QAbstractTextDocumentLayout::PaintContext getPaintContext() const; + void zoomInF(float range); private: Q_DISABLE_COPY(QPlainTextEdit) diff --git a/src/widgets/widgets/qtextedit.cpp b/src/widgets/widgets/qtextedit.cpp index f106b70c12..b4dd09d6d4 100644 --- a/src/widgets/widgets/qtextedit.cpp +++ b/src/widgets/widgets/qtextedit.cpp @@ -1813,11 +1813,8 @@ void QTextEdit::wheelEvent(QWheelEvent *e) Q_D(QTextEdit); if (!(d->control->textInteractionFlags() & Qt::TextEditable)) { if (e->modifiers() & Qt::ControlModifier) { - const int delta = e->delta(); - if (delta < 0) - zoomOut(); - else if (delta > 0) - zoomIn(); + float delta = e->angleDelta().y() / 120.f; + zoomInF(delta); return; } } @@ -2276,12 +2273,7 @@ void QTextEdit::scrollToAnchor(const QString &name) */ void QTextEdit::zoomIn(int range) { - QFont f = font(); - const int newSize = f.pointSize() + range; - if (newSize <= 0) - return; - f.setPointSize(newSize); - setFont(f); + zoomInF(range); } /*! @@ -2297,7 +2289,22 @@ void QTextEdit::zoomIn(int range) */ void QTextEdit::zoomOut(int range) { - zoomIn(-range); + zoomInF(-range); +} + +/*! + \internal +*/ +void QTextEdit::zoomInF(float range) +{ + if (range == 0.f) + return; + QFont f = font(); + const float newSize = f.pointSizeF() + range; + if (newSize <= 0) + return; + f.setPointSizeF(newSize); + setFont(f); } /*! diff --git a/src/widgets/widgets/qtextedit.h b/src/widgets/widgets/qtextedit.h index a283a51b90..d23d2d59ce 100644 --- a/src/widgets/widgets/qtextedit.h +++ b/src/widgets/widgets/qtextedit.h @@ -304,6 +304,8 @@ protected: virtual void scrollContentsBy(int dx, int dy); virtual void doSetTextCursor(const QTextCursor &cursor); + void zoomInF(float range); + private: Q_DISABLE_COPY(QTextEdit) Q_PRIVATE_SLOT(d_func(), void _q_repaintContents(const QRectF &r)) diff --git a/tests/auto/widgets/widgets/qtextedit/tst_qtextedit.cpp b/tests/auto/widgets/widgets/qtextedit/tst_qtextedit.cpp index fbd8b46466..ea27405fb3 100644 --- a/tests/auto/widgets/widgets/qtextedit/tst_qtextedit.cpp +++ b/tests/auto/widgets/widgets/qtextedit/tst_qtextedit.cpp @@ -211,6 +211,10 @@ private slots: void findWithRegExpReturnsFalseIfNoMoreResults(); #endif +#ifndef QT_NO_WHEELEVENT + void wheelEvent(); +#endif + private: void createSelection(); int blockCount() const; @@ -2564,5 +2568,39 @@ void tst_QTextEdit::findWithRegExpReturnsFalseIfNoMoreResults() } #endif +#ifndef QT_NO_WHEELEVENT + +class TextEdit : public QTextEdit +{ +public: + TextEdit(QWidget *parent = 0) + : QTextEdit(parent) + {} + void wheelEvent(QWheelEvent *event) + { + QTextEdit::wheelEvent(event); + } +}; + +void tst_QTextEdit::wheelEvent() +{ + TextEdit ed(0); + ed.setPlainText(QStringLiteral("Line\nLine\nLine\n")); + ed.setReadOnly(true); + + float defaultFontSize = ed.font().pointSizeF(); + QWheelEvent wheelUp(QPointF(), QPointF(), QPoint(), QPoint(0, 120), 120, Qt::Vertical, Qt::NoButton, Qt::ControlModifier); + ed.wheelEvent(&wheelUp); + + QCOMPARE(defaultFontSize + 1, ed.font().pointSizeF()); + + QWheelEvent wheelHalfDown(QPointF(), QPointF(), QPoint(), QPoint(0, -60), -60, Qt::Vertical, Qt::NoButton, Qt::ControlModifier); + ed.wheelEvent(&wheelHalfDown); + + QCOMPARE(defaultFontSize + 0.5, ed.font().pointSizeF()); +} + +#endif + QTEST_MAIN(tst_QTextEdit) #include "tst_qtextedit.moc"