ctkVTKAbstractView.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 __ctkVTKAbstractView_h
  15. #define __ctkVTKAbstractView_h
  16. // Qt includes
  17. #include <QWidget>
  18. // CTK includes
  19. #include "ctkVTKOpenGLNativeWidget.h"
  20. #include "ctkVTKObject.h"
  21. #include "ctkVisualizationVTKWidgetsExport.h"
  22. class ctkVTKAbstractViewPrivate;
  23. class vtkCornerAnnotation;
  24. class vtkInteractorObserver;
  25. class vtkRenderWindowInteractor;
  26. class vtkRenderWindow;
  27. /// \ingroup Visualization_VTK_Widgets
  28. class CTK_VISUALIZATION_VTK_WIDGETS_EXPORT ctkVTKAbstractView : public QWidget
  29. {
  30. Q_OBJECT
  31. QVTK_OBJECT
  32. Q_PROPERTY(QString cornerAnnotationText READ cornerAnnotationText WRITE setCornerAnnotationText)
  33. Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor)
  34. Q_PROPERTY(QColor backgroundColor2 READ backgroundColor2 WRITE setBackgroundColor)
  35. Q_PROPERTY(bool gradientBackground READ gradientBackground WRITE setGradientBackground)
  36. Q_PROPERTY(bool renderEnabled READ renderEnabled WRITE setRenderEnabled)
  37. /// This property controls whether a corner annotation is visible with the
  38. /// last frames per second (FPS) value.
  39. /// false by default.
  40. Q_PROPERTY(bool fpsVisible READ isFPSVisible WRITE setFPSVisible)
  41. /// This property controls whether the render window uses depth peeling or
  42. /// not.
  43. /// false by default.
  44. Q_PROPERTY(bool useDepthPeeling READ useDepthPeeling WRITE setUseDepthPeeling)
  45. /// Set a maximum rate (in frames per second) for rendering.
  46. Q_PROPERTY(double maximumUpdateRate READ maximumUpdateRate WRITE setMaximumUpdateRate)
  47. public:
  48. typedef QWidget Superclass;
  49. explicit ctkVTKAbstractView(QWidget* parent = 0);
  50. virtual ~ctkVTKAbstractView();
  51. public Q_SLOTS:
  52. /// Notify QVTKWidget that the view needs to be rendered.
  53. /// scheduleRender() respects the maximum update rate of the view,
  54. /// it won't render the window more frequently than what the maximum
  55. /// update rate is.
  56. /// \sa setMaximumUpdateRate
  57. virtual void scheduleRender();
  58. /// Force a render even if a render is already ocurring
  59. /// Be careful when calling forceRender() as it can slow down your
  60. /// application. It is preferable to use scheduleRender() instead.
  61. /// \sa scheduleRender
  62. virtual void forceRender();
  63. /// Calls pauseRender() if pause is true or resumeRender() if pause is false
  64. /// When pause render count is greater than 0, prevents requestRender() from calling forceRender()
  65. /// Callers are responsible for calling both setPauseRender(true) and setPauseRender(false)
  66. /// Ex.
  67. /// \code{.cpp}
  68. /// view->pauseRender() // Or setPauseRender(true)
  69. /// // Perform operations that may call view->scheduleRender().
  70. /// view->resumeRender(); // Or setPauseRender(false)
  71. /// \endcode
  72. ///
  73. /// If the pause render count reaches zero when calling resumeRender(), scheduleRender() will be
  74. /// called if a scheduleRender() was invoked while rendering was paused.
  75. /// Rendering can still be triggered while the paused with forceRender()
  76. ///
  77. /// This behaviour is different from renderEnabled(), which will prevent all rendering calls
  78. /// from both scheduleRender() and forceRender(), and will not invoke either when re-enabled.
  79. /// \sa renderEnabled
  80. virtual int setRenderPaused(bool pause);
  81. /// Increments the pause render count
  82. /// \sa setPauseRender
  83. virtual int pauseRender();
  84. /// De-increments the pause render count and calls scheduleRender() if one is currently pending
  85. /// \sa setPauseRender
  86. virtual int resumeRender();
  87. /// Returns true if the current pause render count is greater than 0
  88. /// \sa setPauseRender
  89. virtual bool isRenderPaused()const;
  90. /// Set maximum rate for rendering (in frames per second).
  91. /// If rendering is requested more frequently than this rate using scheduleRender,
  92. /// actual rendering will happen at this rate.
  93. /// This mechanism prevents repeated rendering caused by cluster of rendering requests.
  94. ///
  95. /// If maximum update rate is set to 0 then it indicates that rendering is done next time
  96. /// the application is idle, i.e., pending timer events are processed. This option should be used
  97. /// with caution, as policy of timer event processing may differ between operating systems.
  98. /// Specifically, on macOS, timer events may be indefinitely delayed if user interface continuously
  99. /// generates events.
  100. ///
  101. /// RenderWindow's DesiredUpdateRate property is intended for determining rendering quality settings,
  102. /// and is not suitable to be used as maximum update rate. The main reason is that VTK usually makes the
  103. /// rendering much faster and lower quality than DesiredUpdateRate would dictate, and so it would
  104. /// unnecessarily decrease the actual refresh rate.
  105. ///
  106. /// By default maximum update rate is set to 60FPS, which allows smooth updates, while effectively
  107. /// suppressing repeated update requests (after a rendering has been completed,
  108. /// repeated rendering requests will be ignored for 17 milliseconds).
  109. ///
  110. /// \sa scheduleRender
  111. void setMaximumUpdateRate(double fps);
  112. /// Set the background color of the rendering screen.
  113. virtual void setBackgroundColor(const QColor& newBackgroundColor);
  114. /// Set the second background color of the rendering screen for gradient
  115. /// backgrounds.
  116. virtual void setBackgroundColor2(const QColor& newBackgroundColor);
  117. /// Set whether this view should have a gradient background using the
  118. /// Background (top) and Background2 (bottom) colors. Default is off.
  119. virtual void setGradientBackground(bool enable);
  120. /// Enable/Disable rendering
  121. void setRenderEnabled(bool value);
  122. /// Set corner annotation \a text
  123. virtual void setCornerAnnotationText(const QString& text);
  124. /// Show/Hide the FPS annotation
  125. void setFPSVisible(bool show);
  126. /// Set the useDepthPeeling property value.
  127. /// \sa useDepthPeeling
  128. void setUseDepthPeeling(bool use);
  129. public:
  130. /// Get underlying RenderWindow
  131. Q_INVOKABLE vtkRenderWindow* renderWindow()const;
  132. /// Set/Get window interactor
  133. Q_INVOKABLE vtkRenderWindowInteractor* interactor()const;
  134. /// QVTKWidget catches all render requests, and ensure the desired framerate
  135. /// is respected.
  136. /// The interactor never calls Render() on the render window.
  137. /// TBD: can we only set a QVTKRenderWindowInteractor ?
  138. virtual void setInteractor(vtkRenderWindowInteractor* interactor);
  139. /// Get current interactor style
  140. Q_INVOKABLE vtkInteractorObserver* interactorStyle()const;
  141. /// Get corner annotation \a text
  142. QString cornerAnnotationText() const;
  143. Q_INVOKABLE vtkCornerAnnotation* cornerAnnotation()const;
  144. /// Get the underlying QVTKWidget
  145. Q_INVOKABLE ctkVTKOpenGLNativeWidget * VTKWidget() const;
  146. /// Get background color
  147. virtual QColor backgroundColor() const;
  148. /// Get the second background color
  149. virtual QColor backgroundColor2() const;
  150. /// Is the background a gradient
  151. virtual bool gradientBackground() const;
  152. /// Return if rendering is enabled
  153. bool renderEnabled() const;
  154. /// Return true if the FPS annotation is visible, false otherwise.
  155. bool isFPSVisible() const;
  156. /// Return the current FPS
  157. double fps()const;
  158. /// Returns maximum rate for rendering (in frames per second).
  159. /// \\sa setMaximumUpdateRate
  160. double maximumUpdateRate()const;
  161. /// Returns true if depth peeling is enabled.
  162. /// \sa setUseDepthPeeling
  163. bool useDepthPeeling()const;
  164. /// Set the default number of multisamples to use. Note that a negative
  165. /// value means "auto", which means the renderer will attempt to select
  166. /// the maximum number (but is not guaranteed to work).
  167. ///
  168. /// WARNING: Multisampling should be set *before* creation of the
  169. /// OpenGL context (e.g., initializing the rendering window) in order
  170. /// to have an effect. Consider using setMultisamples before
  171. /// instantiating ctkVTKAbstractView objects.
  172. /// \sa multiSamples
  173. static void setMultiSamples(int);
  174. /// Return the current multisamples default
  175. /// \sa setMultiSamples()
  176. static int multiSamples();
  177. virtual QSize minimumSizeHint()const;
  178. virtual QSize sizeHint()const;
  179. virtual bool hasHeightForWidth()const;
  180. virtual int heightForWidth(int width)const;
  181. protected Q_SLOTS:
  182. void onRender();
  183. void updateFPS();
  184. /// Calls forceRender if the rendering has not been paused from pauseRender()
  185. /// \sa pauseRender
  186. virtual void requestRender();
  187. protected:
  188. QScopedPointer<ctkVTKAbstractViewPrivate> d_ptr;
  189. ctkVTKAbstractView(ctkVTKAbstractViewPrivate* pimpl, QWidget* parent);
  190. private:
  191. Q_DECLARE_PRIVATE(ctkVTKAbstractView);
  192. Q_DISABLE_COPY(ctkVTKAbstractView);
  193. };
  194. #endif