ctkVTKObjectTestHelper.cpp 7.4 KB

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