diff --git a/ChangeLog.txt b/ChangeLog.txt index cff3f0e87..a7586e318 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1,4 +1,5 @@ # Version 0.5.0 +- Added horizontal scrolling by pressiong Shift + mouse wheel. - [#366] Update 'Point from Distance and Angle' tool to read distance and angle between points. - [#395] Create Curve tool which uses point as control handle. - pdftops updated to version 3.04. diff --git a/src/libs/vwidgets/vmaingraphicsview.cpp b/src/libs/vwidgets/vmaingraphicsview.cpp index 084fef809..aea47ee32 100644 --- a/src/libs/vwidgets/vmaingraphicsview.cpp +++ b/src/libs/vwidgets/vmaingraphicsview.cpp @@ -44,15 +44,27 @@ const int GraphicsViewZoom::updateInterval = 40; //--------------------------------------------------------------------------------------------------------------------- GraphicsViewZoom::GraphicsViewZoom(QGraphicsView* view) - : QObject(view), _view(view), _modifiers(Qt::ControlModifier), _zoom_factor_base(1.0015), - target_scene_pos(QPointF()), target_viewport_pos(QPointF()), anim(nullptr), _numScheduledScrollings(0) + : QObject(view), + _view(view), + _modifiers(Qt::ControlModifier), + _zoom_factor_base(1.0015), + target_scene_pos(QPointF()), + target_viewport_pos(QPointF()), + verticalScrollAnim(new QTimeLine(duration, this)), + _numScheduledVerticalScrollings(0), + horizontalScrollAnim(new QTimeLine(duration, this)), + _numScheduledHorizontalScrollings(0) { _view->viewport()->installEventFilter(this); _view->setMouseTracking(true); - anim = new QTimeLine(duration, this); - anim->setUpdateInterval(updateInterval); - connect(anim, &QTimeLine::valueChanged, this, &GraphicsViewZoom::scrollingTime, Qt::UniqueConnection); - connect(anim, &QTimeLine::finished, this, &GraphicsViewZoom::animFinished, Qt::UniqueConnection); + + verticalScrollAnim->setUpdateInterval(updateInterval); + connect(verticalScrollAnim, &QTimeLine::valueChanged, this, &GraphicsViewZoom::VerticalScrollingTime); + connect(verticalScrollAnim, &QTimeLine::finished, this, &GraphicsViewZoom::animFinished); + + horizontalScrollAnim->setUpdateInterval(updateInterval); + connect(horizontalScrollAnim, &QTimeLine::valueChanged, this, &GraphicsViewZoom::HorizontalScrollingTime); + connect(horizontalScrollAnim, &QTimeLine::finished, this, &GraphicsViewZoom::animFinished); } //--------------------------------------------------------------------------------------------------------------------- @@ -91,31 +103,53 @@ void GraphicsViewZoom::set_zoom_factor_base(double value) } //--------------------------------------------------------------------------------------------------------------------- -void GraphicsViewZoom::scrollingTime(qreal x) +void GraphicsViewZoom::VerticalScrollingTime(qreal x) { Q_UNUSED(x); // Try to adapt scrolling to speed of rotating mouse wheel and scale factor // Value of _numScheduledScrollings is too short, so we scale the value - qreal scroll = (qAbs(_numScheduledScrollings)*(10 + 10/_view->transform().m22()))/(duration/updateInterval); + qreal scroll = (qAbs(_numScheduledVerticalScrollings)*(10 + 10/_view->transform().m22()))/(duration/updateInterval); if (qAbs(scroll) < 1) { scroll = 1; } - if (_numScheduledScrollings > 0) + if (_numScheduledVerticalScrollings > 0) { scroll = scroll * -1; } _view->verticalScrollBar()->setValue(qRound(_view->verticalScrollBar()->value() + scroll)); } +//--------------------------------------------------------------------------------------------------------------------- +void GraphicsViewZoom::HorizontalScrollingTime(qreal x) +{ + Q_UNUSED(x); + // Try to adapt scrolling to speed of rotating mouse wheel and scale factor + // Value of _numScheduledScrollings is too short, so we scale the value + + qreal scroll = (qAbs(_numScheduledHorizontalScrollings)*(10 + 10/_view->transform().m11()))/ + (duration/updateInterval); + + if (qAbs(scroll) < 1) + { + scroll = 1; + } + + if (_numScheduledHorizontalScrollings > 0) + { + scroll = scroll * -1; + } + _view->horizontalScrollBar()->setValue(qRound(_view->horizontalScrollBar()->value() + scroll)); +} + //--------------------------------------------------------------------------------------------------------------------- void GraphicsViewZoom::animFinished() { - _numScheduledScrollings = 0; - anim->stop(); + _numScheduledVerticalScrollings = 0; + verticalScrollAnim->stop(); /* * In moust cases cursor position on view doesn't change, but for scene after scrolling position will be different. @@ -154,6 +188,7 @@ bool GraphicsViewZoom::eventFilter(QObject *object, QEvent *event) else if (event->type() == QEvent::Wheel) { QWheelEvent* wheel_event = static_cast(event); + SCASSERT(wheel_event != nullptr); if (QApplication::keyboardModifiers() == _modifiers) { if (wheel_event->orientation() == Qt::Vertical) @@ -166,34 +201,14 @@ bool GraphicsViewZoom::eventFilter(QObject *object, QEvent *event) } else { - const QPoint numPixels = wheel_event->pixelDelta(); - const QPoint numDegrees = wheel_event->angleDelta() / 8; - int numSteps; - - if (not numPixels.isNull()) + if (QApplication::keyboardModifiers() == Qt::ShiftModifier) { - numSteps = numPixels.y(); - } - else if (not numDegrees.isNull()) - { - numSteps = numDegrees.y() / 15; + return StartHorizontalScrollings(wheel_event); } else { - return true;//Just ignore + return StartVerticalScrollings(wheel_event); } - - _numScheduledScrollings += numSteps; - if (_numScheduledScrollings * numSteps < 0) - { // if user moved the wheel in another direction, we reset previously scheduled scalings - _numScheduledScrollings = numSteps; - } - - if (anim->state() != QTimeLine::Running) - { - anim->start(); - } - return true; } } @@ -230,6 +245,76 @@ void GraphicsViewZoom::FictiveSceneRect(QGraphicsScene *sc, QGraphicsView *view) sc->setSceneRect(newRect); } +//--------------------------------------------------------------------------------------------------------------------- +bool GraphicsViewZoom::StartVerticalScrollings(QWheelEvent *wheel_event) +{ + SCASSERT(wheel_event != nullptr); + + 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 + } + + _numScheduledVerticalScrollings += numSteps; + if (_numScheduledVerticalScrollings * numSteps < 0) + { // if user moved the wheel in another direction, we reset previously scheduled scalings + _numScheduledVerticalScrollings = numSteps; + } + + if (verticalScrollAnim->state() != QTimeLine::Running) + { + verticalScrollAnim->start(); + } + return true; +} + +//--------------------------------------------------------------------------------------------------------------------- +bool GraphicsViewZoom::StartHorizontalScrollings(QWheelEvent *wheel_event) +{ + SCASSERT(wheel_event != nullptr); + + 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 + } + + _numScheduledHorizontalScrollings += numSteps; + if (_numScheduledHorizontalScrollings * numSteps < 0) + { // if user moved the wheel in another direction, we reset previously scheduled scalings + _numScheduledHorizontalScrollings = numSteps; + } + + if (horizontalScrollAnim->state() != QTimeLine::Running) + { + horizontalScrollAnim->start(); + } + return true; +} + //--------------------------------------------------------------------------------------------------------------------- /** * @brief VMainGraphicsView constructor. diff --git a/src/libs/vwidgets/vmaingraphicsview.h b/src/libs/vwidgets/vmaingraphicsview.h index 45c313551..ea2677432 100644 --- a/src/libs/vwidgets/vmaingraphicsview.h +++ b/src/libs/vwidgets/vmaingraphicsview.h @@ -71,25 +71,32 @@ public: signals: void zoomed(); public slots: - void scrollingTime(qreal x); + void VerticalScrollingTime(qreal x); + void HorizontalScrollingTime(qreal x); void animFinished(); protected: virtual bool eventFilter(QObject* object, QEvent* event) Q_DECL_OVERRIDE; private: Q_DISABLE_COPY(GraphicsViewZoom) - QGraphicsView* _view; + QGraphicsView *_view; Qt::KeyboardModifiers _modifiers; double _zoom_factor_base; QPointF target_scene_pos; QPointF target_viewport_pos; - QTimeLine *anim; - /** @brief _numScheduledScalings keep number scheduled scalings. */ - qint32 _numScheduledScrollings; + QTimeLine *verticalScrollAnim; + /** @brief _numScheduledVerticalScrollings keep number scheduled vertical scrollings. */ + qint32 _numScheduledVerticalScrollings; + QTimeLine *horizontalScrollAnim; + /** @brief _numScheduledHorizontalScrollings keep number scheduled horizontal scrollings. */ + qint32 _numScheduledHorizontalScrollings; static const int duration; static const int updateInterval; void FictiveSceneRect(QGraphicsScene *sc, QGraphicsView *view); + + bool StartVerticalScrollings(QWheelEvent* wheel_event); + bool StartHorizontalScrollings(QWheelEvent* wheel_event); }; /**