ctkVTKObjectEventsObserver.cpp 11 KB

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