ctkVTKObjectTestHelper.cpp 7.6 KB

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