ctkVTKObjectTestHelper.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 <QDebug>
  16. #include <QApplication>
  17. // CTKVTK includes
  18. #include "ctkVTKObjectTestHelper.h"
  19. // VTK includes
  20. #include <vtkObject.h>
  21. #include <vtkTimerLog.h>
  22. #include <vtkSmartPointer.h>
  23. //------------------------------------------------------------------------------
  24. class ctkVTKObjectTestPrivate
  25. {
  26. public:
  27. ctkVTKObjectTestPrivate();
  28. int PublicSlotCalled ;
  29. int ProtectedSlotCalled;
  30. int PrivateSlotCalled;
  31. };
  32. //------------------------------------------------------------------------------
  33. ctkVTKObjectTestPrivate::ctkVTKObjectTestPrivate()
  34. {
  35. this->PublicSlotCalled = 0;
  36. this->ProtectedSlotCalled = 0;
  37. this->PrivateSlotCalled = 0;
  38. }
  39. //------------------------------------------------------------------------------
  40. ctkVTKObjectTest::ctkVTKObjectTest(QObject* parentObject)
  41. : QObject(parentObject)
  42. , d_ptr(new ctkVTKObjectTestPrivate)
  43. {
  44. }
  45. //------------------------------------------------------------------------------
  46. ctkVTKObjectTest::~ctkVTKObjectTest()
  47. {
  48. }
  49. //------------------------------------------------------------------------------
  50. bool ctkVTKObjectTest::test()
  51. {
  52. Q_D(ctkVTKObjectTest);
  53. // should do nothing but shouldn't fail neither
  54. qDebug() << "The following can generate error message.";
  55. qDebug() << "Disconnect:";
  56. this->qvtkDisconnect(0, static_cast<unsigned long>(-1), this, SLOT(onVTKObjectModifiedPublic()));
  57. qDebug() << "Connect:";
  58. QString connection = this->qvtkConnect(0, static_cast<unsigned long>(-1), this, SLOT(onVTKObjectModifiedPublic()));
  59. if (!connection.isEmpty())
  60. {
  61. qDebug() << "ctkVTKObject::qvtkConnect() failed: "<< connection;
  62. return false;
  63. }
  64. qDebug() << "Reconnect:";
  65. connection = this->qvtkReconnect(0, 0, static_cast<unsigned long>(-1), this, SLOT(onVTKObjectModifiedPublic()));
  66. if (!connection.isEmpty())
  67. {
  68. qDebug() << "ctkVTKObject::qvtkReconnect() failed: "<< connection;
  69. return false;
  70. }
  71. qDebug() << "End of possible error messages.";
  72. vtkObject* object = vtkObject::New();
  73. int numberOfConnections=10000;
  74. qDebug() << "Create "<<numberOfConnections<<" connections...";
  75. vtkSmartPointer<vtkTimerLog> timerLog = vtkSmartPointer<vtkTimerLog>::New();
  76. timerLog->StartTimer();
  77. for (int i = 1; i <= numberOfConnections; ++i)
  78. {
  79. this->qvtkConnect(object, i, this, SLOT(onVTKObjectModifiedPublic()));
  80. }
  81. timerLog->StopTimer();
  82. double t1 = timerLog->GetElapsedTime();
  83. qDebug() << " elapsed time: " << t1 << "seconds";
  84. qDebug() << "Remove "<<numberOfConnections<<" connections...";
  85. timerLog->StartTimer();
  86. for (int i = numberOfConnections; i>=1 ; --i)
  87. {
  88. this->qvtkDisconnect(object, i, this, SLOT(onVTKObjectModifiedPublic()));
  89. }
  90. timerLog->StopTimer();
  91. t1 = timerLog->GetElapsedTime();
  92. qDebug() << " elapsed time: " << t1 << "seconds";
  93. connection = this->qvtkConnect(object, vtkCommand::ModifiedEvent,
  94. this, SLOT(onVTKObjectModifiedPublic()));
  95. if (connection.isEmpty() || object->GetReferenceCount() != 1)
  96. {
  97. qDebug() << "ctkVTKObject::qvtkConnect() failed: "<< connection;
  98. return false;
  99. }
  100. object->Modified();
  101. if (d->PublicSlotCalled != 1)
  102. {
  103. qDebug() << "qvtkConnect failed";
  104. return false;
  105. }
  106. this->resetSlotCalls();
  107. // should do nothing...
  108. connection = this->qvtkConnect(object, vtkCommand::ModifiedEvent,
  109. this, SLOT(onVTKObjectModifiedPublic()));
  110. if (!connection.isEmpty())
  111. {
  112. qDebug() << __LINE__ << "ctkVTKObject::qvtkConnect() failed: "<< connection;
  113. return false;
  114. }
  115. object->Modified();
  116. if (d->PublicSlotCalled != 1)
  117. {
  118. qDebug() << __LINE__ << "qvtkConnect failed";
  119. return false;
  120. }
  121. this->resetSlotCalls();
  122. this->qvtkDisconnect(object, vtkCommand::WarningEvent,
  123. this, SLOT(onVTKObjectModifiedPublic()));
  124. object->Modified();
  125. if (d->PublicSlotCalled != 1)
  126. {
  127. qDebug() << __LINE__ << "qvtkDisconnect failed" << d->PublicSlotCalled;
  128. return false;
  129. }
  130. this->resetSlotCalls();
  131. this->qvtkDisconnect(object, vtkCommand::ModifiedEvent,
  132. this, SLOT(onVTKObjectModifiedPublic()));
  133. QCoreApplication::instance()->processEvents();
  134. object->Modified();
  135. if (d->PublicSlotCalled != 0)
  136. {
  137. qDebug() << __LINE__ << "qvtkDisconnect failed" << d->PublicSlotCalled;
  138. return false;
  139. }
  140. this->resetSlotCalls();
  141. // Set a new connection (protected)
  142. connection = this->qvtkConnect(object, vtkCommand::ModifiedEvent,
  143. this, SLOT(onVTKObjectModifiedProtected()));
  144. if (connection.isEmpty())
  145. {
  146. qDebug() << __LINE__ << "ctkVTKObject::qvtkConnect() failed: "<< connection;
  147. return false;
  148. }
  149. object->Modified();
  150. if (d->ProtectedSlotCalled != 1)
  151. {
  152. qDebug() << __LINE__ << "ctkVTKObject::qvtkConnect failed" << d->ProtectedSlotCalled;
  153. return false;
  154. }
  155. this->resetSlotCalls();
  156. // remove the connection using flags, 0 means any event, qt object or slot
  157. this->qvtkDisconnect(object, vtkCommand::NoEvent, 0, 0);
  158. object->Modified();
  159. if (d->ProtectedSlotCalled != 0)
  160. {
  161. qDebug() << __LINE__ << "qvtkDisconnect failed" << d->ProtectedSlotCalled;
  162. return false;
  163. }
  164. this->resetSlotCalls();
  165. // Set new connections
  166. this->qvtkConnect(object, vtkCommand::ModifiedEvent,
  167. this, SLOT(onVTKObjectModifiedProtected()));
  168. this->qvtkConnect(object, vtkCommand::ModifiedEvent,
  169. this, SLOT(onVTKObjectModifiedPrivate()));
  170. object->Modified();
  171. if (d->ProtectedSlotCalled != 1 ||
  172. d->PrivateSlotCalled != 1)
  173. {
  174. qDebug() << __LINE__ << "qvtkConnect failed"
  175. << d->ProtectedSlotCalled
  176. << d->PrivateSlotCalled;
  177. return false;
  178. }
  179. this->resetSlotCalls();
  180. // remove the connection using flags, 0 means any event, qt object or slot
  181. this->qvtkDisconnect(object, vtkCommand::ModifiedEvent, this, 0);
  182. object->Modified();
  183. if (d->ProtectedSlotCalled != 0 || d->PrivateSlotCalled != 0)
  184. {
  185. qDebug() << __LINE__ << "qvtkDisconnect failed"
  186. << d->ProtectedSlotCalled
  187. << d->PrivateSlotCalled;
  188. return false;
  189. }
  190. this->resetSlotCalls();
  191. // Set new connections
  192. this->qvtkConnect(object, vtkCommand::ModifiedEvent,
  193. this, SLOT(onVTKObjectModifiedPublic()));
  194. this->qvtkConnect(object, vtkCommand::WarningEvent,
  195. this, SLOT(onVTKObjectModifiedPublic()));
  196. int disconnected = this->qvtkDisconnect(object, vtkCommand::NoEvent,
  197. this, SLOT(onVTKObjectModifiedPublic()));
  198. if (disconnected != 2)
  199. {
  200. qDebug() << __LINE__ << "qvtkDisconnect failed" << disconnected;
  201. return false;
  202. }
  203. object->InvokeEvent(vtkCommand::ModifiedEvent, 0);
  204. object->InvokeEvent(vtkCommand::WarningEvent, 0);
  205. if (d->PublicSlotCalled != 0)
  206. {
  207. qDebug() << __LINE__ << "qvtkConnect failed"
  208. << d->PublicSlotCalled;
  209. return false;
  210. }
  211. this->resetSlotCalls();
  212. disconnected = this->qvtkDisconnectAll();
  213. if (disconnected != 0)
  214. {
  215. qDebug() << __LINE__ << "qvtkDisconnectAll failed" << disconnected;
  216. return false;
  217. }
  218. this->qvtkConnect(object, vtkCommand::ModifiedEvent,
  219. this, SLOT(deleteConnection()));
  220. object->InvokeEvent(vtkCommand::ModifiedEvent, 0);
  221. object->Delete();
  222. return true;
  223. }
  224. //------------------------------------------------------------------------------
  225. void ctkVTKObjectTest::resetSlotCalls()
  226. {
  227. Q_D(ctkVTKObjectTest);
  228. d->PublicSlotCalled = 0;
  229. d->ProtectedSlotCalled = 0;
  230. d->PrivateSlotCalled = 0;
  231. }
  232. //------------------------------------------------------------------------------
  233. void ctkVTKObjectTest::emitSignalEmitted()
  234. {
  235. emit signalEmitted();
  236. }
  237. //------------------------------------------------------------------------------
  238. void ctkVTKObjectTest::onVTKObjectModifiedPublic()
  239. {
  240. Q_D(ctkVTKObjectTest);
  241. //qDebug() << __FUNCTION__;
  242. d->PublicSlotCalled = true;
  243. }
  244. //------------------------------------------------------------------------------
  245. void ctkVTKObjectTest::deleteConnection()
  246. {
  247. //qDebug() << __FUNCTION__;
  248. this->qvtkDisconnect(0, vtkCommand::NoEvent, 0, 0);
  249. }
  250. //------------------------------------------------------------------------------
  251. void ctkVTKObjectTest::onVTKObjectModifiedProtected()
  252. {
  253. Q_D(ctkVTKObjectTest);
  254. //qDebug() << __FUNCTION__;
  255. d->ProtectedSlotCalled = true;
  256. }
  257. //------------------------------------------------------------------------------
  258. void ctkVTKObjectTest::onVTKObjectModifiedPrivate()
  259. {
  260. Q_D(ctkVTKObjectTest);
  261. //qDebug() << __FUNCTION__;
  262. d->PrivateSlotCalled = true;
  263. }