EnhancedGraphicsView.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package ArmarXGui::EnhancedGraphicsView
17  * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
18  * @date 2017
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 #include <QGraphicsItem>
23 
24 #include "EnhancedGraphicsView.h"
25 
26 namespace armarx
27 {
28 
30  {
31  return rotationModifier;
32  }
33 
35  {
36  return rotationFactor;
37  }
38 
39  Qt::KeyboardModifier EnhancedGraphicsView::getZoomKeyboardModifier() const
40  {
41  return zoomModifier;
42  }
43 
45  {
46  return zoomFacor;
47  }
48 
50  {
51  return draggingModifier;
52  }
53 
55  {
56  return draggingButton;
57  }
58 
60  {
62  }
63 
65  {
66  rotationEnabled = !disabled;
67  }
68 
70  {
71  rotationModifier = mod;
72  }
73 
75  {
76  rotationFactor = factor;
77  }
78 
80  {
82  }
83 
85  {
86  zoomEnabled = !disabled;
87  }
88 
89  void EnhancedGraphicsView::setZoomKeyboardModifier(Qt::KeyboardModifier mod)
90  {
91  zoomModifier = mod;
92  }
93 
95  {
96  zoomFacor = factor;
97  }
98 
100  {
102  }
103 
105  {
106  draggingEnabled = !disabled;
107  }
108 
110  {
111  draggingModifier = mod;
112  }
113 
114  void EnhancedGraphicsView::setDraggingMouseButton(Qt::MouseButton button)
115  {
116  draggingButton = button;
117  }
118 
120  {
121  for (QGraphicsItem* i : items())
122  {
123  i->setVisible(visible);
124  }
125  }
126 
128  {
129  draggingStartPosition = mapToScene(e->pos());
130  QGraphicsView::mousePressEvent(e);
131  }
132 
134  {
135  if (
136  (e->modifiers() & draggingModifier).testFlag(draggingModifier) &&
137  (e->buttons() & draggingButton).testFlag(draggingButton)
138  )
139  {
140  const auto oldAnchor = transformationAnchor();
141  setTransformationAnchor(QGraphicsView::NoAnchor);
142 
143  const QPointF vector = mapToScene(e->pos()) - draggingStartPosition;
144  translate(vector.x(), vector.y());
145  draggingStartPosition = mapToScene(e->pos());
146  e->accept();
147 
148  setTransformationAnchor(oldAnchor);
149  return;
150  }
151  QGraphicsView::mouseMoveEvent(e);
152  }
153 
154  void EnhancedGraphicsView::wheelEvent(QWheelEvent* e)
155  {
156  bool used = false;
157  if ((e->modifiers() & zoomModifier).testFlag(zoomModifier))
158  {
159  const auto oldAnchor = transformationAnchor();
160  setTransformationAnchor(QGraphicsView::NoAnchor);
161 
162  float factor = std::pow(zoomFacor, e->delta() < 0 ? -1 : +1);
163  scale(factor, factor);
164  used = true;
165 
166  setTransformationAnchor(oldAnchor);
167  }
168  if ((e->modifiers() & rotationModifier).testFlag(rotationModifier))
169  {
170  const auto oldAnchor = transformationAnchor();
171  setTransformationAnchor(QGraphicsView::NoAnchor);
172 
173  rotate(e->delta() * rotationFactor);
174  used = true;
175 
176  setTransformationAnchor(oldAnchor);
177  }
178  if (used)
179  {
180  e->accept();
181  }
182  else
183  {
184  QGraphicsView::wheelEvent(e);
185  }
186  }
187 
188 }
armarx::EnhancedGraphicsView::getZoomKeyboardModifier
Qt::KeyboardModifier getZoomKeyboardModifier() const
Definition: EnhancedGraphicsView.cpp:39
armarx::EnhancedGraphicsView::setZoomEnabled
void setZoomEnabled(bool enabled=true)
Definition: EnhancedGraphicsView.cpp:79
armarx::EnhancedGraphicsView::setRotationKeyboardModifier
void setRotationKeyboardModifier(Qt::KeyboardModifier mod)
Definition: EnhancedGraphicsView.cpp:69
armarx::EnhancedGraphicsView::setRotationFactor
void setRotationFactor(qreal factor)
Definition: EnhancedGraphicsView.cpp:74
armarx::EnhancedGraphicsView::getZoomFactor
qreal getZoomFactor() const
Definition: EnhancedGraphicsView.cpp:44
armarx::EnhancedGraphicsView::draggingEnabled
bool draggingEnabled
Definition: EnhancedGraphicsView.h:87
armarx::EnhancedGraphicsView::zoomEnabled
bool zoomEnabled
Definition: EnhancedGraphicsView.h:83
armarx::EnhancedGraphicsView::mouseMoveEvent
void mouseMoveEvent(QMouseEvent *e) override
Definition: EnhancedGraphicsView.cpp:133
armarx::EnhancedGraphicsView::rotationEnabled
bool rotationEnabled
Definition: EnhancedGraphicsView.h:79
armarx::EnhancedGraphicsView::zoomFacor
qreal zoomFacor
Definition: EnhancedGraphicsView.h:85
armarx::EnhancedGraphicsView::draggingButton
Qt::MouseButton draggingButton
Definition: EnhancedGraphicsView.h:89
armarx::EnhancedGraphicsView::setZoomDisabled
void setZoomDisabled(bool disabled=true)
Definition: EnhancedGraphicsView.cpp:84
armarx::EnhancedGraphicsView::setZoomKeyboardModifier
void setZoomKeyboardModifier(Qt::KeyboardModifier mod)
Definition: EnhancedGraphicsView.cpp:89
armarx::EnhancedGraphicsView::setDraggingDisabled
void setDraggingDisabled(bool disabled=true)
Definition: EnhancedGraphicsView.cpp:104
armarx::EnhancedGraphicsView::mousePressEvent
void mousePressEvent(QMouseEvent *e) override
Definition: EnhancedGraphicsView.cpp:127
enabled
std::atomic< bool > * enabled
Definition: RemoteGuiWidgetController.cpp:75
armarx::EnhancedGraphicsView::rotationFactor
qreal rotationFactor
Definition: EnhancedGraphicsView.h:81
armarx::EnhancedGraphicsView::rotationModifier
Qt::KeyboardModifier rotationModifier
Definition: EnhancedGraphicsView.h:80
armarx::EnhancedGraphicsView::getRotationFactor
qreal getRotationFactor() const
Definition: EnhancedGraphicsView.cpp:34
EnhancedGraphicsView.h
armarx::EnhancedGraphicsView::getDraggingMouseButton
Qt::MouseButton getDraggingMouseButton() const
Definition: EnhancedGraphicsView.cpp:54
armarx::EnhancedGraphicsView::setRotationDisabled
void setRotationDisabled(bool disabled=true)
Definition: EnhancedGraphicsView.cpp:64
armarx::EnhancedGraphicsView::setDraggingKeyboardModifier
void setDraggingKeyboardModifier(Qt::KeyboardModifier mod)
Definition: EnhancedGraphicsView.cpp:109
armarx::EnhancedGraphicsView::zoomModifier
Qt::KeyboardModifier zoomModifier
Definition: EnhancedGraphicsView.h:84
armarx::EnhancedGraphicsView::setZoomFactor
qreal setZoomFactor()
armarx::EnhancedGraphicsView::wheelEvent
void wheelEvent(QWheelEvent *e) override
Definition: EnhancedGraphicsView.cpp:154
armarx::EnhancedGraphicsView::setDraggingMouseButton
void setDraggingMouseButton(Qt::MouseButton button)
Definition: EnhancedGraphicsView.cpp:114
armarx::EnhancedGraphicsView::setRotationEnabled
void setRotationEnabled(bool enabled=true)
Definition: EnhancedGraphicsView.cpp:59
armarx::EnhancedGraphicsView::draggingModifier
Qt::KeyboardModifier draggingModifier
Definition: EnhancedGraphicsView.h:88
armarx::EnhancedGraphicsView::setDraggingEnabled
void setDraggingEnabled(bool enabled=true)
Definition: EnhancedGraphicsView.cpp:99
armarx::EnhancedGraphicsView::draggingStartPosition
QPointF draggingStartPosition
Definition: EnhancedGraphicsView.h:90
armarx::EnhancedGraphicsView::setAllVisible
void setAllVisible(bool visible)
Definition: EnhancedGraphicsView.cpp:119
armarx::EnhancedGraphicsView::getDraggingKeyboardModifier
Qt::KeyboardModifier getDraggingKeyboardModifier() const
Definition: EnhancedGraphicsView.cpp:49
armarx::EnhancedGraphicsView::getRotationKeyboardModifier
Qt::KeyboardModifier getRotationKeyboardModifier() const
Definition: EnhancedGraphicsView.cpp:29
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28