Commit 678d1424 authored by Tim Jenssen's avatar Tim Jenssen Committed by Thomas Hartmann

QmlDesigner: add scrolling via space + mouse

Change-Id: Ia6377114267fe0f708826b5a9ea43d7c46998a17
Task-number: QTCREATORBUG-11321
Reviewed-by: default avatarThomas Hartmann <Thomas.Hartmann@theqtcompany.com>
parent c0e00a05
...@@ -32,8 +32,7 @@ ...@@ -32,8 +32,7 @@
namespace QmlDesigner { namespace QmlDesigner {
FormEditorGraphicsView::FormEditorGraphicsView(QWidget *parent) : FormEditorGraphicsView::FormEditorGraphicsView(QWidget *parent) :
QGraphicsView(parent), QGraphicsView(parent)
m_isPanning(false)
{ {
setTransformationAnchor(QGraphicsView::AnchorUnderMouse); setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
setResizeAnchor(QGraphicsView::AnchorViewCenter); setResizeAnchor(QGraphicsView::AnchorViewCenter);
...@@ -60,29 +59,26 @@ void FormEditorGraphicsView::wheelEvent(QWheelEvent *event) ...@@ -60,29 +59,26 @@ void FormEditorGraphicsView::wheelEvent(QWheelEvent *event)
event->ignore(); event->ignore();
else else
QGraphicsView::wheelEvent(event); QGraphicsView::wheelEvent(event);
} }
void FormEditorGraphicsView::mousePressEvent(QMouseEvent *event) void FormEditorGraphicsView::mousePressEvent(QMouseEvent *event)
{ {
if (event->buttons().testFlag(Qt::MiddleButton)) { if (event->buttons().testFlag(Qt::MiddleButton) && m_isPanning == Panning::NotStarted)
m_isPanning = true; startPanning(event);
m_panStartX = event->x(); else
m_panStartY = event->y();
setCursor(Qt::ClosedHandCursor);
event->accept();
} else {
QGraphicsView::mousePressEvent(event); QGraphicsView::mousePressEvent(event);
}
} }
void FormEditorGraphicsView::mouseMoveEvent(QMouseEvent *event) void FormEditorGraphicsView::mouseMoveEvent(QMouseEvent *event)
{ {
if (m_isPanning) { if (m_isPanning != Panning::NotStarted) {
horizontalScrollBar()->setValue(horizontalScrollBar()->value() - (event->x() - m_panStartX)); if (!m_panningStartPosition.isNull()) {
verticalScrollBar()->setValue(verticalScrollBar()->value() - (event->y() - m_panStartY)); horizontalScrollBar()->setValue(horizontalScrollBar()->value() -
m_panStartX = event->x(); (event->x() - m_panningStartPosition.x()));
m_panStartY = event->y(); verticalScrollBar()->setValue(verticalScrollBar()->value() -
(event->y() - m_panningStartPosition.y()));
}
m_panningStartPosition = event->pos();
event->accept(); event->accept();
}else { }else {
QGraphicsView::mouseMoveEvent(event); QGraphicsView::mouseMoveEvent(event);
...@@ -91,14 +87,44 @@ void FormEditorGraphicsView::mouseMoveEvent(QMouseEvent *event) ...@@ -91,14 +87,44 @@ void FormEditorGraphicsView::mouseMoveEvent(QMouseEvent *event)
void FormEditorGraphicsView::mouseReleaseEvent(QMouseEvent *event) void FormEditorGraphicsView::mouseReleaseEvent(QMouseEvent *event)
{ {
if (m_isPanning) { if (m_isPanning == Panning::MouseWheelStarted)
stopPanning(event);
m_isPanning = false; else
setCursor(Qt::ArrowCursor);
event->accept();
}else {
QGraphicsView::mouseReleaseEvent(event); QGraphicsView::mouseReleaseEvent(event);
} }
void FormEditorGraphicsView::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Space && m_isPanning == Panning::NotStarted)
startPanning(event);
else
QGraphicsView::keyPressEvent(event);
}
void FormEditorGraphicsView::keyReleaseEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Space && !event->isAutoRepeat())
stopPanning(event);
else
QGraphicsView::keyReleaseEvent(event);
}
void FormEditorGraphicsView::startPanning(QEvent *event)
{
if (event->type() == QEvent::KeyPress)
m_isPanning = Panning::SpaceKeyStarted;
else
m_isPanning = Panning::MouseWheelStarted;
setCursor(Qt::ClosedHandCursor);
event->accept();
}
void FormEditorGraphicsView::stopPanning(QEvent *event)
{
m_isPanning = Panning::NotStarted;
m_panningStartPosition = QPoint();
setCursor(Qt::ArrowCursor);
event->accept();
} }
void FormEditorGraphicsView::setRootItemRect(const QRectF &rect) void FormEditorGraphicsView::setRootItemRect(const QRectF &rect)
......
...@@ -48,9 +48,17 @@ protected: ...@@ -48,9 +48,17 @@ protected:
void mousePressEvent(QMouseEvent *event) override; void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override; void mouseReleaseEvent(QMouseEvent *event) override;
void keyPressEvent(QKeyEvent *event) override;
void keyReleaseEvent(QKeyEvent *event) override;
private: private:
bool m_isPanning; enum Panning{
int m_panStartX, m_panStartY; NotStarted, MouseWheelStarted, SpaceKeyStarted
};
void startPanning(QEvent *event);
void stopPanning(QEvent *event);
Panning m_isPanning = Panning::NotStarted;
QPoint m_panningStartPosition;
QRectF m_rootItemRect; QRectF m_rootItemRect;
}; };
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment