ctkVTKRenderView.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. All rights reserved.
  5. Distributed under a BSD License. See LICENSE.txt file.
  6. This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the above copyright notice for more information.
  9. =========================================================================*/
  10. // Qt includes
  11. #include <QTimer>
  12. #include <QVBoxLayout>
  13. // CTK includes
  14. #include "ctkVTKRenderView.h"
  15. #include "ctkVTKRenderView_p.h"
  16. // VTK includes
  17. #include <vtkRendererCollection.h>
  18. #include <vtkRenderWindowInteractor.h>
  19. #include <vtkTextProperty.h>
  20. // --------------------------------------------------------------------------
  21. // ctkVTKRenderViewPrivate methods
  22. // --------------------------------------------------------------------------
  23. ctkVTKRenderViewPrivate::ctkVTKRenderViewPrivate()
  24. {
  25. this->Renderer = vtkSmartPointer<vtkRenderer>::New();
  26. this->RenderWindow = vtkSmartPointer<vtkRenderWindow>::New();
  27. this->Axes = vtkSmartPointer<vtkAxesActor>::New();
  28. this->Orientation = vtkSmartPointer<vtkOrientationMarkerWidget>::New();
  29. this->CornerAnnotation = vtkSmartPointer<vtkCornerAnnotation>::New();
  30. this->RenderPending = false;
  31. }
  32. // --------------------------------------------------------------------------
  33. void ctkVTKRenderViewPrivate::setupCornerAnnotation()
  34. {
  35. if (!this->Renderer->HasViewProp(this->CornerAnnotation))
  36. {
  37. this->Renderer->AddViewProp(this->CornerAnnotation);
  38. this->CornerAnnotation->SetMaximumLineHeight(0.07);
  39. vtkTextProperty *tprop = this->CornerAnnotation->GetTextProperty();
  40. tprop->ShadowOn();
  41. }
  42. this->CornerAnnotation->ClearAllTexts();
  43. }
  44. //---------------------------------------------------------------------------
  45. void ctkVTKRenderViewPrivate::setupRendering()
  46. {
  47. Q_ASSERT(this->RenderWindow);
  48. this->RenderWindow->SetAlphaBitPlanes(1);
  49. this->RenderWindow->SetMultiSamples(0);
  50. this->RenderWindow->StereoCapableWindowOn();
  51. this->RenderWindow->GetRenderers()->RemoveAllItems();
  52. // Add renderer
  53. this->RenderWindow->AddRenderer(this->Renderer);
  54. // Setup the corner annotation
  55. this->setupCornerAnnotation();
  56. this->VTKWidget->SetRenderWindow(this->RenderWindow);
  57. }
  58. //---------------------------------------------------------------------------
  59. void ctkVTKRenderViewPrivate::setupDefaultInteractor()
  60. {
  61. CTK_P(ctkVTKRenderView);
  62. p->setInteractor(this->RenderWindow->GetInteractor());
  63. }
  64. //---------------------------------------------------------------------------
  65. // ctkVTKRenderView methods
  66. // --------------------------------------------------------------------------
  67. ctkVTKRenderView::ctkVTKRenderView(QWidget* _parent) : Superclass(_parent)
  68. {
  69. CTK_INIT_PRIVATE(ctkVTKRenderView);
  70. CTK_D(ctkVTKRenderView);
  71. d->VTKWidget = new QVTKWidget(this);
  72. this->setLayout(new QVBoxLayout);
  73. this->layout()->setMargin(0);
  74. this->layout()->setSpacing(0);
  75. this->layout()->addWidget(d->VTKWidget);
  76. d->setupRendering();
  77. d->setupDefaultInteractor();
  78. }
  79. // --------------------------------------------------------------------------
  80. ctkVTKRenderView::~ctkVTKRenderView()
  81. {
  82. }
  83. //----------------------------------------------------------------------------
  84. CTK_GET_CXX(ctkVTKRenderView, vtkRenderWindowInteractor*, interactor, CurrentInteractor);
  85. //----------------------------------------------------------------------------
  86. void ctkVTKRenderView::scheduleRender()
  87. {
  88. CTK_D(ctkVTKRenderView);
  89. if (!d->RenderPending)
  90. {
  91. d->RenderPending = true;
  92. QTimer::singleShot(0, this, SLOT(forceRender()));
  93. }
  94. }
  95. //----------------------------------------------------------------------------
  96. void ctkVTKRenderView::forceRender()
  97. {
  98. CTK_D(ctkVTKRenderView);
  99. d->RenderWindow->Render();
  100. d->RenderPending = false;
  101. }
  102. //----------------------------------------------------------------------------
  103. void ctkVTKRenderView::setInteractor(vtkRenderWindowInteractor* newInteractor)
  104. {
  105. Q_ASSERT(newInteractor);
  106. CTK_D(ctkVTKRenderView);
  107. d->RenderWindow->SetInteractor(newInteractor);
  108. d->Orientation->SetOrientationMarker(d->Axes);
  109. d->Orientation->SetInteractor(newInteractor);
  110. d->Orientation->SetEnabled(1);
  111. d->Orientation->InteractiveOff();
  112. d->CurrentInteractor = newInteractor;
  113. }
  114. //----------------------------------------------------------------------------
  115. void ctkVTKRenderView::setCornerAnnotationText(const QString& text)
  116. {
  117. CTK_D(ctkVTKRenderView);
  118. d->CornerAnnotation->ClearAllTexts();
  119. d->CornerAnnotation->SetText(2, text.toLatin1());
  120. }
  121. // --------------------------------------------------------------------------
  122. void ctkVTKRenderView::setBackgroundColor(double r, double g, double b)
  123. {
  124. CTK_D(ctkVTKRenderView);
  125. double background_color[3] = {r, g, b};
  126. d->Renderer->SetBackground(background_color);
  127. }
  128. //----------------------------------------------------------------------------
  129. void ctkVTKRenderView::resetCamera()
  130. {
  131. CTK_D(ctkVTKRenderView);
  132. d->Renderer->ResetCamera();
  133. }