Medical Imaging Interaction Toolkit  2018.4.99-3e3f1a6e
Medical Imaging Interaction Toolkit
QmitkRenderWindowWidget.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
14 
15 // vtk
16 #include <vtkCornerAnnotation.h>
17 #include <vtkTextProperty.h>
18 
20  const QString& widgetName/* = ""*/,
21  mitk::DataStorage* dataStorage/* = nullptr*/)
22  : QFrame(parent)
23  , m_WidgetName(widgetName)
24  , m_DataStorage(dataStorage)
25  , m_RenderWindow(nullptr)
26  , m_PointSetNode(nullptr)
27  , m_PointSet(nullptr)
28 {
29  this->InitializeGUI();
30 }
31 
33 {
34  auto sliceNavigationController = m_RenderWindow->GetSliceNavigationController();
35  if (nullptr != sliceNavigationController)
36  {
37  sliceNavigationController->SetCrosshairEvent.RemoveListener(mitk::MessageDelegate1<QmitkRenderWindowWidget, mitk::Point3D>(this, &QmitkRenderWindowWidget::SetCrosshair));
38  }
39  if (nullptr != m_DataStorage)
40  {
41  m_DataStorage->Remove(m_PointSetNode);
42  }
43 }
44 
46 {
47  if (dataStorage == m_DataStorage)
48  {
49  return;
50  }
51 
52  m_DataStorage = dataStorage;
53  if (nullptr != m_RenderWindow)
54  {
55  mitk::BaseRenderer::GetInstance(m_RenderWindow->GetRenderWindow())->SetDataStorage(dataStorage);
56  }
57 }
58 
60 {
61  return m_RenderWindow->GetSliceNavigationController();
62 }
63 
65 {
66  mitk::RenderingManager::GetInstance()->RequestUpdate(m_RenderWindow->GetRenderWindow());
67 }
68 
70 {
71  mitk::RenderingManager::GetInstance()->ForceImmediateUpdate(m_RenderWindow->GetRenderWindow());
72 }
73 
75 {
76  vtkRenderer* vtkRenderer = m_RenderWindow->GetRenderer()->GetVtkRenderer();
77  if (nullptr == vtkRenderer)
78  {
79  return;
80  }
81 
82  m_GradientBackgroundColors.first = upper;
83  m_GradientBackgroundColors.second = lower;
84  vtkRenderer->SetBackground(lower[0], lower[1], lower[2]);
85  vtkRenderer->SetBackground2(upper[0], upper[1], upper[2]);
86 
88 }
89 
91 {
92  m_RenderWindow->GetRenderer()->GetVtkRenderer()->SetGradientBackground(show);
93 }
94 
96 {
97  return m_RenderWindow->GetRenderer()->GetVtkRenderer()->GetGradientBackground();
98 }
99 
101 {
102  m_DecorationColor = color;
103  m_CornerAnnotation->GetTextProperty()->SetColor(m_DecorationColor[0], m_DecorationColor[1], m_DecorationColor[2]);
104 
105  QColor hexColor(m_DecorationColor[0] * 255, m_DecorationColor[1] * 255, m_DecorationColor[2] * 255);
106  setStyleSheet("QmitkRenderWindowWidget { border: 2px solid " + hexColor.name(QColor::HexRgb) + "; }");
107 }
108 
110 {
111  if (show)
112  {
113  setFrameStyle(QFrame::Box | QFrame::Plain);
114  }
115  else
116  {
117  setFrameStyle(NoFrame);
118  }
119 }
120 
122 {
123  return frameStyle() > 0;
124 }
125 
127 {
128  m_CornerAnnotation->SetVisibility(show);
129 }
130 
132 {
133  return m_CornerAnnotation->GetVisibility() > 0;
134 }
135 
136 void QmitkRenderWindowWidget::SetCornerAnnotationText(const std::string& cornerAnnotation)
137 {
138  m_CornerAnnotation->SetText(0, cornerAnnotation.c_str());
139 }
140 
142 {
143  return std::string(m_CornerAnnotation->GetText(0));
144 }
145 
147 {
148  return m_RenderWindow->GetActivateMenuWidgetFlag();
149 }
150 
152 {
153  if (nullptr == m_DataStorage)
154  {
155  return;
156  }
157 
158  if (activate)
159  {
160  try
161  {
162  m_DataStorage->Add(m_PointSetNode);
163  }
164  catch(std::invalid_argument& /*e*/)
165  {
166  // crosshair already existing
167  return;
168  }
169  }
170  else
171  {
172  m_DataStorage->Remove(m_PointSetNode);
173  }
174 }
175 
176 void QmitkRenderWindowWidget::InitializeGUI()
177 {
178  m_Layout = new QHBoxLayout(this);
179  m_Layout->setMargin(0);
180  setLayout(m_Layout);
181  setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
182  setContentsMargins(0, 0, 0, 0);
183 
184  if (nullptr == m_DataStorage)
185  {
186  return;
187  }
188 
190 
191  // create render window for this render window widget
192  m_RenderWindow = new QmitkRenderWindow(this, m_WidgetName, nullptr);
194  m_RenderWindow->GetSliceNavigationController()->SetDefaultViewDirection(mitk::SliceNavigationController::Sagittal);
195  m_RenderWindow->GetSliceNavigationController()->SetCrosshairEvent.AddListener(mitk::MessageDelegate1<QmitkRenderWindowWidget, mitk::Point3D>(this, &QmitkRenderWindowWidget::SetCrosshair));
196 
197  connect(m_RenderWindow, &QVTKOpenGLWidget::mouseEvent, this, &QmitkRenderWindowWidget::MouseEvent);
198 
199  mitk::TimeGeometry::ConstPointer timeGeometry = m_DataStorage->ComputeBoundingGeometry3D(m_DataStorage->GetAll());
201  m_Layout->addWidget(m_RenderWindow);
202 
203  // add point set as a crosshair
204  m_PointSetNode = mitk::DataNode::New();
205  m_PointSetNode->SetProperty("name", mitk::StringProperty::New("Crosshair of render window " + m_WidgetName.toStdString()));
206  m_PointSetNode->SetProperty("helper object", mitk::BoolProperty::New(true)); // crosshair-node should typically be invisible
207 
208  // set the crosshair only visible for this specific renderer
209  m_PointSetNode->SetBoolProperty("fixedLayer", true, m_RenderWindow->GetRenderer());
210  m_PointSetNode->SetVisibility(true, m_RenderWindow->GetRenderer());
211  m_PointSetNode->SetVisibility(false);
212 
213  m_PointSet = mitk::PointSet::New();
214  m_PointSetNode->SetData(m_PointSet);
215 
216  // set colors and corner annotation
217  InitializeDecorations();
218 }
219 
220 void QmitkRenderWindowWidget::InitializeDecorations()
221 {
222  vtkRenderer* vtkRenderer = m_RenderWindow->GetRenderer()->GetVtkRenderer();
223  if (nullptr == vtkRenderer)
224  {
225  return;
226  }
227 
228  // initialize background color gradients
229  float black[3] = { 0.0f, 0.0f, 0.0f };
230  SetGradientBackgroundColors(black, black);
231 
232  // initialize decoration color, rectangle and annotation text
233  float white[3] = { 1.0f, 1.0f, 1.0f };
234  m_DecorationColor = white;
235 
236  setFrameStyle(QFrame::Box | QFrame::Plain);
237  QColor hexColor(m_DecorationColor[0] * 255, m_DecorationColor[1] * 255, m_DecorationColor[2] * 255);
238  setStyleSheet("border: 2px solid " + hexColor.name(QColor::HexRgb));
239 
240  m_CornerAnnotation = vtkSmartPointer<vtkCornerAnnotation>::New();
241  m_CornerAnnotation->SetText(0, "Sagittal");
242  m_CornerAnnotation->SetMaximumFontSize(12);
243  m_CornerAnnotation->GetTextProperty()->SetColor(m_DecorationColor[0], m_DecorationColor[1], m_DecorationColor[2]);
244  if (0 == vtkRenderer->HasViewProp(m_CornerAnnotation))
245  {
246  vtkRenderer->AddViewProp(m_CornerAnnotation);
247  }
248 }
249 
250 void QmitkRenderWindowWidget::SetCrosshair(mitk::Point3D selectedPoint)
251 {
252  m_PointSet->SetPoint(1, selectedPoint, 0);
253  mitk::RenderingManager::GetInstance()->RequestUpdate(m_RenderWindow->GetRenderWindow());
254 }
void SetCornerAnnotationText(const std::string &cornerAnnotation)
virtual bool InitializeViews(const BaseGeometry *geometry, RequestType type=REQUEST_UPDATE_ALL, bool preserveRoughOrientationInWorldSpace=false)
void ActivateCrosshair(bool activate)
Data management class that handles &#39;was created by&#39; relations.
void SetDecorationColor(const mitk::Color &color)
QmitkRenderWindowWidget(QWidget *parent=nullptr, const QString &widgetName="", mitk::DataStorage *dataStorage=nullptr)
static BaseRenderer * GetInstance(vtkRenderWindow *renWin)
vtkRenderer * GetVtkRenderer() const
void ShowGradientBackground(bool enable)
TimeGeometry::ConstPointer ComputeBoundingGeometry3D(const SetOfObjects *input, const char *boolPropertyKey=nullptr, const BaseRenderer *renderer=nullptr, const char *boolPropertyKey2=nullptr) const
Compute the axis-parallel bounding geometry of the input objects.
static Pointer New()
mitk::SliceNavigationController * GetSliceNavigationController() const
void SetDataStorage(mitk::DataStorage *dataStorage)
virtual void Add(DataNode *node, const DataStorage::SetOfObjects *parents=nullptr)=0
Adds a DataNode containing a data object to its internal storage.
Controls the selection of the slice the associated BaseRenderer will display.
static Pointer New()
virtual SetOfObjects::ConstPointer GetAll() const =0
returns a set of all data objects that are stored in the data storage
std::string GetCornerAnnotationText() const
mitk::DataStorage::Pointer m_DataStorage
static Pointer New()
bool GetActivateMenuWidgetFlag()
static RenderingManager * GetInstance()
void SetDataStorage(mitk::DataStorage *storage)
Setter / Getter for internal DataStorage.
MITK implementation of the QVTKWidget.
virtual mitk::VtkPropRenderer * GetRenderer()
void ForceImmediateUpdate(vtkRenderWindow *renderWindow)
void SetGradientBackgroundColors(const mitk::Color &upper, const mitk::Color &lower)
itk::RGBPixel< float > Color
Color Standard RGB color typedef (float)
virtual void Remove(const DataNode *node)=0
Removes node from the DataStorage.
void RequestUpdate(vtkRenderWindow *renderWindow)
virtual mitk::SliceNavigationController * GetSliceNavigationController()
void MouseEvent(QMouseEvent *e)
void SetLayoutIndex(QmitkRenderWindowMenu::LayoutIndex layoutIndex)
static Pointer New()