ctkVTKConnection.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. #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->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(), Qt::AutoConnection);
  71. break;
  72. case ctkVTKConnectionPrivate::ARG_VTKOBJECT_VOID_ULONG_VOID:
  73. QObject::connect(q, SIGNAL(emitExecute(vtkObject*, void*, unsigned long, void*)),
  74. this->QtObject, this->QtSlot.toLatin1(), Qt::AutoConnection);
  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. // Remove itself from its parent when vtkObject is deleted
  95. QObject::connect(this->QtObject, SIGNAL(destroyed(QObject*)),
  96. q, SLOT(qobjectDeleted()));
  97. }
  98. this->Connected = true;
  99. }
  100. //-----------------------------------------------------------------------------
  101. void ctkVTKConnectionPrivate::disconnect()
  102. {
  103. Q_Q(ctkVTKConnection);
  104. if (!this->Connected)
  105. {
  106. return;
  107. }
  108. if (this->QtObject)
  109. {
  110. switch (this->SlotType)
  111. {
  112. case ctkVTKConnectionPrivate::ARG_VTKOBJECT_AND_VTKOBJECT:
  113. QObject::disconnect(q, SIGNAL(emitExecute(vtkObject*, vtkObject*)),
  114. this->QtObject,this->QtSlot.toLatin1().data());
  115. break;
  116. case ctkVTKConnectionPrivate::ARG_VTKOBJECT_VOID_ULONG_VOID:
  117. QObject::disconnect(q, SIGNAL(emitExecute(vtkObject*, void*, unsigned long, void*)),
  118. this->QtObject, this->QtSlot.toLatin1().data());
  119. break;
  120. default:
  121. Q_ASSERT(false);
  122. qCritical() << "Failed to disconnect - "
  123. << "The slot (" << this->QtSlot << ") owned by "
  124. << "QObject(" << this->QtObject->objectName() << ")"
  125. << " seems to have a wrong signature.";
  126. break;
  127. }
  128. }
  129. if (this->VTKObject)
  130. {
  131. q->removeObserver(this->VTKObject, this->VTKEvent, this->Callback);
  132. this->VTKObject->RemoveObservers(vtkCommand::DeleteEvent, this->Callback);
  133. }
  134. if (this->ObserveDeletion && this->QtObject)
  135. {
  136. //this->VTKObject->AddObserver(vtkCommand::DeleteEvent, this->Callback); has already been removed
  137. QObject::disconnect(this->QtObject, SIGNAL(destroyed(QObject*)),
  138. q, SIGNAL(isBroke()));
  139. }
  140. this->Connected = false;
  141. }
  142. //-----------------------------------------------------------------------------
  143. // ctkVTKConnection methods
  144. //-----------------------------------------------------------------------------
  145. ctkVTKConnection::ctkVTKConnection(QObject* _parent):
  146. Superclass(_parent)
  147. , d_ptr(new ctkVTKConnectionPrivate(*this))
  148. {
  149. }
  150. //-----------------------------------------------------------------------------
  151. ctkVTKConnection::~ctkVTKConnection()
  152. {
  153. Q_D(ctkVTKConnection);
  154. if (d->ObserveDeletion)
  155. {
  156. this->disconnect();
  157. }
  158. }
  159. //-----------------------------------------------------------------------------
  160. QString ctkVTKConnection::id()const
  161. {
  162. Q_D(const ctkVTKConnection);
  163. return d->Id;
  164. }
  165. //-----------------------------------------------------------------------------
  166. QObject* ctkVTKConnection::object()const
  167. {
  168. Q_D(const ctkVTKConnection);
  169. return const_cast<QObject*>(d->QtObject);
  170. }
  171. //-----------------------------------------------------------------------------
  172. QDebug operator<<(QDebug dbg, const ctkVTKConnection& connection)
  173. {
  174. const ctkVTKConnectionPrivate* d = connection.d_func();
  175. dbg.nospace() << "ctkVTKConnection:" << &connection << endl
  176. << "Id:" << d->Id << endl
  177. << " VTKObject:" << d->VTKObject->GetClassName()
  178. << "(" << d->VTKObject << ")" << endl
  179. << " QtObject:" << d->QtObject << endl
  180. << " VTKEvent:" << d->VTKEvent << endl
  181. << " QtSlot:" << d->QtSlot << endl
  182. << " SlotType:" << d->SlotType << endl
  183. << " Priority:" << d->Priority << endl
  184. << " Connected:" << d->Connected << endl
  185. << " Blocked:" << d->Blocked;
  186. return dbg.space();
  187. }
  188. //-----------------------------------------------------------------------------
  189. QString ctkVTKConnection::shortDescription()
  190. {
  191. Q_D(ctkVTKConnection);
  192. return ctkVTKConnection::shortDescription(d->VTKObject, d->VTKEvent, d->QtObject, d->QtSlot);
  193. }
  194. //-----------------------------------------------------------------------------
  195. QString ctkVTKConnection::shortDescription(vtkObject* vtk_obj, unsigned long vtk_event,
  196. const QObject* qt_obj, QString qt_slot)
  197. {
  198. QString ret;
  199. QTextStream ts( &ret );
  200. ts << (vtk_obj ? vtk_obj->GetClassName() : "NULL") << " "
  201. << vtk_event << " " << qt_obj << " " << qt_slot;
  202. return ret;
  203. }
  204. //-----------------------------------------------------------------------------
  205. bool ctkVTKConnection::isValid(vtkObject* vtk_obj, unsigned long vtk_event,
  206. const QObject* qt_obj, QString qt_slot)
  207. {
  208. Q_UNUSED(vtk_event);
  209. if (!vtk_obj)
  210. {
  211. return false;
  212. }
  213. if (!qt_obj)
  214. {
  215. return false;
  216. }
  217. if (qt_slot.isEmpty())
  218. {
  219. return false;
  220. }
  221. return true;
  222. }
  223. //-----------------------------------------------------------------------------
  224. void ctkVTKConnection::setup(vtkObject* vtk_obj, unsigned long vtk_event,
  225. const QObject* qt_obj, QString qt_slot,
  226. float priority)
  227. {
  228. Q_D(ctkVTKConnection);
  229. if (!ctkVTKConnection::isValid(vtk_obj, vtk_event, qt_obj, qt_slot))
  230. {
  231. return;
  232. }
  233. d->VTKObject = vtk_obj;
  234. d->QtObject = qt_obj;
  235. d->VTKEvent = vtk_event;
  236. d->QtSlot = qt_slot;
  237. d->Priority = priority;
  238. if (qt_slot.contains(QRegExp(QString("\\( ?vtkObject ?\\* ?, ?vtkObject ?\\* ?\\)"))))
  239. {
  240. d->SlotType = ctkVTKConnectionPrivate::ARG_VTKOBJECT_AND_VTKOBJECT;
  241. }
  242. else
  243. {
  244. d->SlotType = ctkVTKConnectionPrivate::ARG_VTKOBJECT_VOID_ULONG_VOID;
  245. }
  246. d->connect();
  247. }
  248. //-----------------------------------------------------------------------------
  249. void ctkVTKConnection::setBlocked(bool block)
  250. {
  251. Q_D(ctkVTKConnection);
  252. d->Blocked = block;
  253. }
  254. //-----------------------------------------------------------------------------
  255. bool ctkVTKConnection::isBlocked()const
  256. {
  257. Q_D(const ctkVTKConnection);
  258. return d->Blocked;
  259. }
  260. //-----------------------------------------------------------------------------
  261. bool ctkVTKConnection::isEqual(vtkObject* vtk_obj, unsigned long vtk_event,
  262. const QObject* qt_obj, QString qt_slot)const
  263. {
  264. Q_D(const ctkVTKConnection);
  265. if (vtk_obj && d->VTKObject != vtk_obj)
  266. {
  267. return false;
  268. }
  269. if (vtk_event != vtkCommand::NoEvent && d->VTKEvent != vtk_event)
  270. {
  271. return false;
  272. }
  273. if (qt_obj && d->QtObject != qt_obj)
  274. {
  275. return false;
  276. }
  277. if (!qt_slot.isEmpty() &&
  278. (QString(d->QtSlot).remove(' ').compare(
  279. QString(qt_slot).remove(' ')) != 0))
  280. {
  281. return false;
  282. }
  283. return true;
  284. }
  285. //-----------------------------------------------------------------------------
  286. void ctkVTKConnectionPrivate::DoCallback(vtkObject* vtk_obj, unsigned long event,
  287. void* client_data, void* call_data)
  288. {
  289. ctkVTKConnectionPrivate* conn = reinterpret_cast<ctkVTKConnectionPrivate*>(client_data);
  290. Q_ASSERT(conn);
  291. conn->execute(vtk_obj, event, client_data, call_data);
  292. }
  293. //-----------------------------------------------------------------------------
  294. // callback from VTK to emit signal
  295. void ctkVTKConnectionPrivate::execute(vtkObject* vtk_obj, unsigned long vtk_event,
  296. void* client_data, void* call_data)
  297. {
  298. Q_Q(ctkVTKConnection);
  299. Q_ASSERT(this->Connected);
  300. if (this->Blocked)
  301. {
  302. return;
  303. }
  304. QPointer<ctkVTKConnection> connection(q);
  305. if(!this->ObserveDeletion ||
  306. vtk_event != vtkCommand::DeleteEvent ||
  307. this->VTKEvent == vtkCommand::DeleteEvent)
  308. {
  309. vtkObject* callDataAsVtkObject = 0;
  310. switch (this->SlotType)
  311. {
  312. case ctkVTKConnectionPrivate::ARG_VTKOBJECT_AND_VTKOBJECT:
  313. if (this->VTKEvent == vtk_event)
  314. {
  315. callDataAsVtkObject = reinterpret_cast<vtkObject*>( call_data );
  316. if (!callDataAsVtkObject)
  317. {
  318. qCritical() << "The VTKEvent(" << this->VTKEvent<< ") triggered by vtkObject("
  319. << this->VTKObject->GetClassName() << ") "
  320. << "doesn't return data of type vtkObject." << endl
  321. << "The slot (" << this->QtSlot << ") owned by "
  322. << "QObject(" << this->QtObject->objectName() << ")"
  323. << " may be incorrect.";
  324. }
  325. emit q->emitExecute(vtk_obj, callDataAsVtkObject);
  326. }
  327. break;
  328. case ctkVTKConnectionPrivate::ARG_VTKOBJECT_VOID_ULONG_VOID:
  329. emit q->emitExecute(vtk_obj, call_data, vtk_event, client_data);
  330. break;
  331. default:
  332. // Should never reach
  333. qCritical() << "Unknown SlotType:" << this->SlotType;
  334. return;
  335. break;
  336. }
  337. }
  338. if (!connection.isNull() &&
  339. vtk_event == vtkCommand::DeleteEvent)
  340. {
  341. q->vtkObjectDeleted();
  342. }
  343. }
  344. //-----------------------------------------------------------------------------
  345. void ctkVTKConnection::observeDeletion(bool enable)
  346. {
  347. Q_D(ctkVTKConnection);
  348. d->ObserveDeletion = enable;
  349. }
  350. //-----------------------------------------------------------------------------
  351. bool ctkVTKConnection::deletionObserved()const
  352. {
  353. Q_D(const ctkVTKConnection);
  354. return d->ObserveDeletion;
  355. }
  356. //-----------------------------------------------------------------------------
  357. void ctkVTKConnection::disconnect()
  358. {
  359. Q_D(ctkVTKConnection);
  360. d->disconnect();
  361. }
  362. //-----------------------------------------------------------------------------
  363. void ctkVTKConnection::vtkObjectDeleted()
  364. {
  365. Q_D(ctkVTKConnection);
  366. d->VTKObject = 0;
  367. d->disconnect();
  368. emit isBroke();
  369. }
  370. //-----------------------------------------------------------------------------
  371. void ctkVTKConnection::qobjectDeleted()
  372. {
  373. Q_D(ctkVTKConnection);
  374. d->QtObject = 0;
  375. d->disconnect();
  376. emit isBroke();
  377. }
  378. //-----------------------------------------------------------------------------
  379. void ctkVTKConnection::addObserver(vtkObject* caller, unsigned long vtk_event,
  380. vtkCallbackCommand* callback, float priority)
  381. {
  382. caller->AddObserver(vtk_event, callback, priority);
  383. }
  384. //-----------------------------------------------------------------------------
  385. void ctkVTKConnection::removeObserver(vtkObject* caller, unsigned long vtk_event, vtkCallbackCommand* callback)
  386. {
  387. caller->RemoveObservers(vtk_event, callback);
  388. }