ctkVTKConnection.cpp 13 KB

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