VPTissue Reference Manual
PanAndZoomView.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Universiteit Antwerpen
3  *
4  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
5  * the European Commission - subsequent versions of the EUPL (the "Licence");
6  * You may not use this work except in compliance with the Licence.
7  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the Licence is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the Licence for the specific language governing
13  * permissions and limitations under the Licence.
14  */
20 #include "PanAndZoomView.h"
21 
22 #include <QApplication>
23 #include <QEvent>
24 #include <QKeyEvent>
25 #include <QWheelEvent>
26 
27 #include <cmath>
28 
29 namespace SimShell {
30 namespace Gui {
31 
32 PanAndZoomView::PanAndZoomView(double zoomMin, double zoomMax, QWidget *parent)
33  : QGraphicsView(parent), m_zoom_min(zoomMin), m_zoom_max(zoomMax)
34 {
35  setTransformationAnchor(QGraphicsView::AnchorViewCenter);
36  setResizeAnchor(QGraphicsView::AnchorViewCenter);
37 }
38 
40 {
41 }
42 
43 void PanAndZoomView::ScaleView(double factor)
44 {
45  double zoomX = transform().m11() * factor;
46  double zoomY = transform().m22() * factor;
47 
48  if ((zoomX >= m_zoom_min) && (zoomY >= m_zoom_min) && (zoomX <= m_zoom_max) && (zoomY <= m_zoom_max)) {
49  scale(factor, factor);
50  }
51 }
52 
54  scale(1 / transform().m11(), 1 / transform().m22());
55 }
56 
57 void PanAndZoomView::keyPressEvent(QKeyEvent *event) {
58  if (event->key() == Qt::Key_Control) {
59  setDragMode(QGraphicsView::ScrollHandDrag);
60  }
61 
62  QGraphicsView::keyPressEvent(event);
63 }
64 
65 void PanAndZoomView::keyReleaseEvent(QKeyEvent *event) {
66  if (event->key() == Qt::Key_Control) {
67  setDragMode(QGraphicsView::NoDrag);
68  }
69 
70  QGraphicsView::keyReleaseEvent(event);
71 }
72 
73 
74 void PanAndZoomView::enterEvent(QEvent *event) {
75  if (QApplication::keyboardModifiers().testFlag(Qt::ControlModifier)) {
76  setDragMode(QGraphicsView::ScrollHandDrag);
77  }
78  else {
79  setDragMode(QGraphicsView::NoDrag);
80  }
81 
82  setFocus();
83 
84  QGraphicsView::enterEvent(event);
85 }
86 
88 {
89  return transform().m11();
90 }
91 
92 void PanAndZoomView::wheelEvent(QWheelEvent *event)
93 {
94  setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
95  ScaleView(std::pow(2, -event->delta() / 240.0));
96  setTransformationAnchor(QGraphicsView::AnchorViewCenter);
97 }
98 
99 } // namespace Gui
100 } // namespace SimShell
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.
Definition: SimSession.h:32
virtual ~PanAndZoomView()
Destructor.