ctkVTKConnection.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 <QRegExp>
  17. #include <QString>
  18. #include <QTextStream>
  19. // CTK includes
  20. #include "ctkVTKConnection.h"
  21. // VTK includes
  22. #include <vtkObject.h>
  23. #include <vtkSmartPointer.h>
  24. #include <vtkCallbackCommand.h>
  25. //-----------------------------------------------------------------------------
  26. QString convertPointerToString(void* pointer)
  27. {
  28. QString pointerAsString;
  29. QTextStream(&pointerAsString) << pointer;
  30. return pointerAsString;
  31. }
  32. //-----------------------------------------------------------------------------
  33. class ctkVTKConnectionPrivate: public ctkPrivate<ctkVTKConnection>
  34. {
  35. public:
  36. enum
  37. {
  38. ARG_UNKNOWN = 0,
  39. ARG_VTKOBJECT_AND_VTKOBJECT,
  40. ARG_VTKOBJECT_VOID_ULONG_VOID
  41. };
  42. typedef ctkVTKConnectionPrivate Self;
  43. ctkVTKConnectionPrivate();
  44. ~ctkVTKConnectionPrivate();
  45. void connect();
  46. void disconnect();
  47. ///
  48. /// VTK Callback
  49. static void DoCallback(vtkObject* vtk_obj, unsigned long event,
  50. void* client_data, void* call_data);
  51. ///
  52. /// Called by 'DoCallback' to emit signal
  53. void Execute(vtkObject* vtk_obj, unsigned long vtk_event, void* client_data, void* call_data);
  54. vtkSmartPointer<vtkCallbackCommand> Callback;
  55. vtkObject* VTKObject;
  56. const QObject* QtObject;
  57. unsigned long VTKEvent;
  58. QString QtSlot;
  59. float Priority;
  60. int SlotType;
  61. bool Connected;
  62. bool Blocked;
  63. QString Id;
  64. bool AboutToBeDeleted;
  65. };
  66. //-----------------------------------------------------------------------------
  67. // ctkVTKConnectionPrivate methods
  68. //-----------------------------------------------------------------------------
  69. ctkVTKConnectionPrivate::ctkVTKConnectionPrivate()
  70. {
  71. this->Callback = vtkSmartPointer<vtkCallbackCommand>::New();
  72. this->Callback->SetCallback(ctkVTKConnectionPrivate::DoCallback);
  73. this->Callback->SetClientData(this);
  74. this->VTKObject = 0;
  75. this->QtObject = 0;
  76. this->VTKEvent = vtkCommand::NoEvent;
  77. this->Priority = 0.0;
  78. this->SlotType = ARG_UNKNOWN;
  79. this->Connected = false;
  80. this->Blocked = false;
  81. this->Id = convertPointerToString(this);
  82. this->AboutToBeDeleted = false;
  83. }
  84. //-----------------------------------------------------------------------------
  85. ctkVTKConnectionPrivate::~ctkVTKConnectionPrivate()
  86. {
  87. /*
  88. if(this->VTKObject && this->Connected)
  89. {
  90. this->VTKObject->RemoveObserver(this->Callback);
  91. //Qt takes care of disconnecting slots
  92. }
  93. */
  94. }
  95. //-----------------------------------------------------------------------------
  96. void ctkVTKConnectionPrivate::connect()
  97. {
  98. CTK_P(ctkVTKConnection);
  99. if (this->Connected)
  100. {
  101. qDebug() << "ctkVTKConnection already connected.";
  102. return;
  103. }
  104. switch (this->SlotType)
  105. {
  106. case ctkVTKConnectionPrivate::ARG_VTKOBJECT_AND_VTKOBJECT:
  107. QObject::connect(p, SIGNAL(emitExecute(vtkObject*, vtkObject*)),
  108. this->QtObject, this->QtSlot.toLatin1().data());
  109. break;
  110. case ctkVTKConnectionPrivate::ARG_VTKOBJECT_VOID_ULONG_VOID:
  111. QObject::connect(p, SIGNAL(emitExecute(vtkObject*, void*, unsigned long, void*)),
  112. this->QtObject, this->QtSlot.toLatin1().data());
  113. break;
  114. default:
  115. Q_ASSERT(false);
  116. qCritical() << "Failed to connect - "
  117. << "The slot (" << this->QtSlot << ") owned by "
  118. << "QObject(" << this->QtObject->objectName() << ")"
  119. << " seems to have a wrong signature.";
  120. break;
  121. }
  122. // Make a connection between this and the vtk object
  123. this->VTKObject->AddObserver(this->VTKEvent, this->Callback, this->Priority);
  124. // If necessary, observe vtk DeleteEvent
  125. if(this->VTKEvent != vtkCommand::DeleteEvent)
  126. {
  127. this->VTKObject->AddObserver(vtkCommand::DeleteEvent, this->Callback);
  128. }
  129. // Remove itself from its parent when vtkObject is deleted
  130. QObject::connect(this->QtObject, SIGNAL(destroyed(QObject*)),
  131. p, SLOT(deleteConnection()));
  132. this->Connected = true;
  133. }
  134. //-----------------------------------------------------------------------------
  135. void ctkVTKConnectionPrivate::disconnect()
  136. {
  137. CTK_P(ctkVTKConnection);
  138. if (!this->Connected)
  139. {
  140. Q_ASSERT(this->Connected);
  141. return;
  142. }
  143. this->VTKObject->RemoveObserver(this->Callback);
  144. switch (this->SlotType)
  145. {
  146. case ctkVTKConnectionPrivate::ARG_VTKOBJECT_AND_VTKOBJECT:
  147. QObject::disconnect(p, SIGNAL(emitExecute(vtkObject*, vtkObject*)),
  148. this->QtObject,this->QtSlot.toLatin1().data());
  149. break;
  150. case ctkVTKConnectionPrivate::ARG_VTKOBJECT_VOID_ULONG_VOID:
  151. QObject::disconnect(p, SIGNAL(emitExecute(vtkObject*, void*, unsigned long, void*)),
  152. this->QtObject, this->QtSlot.toLatin1().data());
  153. break;
  154. default:
  155. Q_ASSERT(false);
  156. qCritical() << "Failed to disconnect - "
  157. << "The slot (" << this->QtSlot << ") owned by "
  158. << "QObject(" << this->QtObject->objectName() << ")"
  159. << " seems to have a wrong signature.";
  160. break;
  161. }
  162. QObject::disconnect(this->QtObject, SIGNAL(destroyed(QObject*)),
  163. p, SLOT(deleteConnection()));
  164. this->Connected = false;
  165. }
  166. //-----------------------------------------------------------------------------
  167. // ctkVTKConnection methods
  168. //-----------------------------------------------------------------------------
  169. ctkVTKConnection::ctkVTKConnection(QObject* _parent):
  170. Superclass(_parent)
  171. {
  172. CTK_INIT_PRIVATE(ctkVTKConnection);
  173. }
  174. //-----------------------------------------------------------------------------
  175. ctkVTKConnection::~ctkVTKConnection()
  176. {
  177. this->setEnabled(false);
  178. }
  179. //-----------------------------------------------------------------------------
  180. CTK_GET_CXX(ctkVTKConnection, QString, id, Id);
  181. //-----------------------------------------------------------------------------
  182. void ctkVTKConnection::printAdditionalInfo()
  183. {
  184. this->Superclass::dumpObjectInfo();
  185. CTK_D(ctkVTKConnection);
  186. qDebug() << "ctkVTKConnection:" << this << endl
  187. << "Id:" << d->Id << endl
  188. << " VTKObject:" << d->VTKObject->GetClassName()
  189. << "(" << d->VTKObject << ")" << endl
  190. << " QtObject:" << d->QtObject << endl
  191. << " VTKEvent:" << d->VTKEvent << endl
  192. << " QtSlot:" << d->QtSlot << endl
  193. << " SlotType:" << d->SlotType << endl
  194. << " Priority:" << d->Priority << endl
  195. << " Connected:" << d->Connected << endl
  196. << " Blocked:" << d->Blocked;
  197. }
  198. //-----------------------------------------------------------------------------
  199. QString ctkVTKConnection::shortDescription()
  200. {
  201. CTK_D(ctkVTKConnection);
  202. return Self::shortDescription(d->VTKObject, d->VTKEvent, d->QtObject, d->QtSlot);
  203. }
  204. //-----------------------------------------------------------------------------
  205. QString ctkVTKConnection::shortDescription(vtkObject* vtk_obj, unsigned long vtk_event,
  206. const QObject* qt_obj, QString qt_slot)
  207. {
  208. QString ret;
  209. QTextStream ts( &ret );
  210. ts << (vtk_obj ? vtk_obj->GetClassName() : "NULL") << " "
  211. << vtk_event << " " << qt_obj << " " << qt_slot;
  212. return ret;
  213. }
  214. //-----------------------------------------------------------------------------
  215. bool ctkVTKConnection::ValidateParameters(vtkObject* vtk_obj, unsigned long vtk_event,
  216. const QObject* qt_obj, QString qt_slot)
  217. {
  218. Q_UNUSED(vtk_event);
  219. if (!vtk_obj)
  220. {
  221. return false;
  222. }
  223. if (!qt_obj)
  224. {
  225. return false;
  226. }
  227. if (qt_slot.isEmpty())
  228. {
  229. return false;
  230. }
  231. return true;
  232. }
  233. //-----------------------------------------------------------------------------
  234. void ctkVTKConnection::SetParameters(vtkObject* vtk_obj, unsigned long vtk_event,
  235. const QObject* qt_obj, QString qt_slot, float priority)
  236. {
  237. CTK_D(ctkVTKConnection);
  238. if (!Self::ValidateParameters(vtk_obj, vtk_event, qt_obj, qt_slot))
  239. {
  240. return;
  241. }
  242. d->VTKObject = vtk_obj;
  243. d->QtObject = qt_obj;
  244. d->VTKEvent = vtk_event;
  245. d->QtSlot = qt_slot;
  246. d->Priority = priority;
  247. if (qt_slot.contains(QRegExp(QString("\\( ?vtkObject ?\\* ?, ?vtkObject ?\\* ?\\)"))))
  248. {
  249. d->SlotType = ctkVTKConnectionPrivate::ARG_VTKOBJECT_AND_VTKOBJECT;
  250. }
  251. else
  252. {
  253. d->SlotType = ctkVTKConnectionPrivate::ARG_VTKOBJECT_VOID_ULONG_VOID;
  254. }
  255. }
  256. //-----------------------------------------------------------------------------
  257. void ctkVTKConnection::setEnabled(bool enable)
  258. {
  259. CTK_D(ctkVTKConnection);
  260. if (d->Connected == enable)
  261. {
  262. return;
  263. }
  264. if (enable)
  265. {
  266. d->connect();
  267. }
  268. else
  269. {
  270. d->disconnect();
  271. }
  272. }
  273. //-----------------------------------------------------------------------------
  274. bool ctkVTKConnection::isEnabled()const
  275. {
  276. CTK_D(const ctkVTKConnection);
  277. return d->Connected;
  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 = static_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. if (vtk_event == vtkCommand::DeleteEvent)
  336. {
  337. this->AboutToBeDeleted = true;
  338. }
  339. if(vtk_event != vtkCommand::DeleteEvent ||
  340. (vtk_event == vtkCommand::DeleteEvent && this->VTKEvent == vtkCommand::DeleteEvent))
  341. {
  342. vtkObject* callDataAsVtkObject = 0;
  343. switch (this->SlotType)
  344. {
  345. case ctkVTKConnectionPrivate::ARG_VTKOBJECT_AND_VTKOBJECT:
  346. if (this->VTKEvent == vtk_event)
  347. {
  348. callDataAsVtkObject = reinterpret_cast<vtkObject*>( call_data );
  349. if (!callDataAsVtkObject)
  350. {
  351. qCritical() << "The VTKEvent(" << this->VTKEvent<< ") triggered by vtkObject("
  352. << this->VTKObject->GetClassName() << ") "
  353. << "doesn't return data of type vtkObject." << endl
  354. << "The slot (" << this->QtSlot << ") owned by "
  355. << "QObject(" << this->QtObject->objectName() << ")"
  356. << " may be incorrect.";
  357. }
  358. emit p->emitExecute(vtk_obj, callDataAsVtkObject);
  359. }
  360. break;
  361. case ctkVTKConnectionPrivate::ARG_VTKOBJECT_VOID_ULONG_VOID:
  362. emit p->emitExecute(vtk_obj, call_data, vtk_event, client_data);
  363. break;
  364. default:
  365. // Should never reach
  366. qCritical() << "Unknown SlotType:" << this->SlotType;
  367. return;
  368. break;
  369. }
  370. }
  371. if(vtk_event == vtkCommand::DeleteEvent)
  372. {
  373. this->AboutToBeDeleted = false;
  374. p->deleteConnection();
  375. }
  376. }
  377. void ctkVTKConnection::deleteConnection()
  378. {
  379. CTK_D(ctkVTKConnection);
  380. if (d->AboutToBeDeleted)
  381. {
  382. return;
  383. }
  384. delete this;
  385. }