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 <shawn.rutledge@digia.com>
This commit is contained in:
Allan Sandfeld Jensen 2014-09-08 15:58:03 +02:00
parent a2b453502c
commit 710530bc55
5 changed files with 79 additions and 24 deletions

View File

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

View File

@ -271,6 +271,7 @@ protected:
QRectF blockBoundingGeometry(const QTextBlock &block) const;
QAbstractTextDocumentLayout::PaintContext getPaintContext() const;
void zoomInF(float range);
private:
Q_DISABLE_COPY(QPlainTextEdit)

View File

@ -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);
}
/*!

View File

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

View File

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