ctkVTKObjectTestHelper.cpp 8.4 KB

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