Skip to content

Commit

Permalink
Added scrolling the map view with the middle mouse button
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn committed Nov 13, 2009
1 parent 8851f3b commit b5d5dda
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
54 changes: 53 additions & 1 deletion src/mapview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
#include "mapview.h"

#include <QWheelEvent>
#include <QScrollBar>

using namespace Tiled::Internal;

MapView::MapView(QWidget *parent):
QGraphicsView(parent),
mScale(1)
mScale(1),
mHandScrolling(false)
{
setTransformationAnchor(QGraphicsView::AnchorViewCenter);

Expand Down Expand Up @@ -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();
}
9 changes: 8 additions & 1 deletion src/mapview.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b5d5dda

Please sign in to comment.