ctkTestApplication.cpp 7.3 KB

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