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 @@
namespace QmlDesigner {
FormEditorGraphicsView::FormEditorGraphicsView(QWidget *parent) :
QGraphicsView(parent),
m_isPanning(false)
QGraphicsView(parent)
{
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
setResizeAnchor(QGraphicsView::AnchorViewCenter);
......@@ -60,29 +59,26 @@ void FormEditorGraphicsView::wheelEvent(QWheelEvent *event)
event->ignore();
else
QGraphicsView::wheelEvent(event);
}
void FormEditorGraphicsView::mousePressEvent(QMouseEvent *event)
{
if (event->buttons().testFlag(Qt::MiddleButton)) {
m_isPanning = true;
m_panStartX = event->x();
m_panStartY = event->y();
setCursor(Qt::ClosedHandCursor);
event->accept();
} else {
if (event->buttons().testFlag(Qt::MiddleButton) && m_isPanning == Panning::NotStarted)
startPanning(event);
else
QGraphicsView::mousePressEvent(event);
}
}
void FormEditorGraphicsView::mouseMoveEvent(QMouseEvent *event)
{
if (m_isPanning) {
horizontalScrollBar()->setValue(horizontalScrollBar()->value() - (event->x() - m_panStartX));
verticalScrollBar()->setValue(verticalScrollBar()->value() - (event->y() - m_panStartY));
m_panStartX = event->x();
m_panStartY = event->y();
if (m_isPanning != Panning::NotStarted) {
if (!m_panningStartPosition.isNull()) {
horizontalScrollBar()->setValue(horizontalScrollBar()->value() -
(event->x() - m_panningStartPosition.x()));
verticalScrollBar()->setValue(verticalScrollBar()->value() -
(event->y() - m_panningStartPosition.y()));
}
m_panningStartPosition = event->pos();
event->accept();
}else {
QGraphicsView::mouseMoveEvent(event);
......@@ -91,14 +87,44 @@ void FormEditorGraphicsView::mouseMoveEvent(QMouseEvent *event)
void FormEditorGraphicsView::mouseReleaseEvent(QMouseEvent *event)
{
if (m_isPanning) {
m_isPanning = false;
setCursor(Qt::ArrowCursor);
event->accept();
}else {
if (m_isPanning == Panning::MouseWheelStarted)
stopPanning(event);
else
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)
......
......@@ -48,9 +48,17 @@ protected:
void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
void keyPressEvent(QKeyEvent *event) override;
void keyReleaseEvent(QKeyEvent *event) override;
private:
bool m_isPanning;
int m_panStartX, m_panStartY;
enum Panning{
NotStarted, MouseWheelStarted, SpaceKeyStarted
};
void startPanning(QEvent *event);
void stopPanning(QEvent *event);
Panning m_isPanning = Panning::NotStarted;
QPoint m_panningStartPosition;
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