ctkVTKObjectEventsObserver.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #ifndef __ctkVTKObjectEventsObserver_h
  15. #define __ctkVTKObjectEventsObserver_h
  16. /// Qt includes
  17. #include <QObject>
  18. #include <QList>
  19. #include <QString>
  20. /// CTK includes
  21. #include <ctkSingleton.h>
  22. #include "ctkVisualizationVTKCoreExport.h"
  23. class ctkVTKConnection;
  24. class ctkVTKObjectEventsObserverPrivate;
  25. /// VTK includes
  26. #include <vtkCommand.h>
  27. class vtkObject;
  28. //-----------------------------------------------------------------------------
  29. /// \ingroup Visualization_VTK_Core
  30. /// \brief Connect vtkObject events with QObject slots.
  31. /// Helper class that provides utility methods for connecting vtkObjects with
  32. /// QObjects.
  33. class CTK_VISUALIZATION_VTK_CORE_EXPORT ctkVTKObjectEventsObserver : public QObject
  34. {
  35. Q_OBJECT
  36. /// This property controls wether or not you can replace a
  37. /// connection by a connection from an object of a different VTK class tha
  38. /// the first.
  39. /// For example, if strictTypeCheck is on, the following will generate an error
  40. /// \code
  41. /// vtkActor* actor = vtkActor::New();
  42. /// objectEventsObserver->addConnection(actor, vtkCommand::ModifiedEvent, ...);
  43. /// vtkMapper* mapper = vtkMapper::New();
  44. /// objectEventsObserver->addConnection(actor, mapper, vtkCommand::ModifiedEvent, ...);
  45. /// \endcode
  46. /// False by default.
  47. /// \sa strictTypeCheck(), setStrictTypeCheck(),
  48. /// addConnection()
  49. Q_PROPERTY(bool strictTypeCheck READ strictTypeCheck WRITE setStrictTypeCheck)
  50. public:
  51. typedef QObject Superclass;
  52. explicit ctkVTKObjectEventsObserver(QObject* parent = 0);
  53. virtual ~ctkVTKObjectEventsObserver();
  54. virtual void printAdditionalInfo();
  55. /// Return the strictTypeCheck value.
  56. /// \sa strictTypeCheck, setStrictTypeCheck()
  57. bool strictTypeCheck()const;
  58. /// Set the strictTypeCheck value.
  59. /// \sa strictTypeCheck, strictTypeCheck()
  60. /// \note By default, strict type checking is disabled.
  61. void setStrictTypeCheck(bool check);
  62. ///
  63. /// Add a connection between a \c vtkObject and a \c QObject.
  64. /// When the \a vtk_obj \c vtkObject invokes the \a vtk_event event,
  65. /// the slot \a qt_slot of the \c QObject \a qt_obj is called
  66. /// \a priority is used for the \c vtkObject observation and
  67. /// \a connectionType controls when the slot is called.
  68. /// The slot must have the signature
  69. /// \code (vtkObject*,void*,unsigned long,void*) \endcode where the
  70. /// parameters are respectively \code (sender,callData,eventId,clientData)
  71. /// \endcode.
  72. /// Or with the signature
  73. /// \code (vtkObject*,vtkObject*) \endcode where the first \c vtkObject* is
  74. /// the sender and the second \c vtkObject* is the callData casted into a
  75. /// \c vtkObject.
  76. /// The slot can contain less parameters, but must be in the same order.
  77. /// An ID allowing to uniquely identify the connection is
  78. /// returned. It is a no-op if vtk_obj is NULL, the parameters are invalid or
  79. /// if the connection already exist.
  80. /// \sa addConnection(vtkObject* old_vtk_obj,vtkObject* vtk_obj,
  81. /// unsigned long vtk_event, const QObject* qt_obj, const char* qt_slot,
  82. /// float priority = 0.0, Qt::ConnectionType connectionType =
  83. /// Qt::AutoConnection), reconnection(), removeConnection(),
  84. /// removeAllConnections(), containsConnection()
  85. QString addConnection(vtkObject* vtk_obj, unsigned long vtk_event,
  86. const QObject* qt_obj, const char* qt_slot, float priority = 0.0,
  87. Qt::ConnectionType connectionType = Qt::AutoConnection);
  88. ///
  89. /// Remove any connection to \a old_vtk_obj and add a connection
  90. /// to \a vtk_obj (with same event, object, slot, priority and type).
  91. /// \sa addConnection(), reconnection(),
  92. /// removeConnection(), removeAllConnections(), containsConnection()
  93. QString addConnection(vtkObject* old_vtk_obj,
  94. vtkObject* vtk_obj, unsigned long vtk_event,
  95. const QObject* qt_obj, const char* qt_slot, float priority = 0.0,
  96. Qt::ConnectionType connectionType = Qt::AutoConnection);
  97. ///
  98. /// Utility function that remove a connection on old_vtk_obj and add a connection
  99. /// to vtk_obj (with same event, object, slot, priority)
  100. /// \sa addConnection(), removeConnection(), removeAllConnections(),
  101. /// containsConnection()
  102. QString reconnection(vtkObject* vtk_obj, unsigned long vtk_event,
  103. const QObject* qt_obj, const char* qt_slot,
  104. float priority = 0.0,
  105. Qt::ConnectionType connectionType = Qt::AutoConnection);
  106. ///
  107. /// Remove all the connections matching vtkobj, event, qtobj and slot using
  108. /// wildcards or not.
  109. /// Returns the number of connection removed.
  110. /// \sa addConnection(), reconnection(), removeAllConnections(),
  111. /// containsConnection()
  112. int removeConnection(vtkObject* vtk_obj, unsigned long vtk_event = vtkCommand::NoEvent,
  113. const QObject* qt_obj = 0, const char* qt_slot = 0);
  114. ///
  115. /// Remove all the connections
  116. /// \sa removeConnection()
  117. int removeAllConnections();
  118. ///
  119. /// Temporarilly block all the connection
  120. /// Returns the previous value of connectionsBlocked()
  121. bool blockAllConnections(bool block);
  122. ///
  123. /// Returns true if connections are blocked; otherwise returns false.
  124. /// Connections are not blocked by default.
  125. bool connectionsBlocked()const;
  126. ///
  127. /// Block/Unblock one or multiple connection.
  128. /// Return the number of connections blocked/unblocked
  129. int blockConnection(bool block, vtkObject* vtk_obj,
  130. unsigned long vtk_event, const QObject* qt_obj);
  131. /// Block/Unblock a connection
  132. /// Return true if the connection exists and was blocked, otherwise returns
  133. /// false.
  134. bool blockConnection(const QString& id, bool blocked);
  135. /// Return true if there is at least 1 connection matching the parameters,
  136. /// false otherwise.
  137. /// \sa addConnection(), reconnection(), removeConnection(),
  138. /// removeAllConnections()
  139. bool containsConnection(vtkObject* vtk_obj, unsigned long vtk_event = vtkCommand::NoEvent,
  140. const QObject* qt_obj =0, const char* qt_slot =0)const;
  141. protected:
  142. QScopedPointer<ctkVTKObjectEventsObserverPrivate> d_ptr;
  143. private:
  144. Q_DECLARE_PRIVATE(ctkVTKObjectEventsObserver);
  145. Q_DISABLE_COPY(ctkVTKObjectEventsObserver);
  146. };
  147. //-----------------------------------------------------------------------------
  148. /// \ingroup Visualization_VTK_Core
  149. class CTK_VISUALIZATION_VTK_CORE_EXPORT ctkVTKConnectionFactory
  150. {
  151. public:
  152. static ctkVTKConnectionFactory* instance();
  153. /// The singleton takes ownerchip of the new factory instance and will take care
  154. /// of cleaning the memory.
  155. /// \note If \a newInstance is not null, the current factory instance will be
  156. /// deleted. Note also that setting a null \a newInstance is a no-op.
  157. static void setInstance(ctkVTKConnectionFactory* newInstance);
  158. virtual ctkVTKConnection* createConnection(ctkVTKObjectEventsObserver*)const;
  159. protected:
  160. ctkVTKConnectionFactory();
  161. virtual ~ctkVTKConnectionFactory();
  162. CTK_SINGLETON_DECLARE(ctkVTKConnectionFactory)
  163. };
  164. CTK_SINGLETON_DECLARE_INITIALIZER(CTK_VISUALIZATION_VTK_CORE_EXPORT, ctkVTKConnectionFactory)
  165. #endif