ctkVTKRenderView.cpp 5.2 KB

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