MNE-CPP  0.1.9
A Framework for Electrophysiology
layoutscene.cpp
Go to the documentation of this file.
1 //=============================================================================================================
36 //=============================================================================================================
37 // INCLUDES
38 //=============================================================================================================
39 
40 #include "layoutscene.h"
41 
42 //=============================================================================================================
43 // QT INCLUDES
44 //=============================================================================================================
45 
46 #include <QGraphicsView>
47 #include <QGraphicsSceneMouseEvent>
48 #include <QGestureEvent>
49 #include <QScrollBar>
50 #include <QDebug>
51 
52 //=============================================================================================================
53 // USED NAMESPACES
54 //=============================================================================================================
55 
56 using namespace DISPLIB;
57 
58 //=============================================================================================================
59 // DEFINE MEMBER METHODS
60 //=============================================================================================================
61 
62 LayoutScene::LayoutScene(QGraphicsView* view,
63  QObject* parent)
64 : QGraphicsScene(parent)
65 , m_qvView(view)
66 , m_bDragMode(false)
67 //, m_bExtendedSelectionMode(false)
68 {
69  m_qvView->grabGesture(Qt::PanGesture);
70  m_qvView->grabGesture(Qt::PinchGesture);
71 
72  //Install event filter to overcome QGrabGesture and QScrollBar problem
73  m_qvView->installEventFilter(this);
74 }
75 
76 //=============================================================================================================
77 
78 void LayoutScene::wheelEvent(QGraphicsSceneWheelEvent* event) {
79  m_qvView->setTransformationAnchor(QGraphicsView::AnchorViewCenter);
80 
81  // Scale the view / do the zoom
82  double scaleFactor = 1.15;
83  if(event->delta() > 0) {
84  // Zoom in
85  m_qvView->scale(scaleFactor, scaleFactor);
86  } else {
87  // Zooming out
88  m_qvView->scale(1.0 / scaleFactor, 1.0 / scaleFactor);
89  }
90 }
91 
92 //=============================================================================================================
93 
94 void LayoutScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* mouseEvent)
95 {
96  if(mouseEvent->button() == Qt::LeftButton)
97  m_qvView->fitInView(this->itemsBoundingRect(), Qt::KeepAspectRatio);
98 
99  QGraphicsScene::mouseDoubleClickEvent(mouseEvent);
100 }
101 
102 //=============================================================================================================
103 
104 void LayoutScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
105 {
106  switch(mouseEvent->button()) {
107  case Qt::LeftButton:
108  m_qvView->setDragMode(QGraphicsView::RubberBandDrag);
109 
110 // //If a normal left lick occurs without holding down CTRL -> Delete all selected items
111 // if(!m_bExtendedSelectionMode)
112 // m_selectedItems.clear();
113 
114  break;
115 
116  case Qt::RightButton:
117  m_bDragMode = true;
118  m_qvView->setDragMode(QGraphicsView::NoDrag);
119  m_mousePressPosition = mouseEvent->screenPos();
120 
121  //return so that no selection event is called
122  return;
123  }
124 
125  QGraphicsScene::mousePressEvent(mouseEvent);
126 }
127 
128 //=============================================================================================================
129 
130 void LayoutScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
131 {
132  if(m_bDragMode) {
133  int diffX = mouseEvent->screenPos().x() - m_mousePressPosition.x();
134  int diffY = mouseEvent->screenPos().y() - m_mousePressPosition.y();
135 
136  m_mousePressPosition = mouseEvent->screenPos();
137 
138  m_qvView->verticalScrollBar()->setValue(m_qvView->verticalScrollBar()->value() - diffY);
139  m_qvView->horizontalScrollBar()->setValue(m_qvView->horizontalScrollBar()->value() - diffX);
140  }
141 
142  QGraphicsScene::mouseMoveEvent(mouseEvent);
143 }
144 
145 //=============================================================================================================
146 
147 void LayoutScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
148 {
149 // if(m_bExtendedSelectionMode) {
150 // qDebug()<<"Extended Selection";
151 // //List of selected items during last rubberband action
152 // QList<QGraphicsItem *> currentSelectedItemsList = this->selectedItems();
153 
154 // //Generate list of all selected items (dont add duplicates)
155 // for(int i = 0; i<currentSelectedItemsList.size(); i++) {
156 // if(!m_selectedItems.contains(currentSelectedItemsList.at(i))) {
157 // //Duplicate not found - new item was selected since the last extended selection
158 // qDebug()<<"add item to m_selectedItems";
159 // m_selectedItems << currentSelectedItemsList.at(i);
160 // }
161 // else {
162 // qDebug()<<"delete item from m_selectedItems";
163 // //Duplicate found - already selected item was selected again since the last extended selection
164 // m_selectedItems.removeAll(currentSelectedItemsList.at(i));
165 // currentSelectedItemsList.at(i)->setSelected(false);
166 // }
167 // }
168 
169 // for(int i = 0; i<m_selectedItems.size(); i++)
170 // m_selectedItems.at(i)->setSelected(true);
171 
172 // qDebug()<<m_selectedItems.size();
173 // }
174 
175  if(m_bDragMode) {
176  m_bDragMode = false;
177  this->update();
178  }
179 
180  QGraphicsScene::mouseReleaseEvent(mouseEvent);
181 }
182 
183 //=============================================================================================================
184 
185 void LayoutScene::keyPressEvent(QKeyEvent *keyEvent)
186 {
187 // if(keyEvent->key() == Qt::Key_Control)
188 // m_bExtendedSelectionMode = true;
189 
190  QGraphicsScene::keyPressEvent(keyEvent);
191 }
192 
193 //=============================================================================================================
194 
195 void LayoutScene::keyReleaseEvent(QKeyEvent *keyEvent)
196 {
197 // if(keyEvent->key() == Qt::Key_Control)
198 // m_bExtendedSelectionMode = false;
199 
200  QGraphicsScene::keyReleaseEvent(keyEvent);
201 }
202 
203 //=============================================================================================================
204 
205 bool LayoutScene::event(QEvent *event)
206 {
207  if (event->type() == QEvent::Gesture) {
208  QGestureEvent* gestureEventCast = static_cast<QGestureEvent*>(event);
209 
210 // QList<QGesture *> gestureList = gestureEventCast->gestures();
211 // for(int i = 0; i<gestureList.size(); i++)
212 // qDebug()<<gestureList.at(i)->gestureType();
213 // qDebug()<<"-----------------------";
214 
215  return gestureEvent(static_cast<QGestureEvent*>(gestureEventCast));
216  }
217 
218  return QGraphicsScene::event(event);
219 }
220 
221 //=============================================================================================================
222 
223 bool LayoutScene::gestureEvent(QGestureEvent *event)
224 {
225  //Pan event
226  if (QGesture *pan = event->gesture(Qt::PanGesture))
227  panTriggered(static_cast<QPanGesture *>(pan));
228 
229  //Pinch event
230  if (QGesture *pinch = event->gesture(Qt::PinchGesture))
231  pinchTriggered(static_cast<QPinchGesture *>(pinch));
232 
233  return true;
234 }
235 
236 //=============================================================================================================
237 
238 void LayoutScene::panTriggered(QPanGesture *gesture)
239 {
240  QPointF delta = gesture->delta();
241 
242  m_qvView->verticalScrollBar()->setValue(m_qvView->verticalScrollBar()->value() + delta.y());
243  m_qvView->horizontalScrollBar()->setValue(m_qvView->horizontalScrollBar()->value() + delta.x());
244 }
245 
246 //=============================================================================================================
247 
248 void LayoutScene::pinchTriggered(QPinchGesture *gesture)
249 {
250  m_qvView->setTransformationAnchor(QGraphicsView::NoAnchor);
251  m_qvView->scale(gesture->scaleFactor(), gesture->scaleFactor());
252 }
253 
254 //=============================================================================================================
255 
256 bool LayoutScene::eventFilter(QObject *object, QEvent *event)
257 {
258  if (object == m_qvView && event->type() == QEvent::Gesture) {
259  QGestureEvent* gestureEventCast = static_cast<QGestureEvent*>(event);
260 
261  return gestureEvent(static_cast<QGestureEvent*>(gestureEventCast));
262  }
263 
264  return false;
265 }
DISPLIB::LayoutScene::keyReleaseEvent
void keyReleaseEvent(QKeyEvent *keyEvent)
Definition: layoutscene.cpp:195
DISPLIB::LayoutScene::mousePressEvent
void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
Definition: layoutscene.cpp:104
DISPLIB::LayoutScene::event
bool event(QEvent *event)
Definition: layoutscene.cpp:205
DISPLIB::LayoutScene::m_qvView
QGraphicsView * m_qvView
Definition: layoutscene.h:175
DISPLIB::LayoutScene::mouseMoveEvent
void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
Definition: layoutscene.cpp:130
DISPLIB::LayoutScene::mouseDoubleClickEvent
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *mouseEvent)
Definition: layoutscene.cpp:94
DISPLIB::LayoutScene::m_bDragMode
bool m_bDragMode
Definition: layoutscene.h:178
DISPLIB::LayoutScene::mouseReleaseEvent
void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
Definition: layoutscene.cpp:147
DISPLIB::LayoutScene::m_mousePressPosition
QPointF m_mousePressPosition
Definition: layoutscene.h:180
DISPLIB::LayoutScene::gestureEvent
bool gestureEvent(QGestureEvent *event)
Definition: layoutscene.cpp:223
layoutscene.h
Contains the declaration of the LayoutScene class.
DISPLIB::LayoutScene::LayoutScene
LayoutScene(QGraphicsView *view, QObject *parent=0)
Definition: layoutscene.cpp:62
DISPLIB::LayoutScene::panTriggered
void panTriggered(QPanGesture *)
Definition: layoutscene.cpp:238
DISPLIB::LayoutScene::wheelEvent
void wheelEvent(QGraphicsSceneWheelEvent *event)
Definition: layoutscene.cpp:78
DISPLIB::LayoutScene::keyPressEvent
void keyPressEvent(QKeyEvent *keyEvent)
Definition: layoutscene.cpp:185
DISPLIB::LayoutScene::pinchTriggered
void pinchTriggered(QPinchGesture *)
Definition: layoutscene.cpp:248
DISPLIB::LayoutScene::eventFilter
bool eventFilter(QObject *object, QEvent *event)
Definition: layoutscene.cpp:256