ctkVTKAbstractView.h 7.7 KB

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