diff --git a/NEWS b/NEWS index 05ef862e67..2a547ddb52 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,7 @@ * Added support for cut, copy and paste * Added current cursor position to the status bar * Added keyboard shortcuts to switch tools +* Added scrolling the map view with middle mouse button * Snap objects to the grid when Ctrl is pressed 0.2.0 (1 October 2009) diff --git a/src/mapview.cpp b/src/mapview.cpp index 7b78a3d70b..fbb34c2471 100644 --- a/src/mapview.cpp +++ b/src/mapview.cpp @@ -22,12 +22,14 @@ #include "mapview.h" #include +#include using namespace Tiled::Internal; MapView::MapView(QWidget *parent): QGraphicsView(parent), - mScale(1) + mScale(1), + mHandScrolling(false) { setTransformationAnchor(QGraphicsView::AnchorViewCenter); @@ -121,3 +123,53 @@ void MapView::wheelEvent(QWheelEvent *event) QGraphicsView::wheelEvent(event); } + +/** + * Activates hand scrolling when the middle mouse button is pressed. + */ +void MapView::mousePressEvent(QMouseEvent *event) +{ + if (event->button() == Qt::MidButton) { + viewport()->setCursor(Qt::ClosedHandCursor); + setInteractive(false); + mHandScrolling = true; + return; + } + + QGraphicsView::mousePressEvent(event); +} + +/** + * Deactivates hand scrolling when the middle mouse button is released. + */ +void MapView::mouseReleaseEvent(QMouseEvent *event) +{ + if (event->button() == Qt::MidButton) { + viewport()->setCursor(QCursor()); + setInteractive(true); + mHandScrolling = false; + return; + } + + QGraphicsView::mouseReleaseEvent(event); +} + +/** + * Moves the view with the mouse while hand scrolling. + */ +void MapView::mouseMoveEvent(QMouseEvent *event) +{ + if (mHandScrolling) { + QScrollBar *hBar = horizontalScrollBar(); + QScrollBar *vBar = verticalScrollBar(); + const QPoint d = event->pos() - mLastMousePos; + hBar->setValue(hBar->value() + (isRightToLeft() ? d.x() : -d.x())); + vBar->setValue(vBar->value() - d.y()); + + mLastMousePos = event->pos(); + return; + } + + QGraphicsView::mouseMoveEvent(event); + mLastMousePos = event->pos(); +} diff --git a/src/mapview.h b/src/mapview.h index 6ab2dcaec6..5d6ee36ecc 100644 --- a/src/mapview.h +++ b/src/mapview.h @@ -29,7 +29,8 @@ namespace Internal { /** * The map view shows the map scene. This class sets some MapScene specific - * properties on the viewport and implements zooming. + * properties on the viewport and implements zooming. It also allows the view + * to be scrolled with the middle mouse button. * * @see MapScene */ @@ -60,8 +61,14 @@ public slots: protected: void wheelEvent(QWheelEvent *event); + void mousePressEvent(QMouseEvent *event); + void mouseReleaseEvent(QMouseEvent *event); + void mouseMoveEvent(QMouseEvent *event); + private: qreal mScale; + QPoint mLastMousePos; + bool mHandScrolling; }; } // namespace Internal