ctkVTKAbstractView.h 7.9 KB

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