22 #include <QApplication>
25 #include <QWheelEvent>
33 :
QGraphicsView(parent), m_zoom_min(zoomMin), m_zoom_max(zoomMax)
35 setTransformationAnchor(QGraphicsView::AnchorViewCenter);
36 setResizeAnchor(QGraphicsView::AnchorViewCenter);
45 double zoomX = transform().m11() * factor;
46 double zoomY = transform().m22() * factor;
48 if ((zoomX >= m_zoom_min) && (zoomY >= m_zoom_min) && (zoomX <= m_zoom_max) && (zoomY <= m_zoom_max)) {
49 scale(factor, factor);
54 scale(1 / transform().m11(), 1 / transform().m22());
58 if (event->key() == Qt::Key_Control) {
59 setDragMode(QGraphicsView::ScrollHandDrag);
62 QGraphicsView::keyPressEvent(event);
66 if (event->key() == Qt::Key_Control) {
67 setDragMode(QGraphicsView::NoDrag);
70 QGraphicsView::keyReleaseEvent(event);
75 if (QApplication::keyboardModifiers().testFlag(Qt::ControlModifier)) {
76 setDragMode(QGraphicsView::ScrollHandDrag);
79 setDragMode(QGraphicsView::NoDrag);
84 QGraphicsView::enterEvent(event);
89 return transform().m11();
94 setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
95 ScaleView(std::pow(2, -event->delta() / 240.0));
96 setTransformationAnchor(QGraphicsView::AnchorViewCenter);
virtual void enterEvent(QEvent *event)
Overriding enterEvent to grab focus when the view is entered.
see the online Qt documentation
virtual void keyPressEvent(QKeyEvent *event)
Overriding keyPressEvent to detect when control key is pressed.
double Scaling()
Gets the current scaling of the view (assuming the view was only scaled with ScaleView() and the hori...
PanAndZoomView(double zoomMin=0.07, double zoomMax=100.0, QWidget *parent=nullptr)
Constructor.
virtual void keyReleaseEvent(QKeyEvent *event)
Overriding releasePressEvent to detect when control key is released.
Interface for PanAndZoomView.
virtual void wheelEvent(QWheelEvent *event)
Overriding wheelEvent to scroll the view when control key is held down.
see the online Qt documentation
void ScaleView(double factor)
Scales the view, bounded by the minimum and maximum zooming factor.
void ResetZoom()
Resets the view to the normal zoom level.
Namespace for generic graphical shell for simulators.
virtual ~PanAndZoomView()
Destructor.