Created
February 7, 2013 08:02
-
-
Save dokinkon/4729365 to your computer and use it in GitHub Desktop.
Pan GraphicsView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void WorkspaceView::mousePressEvent(QMouseEvent *event) | |
{ | |
Q_D(WorkspaceView); | |
QGraphicsView::mousePressEvent(event); | |
if (event->button()==Qt::MiddleButton) { | |
d->m_lastPanPosition = event->pos(); | |
d->m_panning = true; | |
setCursor(Qt::ClosedHandCursor); | |
} | |
} | |
void WorkspaceView::mouseMoveEvent(QMouseEvent *event) | |
{ | |
Q_D(WorkspaceView); | |
QGraphicsView::mouseMoveEvent(event); | |
if(d->m_panning) { | |
//Get how much we panned | |
QPointF delta = mapToScene(d->m_lastPanPosition.toPoint()) - mapToScene(event->pos()); | |
d->m_lastPanPosition = event->pos(); | |
setCenter(center() + delta); | |
} | |
} | |
void WorkspaceView::mouseReleaseEvent(QMouseEvent *event) | |
{ | |
Q_D(WorkspaceView); | |
QGraphicsView::mouseReleaseEvent(event); | |
if (d->m_panning) { | |
setCursor(Qt::ArrowCursor); | |
d->m_lastPanPosition = QPoint(); | |
d->m_panning = false; | |
} | |
} | |
void updateSceneRect() | |
{ | |
Q_Q(WorkspaceScene); | |
QRectF rect = q->itemsBoundingRect(); | |
const int width = rect.width(); | |
const int height = rect.height(); | |
rect.adjust(-width/2, -height/2 , width/2, height/2); | |
q->setSceneRect(rect); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment