Refactoring. Smooth scrolling.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2015-12-17 21:13:07 +02:00
parent 6ba9bb8b28
commit 3beb8a8b7c
2 changed files with 67 additions and 60 deletions

View File

@ -90,29 +90,19 @@ void GraphicsViewZoom::set_zoom_factor_base(double value)
void GraphicsViewZoom::scrollingTime(qreal x) void GraphicsViewZoom::scrollingTime(qreal x)
{ {
Q_UNUSED(x); Q_UNUSED(x);
qreal factor = 1.0;
if (_numScheduledScalings < 0) qreal scroll = _view->verticalScrollBar()->pageStep()/60;
if (_numScheduledScalings > 0)
{ {
factor = factor*13.8; scroll = scroll * -1;
} }
else _view->verticalScrollBar()->setValue(qRound(_view->verticalScrollBar()->value() + scroll));
{
factor = factor*-13.8;
}
_view->verticalScrollBar()->setValue(qRound(_view->verticalScrollBar()->value() + factor));
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void GraphicsViewZoom::animFinished() void GraphicsViewZoom::animFinished()
{ {
if (_numScheduledScalings > 0) _numScheduledScalings > 0 ? _numScheduledScalings-- : _numScheduledScalings++;
{
_numScheduledScalings--;
}
else
{
_numScheduledScalings++;
}
anim->stop(); anim->stop();
/* /*
@ -147,6 +137,7 @@ bool GraphicsViewZoom::eventFilter(QObject *object, QEvent *event)
target_viewport_pos = mouse_event->pos(); target_viewport_pos = mouse_event->pos();
target_scene_pos = _view->mapToScene(mouse_event->pos()); target_scene_pos = _view->mapToScene(mouse_event->pos());
} }
return false;
} }
else if (event->type() == QEvent::Wheel) else if (event->type() == QEvent::Wheel)
{ {
@ -163,7 +154,22 @@ bool GraphicsViewZoom::eventFilter(QObject *object, QEvent *event)
} }
else else
{ {
int numSteps = wheel_event->delta() / 8 / 15; // see QWheelEvent documentation const QPoint numPixels = wheel_event->pixelDelta();
const QPoint numDegrees = wheel_event->angleDelta() / 8;
int numSteps;
if (not numPixels.isNull())
{
numSteps = numPixels.y();
}
else if (not numDegrees.isNull())
{
numSteps = numDegrees.y() / 15;
}
else
{
return true;//Just ignore
}
_numScheduledScalings += numSteps; _numScheduledScalings += numSteps;
if (_numScheduledScalings * numSteps < 0) if (_numScheduledScalings * numSteps < 0)
@ -178,8 +184,8 @@ bool GraphicsViewZoom::eventFilter(QObject *object, QEvent *event)
return true; return true;
} }
} }
Q_UNUSED(object)
return false; return QObject::eventFilter(object, event);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------

View File

@ -73,6 +73,8 @@ signals:
public slots: public slots:
void scrollingTime(qreal x); void scrollingTime(qreal x);
void animFinished(); void animFinished();
protected:
virtual bool eventFilter(QObject* object, QEvent* event) Q_DECL_OVERRIDE;
private: private:
Q_DISABLE_COPY(GraphicsViewZoom) Q_DISABLE_COPY(GraphicsViewZoom)
QGraphicsView* _view; QGraphicsView* _view;
@ -84,7 +86,6 @@ private:
/** @brief _numScheduledScalings keep number scheduled scalings. */ /** @brief _numScheduledScalings keep number scheduled scalings. */
qint32 _numScheduledScalings; qint32 _numScheduledScalings;
virtual bool eventFilter(QObject* object, QEvent* event) Q_DECL_OVERRIDE;
void FictiveSceneRect(QGraphicsScene *sc, QGraphicsView *view); void FictiveSceneRect(QGraphicsScene *sc, QGraphicsView *view);
}; };