ctkVTKConnection.cpp 13 KB

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