ctkTestApplication.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. All rights reserved.
  5. Distributed under a BSD License. See LICENSE.txt file.
  6. This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the above copyright notice for more information.
  9. =========================================================================*/
  10. /*=========================================================================
  11. Program: Visualization Toolkit
  12. Module: $RCSfile: ctkTestApplication.cxx,v $
  13. Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  14. All rights reserved.
  15. See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
  16. This software is distributed WITHOUT ANY WARRANTY; without even
  17. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  18. PURPOSE. See the above copyright notice for more information.
  19. =========================================================================*/
  20. /*-------------------------------------------------------------------------
  21. Copyright 2008 Sandia Corporation.
  22. Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
  23. the U.S. Government retains certain rights in this software.
  24. -------------------------------------------------------------------------*/
  25. // Qt includes
  26. #include <QTimer>
  27. #include <QWidget>
  28. #include <QKeyEvent>
  29. #include <QMouseEvent>
  30. // CTK includes
  31. #include "ctkTestApplication.h"
  32. // STD includes
  33. #include <iostream>
  34. int ctkTestApplication::Error = 0;
  35. //-----------------------------------------------------------------------------
  36. ctkTestApplication::ctkTestApplication(int _argc, char** _argv)
  37. {
  38. qInstallMsgHandler(ctkTestApplication::messageHandler);
  39. // CMake generated driver removes argv[0],
  40. // so let's put a dummy back in
  41. this->Argv.append("ctkTestApplication");
  42. for(int i=0; i<_argc; i++)
  43. {
  44. this->Argv.append(_argv[i]);
  45. }
  46. for(int j=0; j<this->Argv.size(); j++)
  47. {
  48. this->Argvp.append(this->Argv[j].data());
  49. }
  50. this->Argc = this->Argvp.size();
  51. this->App = new QApplication(this->Argc, this->Argvp.data());
  52. }
  53. //-----------------------------------------------------------------------------
  54. ctkTestApplication::~ctkTestApplication()
  55. {
  56. delete this->App;
  57. qInstallMsgHandler(0);
  58. }
  59. //-----------------------------------------------------------------------------
  60. void ctkTestApplication::runTestSlot()
  61. {
  62. this->runTest();
  63. }
  64. //-----------------------------------------------------------------------------
  65. void ctkTestApplication::runTest()
  66. {
  67. }
  68. //-----------------------------------------------------------------------------
  69. int ctkTestApplication::exec(bool reportErrorsOnExit)
  70. {
  71. if(QCoreApplication::arguments().contains("--exit"))
  72. {
  73. QTimer::singleShot(100, QApplication::instance(),
  74. SLOT(quit()));
  75. }
  76. int ret = QApplication::exec();
  77. if (reportErrorsOnExit)
  78. {
  79. return Error + ret;
  80. }
  81. else
  82. {
  83. return ret;
  84. }
  85. }
  86. //-----------------------------------------------------------------------------
  87. void ctkTestApplication::messageHandler(QtMsgType type, const char *msg)
  88. {
  89. switch(type)
  90. {
  91. case QtDebugMsg:
  92. std::cerr << "Debug: " << msg << std::endl;
  93. break;
  94. case QtWarningMsg:
  95. std::cerr << "Warning: " << msg << std::endl;
  96. Error++;
  97. break;
  98. case QtCriticalMsg:
  99. std::cerr << "Critical: " << msg << std::endl;
  100. Error++;
  101. break;
  102. case QtFatalMsg:
  103. std::cerr << "Fatal: " << msg << std::endl;
  104. abort();
  105. }
  106. }
  107. //-----------------------------------------------------------------------------
  108. void ctkTestApplication::delay(int ms)
  109. {
  110. if(ms > 0)
  111. {
  112. QTimer::singleShot(ms, QApplication::instance(), SLOT(quit()));
  113. QApplication::exec();
  114. }
  115. }
  116. //-----------------------------------------------------------------------------
  117. bool ctkTestApplication::simulateEvent(QWidget* w, QEvent* e)
  118. {
  119. bool status = QApplication::sendEvent(w, e);
  120. QApplication::processEvents();
  121. return status;
  122. }
  123. //-----------------------------------------------------------------------------
  124. void ctkTestApplication::keyUp(QWidget* w, Qt::Key key, Qt::KeyboardModifiers mod, int ms)
  125. {
  126. if(!w)
  127. return;
  128. delay(ms);
  129. QString text;
  130. char off = 'a';
  131. if(mod & Qt::ShiftModifier)
  132. off = 'A';
  133. if(key >= Qt::Key_A && key <= Qt::Key_Z)
  134. {
  135. text.append(QChar::fromAscii(key - Qt::Key_A + off));
  136. }
  137. QKeyEvent e(QEvent::KeyRelease, key, mod, text);
  138. if(!simulateEvent(w, &e))
  139. {
  140. qWarning("keyUp not handled\n");
  141. }
  142. }
  143. //-----------------------------------------------------------------------------
  144. void ctkTestApplication::keyDown(QWidget* w, Qt::Key key, Qt::KeyboardModifiers mod, int ms)
  145. {
  146. if(!w)
  147. return;
  148. delay(ms);
  149. QString text;
  150. char off = 'a';
  151. if(mod & Qt::ShiftModifier)
  152. off = 'A';
  153. if(key >= Qt::Key_A && key <= Qt::Key_Z)
  154. {
  155. text.append(QChar::fromAscii(key - Qt::Key_A + off));
  156. }
  157. QKeyEvent e(QEvent::KeyPress, key, mod, text);
  158. if(!simulateEvent(w, &e))
  159. {
  160. qWarning("keyDown not handled\n");
  161. }
  162. }
  163. //-----------------------------------------------------------------------------
  164. void ctkTestApplication::keyClick(QWidget* w, Qt::Key key, Qt::KeyboardModifiers mod, int ms)
  165. {
  166. delay(ms);
  167. keyDown(w, key, mod, 0);
  168. keyUp(w, key, mod, 0);
  169. }
  170. //-----------------------------------------------------------------------------
  171. void ctkTestApplication::mouseDown(QWidget* w, QPoint pos, Qt::MouseButton btn,
  172. Qt::KeyboardModifiers mod, int ms)
  173. {
  174. delay(ms);
  175. QMouseEvent e(QEvent::MouseButtonPress, pos, btn, btn, mod);
  176. if(!simulateEvent(w, &e))
  177. {
  178. qWarning("mouseDown not handled\n");
  179. }
  180. }
  181. //-----------------------------------------------------------------------------
  182. void ctkTestApplication::mouseUp(QWidget* w, QPoint pos, Qt::MouseButton btn,
  183. Qt::KeyboardModifiers mod, int ms)
  184. {
  185. delay(ms);
  186. QMouseEvent e(QEvent::MouseButtonRelease, pos, btn, btn, mod);
  187. if(!simulateEvent(w, &e))
  188. {
  189. qWarning("mouseUp not handled\n");
  190. }
  191. }
  192. //-----------------------------------------------------------------------------
  193. void ctkTestApplication::mouseMove(QWidget* w, QPoint pos, Qt::MouseButton btn,
  194. Qt::KeyboardModifiers mod, int ms)
  195. {
  196. delay(ms);
  197. QMouseEvent e(QEvent::MouseMove, pos, btn, btn, mod);
  198. if(!simulateEvent(w, &e))
  199. {
  200. qWarning("mouseMove not handled\n");
  201. }
  202. }
  203. //-----------------------------------------------------------------------------
  204. void ctkTestApplication::mouseClick(QWidget* w, QPoint pos, Qt::MouseButton btn,
  205. Qt::KeyboardModifiers mod, int ms)
  206. {
  207. delay(ms);
  208. mouseDown(w, pos, btn, mod, 0);
  209. mouseUp(w, pos, btn, mod, 0);
  210. }
  211. //-----------------------------------------------------------------------------
  212. void ctkTestApplication::mouseDClick(QWidget* w, QPoint pos, Qt::MouseButton btn,
  213. Qt::KeyboardModifiers mod, int ms)
  214. {
  215. delay(ms);
  216. QMouseEvent e(QEvent::MouseButtonDblClick, pos, btn, btn, mod);
  217. if(!simulateEvent(w, &e))
  218. {
  219. qWarning("mouseMove not handled\n");
  220. }
  221. }