ctkVTKObjectEventsObserver.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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.apache.org/licenses/LICENSE-2.0.txt
  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. #include <QMultiMap>
  21. // CTK includes
  22. #include "ctkVTKObjectEventsObserver.h"
  23. #include "ctkVTKConnection.h"
  24. // VTK includes
  25. #include <vtkObject.h>
  26. //-----------------------------------------------------------------------------
  27. CTK_SINGLETON_DEFINE(ctkVTKConnectionFactory)
  28. //-----------------------------------------------------------------------------
  29. // ctkVTKConnectionFactory
  30. //-----------------------------------------------------------------------------
  31. ctkVTKConnectionFactory* ctkVTKConnectionFactory::instance()
  32. {
  33. return Self::Instance;
  34. }
  35. //-----------------------------------------------------------------------------
  36. void ctkVTKConnectionFactory::setInstance(ctkVTKConnectionFactory* newInstance)
  37. {
  38. if (!newInstance)
  39. {
  40. qCritical() << "ctkVTKConnectionFactory::setInstance - Failed to set a null instance !";
  41. return;
  42. }
  43. delete Self::Instance;
  44. Self::Instance = newInstance;
  45. }
  46. //-----------------------------------------------------------------------------
  47. ctkVTKConnectionFactory::ctkVTKConnectionFactory()
  48. {
  49. }
  50. //-----------------------------------------------------------------------------
  51. ctkVTKConnectionFactory::~ctkVTKConnectionFactory()
  52. {
  53. }
  54. //-----------------------------------------------------------------------------
  55. ctkVTKConnection* ctkVTKConnectionFactory::createConnection(ctkVTKObjectEventsObserver* parent)const
  56. {
  57. return new ctkVTKConnection(parent);
  58. }
  59. //-----------------------------------------------------------------------------
  60. // ctkVTKObjectEventsObserverPrivate
  61. //-----------------------------------------------------------------------------
  62. class ctkVTKObjectEventsObserverPrivate
  63. {
  64. Q_DECLARE_PUBLIC(ctkVTKObjectEventsObserver);
  65. protected:
  66. ctkVTKObjectEventsObserver* const q_ptr;
  67. public:
  68. /// ConnectionIndexType:
  69. /// key = hash (generated from the connection parameters)
  70. /// value = QT connection object name
  71. typedef QMultiMap<unsigned long, ctkVTKConnection*> ConnectionIndexType;
  72. ctkVTKObjectEventsObserverPrivate(ctkVTKObjectEventsObserver& object);
  73. ///
  74. /// Return a reference toward the corresponding connection or 0 if doesn't exist
  75. ctkVTKConnection* findConnection(const QString& id)const;
  76. ///
  77. /// Return a reference toward the corresponding connection or 0 if doesn't exist
  78. ctkVTKConnection* findConnection(vtkObject* vtk_obj, unsigned long vtk_event,
  79. const QObject* qt_obj, const char* qt_slot)const;
  80. ///
  81. /// Return all the references that match the given parameters
  82. QList<ctkVTKConnection*> findConnections(vtkObject* vtk_obj, unsigned long vtk_event,
  83. const QObject* qt_obj, const char* qt_slot)const;
  84. inline QList<ctkVTKConnection*> connections()const
  85. {
  86. Q_Q(const ctkVTKObjectEventsObserver);
  87. return q->findChildren<ctkVTKConnection*>();
  88. }
  89. /// Generate a number from the connection parameters that is most often
  90. /// unique for each connection. The slot parameter is not used, as probably
  91. /// the same objects with the same event are not connected to different slot.
  92. static unsigned long generateConnectionIndexHash(const vtkObject* vtk_obj, unsigned long vtk_event, const QObject* qt_obj)
  93. {
  94. return reinterpret_cast<const unsigned char*>(vtk_obj)-reinterpret_cast<const unsigned char*>(qt_obj)+vtk_event;
  95. }
  96. bool StrictTypeCheck;
  97. bool AllBlocked;
  98. bool ObserveDeletion;
  99. /// An associative container to speed up findConnection.
  100. /// No need to iterate through all the existing connections and check if it is
  101. /// equal with the searched one.
  102. /// All the connections are present in the index (to allow quick decision that
  103. /// a connection does not exist), but a lazy deletion method is used (items
  104. /// not necessarily removed from the index immediately when a connection is deleted).
  105. mutable ConnectionIndexType ConnectionIndex;
  106. };
  107. //-----------------------------------------------------------------------------
  108. // ctkVTKObjectEventsObserverPrivate methods
  109. //-----------------------------------------------------------------------------
  110. ctkVTKObjectEventsObserverPrivate::ctkVTKObjectEventsObserverPrivate(ctkVTKObjectEventsObserver& object)
  111. :q_ptr(&object)
  112. {
  113. this->StrictTypeCheck = false;
  114. this->AllBlocked = false;
  115. this->ObserveDeletion = false;
  116. }
  117. //-----------------------------------------------------------------------------
  118. ctkVTKConnection*
  119. ctkVTKObjectEventsObserverPrivate::findConnection(const QString& id)const
  120. {
  121. foreach(ctkVTKConnection* connection, this->connections())
  122. {
  123. if (connection->id() == id)
  124. {
  125. return connection;
  126. }
  127. }
  128. return 0;
  129. }
  130. //-----------------------------------------------------------------------------
  131. ctkVTKConnection*
  132. ctkVTKObjectEventsObserverPrivate::findConnection(
  133. vtkObject* vtk_obj, unsigned long vtk_event,
  134. const QObject* qt_obj, const char* qt_slot)const
  135. {
  136. // Linear search for connections is prohibitively slow when observing many objects
  137. // (because connection->isEqual is slow)
  138. Q_Q(const ctkVTKObjectEventsObserver);
  139. if(vtk_obj != NULL && qt_slot != NULL &&
  140. qt_obj != NULL && vtk_event != vtkCommand::NoEvent)
  141. {
  142. // All information is specified, so we can use the index to find the connection
  143. unsigned long hash=generateConnectionIndexHash(vtk_obj, vtk_event, qt_obj);
  144. ConnectionIndexType::iterator connectionIt = this->ConnectionIndex.find(hash);
  145. while (connectionIt != this->ConnectionIndex.end() && connectionIt.key() == hash)
  146. {
  147. ctkVTKConnection* connection=connectionIt.value();
  148. if (!q->children().contains(connection))
  149. {
  150. // connection has been deleted, so remove it from the index
  151. connectionIt=this->ConnectionIndex.erase(connectionIt);
  152. continue;
  153. }
  154. if (connection->isEqual(vtk_obj, vtk_event, qt_obj, qt_slot))
  155. {
  156. return connection;
  157. }
  158. ++connectionIt;
  159. }
  160. return 0;
  161. }
  162. foreach (ctkVTKConnection* connection, this->connections())
  163. {
  164. if (connection->isEqual(vtk_obj, vtk_event, qt_obj, qt_slot))
  165. {
  166. return connection;
  167. }
  168. }
  169. return 0;
  170. }
  171. //-----------------------------------------------------------------------------
  172. QList<ctkVTKConnection*>
  173. ctkVTKObjectEventsObserverPrivate::findConnections(
  174. vtkObject* vtk_obj, unsigned long vtk_event,
  175. const QObject* qt_obj, const char* qt_slot)const
  176. {
  177. QList<ctkVTKConnection*> foundConnections;
  178. if(vtk_obj != NULL && qt_slot != NULL &&
  179. qt_obj != NULL && vtk_event != vtkCommand::NoEvent)
  180. {
  181. // All information is specified, so we can use the index to find the connection
  182. ctkVTKConnection* connection=findConnection(vtk_obj, vtk_event, qt_obj, qt_slot);
  183. if (connection)
  184. {
  185. foundConnections.append(connection);
  186. }
  187. return foundConnections;
  188. }
  189. // Loop through all connection
  190. foreach (ctkVTKConnection* connection, this->connections())
  191. {
  192. if (connection->isEqual(vtk_obj, vtk_event, qt_obj, qt_slot))
  193. {
  194. foundConnections.append(connection);
  195. }
  196. }
  197. return foundConnections;
  198. }
  199. //-----------------------------------------------------------------------------
  200. // ctkVTKObjectEventsObserver methods
  201. //-----------------------------------------------------------------------------
  202. ctkVTKObjectEventsObserver::ctkVTKObjectEventsObserver(QObject* _parent):Superclass(_parent)
  203. , d_ptr(new ctkVTKObjectEventsObserverPrivate(*this))
  204. {
  205. this->setProperty("QVTK_OBJECT", true);
  206. }
  207. //-----------------------------------------------------------------------------
  208. ctkVTKObjectEventsObserver::~ctkVTKObjectEventsObserver()
  209. {
  210. }
  211. //-----------------------------------------------------------------------------
  212. void ctkVTKObjectEventsObserver::printAdditionalInfo()
  213. {
  214. this->Superclass::dumpObjectInfo();
  215. Q_D(ctkVTKObjectEventsObserver);
  216. qDebug() << "ctkVTKObjectEventsObserver:" << this << endl
  217. << " AllBlocked:" << d->AllBlocked << endl
  218. << " Parent:" << (this->parent()?this->parent()->objectName():"NULL") << endl
  219. << " Connection count:" << d->connections().count();
  220. // Loop through all connection
  221. foreach (const ctkVTKConnection* connection, d->connections())
  222. {
  223. qDebug() << *connection;
  224. }
  225. }
  226. //-----------------------------------------------------------------------------
  227. bool ctkVTKObjectEventsObserver::strictTypeCheck()const
  228. {
  229. Q_D(const ctkVTKObjectEventsObserver);
  230. return d->StrictTypeCheck;
  231. }
  232. //-----------------------------------------------------------------------------
  233. void ctkVTKObjectEventsObserver::setStrictTypeCheck(bool check)
  234. {
  235. Q_D(ctkVTKObjectEventsObserver);
  236. d->StrictTypeCheck = check;
  237. }
  238. //-----------------------------------------------------------------------------
  239. QString ctkVTKObjectEventsObserver::addConnection(vtkObject* old_vtk_obj, vtkObject* vtk_obj,
  240. unsigned long vtk_event, const QObject* qt_obj, const char* qt_slot, float priority,
  241. Qt::ConnectionType connectionType)
  242. {
  243. Q_D(ctkVTKObjectEventsObserver);
  244. if (old_vtk_obj)
  245. {
  246. // Check that old_object and new_object are the same type
  247. // If you have a crash when accessing old_vtk_obj->GetClassName(), that means
  248. // old_vtk_obj has been deleted and you should probably have keep
  249. // old_vtk_obj into a vtkWeakPointer:
  250. // vtkWeakPointer<vtkObject> obj1 = myobj1;
  251. // this->addConnection(obj1, vtkCommand::Modified...)
  252. // myobj1->Delete();
  253. // vtkWeakPointer<vtkObject> obj2 = myobj2;
  254. // this->addConnection(obj1, obj2, vtkCommand::Modified...)
  255. // ...
  256. // Or just call addConnection with a new
  257. // vtk_obj of 0 before the vtk_obj is deleted.
  258. // vtkObject* obj1 = vtkObject::New();
  259. // this->addConnection(obj1, vtkCommand::Modified...)
  260. // this->addConnection(obj1, 0, vtkCommand::Modified...)
  261. // obj1->Delete();
  262. // vtkObject* obj2 = vtkObject::New();
  263. // this->addConnection(0, obj2, vtkCommand::Modified...)
  264. // ...
  265. if (d->StrictTypeCheck && vtk_obj
  266. && !vtk_obj->IsA(old_vtk_obj->GetClassName()))
  267. {
  268. qWarning() << "Previous vtkObject (type:" << old_vtk_obj->GetClassName()
  269. << ") to disconnect"
  270. << "and new vtkObject (type:" << vtk_obj->GetClassName()
  271. << ") to connect"
  272. << "have a different type.";
  273. return QString();
  274. }
  275. // Disconnect old vtkObject
  276. this->removeConnection(old_vtk_obj, vtk_event, qt_obj, qt_slot);
  277. }
  278. return this->addConnection(
  279. vtk_obj, vtk_event, qt_obj, qt_slot, priority, connectionType);
  280. }
  281. //-----------------------------------------------------------------------------
  282. QString ctkVTKObjectEventsObserver::reconnection(vtkObject* vtk_obj,
  283. unsigned long vtk_event, const QObject* qt_obj,
  284. const char* qt_slot, float priority, Qt::ConnectionType connectionType)
  285. {
  286. this->removeConnection(0, vtk_event, qt_obj, qt_slot);
  287. return this->addConnection(
  288. vtk_obj, vtk_event, qt_obj, qt_slot, priority, connectionType);
  289. }
  290. //-----------------------------------------------------------------------------
  291. QString ctkVTKObjectEventsObserver::addConnection(vtkObject* vtk_obj, unsigned long vtk_event,
  292. const QObject* qt_obj, const char* qt_slot, float priority, Qt::ConnectionType connectionType)
  293. {
  294. Q_D(ctkVTKObjectEventsObserver);
  295. // If no vtk_obj is provided, there is no way we can create a connection.
  296. if (!vtk_obj)
  297. {
  298. return QString();
  299. }
  300. if (!ctkVTKConnection::isValid(vtk_obj, vtk_event, qt_obj, qt_slot))
  301. {
  302. qDebug() << "ctkVTKObjectEventsObserver::addConnection(...) - Invalid parameters - "
  303. << ctkVTKConnection::shortDescription(vtk_obj, vtk_event, qt_obj, qt_slot);
  304. return QString();
  305. }
  306. // Check if such event is already observed
  307. if (this->containsConnection(vtk_obj, vtk_event, qt_obj, qt_slot))
  308. {
  309. // if you need to have more than 1 connection, then it's probably time to
  310. // add the same mechanism than Qt does: Qt::UniqueConnection
  311. //qWarning() << "ctkVTKObjectEventsObserver::addConnection - [vtkObject:"
  312. // << vtk_obj->GetClassName()
  313. // << ", event:" << vtk_event << "]"
  314. // << " is already connected with [qObject:" << qt_obj->objectName()
  315. // << ", slot:" << qt_slot << "]";
  316. return QString();
  317. }
  318. // Instantiate a new connection, set its parameters and add it to the list
  319. ctkVTKConnection * connection = ctkVTKConnectionFactory::instance()->createConnection(this);
  320. d->ConnectionIndex.insert(ctkVTKObjectEventsObserverPrivate::generateConnectionIndexHash(vtk_obj, vtk_event, qt_obj), connection);
  321. connection->observeDeletion(d->ObserveDeletion);
  322. connection->setup(vtk_obj, vtk_event, qt_obj, qt_slot, priority, connectionType);
  323. // If required, establish connection
  324. connection->setBlocked(d->AllBlocked);
  325. return connection->id();
  326. }
  327. //-----------------------------------------------------------------------------
  328. bool ctkVTKObjectEventsObserver::blockAllConnections(bool block)
  329. {
  330. Q_D(ctkVTKObjectEventsObserver);
  331. if (d->AllBlocked == block)
  332. {
  333. return d->AllBlocked;
  334. }
  335. bool oldAllBlocked = d->AllBlocked;
  336. foreach (ctkVTKConnection* connection, d->connections())
  337. {
  338. connection->setBlocked(block);
  339. }
  340. d->AllBlocked = block;
  341. return oldAllBlocked;
  342. }
  343. //-----------------------------------------------------------------------------
  344. bool ctkVTKObjectEventsObserver::connectionsBlocked()const
  345. {
  346. Q_D(const ctkVTKObjectEventsObserver);
  347. return d->AllBlocked;
  348. }
  349. //-----------------------------------------------------------------------------
  350. bool ctkVTKObjectEventsObserver::blockConnection(const QString& id, bool blocked)
  351. {
  352. Q_D(ctkVTKObjectEventsObserver);
  353. ctkVTKConnection* connection = d->findConnection(id);
  354. if (connection == 0)
  355. {
  356. qWarning() << "no connection for id " << id;
  357. return false;
  358. }
  359. bool oldBlocked = connection->isBlocked();
  360. connection->setBlocked(blocked);
  361. return oldBlocked;
  362. }
  363. //-----------------------------------------------------------------------------
  364. int ctkVTKObjectEventsObserver::blockConnection(bool block, vtkObject* vtk_obj,
  365. unsigned long vtk_event, const QObject* qt_obj)
  366. {
  367. Q_D(ctkVTKObjectEventsObserver);
  368. if (!vtk_obj)
  369. {
  370. qDebug() << "ctkVTKObjectEventsObserver::blockConnectionRecursive"
  371. << "- Failed to " << (block?"block":"unblock") <<" connection"
  372. << "- vtkObject is NULL";
  373. return 0;
  374. }
  375. QList<ctkVTKConnection*> connections =
  376. d->findConnections(vtk_obj, vtk_event, qt_obj, 0);
  377. foreach (ctkVTKConnection* connection, connections)
  378. {
  379. connection->setBlocked(block);
  380. }
  381. return connections.size();
  382. }
  383. //-----------------------------------------------------------------------------
  384. int ctkVTKObjectEventsObserver::removeConnection(vtkObject* vtk_obj, unsigned long vtk_event,
  385. const QObject* qt_obj, const char* qt_slot)
  386. {
  387. Q_D(ctkVTKObjectEventsObserver);
  388. QList<ctkVTKConnection*> connections =
  389. d->findConnections(vtk_obj, vtk_event, qt_obj, qt_slot);
  390. foreach (ctkVTKConnection* connection, connections)
  391. {
  392. delete connection;
  393. }
  394. // Only remove shadow connections (connections in the index without a corresponding actual connection)
  395. // from the index if the index size grew too big (shadow elements ratio >50% and minimum 100)
  396. if (static_cast<int>(d->ConnectionIndex.size())>100+children().count()*2)
  397. {
  398. for (ctkVTKObjectEventsObserverPrivate::ConnectionIndexType::iterator connectionIt=d->ConnectionIndex.begin();
  399. connectionIt!=d->ConnectionIndex.end();
  400. /*upon deletion the increment is done already, so don't increment here*/)
  401. {
  402. ctkVTKConnection* connection=connectionIt.value();
  403. if (!children().contains(connection))
  404. {
  405. // connection has been deleted, so remove it from the index
  406. connectionIt=d->ConnectionIndex.erase(connectionIt);
  407. continue;
  408. }
  409. ++connectionIt;
  410. }
  411. }
  412. return connections.count();
  413. }
  414. //-----------------------------------------------------------------------------
  415. int ctkVTKObjectEventsObserver::removeAllConnections()
  416. {
  417. return this->removeConnection(0, vtkCommand::NoEvent, 0, 0);
  418. }
  419. //-----------------------------------------------------------------------------
  420. bool ctkVTKObjectEventsObserver::containsConnection(vtkObject* vtk_obj, unsigned long vtk_event,
  421. const QObject* qt_obj, const char* qt_slot)const
  422. {
  423. Q_D(const ctkVTKObjectEventsObserver);
  424. return (d->findConnection(vtk_obj, vtk_event, qt_obj, qt_slot) != 0);
  425. }