ctkVTKConnection.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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 <QDebug>
  16. #include <QPointer>
  17. #include <QRegExp>
  18. #include <QString>
  19. #include <QTextStream>
  20. // CTK includes
  21. #include "ctkVTKConnection.h"
  22. #include "ctkVTKConnection_p.h"
  23. // VTK includes
  24. #include <vtkObject.h>
  25. #include <vtkSmartPointer.h>
  26. #include <vtkCallbackCommand.h>
  27. //-----------------------------------------------------------------------------
  28. QString convertPointerToString(void* pointer)
  29. {
  30. QString pointerAsString;
  31. QTextStream(&pointerAsString) << pointer;
  32. return pointerAsString;
  33. }
  34. //-----------------------------------------------------------------------------
  35. // ctkVTKConnectionPrivate methods
  36. //-----------------------------------------------------------------------------
  37. ctkVTKConnectionPrivate::ctkVTKConnectionPrivate(ctkVTKConnection& object)
  38. :q_ptr(&object)
  39. {
  40. this->Callback = vtkSmartPointer<vtkCallbackCommand>::New();
  41. this->Callback->SetCallback(ctkVTKConnectionPrivate::DoCallback);
  42. this->Callback->SetClientData(this);
  43. this->QtObject = 0;
  44. this->VTKEvent = vtkCommand::NoEvent;
  45. this->Priority = 0.0;
  46. this->ConnectionType = Qt::AutoConnection;
  47. this->SlotType = ARG_UNKNOWN;
  48. this->Connected = false;
  49. this->Blocked = false;
  50. this->Id = convertPointerToString(this);
  51. this->ObserveDeletion = false;
  52. }
  53. //-----------------------------------------------------------------------------
  54. ctkVTKConnectionPrivate::~ctkVTKConnectionPrivate()
  55. {
  56. }
  57. //-----------------------------------------------------------------------------
  58. void ctkVTKConnectionPrivate::connect()
  59. {
  60. Q_Q(ctkVTKConnection);
  61. if (this->Connected)
  62. {
  63. qDebug() << "ctkVTKConnection already connected.";
  64. return;
  65. }
  66. switch (this->SlotType)
  67. {
  68. case ctkVTKConnectionPrivate::ARG_VTKOBJECT_AND_VTKOBJECT:
  69. QObject::connect(q, SIGNAL(emitExecute(vtkObject*,vtkObject*)),
  70. this->QtObject, this->QtSlot.toLatin1(), this->ConnectionType);
  71. break;
  72. case ctkVTKConnectionPrivate::ARG_VTKOBJECT_VOID_ULONG_VOID:
  73. QObject::connect(q, SIGNAL(emitExecute(vtkObject*,void*,ulong,void*)),
  74. this->QtObject, this->QtSlot.toLatin1(), this->ConnectionType);
  75. break;
  76. default:
  77. Q_ASSERT(false);
  78. qCritical() << "Failed to connect - "
  79. << "The slot (" << this->QtSlot << ") owned by "
  80. << "QObject(" << this->QtObject->objectName() << ")"
  81. << " seems to have a wrong signature.";
  82. break;
  83. }
  84. // Make a connection between this and the vtk object
  85. q->addObserver(this->VTKObject, this->VTKEvent, this->Callback, this->Priority);
  86. // If necessary, observe vtk DeleteEvent
  87. if(this->ObserveDeletion)
  88. {
  89. // don't observe it twice
  90. if (this->VTKEvent != vtkCommand::DeleteEvent)
  91. {
  92. this->VTKObject->AddObserver(vtkCommand::DeleteEvent, this->Callback);
  93. }
  94. }
  95. // When Qt object is destroyed: (1) remove VTK observers and
  96. // (2) set QtObject pointer to 0.
  97. QObject::connect(this->QtObject, SIGNAL(destroyed(QObject*)),
  98. q, SLOT(qobjectDeleted()));
  99. this->Connected = true;
  100. }
  101. //-----------------------------------------------------------------------------
  102. void ctkVTKConnectionPrivate::disconnectSlots()
  103. {
  104. Q_Q(ctkVTKConnection);
  105. if (!this->Connected)
  106. {
  107. return;
  108. }
  109. if (this->QtObject)
  110. {
  111. switch (this->SlotType)
  112. {
  113. case ctkVTKConnectionPrivate::ARG_VTKOBJECT_AND_VTKOBJECT:
  114. QObject::disconnect(q, SIGNAL(emitExecute(vtkObject*,vtkObject*)),
  115. this->QtObject,this->QtSlot.toLatin1().data());
  116. break;
  117. case ctkVTKConnectionPrivate::ARG_VTKOBJECT_VOID_ULONG_VOID:
  118. QObject::disconnect(q, SIGNAL(emitExecute(vtkObject*,void*,ulong,void*)),
  119. this->QtObject, this->QtSlot.toLatin1().data());
  120. break;
  121. default:
  122. Q_ASSERT(false);
  123. qCritical() << "Failed to disconnect - "
  124. << "The slot (" << this->QtSlot << ") owned by "
  125. << "QObject(" << this->QtObject->objectName() << ")"
  126. << " seems to have a wrong signature.";
  127. break;
  128. }
  129. }
  130. this->Connected = false;
  131. }
  132. //-----------------------------------------------------------------------------
  133. void ctkVTKConnectionPrivate::disconnectVTKObject()
  134. {
  135. Q_Q(ctkVTKConnection);
  136. if (this->VTKObject)
  137. {
  138. q->removeObserver(this->VTKObject, this->VTKEvent, this->Callback);
  139. if (this->ObserveDeletion)
  140. {
  141. this->VTKObject->RemoveObservers(vtkCommand::DeleteEvent, this->Callback);
  142. }
  143. }
  144. }
  145. //-----------------------------------------------------------------------------
  146. bool ctkVTKConnectionPrivate::IsSameQtSlot(const char* qt_slot)const
  147. {
  148. if (qt_slot == 0)
  149. {
  150. return true;
  151. }
  152. const char* ptr = qt_slot;
  153. for (QString::const_iterator it = this->QtSlot.begin();
  154. it != this->QtSlot.end() && *ptr != '\0'; )
  155. {
  156. if (*it == *ptr)
  157. {
  158. ++it;
  159. ++ptr;
  160. }
  161. else if (*it == ' ')
  162. {
  163. ++it;
  164. }
  165. else if (*ptr == ' ')
  166. {
  167. ++ptr;
  168. }
  169. else
  170. {
  171. return false;
  172. }
  173. }
  174. return true;
  175. }
  176. //-----------------------------------------------------------------------------
  177. // ctkVTKConnection methods
  178. //-----------------------------------------------------------------------------
  179. ctkVTKConnection::ctkVTKConnection(QObject* _parent):
  180. Superclass(_parent)
  181. , d_ptr(new ctkVTKConnectionPrivate(*this))
  182. {
  183. }
  184. // --------------------------------------------------------------------------
  185. ctkVTKConnection::ctkVTKConnection(ctkVTKConnectionPrivate* pimpl, QObject* _parent)
  186. : Superclass(_parent)
  187. , d_ptr(pimpl)
  188. {
  189. }
  190. //-----------------------------------------------------------------------------
  191. ctkVTKConnection::~ctkVTKConnection()
  192. {
  193. Q_D(ctkVTKConnection);
  194. d->disconnectVTKObject();
  195. }
  196. //-----------------------------------------------------------------------------
  197. QString ctkVTKConnection::id()const
  198. {
  199. Q_D(const ctkVTKConnection);
  200. return d->Id;
  201. }
  202. //-----------------------------------------------------------------------------
  203. QObject* ctkVTKConnection::object()const
  204. {
  205. Q_D(const ctkVTKConnection);
  206. return const_cast<QObject*>(d->QtObject);
  207. }
  208. //-----------------------------------------------------------------------------
  209. vtkObject* ctkVTKConnection::vtkobject() const
  210. {
  211. Q_D(const ctkVTKConnection);
  212. return const_cast<vtkObject*>(d->VTKObject.GetPointer());
  213. }
  214. //-----------------------------------------------------------------------------
  215. QDebug operator<<(QDebug dbg, const ctkVTKConnection& connection)
  216. {
  217. const ctkVTKConnectionPrivate* d = connection.d_func();
  218. dbg.nospace() << "ctkVTKConnection:" << &connection << endl
  219. << "Id:" << d->Id << endl
  220. << " VTKObject:" << (d->VTKObject ? d->VTKObject->GetClassName() : "<null>")
  221. << "(" << d->VTKObject << ")" << endl
  222. << " QtObject:" << d->QtObject << endl
  223. << " VTKEvent:" << d->VTKEvent << endl
  224. << " QtSlot:" << d->QtSlot << endl
  225. << " SlotType:" << d->SlotType << endl
  226. << " Priority:" << d->Priority << endl
  227. << " Connected:" << d->Connected << endl
  228. << " Blocked:" << d->Blocked;
  229. return dbg.space();
  230. }
  231. //-----------------------------------------------------------------------------
  232. QString ctkVTKConnection::shortDescription()
  233. {
  234. Q_D(ctkVTKConnection);
  235. return ctkVTKConnection::shortDescription(d->VTKObject, d->VTKEvent, d->QtObject, d->QtSlot.toLatin1());
  236. }
  237. //-----------------------------------------------------------------------------
  238. QString ctkVTKConnection::shortDescription(vtkObject* vtk_obj, unsigned long vtk_event,
  239. const QObject* qt_obj, const char* qt_slot)
  240. {
  241. QString ret;
  242. QTextStream ts( &ret );
  243. ts << (vtk_obj ? vtk_obj->GetClassName() : "NULL") << " "
  244. << vtk_event << " " << qt_obj << " " << (qt_slot ? qt_slot : "");
  245. return ret;
  246. }
  247. //-----------------------------------------------------------------------------
  248. bool ctkVTKConnection::isValid(vtkObject* vtk_obj, unsigned long vtk_event,
  249. const QObject* qt_obj, const char* qt_slot)
  250. {
  251. Q_UNUSED(vtk_event);
  252. if (!vtk_obj)
  253. {
  254. return false;
  255. }
  256. if (!qt_obj)
  257. {
  258. return false;
  259. }
  260. if (qt_slot == 0 || strlen(qt_slot) == 0)
  261. {
  262. return false;
  263. }
  264. return true;
  265. }
  266. //-----------------------------------------------------------------------------
  267. void ctkVTKConnection::setup(vtkObject* vtk_obj, unsigned long vtk_event,
  268. const QObject* qt_obj, const char* qt_slot,
  269. float priority,
  270. Qt::ConnectionType connectionType)
  271. {
  272. Q_D(ctkVTKConnection);
  273. if (!ctkVTKConnection::isValid(vtk_obj, vtk_event, qt_obj, qt_slot))
  274. {
  275. return;
  276. }
  277. d->VTKObject = vtk_obj;
  278. d->QtObject = qt_obj;
  279. d->VTKEvent = vtk_event;
  280. d->QtSlot = qt_slot;
  281. d->Priority = priority;
  282. d->ConnectionType = connectionType;
  283. if (d->QtSlot.contains(QRegExp(QString("\\( ?vtkObject ?\\* ?, ?vtkObject ?\\* ?\\)"))))
  284. {
  285. d->SlotType = ctkVTKConnectionPrivate::ARG_VTKOBJECT_AND_VTKOBJECT;
  286. }
  287. else
  288. {
  289. d->SlotType = ctkVTKConnectionPrivate::ARG_VTKOBJECT_VOID_ULONG_VOID;
  290. }
  291. d->connect();
  292. }
  293. //-----------------------------------------------------------------------------
  294. void ctkVTKConnection::setBlocked(bool block)
  295. {
  296. Q_D(ctkVTKConnection);
  297. d->Blocked = block;
  298. }
  299. //-----------------------------------------------------------------------------
  300. bool ctkVTKConnection::isBlocked()const
  301. {
  302. Q_D(const ctkVTKConnection);
  303. return d->Blocked;
  304. }
  305. //-----------------------------------------------------------------------------
  306. bool ctkVTKConnection::isEqual(vtkObject* vtk_obj, unsigned long vtk_event,
  307. const QObject* qt_obj, const char* qt_slot)const
  308. {
  309. Q_D(const ctkVTKConnection);
  310. if (vtk_obj && d->VTKObject != vtk_obj)
  311. {
  312. return false;
  313. }
  314. if (vtk_event != vtkCommand::NoEvent && d->VTKEvent != vtk_event)
  315. {
  316. return false;
  317. }
  318. if (qt_obj && d->QtObject != qt_obj)
  319. {
  320. return false;
  321. }
  322. if (!d->IsSameQtSlot(qt_slot))
  323. {
  324. return false;
  325. }
  326. return true;
  327. }
  328. //-----------------------------------------------------------------------------
  329. void ctkVTKConnectionPrivate::DoCallback(vtkObject* vtk_obj, unsigned long event,
  330. void* client_data, void* call_data)
  331. {
  332. ctkVTKConnectionPrivate* conn = reinterpret_cast<ctkVTKConnectionPrivate*>(client_data);
  333. Q_ASSERT(conn);
  334. conn->execute(vtk_obj, event, client_data, call_data);
  335. }
  336. //-----------------------------------------------------------------------------
  337. // callback from VTK to emit signal
  338. void ctkVTKConnectionPrivate::execute(vtkObject* vtk_obj, unsigned long vtk_event,
  339. void* client_data, void* call_data)
  340. {
  341. Q_Q(ctkVTKConnection);
  342. Q_ASSERT(this->Connected);
  343. if (this->Blocked)
  344. {
  345. return;
  346. }
  347. QPointer<ctkVTKConnection> connection(q);
  348. if(!this->ObserveDeletion ||
  349. vtk_event != vtkCommand::DeleteEvent ||
  350. this->VTKEvent == vtkCommand::DeleteEvent)
  351. {
  352. vtkObject* callDataAsVtkObject = 0;
  353. switch (this->SlotType)
  354. {
  355. case ctkVTKConnectionPrivate::ARG_VTKOBJECT_AND_VTKOBJECT:
  356. if (this->VTKEvent == vtk_event)
  357. {
  358. callDataAsVtkObject = reinterpret_cast<vtkObject*>( call_data );
  359. if (!callDataAsVtkObject)
  360. {
  361. qCritical() << "The VTKEvent(" << this->VTKEvent<< ") triggered by vtkObject("
  362. << this->VTKObject->GetClassName() << ") "
  363. << "doesn't return data of type vtkObject." << endl
  364. << "The slot (" << this->QtSlot << ") owned by "
  365. << "QObject(" << this->QtObject->objectName() << ")"
  366. << " may be incorrect.";
  367. }
  368. emit q->emitExecute(vtk_obj, callDataAsVtkObject);
  369. }
  370. break;
  371. case ctkVTKConnectionPrivate::ARG_VTKOBJECT_VOID_ULONG_VOID:
  372. emit q->emitExecute(vtk_obj, call_data, vtk_event, client_data);
  373. break;
  374. default:
  375. // Should never reach
  376. qCritical() << "Unknown SlotType:" << this->SlotType;
  377. return;
  378. break;
  379. }
  380. }
  381. if (!connection.isNull() &&
  382. vtk_event == vtkCommand::DeleteEvent)
  383. {
  384. q->vtkObjectDeleted();
  385. }
  386. }
  387. //-----------------------------------------------------------------------------
  388. void ctkVTKConnection::observeDeletion(bool enable)
  389. {
  390. Q_D(ctkVTKConnection);
  391. d->ObserveDeletion = enable;
  392. }
  393. //-----------------------------------------------------------------------------
  394. bool ctkVTKConnection::deletionObserved()const
  395. {
  396. Q_D(const ctkVTKConnection);
  397. return d->ObserveDeletion;
  398. }
  399. //-----------------------------------------------------------------------------
  400. void ctkVTKConnection::disconnect()
  401. {
  402. Q_D(ctkVTKConnection);
  403. d->disconnectVTKObject();
  404. }
  405. //-----------------------------------------------------------------------------
  406. void ctkVTKConnection::vtkObjectDeleted()
  407. {
  408. Q_D(ctkVTKConnection);
  409. d->disconnectSlots();
  410. }
  411. //-----------------------------------------------------------------------------
  412. void ctkVTKConnection::qobjectDeleted()
  413. {
  414. Q_D(ctkVTKConnection);
  415. d->QtObject = 0;
  416. d->disconnectVTKObject();
  417. }
  418. //-----------------------------------------------------------------------------
  419. void ctkVTKConnection::addObserver(vtkObject* caller, unsigned long vtk_event,
  420. vtkCallbackCommand* callback, float priority)
  421. {
  422. caller->AddObserver(vtk_event, callback, priority);
  423. }
  424. //-----------------------------------------------------------------------------
  425. void ctkVTKConnection::removeObserver(vtkObject* caller, unsigned long vtk_event, vtkCallbackCommand* callback)
  426. {
  427. caller->RemoveObservers(vtk_event, callback);
  428. }