Permalink
Browse files

Allow hand scrolling while holding the spacebar

Required some hacking about with mouse grabbing, but overall seems to
work nicely apart from the usual painting glitches with the stamp brush.

This closes #28
  • Loading branch information...
bjorn committed Jul 6, 2011
1 parent e0ac305 commit c1379116334bea170ed4f24734046f4ab0e97105
Showing with 75 additions and 12 deletions.
  1. +14 −0 src/tiled/mainwindow.cpp
  2. +3 −0 src/tiled/mainwindow.h
  3. +50 −9 src/tiled/mapview.cpp
  4. +8 −3 src/tiled/mapview.h
View
@@ -395,6 +395,20 @@ void MainWindow::changeEvent(QEvent *event)
}
}
+void MainWindow::keyPressEvent(QKeyEvent *event)
+{
+ if (event->key() == Qt::Key_Space && !event->isAutoRepeat())
+ if (MapView *mapView = mDocumentManager->currentMapView())
+ mapView->setHandScrolling(true);
+}
+
+void MainWindow::keyReleaseEvent(QKeyEvent *event)
+{
+ if (event->key() == Qt::Key_Space && !event->isAutoRepeat())
+ if (MapView *mapView = mDocumentManager->currentMapView())
+ mapView->setHandScrolling(false);
+}
+
void MainWindow::dragEnterEvent(QDragEnterEvent *e)
{
const QList<QUrl> urls = e->mimeData()->urls();
View
@@ -94,6 +94,9 @@ public slots:
void closeEvent(QCloseEvent *event);
void changeEvent(QEvent *event);
+ void keyPressEvent(QKeyEvent *);
+ void keyReleaseEvent(QKeyEvent *);
+
void dragEnterEvent(QDragEnterEvent *);
void dropEvent(QDropEvent *);
View
@@ -24,6 +24,8 @@
#include "preferences.h"
#include "zoomable.h"
+#include <QApplication>
+#include <QCursor>
#include <QWheelEvent>
#include <QScrollBar>
@@ -62,6 +64,11 @@ MapView::MapView(QWidget *parent)
connect(mZoomable, SIGNAL(scaleChanged(qreal)), SLOT(adjustScale(qreal)));
}
+MapView::~MapView()
+{
+ setHandScrolling(false); // Just in case we didn't get a hide event
+}
+
MapScene *MapView::mapScene() const
{
return static_cast<MapScene*>(scene());
@@ -94,6 +101,44 @@ void MapView::setUseOpenGL(bool useOpenGL)
#endif
}
+void MapView::setHandScrolling(bool handScrolling)
+{
+ if (mHandScrolling == handScrolling)
+ return;
+
+ mHandScrolling = handScrolling;
+ setInteractive(!mHandScrolling);
+
+ if (mHandScrolling) {
+ mLastMousePos = QCursor::pos();
+ QApplication::setOverrideCursor(QCursor(Qt::ClosedHandCursor));
+ viewport()->grabMouse();
+ } else {
+ viewport()->releaseMouse();
+ QApplication::restoreOverrideCursor();
+ }
+}
+
+bool MapView::event(QEvent *e)
+{
+ // Ignore space bar events since they're handled by the MainWindow
+ if (e->type() == QEvent::KeyPress || e->type() == QEvent::KeyRelease) {
+ if (static_cast<QKeyEvent*>(e)->key() == Qt::Key_Space) {
+ e->ignore();
+ return false;
+ }
+ }
+
+ return QGraphicsView::event(e);
+}
+
+void MapView::hideEvent(QHideEvent *event)
+{
+ // Disable hand scrolling when the view gets hidden in any way
+ setHandScrolling(false);
+ QGraphicsView::hideEvent(event);
+}
+
/**
* Override to support zooming in and out using the mouse wheel.
*/
@@ -120,9 +165,7 @@ void MapView::wheelEvent(QWheelEvent *event)
void MapView::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::MidButton) {
- viewport()->setCursor(Qt::ClosedHandCursor);
- setInteractive(false);
- mHandScrolling = true;
+ setHandScrolling(true);
return;
}
@@ -135,9 +178,7 @@ void MapView::mousePressEvent(QMouseEvent *event)
void MapView::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::MidButton) {
- viewport()->setCursor(QCursor());
- setInteractive(true);
- mHandScrolling = false;
+ setHandScrolling(false);
return;
}
@@ -152,14 +193,14 @@ void MapView::mouseMoveEvent(QMouseEvent *event)
if (mHandScrolling) {
QScrollBar *hBar = horizontalScrollBar();
QScrollBar *vBar = verticalScrollBar();
- const QPoint d = event->pos() - mLastMousePos;
+ const QPoint d = event->globalPos() - mLastMousePos;
hBar->setValue(hBar->value() + (isRightToLeft() ? d.x() : -d.x()));
vBar->setValue(vBar->value() - d.y());
- mLastMousePos = event->pos();
+ mLastMousePos = event->globalPos();
return;
}
QGraphicsView::mouseMoveEvent(event);
- mLastMousePos = event->pos();
+ mLastMousePos = event->globalPos();
}
View
@@ -41,16 +41,21 @@ class MapView : public QGraphicsView
Q_OBJECT
public:
- /**
- * Constructor.
- */
MapView(QWidget *parent = 0);
+ ~MapView();
MapScene *mapScene() const;
Zoomable *zoomable() const { return mZoomable; }
+ bool handScrolling() const { return mHandScrolling; }
+ void setHandScrolling(bool handScrolling);
+
protected:
+ bool event(QEvent *event);
+
+ void hideEvent(QHideEvent *);
+
void wheelEvent(QWheelEvent *event);
void mousePressEvent(QMouseEvent *event);

0 comments on commit c137911

Please sign in to comment.