ctkVTKObjectEventsObserver.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // Qt includes
  2. #include <QStringList>
  3. #include <QVariant>
  4. #include <QList>
  5. #include <QHash>
  6. #include <QDebug>
  7. // CTK includes
  8. #include "ctkVTKObjectEventsObserver.h"
  9. #include "ctkVTKConnection.h"
  10. // VTK includes
  11. #include <vtkObject.h>
  12. #include <vtkSmartPointer.h>
  13. //-----------------------------------------------------------------------------
  14. class ctkVTKObjectEventsObserverPrivate: public ctkPrivate<ctkVTKObjectEventsObserver>
  15. {
  16. public:
  17. ctkVTKObjectEventsObserverPrivate();
  18. ///
  19. /// Check if a connection has already been added
  20. bool containsConnection(vtkObject* vtk_obj, unsigned long vtk_event,
  21. const QObject* qt_obj, const char* qt_slot);
  22. ///
  23. /// Return a reference toward the corresponding connection or 0 if doesn't exist
  24. ctkVTKConnection* findConnection(const QString& id);
  25. ///
  26. /// Return a reference toward the corresponding connection or 0 if doesn't exist
  27. ctkVTKConnection* findConnection(vtkObject* vtk_obj, unsigned long vtk_event,
  28. const QObject* qt_obj, const char* qt_slot);
  29. ///
  30. /// Return all the references that match the given parameters
  31. QList<ctkVTKConnection*> findConnections(vtkObject* vtk_obj, unsigned long vtk_event,
  32. const QObject* qt_obj, const char* qt_slot);
  33. inline QList<ctkVTKConnection*> connections()const
  34. {
  35. return ctk_p()->findChildren<ctkVTKConnection*>();
  36. }
  37. bool AllEnabled;
  38. bool AllBlocked;
  39. };
  40. //-----------------------------------------------------------------------------
  41. // ctkVTKObjectEventsObserver methods
  42. //-----------------------------------------------------------------------------
  43. ctkVTKObjectEventsObserver::ctkVTKObjectEventsObserver(QObject* _parent):Superclass(_parent)
  44. {
  45. CTK_INIT_PRIVATE(ctkVTKObjectEventsObserver);
  46. this->setProperty("QVTK_OBJECT", true);
  47. }
  48. //-----------------------------------------------------------------------------
  49. void ctkVTKObjectEventsObserver::printAdditionalInfo()
  50. {
  51. this->Superclass::dumpObjectInfo();
  52. CTK_D(ctkVTKObjectEventsObserver);
  53. qDebug() << "ctkVTKObjectEventsObserver:" << this << endl
  54. << " AllEnabled:" << d->AllEnabled << endl
  55. << " AllBlocked:" << d->AllBlocked << endl
  56. << " Parent:" << (this->parent()?this->parent()->objectName():"NULL") << endl
  57. << " Connection count:" << d->connections().count();
  58. // Loop through all connection
  59. foreach (ctkVTKConnection* connection, d->connections())
  60. {
  61. connection->printAdditionalInfo();
  62. }
  63. }
  64. //-----------------------------------------------------------------------------
  65. bool ctkVTKObjectEventsObserver::allEnabled()const
  66. {
  67. return ctk_d()->AllEnabled;
  68. }
  69. //-----------------------------------------------------------------------------
  70. void ctkVTKObjectEventsObserver::setAllEnabled(bool enable)
  71. {
  72. CTK_D(ctkVTKObjectEventsObserver);
  73. // FIXME: maybe a particular module has been enabled/disabled
  74. if (d->AllEnabled == enable)
  75. {
  76. return;
  77. }
  78. // Loop through VTKQtConnections to enable/disable
  79. foreach(ctkVTKConnection* connection, d->connections())
  80. {
  81. connection->setEnabled(enable);
  82. }
  83. d->AllEnabled = enable;
  84. }
  85. //-----------------------------------------------------------------------------
  86. QString ctkVTKObjectEventsObserver::addConnection(vtkObject* old_vtk_obj, vtkObject* vtk_obj,
  87. unsigned long vtk_event, const QObject* qt_obj, const char* qt_slot, float priority)
  88. {
  89. QString connectionId;
  90. if (old_vtk_obj)
  91. {
  92. // Check that old_object and new_object are the same type
  93. if (vtk_obj && !vtk_obj->IsA(old_vtk_obj->GetClassName()))
  94. {
  95. qCritical() << "Old vtkObject (type:" << old_vtk_obj->GetClassName() << ") to disconnect and "
  96. << "the new VtkObject (type:" << vtk_obj->GetClassName() << ") to connect"
  97. << "should have the same type.";
  98. return connectionId;
  99. }
  100. // Disconnect old vtkObject
  101. this->removeConnection(old_vtk_obj, vtk_event, qt_obj, qt_slot);
  102. }
  103. if (vtk_obj)
  104. {
  105. connectionId = this->addConnection(vtk_obj, vtk_event, qt_obj, qt_slot, priority);
  106. }
  107. return connectionId;
  108. }
  109. //-----------------------------------------------------------------------------
  110. QString ctkVTKObjectEventsObserver::reconnection(vtkObject* vtk_obj,
  111. unsigned long vtk_event, const QObject* qt_obj,
  112. const char* qt_slot, float priority)
  113. {
  114. QString connectionId;
  115. this->removeConnection(0, vtk_event, qt_obj, qt_slot);
  116. if (vtk_obj)
  117. {
  118. connectionId = this->addConnection(vtk_obj, vtk_event, qt_obj, qt_slot, priority);
  119. }
  120. return connectionId;
  121. }
  122. //-----------------------------------------------------------------------------
  123. QString ctkVTKObjectEventsObserver::addConnection(vtkObject* vtk_obj, unsigned long vtk_event,
  124. const QObject* qt_obj, const char* qt_slot, float priority)
  125. {
  126. CTK_D(ctkVTKObjectEventsObserver);
  127. if (!ctkVTKConnection::ValidateParameters(vtk_obj, vtk_event, qt_obj, qt_slot))
  128. {
  129. qDebug() << "ctkVTKObjectEventsObserver::addConnection(...) - Invalid parameters - "
  130. << ctkVTKConnection::shortDescription(vtk_obj, vtk_event, qt_obj, qt_slot);
  131. return QString();
  132. }
  133. // Check if such event is already observed
  134. if (d->containsConnection(vtk_obj, vtk_event, qt_obj, qt_slot))
  135. {
  136. qWarning() << "ctkVTKObjectEventsObserver::addConnection - [vtkObject:"
  137. << vtk_obj->GetClassName()
  138. << ", event:" << vtk_event << "]"
  139. << " is already connected with [qObject:" << qt_obj->objectName()
  140. << ", slot:" << qt_slot << "]";
  141. return QString();
  142. }
  143. // Instantiate a new connection, set its parameters and add it to the list
  144. ctkVTKConnection * connection = new ctkVTKConnection(this);
  145. connection->SetParameters(vtk_obj, vtk_event, qt_obj, qt_slot, priority);
  146. // If required, establish connection
  147. connection->setEnabled(d->AllEnabled);
  148. connection->setBlocked(d->AllBlocked);
  149. return connection->id();
  150. }
  151. //-----------------------------------------------------------------------------
  152. void ctkVTKObjectEventsObserver::blockAllConnections(bool block)
  153. {
  154. CTK_D(ctkVTKObjectEventsObserver);
  155. this->printAdditionalInfo();
  156. if (d->AllBlocked == block) { return; }
  157. foreach (ctkVTKConnection* connection, d->connections())
  158. {
  159. connection->setBlocked(block);
  160. }
  161. d->AllBlocked = block;
  162. }
  163. //-----------------------------------------------------------------------------
  164. void ctkVTKObjectEventsObserver::blockConnection(const QString& id, bool blocked)
  165. {
  166. CTK_D(ctkVTKObjectEventsObserver);
  167. ctkVTKConnection* connection = d->findConnection(id);
  168. if (connection == 0)
  169. {
  170. return;
  171. }
  172. connection->setBlocked(blocked);
  173. }
  174. //-----------------------------------------------------------------------------
  175. int ctkVTKObjectEventsObserver::blockConnection(bool block, vtkObject* vtk_obj,
  176. unsigned long vtk_event, const QObject* qt_obj)
  177. {
  178. CTK_D(ctkVTKObjectEventsObserver);
  179. if (!vtk_obj)
  180. {
  181. qDebug() << "ctkVTKObjectEventsObserver::blockConnectionRecursive"
  182. << "- Failed to " << (block?"block":"unblock") <<" connection"
  183. << "- vtkObject is NULL";
  184. return 0;
  185. }
  186. QList<ctkVTKConnection*> connections =
  187. d->findConnections(vtk_obj, vtk_event, qt_obj, 0);
  188. foreach (ctkVTKConnection* connection, connections)
  189. {
  190. connection->setBlocked(block);
  191. }
  192. return connections.size();
  193. }
  194. //-----------------------------------------------------------------------------
  195. int ctkVTKObjectEventsObserver::removeConnection(vtkObject* vtk_obj, unsigned long vtk_event,
  196. const QObject* qt_obj, const char* qt_slot)
  197. {
  198. CTK_D(ctkVTKObjectEventsObserver);
  199. QList<ctkVTKConnection*> connections =
  200. d->findConnections(vtk_obj, vtk_event, qt_obj, qt_slot);
  201. foreach (ctkVTKConnection* connection, connections)
  202. {
  203. connection->deleteConnection();
  204. }
  205. return connections.count();
  206. }
  207. //-----------------------------------------------------------------------------
  208. // ctkVTKObjectEventsObserverPrivate methods
  209. //-----------------------------------------------------------------------------
  210. ctkVTKObjectEventsObserverPrivate::ctkVTKObjectEventsObserverPrivate()
  211. {
  212. this->AllEnabled = true;
  213. this->AllBlocked = false;
  214. }
  215. //-----------------------------------------------------------------------------
  216. bool ctkVTKObjectEventsObserverPrivate::containsConnection(vtkObject* vtk_obj, unsigned long vtk_event,
  217. const QObject* qt_obj, const char* qt_slot)
  218. {
  219. return (this->findConnection(vtk_obj, vtk_event, qt_obj, qt_slot) != 0);
  220. }
  221. //-----------------------------------------------------------------------------
  222. ctkVTKConnection*
  223. ctkVTKObjectEventsObserverPrivate::findConnection(const QString& id)
  224. {
  225. foreach(ctkVTKConnection* connection, this->connections())
  226. {
  227. if (connection->id() == id)
  228. {
  229. return connection;
  230. }
  231. }
  232. return 0;
  233. }
  234. //-----------------------------------------------------------------------------
  235. ctkVTKConnection*
  236. ctkVTKObjectEventsObserverPrivate::findConnection(vtkObject* vtk_obj, unsigned long vtk_event,
  237. const QObject* qt_obj, const char* qt_slot)
  238. {
  239. QList<ctkVTKConnection*> foundConnections =
  240. this->findConnections(vtk_obj, vtk_event, qt_obj, qt_slot);
  241. return foundConnections.size() ? foundConnections[0] : 0;
  242. }
  243. //-----------------------------------------------------------------------------
  244. QList<ctkVTKConnection*>
  245. ctkVTKObjectEventsObserverPrivate::findConnections(
  246. vtkObject* vtk_obj, unsigned long vtk_event,
  247. const QObject* qt_obj, const char* qt_slot)
  248. {
  249. bool all_info = true;
  250. if(qt_slot == NULL || qt_obj == NULL || vtk_event == vtkCommand::NoEvent)
  251. {
  252. all_info = false;
  253. }
  254. QList<ctkVTKConnection*> foundConnections;
  255. // Loop through all connection
  256. foreach (ctkVTKConnection* connection, this->connections())
  257. {
  258. if (connection->isEqual(vtk_obj, vtk_event, qt_obj, qt_slot))
  259. {
  260. foundConnections.append(connection);
  261. if (all_info)
  262. {
  263. break;
  264. }
  265. }
  266. }
  267. return foundConnections;
  268. }