ctkVTKRenderViewEventTranslator.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QCoreApplication>
  16. #include <QDebug>
  17. #include <QEvent>
  18. #include <QWheelEvent>
  19. // CTK includes
  20. #include "ctkVTKRenderView.h"
  21. #include "ctkVTKRenderViewEventTranslator.h"
  22. #include "ctkCheckableModelHelper.h"
  23. // ----------------------------------------------------------------------------
  24. ctkVTKRenderViewEventTranslator::ctkVTKRenderViewEventTranslator(const QByteArray& Classname, QObject* Parent)
  25. : pqWidgetEventTranslator(Parent),
  26. mClassType(Classname),
  27. lastMoveEvent(QEvent::MouseButtonPress, QPoint(), Qt::MouseButton(),
  28. Qt::MouseButtons(), Qt::KeyboardModifiers()),
  29. oldMoveEvent(QEvent::MouseMove, QPoint(), Qt::MouseButton(),
  30. Qt::MouseButtons(), Qt::KeyboardModifiers()),
  31. lastMouseEvent(QEvent::MouseButtonRelease, QPoint(), Qt::MouseButton(),
  32. Qt::MouseButtons(), Qt::KeyboardModifiers())
  33. {
  34. }
  35. // ----------------------------------------------------------------------------
  36. ctkVTKRenderViewEventTranslator::~ctkVTKRenderViewEventTranslator()
  37. {
  38. }
  39. // ----------------------------------------------------------------------------
  40. bool ctkVTKRenderViewEventTranslator::translateEvent(QObject *Object,
  41. QEvent *Event,
  42. bool &Error)
  43. {
  44. Q_UNUSED(Error);
  45. QWidget* widget = qobject_cast<QWidget*>(Object);
  46. if(!widget || !Object->inherits(mClassType.data()))
  47. {
  48. return false;
  49. }
  50. bool handled = false;
  51. switch(Event->type())
  52. {
  53. case QEvent::ContextMenu:
  54. handled=true;
  55. break;
  56. case QEvent::Wheel:
  57. {
  58. QWheelEvent* wheelEvent = dynamic_cast<QWheelEvent*>(Event);
  59. if(wheelEvent)
  60. {
  61. QSize size = widget->size();
  62. double normalized_x = wheelEvent->x()/static_cast<double>(size.width()/2.0);
  63. double normalized_y = wheelEvent->y()/static_cast<double>(size.height()/2.0);
  64. int numStep = (wheelEvent->delta() > 0 ) ? 1 : 0;
  65. int buttons = wheelEvent->buttons();
  66. int modifiers = wheelEvent->modifiers();
  67. emit emit recordEvent(Object, "mouseWheel", QString("(%1,%2,%3,%4,%5)")
  68. .arg(normalized_x)
  69. .arg(normalized_y)
  70. .arg(numStep)
  71. .arg(buttons)
  72. .arg(modifiers));
  73. }
  74. }
  75. handled = true;
  76. break;
  77. case QEvent::MouseButtonPress:
  78. {
  79. QMouseEvent* mouseEvent = dynamic_cast<QMouseEvent*>(Event);
  80. if (mouseEvent)
  81. {
  82. QSize size = widget->size();
  83. int x = mouseEvent->x();
  84. int y = mouseEvent->y();
  85. double x_center = static_cast<double>(size.width())/2.0;
  86. double y_center = static_cast<double>(size.height())/2.0;
  87. double x_norm = (x_center - x)/static_cast<double>(size.width()/2.0);
  88. double y_norm = (y_center - y)/static_cast<double>(size.height()/2.0);
  89. int button = mouseEvent->button();
  90. int buttons = mouseEvent->buttons();
  91. int modifiers = mouseEvent->modifiers();
  92. emit recordEvent(Object, "mouseMove", QString("(%1,%2,%3,%4,%5)")
  93. .arg(x_norm)
  94. .arg(y_norm)
  95. .arg(button)
  96. .arg(buttons)
  97. .arg(modifiers));
  98. emit recordEvent(Object, "mousePress", QString("(%1,%2,%3,%4,%5)")
  99. .arg(x_norm)
  100. .arg(y_norm)
  101. .arg(button)
  102. .arg(buttons)
  103. .arg(modifiers));
  104. }
  105. // reset lastMoveEvent
  106. QMouseEvent e(QEvent::MouseButtonPress, QPoint(), Qt::MouseButton(),
  107. Qt::MouseButtons(), Qt::KeyboardModifiers());
  108. lastMoveEvent = e;
  109. lastMouseEvent = e;
  110. }
  111. handled = true;
  112. break;
  113. case QEvent::MouseMove:
  114. {
  115. QMouseEvent* mouseEvent = dynamic_cast<QMouseEvent*>(Event);
  116. if (mouseEvent)
  117. {
  118. QMouseEvent e(QEvent::MouseMove, QPoint(mouseEvent->x(), mouseEvent->y()),
  119. mouseEvent->button(), mouseEvent->buttons(),
  120. mouseEvent->modifiers());
  121. lastMoveEvent = e;
  122. QSize size = widget->size();
  123. if(lastMouseEvent.type() == QEvent::MouseButtonPress )
  124. {
  125. int x = mouseEvent->x();
  126. int y = mouseEvent->y();
  127. double x_center = static_cast<double>(size.width())/2.0;
  128. double y_center = static_cast<double>(size.height())/2.0;
  129. double x_norm = (x_center - x)/static_cast<double>(size.width()/2.0);
  130. double y_norm = (y_center - y)/static_cast<double>(size.height()/2.0);
  131. int button = mouseEvent->button();
  132. int buttons = mouseEvent->buttons();
  133. int modifiers = mouseEvent->modifiers();
  134. emit recordEvent(Object, "mouseMove", QString("(%1,%2,%3,%4,%5)")
  135. .arg(x_norm)
  136. .arg(y_norm)
  137. .arg(button)
  138. .arg(buttons)
  139. .arg(modifiers));
  140. }
  141. }
  142. }
  143. handled = true;
  144. break;
  145. case QEvent::MouseButtonRelease:
  146. {
  147. QMouseEvent* mouseEvent = dynamic_cast<QMouseEvent*>(Event);
  148. if (mouseEvent)
  149. {
  150. QSize size = widget->size();
  151. // record last move event if it is valid
  152. if(lastMoveEvent.type() == QEvent::MouseMove)
  153. {
  154. int x = mouseEvent->x();
  155. int y = mouseEvent->y();
  156. double x_center = static_cast<double>(size.width())/2.0;
  157. double y_center = static_cast<double>(size.height())/2.0;
  158. double x_norm = (x_center - x)/static_cast<double>(size.width()/2.0);
  159. double y_norm = (y_center - y)/static_cast<double>(size.height()/2.0);
  160. int button = lastMoveEvent.button();
  161. int buttons = lastMoveEvent.buttons();
  162. int modifiers = lastMoveEvent.modifiers();
  163. emit recordEvent(Object, "mouseMove", QString("(%1,%2,%3,%4,%5)")
  164. .arg(x_norm)
  165. .arg(y_norm)
  166. .arg(button)
  167. .arg(buttons)
  168. .arg(modifiers));
  169. }
  170. int x = mouseEvent->x();
  171. int y = mouseEvent->y();
  172. double x_center = static_cast<double>(size.width())/2.0;
  173. double y_center = static_cast<double>(size.height())/2.0;
  174. double x_norm = (x_center - x)/static_cast<double>(size.width ()/2.0);
  175. double y_norm = (y_center - y)/static_cast<double>(size.height()/2.0);
  176. int button = mouseEvent->button();
  177. int buttons = mouseEvent->buttons();
  178. int modifiers = mouseEvent->modifiers();
  179. emit recordEvent(Object, "mouseRelease", QString("(%1,%2,%3,%4,%5)")
  180. .arg(x_norm)
  181. .arg(y_norm)
  182. .arg(button)
  183. .arg(buttons)
  184. .arg(modifiers));
  185. }
  186. // reset lastMoveEvent
  187. QMouseEvent e(QEvent::MouseButtonRelease, QPoint(), Qt::MouseButton(),
  188. Qt::MouseButtons(), Qt::KeyboardModifiers());
  189. lastMouseEvent = e;
  190. }
  191. handled = true;
  192. break;
  193. case QEvent::KeyPress:
  194. case QEvent::KeyRelease:
  195. {
  196. QKeyEvent* ke = static_cast<QKeyEvent*>(Event);
  197. QString data =QString("%1:%2:%3:%4:%5:%6")
  198. .arg(static_cast<int>(ke->type()))
  199. .arg(ke->key())
  200. .arg(static_cast<int>(ke->modifiers()))
  201. .arg(ke->text())
  202. .arg(static_cast<int>(ke->isAutoRepeat()))
  203. .arg(ke->count());
  204. emit recordEvent(Object, "keyEvent", data);
  205. }
  206. break;
  207. default:
  208. break;
  209. }
  210. return handled;
  211. }