Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
QVTKMitkInteractorAdapter.cxx
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 // original copyright below
18 
19 /*=========================================================================
20 
21  Program: Visualization Toolkit
22  Module: QVTKMitkInteractorAdapter.cxx
23 
24  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
25  All rights reserved.
26  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
27 
28  This software is distributed WITHOUT ANY WARRANTY; without even
29  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
30  PURPOSE. See the above copyright notice for more information.
31 
32 =========================================================================*/
33 /*
34  * Copyright 2004 Sandia Corporation.
35  * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
36  * license for use of this work by or on behalf of the
37  * U.S. Government. Redistribution and use in source and binary forms, with
38  * or without modification, are permitted provided that this Notice and any
39  * statement of authorship are reproduced on all copies.
40  */
41 
42 /*========================================================================
43  For general information about using VTK and Qt, see:
44  http://www.trolltech.com/products/3rdparty/vtksupport.html
45 =========================================================================*/
46 
47 #ifdef _MSC_VER
48 // Disable warnings that Qt headers give.
49 #pragma warning(disable:4127)
50 #pragma warning(disable:4512)
51 #endif
52 
54 #include "QVTKInteractor.h"
55 
56 #include <QEvent>
57 #include <QSignalMapper>
58 #include <QTimer>
59 #include <QResizeEvent>
60 
61 #include "vtkCommand.h"
62 
63 
64 // function to get VTK keysyms from ascii characters
65 static const char* ascii_to_key_sym(int);
66 // function to get VTK keysyms from Qt keys
67 static const char* qt_key_to_key_sym(Qt::Key, Qt::KeyboardModifiers modifiers);
68 
70  : QObject(parentObject)
71 {
72 }
73 
75 {
76 }
77 
78 bool QVTKMitkInteractorAdapter::ProcessEvent(QEvent* e, vtkRenderWindowInteractor* iren)
79 {
80  if(iren == NULL || e == NULL)
81  return false;
82 
83  const QEvent::Type t = e->type();
84 
85  if(t == QEvent::Resize)
86  {
87  QResizeEvent* e2 = static_cast<QResizeEvent*>(e);
88  QSize size = e2->size();
89  iren->SetSize(size.width(), size.height());
90  return true;
91  }
92 
93  if(t == QEvent::FocusIn)
94  {
95  // For 3DConnexion devices:
96  QVTKInteractor* qiren = QVTKInteractor::SafeDownCast(iren);
97  if(qiren)
98  {
99  qiren->StartListening();
100  }
101  return true;
102  }
103 
104  if(t == QEvent::FocusOut)
105  {
106  // For 3DConnexion devices:
107  QVTKInteractor* qiren = QVTKInteractor::SafeDownCast(iren);
108  if(qiren)
109  {
110  qiren->StopListening();
111  }
112  return true;
113  }
114 
115  // the following events only happen if the interactor is enabled
116  if(!iren->GetEnabled())
117  return false;
118 
119  if(t == QEvent::MouseButtonPress ||
120  t == QEvent::MouseButtonRelease ||
121  t == QEvent::MouseButtonDblClick ||
122  t == QEvent::MouseMove)
123  {
124  QMouseEvent* e2 = static_cast<QMouseEvent*>(e);
125 
126  // give interactor the event information
127  iren->SetEventInformationFlipY(e2->x(), e2->y(),
128  (e2->modifiers() & Qt::ControlModifier) > 0 ? 1 : 0,
129  (e2->modifiers() & Qt::ShiftModifier ) > 0 ? 1 : 0,
130  0,
131  e2->type() == QEvent::MouseButtonDblClick ? 1 : 0);
132 
133  if(t == QEvent::MouseMove)
134  {
135  iren->InvokeEvent(vtkCommand::MouseMoveEvent, e2);
136  }
137  else if(t == QEvent::MouseButtonPress || t == QEvent::MouseButtonDblClick)
138  {
139  switch(e2->button())
140  {
141  case Qt::LeftButton:
142  iren->InvokeEvent(vtkCommand::LeftButtonPressEvent, e2);
143  break;
144 
145  case Qt::MidButton:
146  iren->InvokeEvent(vtkCommand::MiddleButtonPressEvent, e2);
147  break;
148 
149  case Qt::RightButton:
150  iren->InvokeEvent(vtkCommand::RightButtonPressEvent, e2);
151  break;
152 
153  default:
154  break;
155  }
156  }
157  else if(t == QEvent::MouseButtonRelease)
158  {
159  switch(e2->button())
160  {
161  case Qt::LeftButton:
162  iren->InvokeEvent(vtkCommand::LeftButtonReleaseEvent, e2);
163  break;
164 
165  case Qt::MidButton:
166  iren->InvokeEvent(vtkCommand::MiddleButtonReleaseEvent, e2);
167  break;
168 
169  case Qt::RightButton:
170  iren->InvokeEvent(vtkCommand::RightButtonReleaseEvent, e2);
171  break;
172 
173  default:
174  break;
175  }
176  }
177  return true;
178  }
179 
180  if(t == QEvent::Enter)
181  {
182  iren->InvokeEvent(vtkCommand::EnterEvent, e);
183  return true;
184  }
185 
186  if(t == QEvent::Leave)
187  {
188  iren->InvokeEvent(vtkCommand::LeaveEvent, e);
189  return true;
190  }
191 
192  if(t == QEvent::KeyPress || t == QEvent::KeyRelease)
193  {
194  QKeyEvent* e2 = static_cast<QKeyEvent*>(e);
195 
196  // get key and keysym information
197  int ascii_key = e2->text().length() ? e2->text().unicode()->toLatin1() : 0;
198  const char* keysym = ascii_to_key_sym(ascii_key);
199  if(!keysym ||
200  e2->modifiers() == Qt::KeypadModifier)
201  {
202  // get virtual keys
203  keysym = qt_key_to_key_sym(static_cast<Qt::Key>(e2->key()),
204  e2->modifiers());
205  }
206 
207  if(!keysym)
208  {
209  keysym = "None";
210  }
211 
212  // give interactor event information
213  iren->SetKeyEventInformation(
214  (e2->modifiers() & Qt::ControlModifier),
215  (e2->modifiers() & Qt::ShiftModifier),
216  ascii_key, e2->count(), keysym);
217 
218  if(t == QEvent::KeyPress)
219  {
220  // invoke vtk event
221  iren->InvokeEvent(vtkCommand::KeyPressEvent, e2);
222 
223  // invoke char event only for ascii characters
224  if(ascii_key)
225  {
226  iren->InvokeEvent(vtkCommand::CharEvent, e2);
227  }
228  }
229  else
230  {
231  iren->InvokeEvent(vtkCommand::KeyReleaseEvent, e2);
232  }
233  return true;
234  }
235 
236  if(t == QEvent::Wheel)
237  {
238  QWheelEvent* e2 = static_cast<QWheelEvent*>(e);
239 
240  iren->SetEventInformationFlipY(e2->x(), e2->y(),
241  (e2->modifiers() & Qt::ControlModifier) > 0 ? 1 : 0,
242  (e2->modifiers() & Qt::ShiftModifier ) > 0 ? 1 : 0);
243 
244  // invoke vtk event
245  // if delta is positive, it is a forward wheel event
246  if(e2->delta() > 0)
247  {
248  iren->InvokeEvent(vtkCommand::MouseWheelForwardEvent, e2);
249  }
250  else
251  {
252  iren->InvokeEvent(vtkCommand::MouseWheelBackwardEvent, e2);
253  }
254  return true;
255  }
256 
257  if(t == QEvent::ContextMenu)
258  {
259  QContextMenuEvent* e2 = static_cast<QContextMenuEvent*>(e);
260 
261  // give interactor the event information
262  iren->SetEventInformationFlipY(e2->x(), e2->y(),
263  (e2->modifiers() & Qt::ControlModifier) > 0 ? 1 : 0,
264  (e2->modifiers() & Qt::ShiftModifier ) > 0 ? 1 : 0);
265 
266  // invoke event and pass qt event for additional data as well
267  iren->InvokeEvent(QVTKInteractor::ContextMenuEvent, e2);
268 
269  return true;
270  }
271 
272  if(t == QEvent::DragEnter)
273  {
274  QDragEnterEvent* e2 = static_cast<QDragEnterEvent*>(e);
275 
276  // invoke event and pass qt event for additional data as well
277  iren->InvokeEvent(QVTKInteractor::DragEnterEvent, e2);
278 
279  return true;
280  }
281 
282  if(t == QEvent::DragLeave)
283  {
284  QDragLeaveEvent* e2 = static_cast<QDragLeaveEvent*>(e);
285 
286  // invoke event and pass qt event for additional data as well
287  iren->InvokeEvent(QVTKInteractor::DragLeaveEvent, e2);
288 
289  return true;
290  }
291 
292  if(t == QEvent::DragMove)
293  {
294  QDragMoveEvent* e2 = static_cast<QDragMoveEvent*>(e);
295 
296  // give interactor the event information
297  iren->SetEventInformationFlipY(e2->pos().x(), e2->pos().y());
298 
299  // invoke event and pass qt event for additional data as well
300  iren->InvokeEvent(QVTKInteractor::DragMoveEvent, e2);
301  return true;
302  }
303 
304  if(t == QEvent::Drop)
305  {
306  QDropEvent* e2 = static_cast<QDropEvent*>(e);
307 
308  // give interactor the event information
309  iren->SetEventInformationFlipY(e2->pos().x(), e2->pos().y());
310 
311  // invoke event and pass qt event for additional data as well
312  iren->InvokeEvent(QVTKInteractor::DropEvent, e2);
313  return true;
314  }
315 
316  return false;
317 }
318 
319 // ***** keysym stuff below *****
320 
321 static const char *AsciiToKeySymTable[] = {
322  0, 0, 0, 0, 0, 0, 0, 0, 0, "Tab", 0, 0, 0, 0, 0, 0,
323  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
324  "space", "exclam", "quotedbl", "numbersign",
325  "dollar", "percent", "ampersand", "quoteright",
326  "parenleft", "parenright", "asterisk", "plus",
327  "comma", "minus", "period", "slash",
328  "0", "1", "2", "3", "4", "5", "6", "7",
329  "8", "9", "colon", "semicolon", "less", "equal", "greater", "question",
330  "at", "A", "B", "C", "D", "E", "F", "G",
331  "H", "I", "J", "K", "L", "M", "N", "O",
332  "P", "Q", "R", "S", "T", "U", "V", "W",
333  "X", "Y", "Z", "bracketleft",
334  "backslash", "bracketright", "asciicircum", "underscore",
335  "quoteleft", "a", "b", "c", "d", "e", "f", "g",
336  "h", "i", "j", "k", "l", "m", "n", "o",
337  "p", "q", "r", "s", "t", "u", "v", "w",
338  "x", "y", "z", "braceleft", "bar", "braceright", "asciitilde", "Delete",
339  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
340  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
341  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
342  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
343  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
344  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
345  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
346  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
347 
348 const char* ascii_to_key_sym(int i)
349 {
350  if(i >= 0)
351  {
352  return AsciiToKeySymTable[i];
353  }
354  return 0;
355 }
356 
357 #define QVTK_HANDLE(x,y) \
358  case x : \
359  ret = y; \
360  break;
361 
362 #define QVTK_HANDLE_KEYPAD(x, y, z) \
363  case x : \
364  ret = (modifiers & Qt::KeypadModifier) ? (y) : (z); \
365  break;
366 
367 const char* qt_key_to_key_sym(Qt::Key i, Qt::KeyboardModifiers modifiers)
368 {
369  const char* ret = 0;
370  switch(i)
371  {
372  // Cancel
373  QVTK_HANDLE(Qt::Key_Backspace, "BackSpace")
374  QVTK_HANDLE(Qt::Key_Tab, "Tab")
376  QVTK_HANDLE(Qt::Key_Clear, "Clear")
377  QVTK_HANDLE(Qt::Key_Return, "Return")
378  QVTK_HANDLE(Qt::Key_Enter, "Return")
379  QVTK_HANDLE(Qt::Key_Shift, "Shift_L")
380  QVTK_HANDLE(Qt::Key_Control, "Control_L")
381  QVTK_HANDLE(Qt::Key_Alt, "Alt_L")
382  QVTK_HANDLE(Qt::Key_Pause, "Pause")
383  QVTK_HANDLE(Qt::Key_CapsLock, "Caps_Lock")
384  QVTK_HANDLE(Qt::Key_Escape, "Escape")
385  QVTK_HANDLE(Qt::Key_Space, "space")
386  QVTK_HANDLE(Qt::Key_PageUp, "Prior")
388  QVTK_HANDLE(Qt::Key_End, "End")
389  QVTK_HANDLE(Qt::Key_Home, "Home")
390  QVTK_HANDLE(Qt::Key_Left, "Left")
391  QVTK_HANDLE(Qt::Key_Up, "Up")
392  QVTK_HANDLE(Qt::Key_Right, "Right")
393  QVTK_HANDLE(Qt::Key_Down, "Down")
394  QVTK_HANDLE(Qt::Key_Select, "Select")
395  QVTK_HANDLE(Qt::Key_Execute, "Execute")
396  QVTK_HANDLE(Qt::Key_SysReq, "Snapshot")
397  QVTK_HANDLE(Qt::Key_Insert, "Insert")
398  QVTK_HANDLE(Qt::Key_Delete, "Delete")
399  QVTK_HANDLE(Qt::Key_Help, "Help")
400  QVTK_HANDLE_KEYPAD(Qt::Key_0, "KP_0", "0")
401  QVTK_HANDLE_KEYPAD(Qt::Key_1, "KP_1", "1")
402  QVTK_HANDLE_KEYPAD(Qt::Key_2, "KP_2", "2")
403  QVTK_HANDLE_KEYPAD(Qt::Key_3, "KP_3", "3")
404  QVTK_HANDLE_KEYPAD(Qt::Key_4, "KP_4", "4")
405  QVTK_HANDLE_KEYPAD(Qt::Key_5, "KP_5", "5")
406  QVTK_HANDLE_KEYPAD(Qt::Key_6, "KP_6", "6")
407  QVTK_HANDLE_KEYPAD(Qt::Key_7, "KP_7", "7")
408  QVTK_HANDLE_KEYPAD(Qt::Key_8, "KP_8", "8")
409  QVTK_HANDLE_KEYPAD(Qt::Key_9, "KP_9", "9")
410  QVTK_HANDLE(Qt::Key_A, "a")
411  QVTK_HANDLE(Qt::Key_B, "b")
412  QVTK_HANDLE(Qt::Key_C, "c")
413  QVTK_HANDLE(Qt::Key_D, "d")
414  QVTK_HANDLE(Qt::Key_E, "e")
415  QVTK_HANDLE(Qt::Key_F, "f")
416  QVTK_HANDLE(Qt::Key_G, "g")
417  QVTK_HANDLE(Qt::Key_H, "h")
418  QVTK_HANDLE(Qt::Key_I, "i")
419  QVTK_HANDLE(Qt::Key_J, "h")
420  QVTK_HANDLE(Qt::Key_K, "k")
421  QVTK_HANDLE(Qt::Key_L, "l")
422  QVTK_HANDLE(Qt::Key_M, "m")
423  QVTK_HANDLE(Qt::Key_N, "n")
424  QVTK_HANDLE(Qt::Key_O, "o")
425  QVTK_HANDLE(Qt::Key_P, "p")
426  QVTK_HANDLE(Qt::Key_Q, "q")
427  QVTK_HANDLE(Qt::Key_R, "r")
428  QVTK_HANDLE(Qt::Key_S, "s")
429  QVTK_HANDLE(Qt::Key_T, "t")
430  QVTK_HANDLE(Qt::Key_U, "u")
431  QVTK_HANDLE(Qt::Key_V, "v")
432  QVTK_HANDLE(Qt::Key_W, "w")
433  QVTK_HANDLE(Qt::Key_X, "x")
434  QVTK_HANDLE(Qt::Key_Y, "y")
435  QVTK_HANDLE(Qt::Key_Z, "z")
436  QVTK_HANDLE(Qt::Key_Asterisk, "asterisk")
437  QVTK_HANDLE(Qt::Key_Plus, "plus")
438  QVTK_HANDLE(Qt::Key_Bar, "bar")
439  QVTK_HANDLE(Qt::Key_Minus, "minus")
440  QVTK_HANDLE(Qt::Key_Period, "period")
441  QVTK_HANDLE(Qt::Key_Slash, "slash")
442  QVTK_HANDLE(Qt::Key_F1, "F1")
443  QVTK_HANDLE(Qt::Key_F2, "F2")
444  QVTK_HANDLE(Qt::Key_F3, "F3")
445  QVTK_HANDLE(Qt::Key_F4, "F4")
446  QVTK_HANDLE(Qt::Key_F5, "F5")
447  QVTK_HANDLE(Qt::Key_F6, "F6")
448  QVTK_HANDLE(Qt::Key_F7, "F7")
449  QVTK_HANDLE(Qt::Key_F8, "F8")
450  QVTK_HANDLE(Qt::Key_F9, "F9")
451  QVTK_HANDLE(Qt::Key_F10, "F10")
452  QVTK_HANDLE(Qt::Key_F11, "F11")
453  QVTK_HANDLE(Qt::Key_F12, "F12")
454  QVTK_HANDLE(Qt::Key_F13, "F13")
455  QVTK_HANDLE(Qt::Key_F14, "F14")
456  QVTK_HANDLE(Qt::Key_F15, "F15")
457  QVTK_HANDLE(Qt::Key_F16, "F16")
458  QVTK_HANDLE(Qt::Key_F17, "F17")
459  QVTK_HANDLE(Qt::Key_F18, "F18")
460  QVTK_HANDLE(Qt::Key_F19, "F19")
461  QVTK_HANDLE(Qt::Key_F20, "F20")
462  QVTK_HANDLE(Qt::Key_F21, "F21")
463  QVTK_HANDLE(Qt::Key_F22, "F22")
464  QVTK_HANDLE(Qt::Key_F23, "F23")
465  QVTK_HANDLE(Qt::Key_F24, "F24")
466  QVTK_HANDLE(Qt::Key_NumLock, "Num_Lock")
467  QVTK_HANDLE(Qt::Key_ScrollLock, "Scroll_Lock")
468 
469  default:
470  break;
471  }
472  return ret;
473 }
static const char * AsciiToKeySymTable[]
static const char * qt_key_to_key_sym(Qt::Key, Qt::KeyboardModifiers modifiers)
#define QVTK_HANDLE_KEYPAD(x, y, z)
static const char * ascii_to_key_sym(int)
bool ProcessEvent(QEvent *e, vtkRenderWindowInteractor *iren)
#define QVTK_HANDLE(x, y)