Fix spin box with fine grained wheel events

Only step the value in the spin box when we have accumulated one wheel
tick worth of wheel delta.

Also fixes the obsolete contructors of QWheelEvent so they set the non
obsolete properties.

Change-Id: Ic6ea4b37afa8eec85a6ca7bdc0d919bf8fb02608
Reviewed-by: Shawn Rutledge <shawn.rutledge@digia.com>
This commit is contained in:
Allan Sandfeld Jensen 2014-09-08 16:22:29 +02:00
parent e8ef241d0f
commit dfb4af1fd3
4 changed files with 48 additions and 3 deletions

View File

@ -642,6 +642,10 @@ QWheelEvent::QWheelEvent(const QPointF &pos, int delta,
: QInputEvent(Wheel, modifiers), p(pos), qt4D(delta), qt4O(orient), mouseState(buttons)
{
g = QCursor::pos();
if (orient == Qt::Vertical)
angleD = QPoint(0, delta);
else
angleD = QPoint(delta, 0);
}
/*!
@ -670,7 +674,12 @@ QWheelEvent::QWheelEvent(const QPointF &pos, const QPointF& globalPos, int delta
Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers,
Qt::Orientation orient)
: QInputEvent(Wheel, modifiers), p(pos), g(globalPos), qt4D(delta), qt4O(orient), mouseState(buttons)
{}
{
if (orient == Qt::Vertical)
angleD = QPoint(0, delta);
else
angleD = QPoint(delta, 0);
}
/*!
Constructs a wheel event object.

View File

@ -1100,7 +1100,10 @@ void QAbstractSpinBox::keyReleaseEvent(QKeyEvent *event)
#ifndef QT_NO_WHEELEVENT
void QAbstractSpinBox::wheelEvent(QWheelEvent *event)
{
const int steps = (event->delta() > 0 ? 1 : -1);
Q_D(QAbstractSpinBox);
d->wheelDeltaRemainder += event->angleDelta().y();
const int steps = d->wheelDeltaRemainder / 120;
d->wheelDeltaRemainder -= steps * 120;
if (stepEnabled() & (steps > 0 ? StepUpEnabled : StepDownEnabled))
stepBy(event->modifiers() & Qt::ControlModifier ? steps * 10 : steps);
event->accept();
@ -1344,7 +1347,7 @@ QAbstractSpinBoxPrivate::QAbstractSpinBoxPrivate()
ignoreCursorPositionChanged(false), frame(true), accelerate(false), keyboardTracking(true),
cleared(false), ignoreUpdateEdit(false), correctionMode(QAbstractSpinBox::CorrectToPreviousValue),
acceleration(0), hoverControl(QStyle::SC_None), buttonSymbols(QAbstractSpinBox::UpDownArrows), validator(0),
showGroupSeparator(0)
showGroupSeparator(0), wheelDeltaRemainder(0)
{
}

View File

@ -151,6 +151,7 @@ public:
QAbstractSpinBox::ButtonSymbols buttonSymbols;
QSpinBoxValidator *validator;
uint showGroupSeparator : 1;
int wheelDeltaRemainder;
};
class QSpinBoxValidator : public QValidator

View File

@ -82,6 +82,12 @@ public:
{
return QSpinBox::valueFromText(text);
}
#ifndef QT_NO_WHEELEVENT
void wheelEvent(QWheelEvent *event)
{
QSpinBox::wheelEvent(event);
}
#endif
QLineEdit *lineEdit() const { return QSpinBox::lineEdit(); }
};
@ -148,6 +154,8 @@ private slots:
void setGroupSeparatorShown_data();
void setGroupSeparatorShown();
void wheelEvents();
public slots:
void valueChangedHelper(const QString &);
void valueChangedHelper(int);
@ -1190,5 +1198,29 @@ void tst_QSpinBox::setGroupSeparatorShown()
QCOMPARE(spinBox.value()+1000, 33000);
}
void tst_QSpinBox::wheelEvents()
{
#ifndef QT_NO_WHEELEVENT
SpinBox spinBox;
spinBox.setRange(-20, 20);
spinBox.setValue(0);
QWheelEvent wheelUp(QPointF(), QPointF(), QPoint(), QPoint(0, 120), 120, Qt::Vertical, Qt::NoButton, Qt::NoModifier);
spinBox.wheelEvent(&wheelUp);
QCOMPARE(spinBox.value(), 1);
QWheelEvent wheelDown(QPointF(), QPointF(), QPoint(), QPoint(0, -120), -120, Qt::Vertical, Qt::NoButton, Qt::NoModifier);
spinBox.wheelEvent(&wheelDown);
spinBox.wheelEvent(&wheelDown);
QCOMPARE(spinBox.value(), -1);
QWheelEvent wheelHalfUp(QPointF(), QPointF(), QPoint(), QPoint(0, 60), 60, Qt::Vertical, Qt::NoButton, Qt::NoModifier);
spinBox.wheelEvent(&wheelHalfUp);
QCOMPARE(spinBox.value(), -1);
spinBox.wheelEvent(&wheelHalfUp);
QCOMPARE(spinBox.value(), 0);
#endif
}
QTEST_MAIN(tst_QSpinBox)
#include "tst_qspinbox.moc"