ctkVTKConnection.cpp 14 KB

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