ctkVTKObjectEventsObserver.cpp 10 KB

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