Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkDataNodeOpacityAction.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 // mitk core
16 #include <mitkRenderingManager.h>
17 
18 // qt
19 #include <QHBoxLayout>
20 #include <QLabel>
21 
23  : QWidgetAction(parent)
24  , QmitkAbstractDataNodeAction(workbenchPartSite)
25 {
27 }
28 
30  : QWidgetAction(parent)
31  , QmitkAbstractDataNodeAction(berry::IWorkbenchPartSite::Pointer(workbenchPartSite))
32 {
34 }
35 
37 {
38  m_OpacitySlider = new QSlider;
39  m_OpacitySlider->setMinimum(0);
40  m_OpacitySlider->setMaximum(100);
41  m_OpacitySlider->setOrientation(Qt::Horizontal);
42  connect(m_OpacitySlider, &QSlider::valueChanged, this, &QmitkDataNodeOpacityAction::OnOpacityChanged);
43 
44  QLabel* opacityLabel = new QLabel(tr("Opacity: "));
45  QHBoxLayout* opacityWidgetLayout = new QHBoxLayout;
46  opacityWidgetLayout->setContentsMargins(4, 4, 4, 4);
47  opacityWidgetLayout->addWidget(opacityLabel);
48  opacityWidgetLayout->addWidget(m_OpacitySlider);
49 
50  QWidget* opacityWidget = new QWidget;
51  opacityWidget->setLayout(opacityWidgetLayout);
52 
53  setDefaultWidget(opacityWidget);
54 
55  connect(this, &QAction::changed, this, &QmitkDataNodeOpacityAction::OnActionChanged);
56 }
57 
59 {
60  if (nullptr == dataNode)
61  {
62  m_OpacitySlider->setValue(static_cast<int>(0));
63  return;
64  }
65 
67 
68  float opacity = 0.0;
69  if (dataNode->GetFloatProperty("opacity", opacity, baseRenderer))
70  {
71  m_OpacitySlider->setValue(static_cast<int>(opacity * 100));
72  }
73 }
74 
75 void QmitkDataNodeOpacityAction::OnOpacityChanged(int value)
76 {
77  auto dataNode = GetSelectedNode();
78  if (dataNode.IsNull())
79  {
80  return;
81  }
82 
84 
85  float opacity = static_cast<float>(value) / 100.0f;
86  dataNode->SetFloatProperty("opacity", opacity, baseRenderer);
87 
88  if (nullptr == baseRenderer)
89  {
91  }
92  else
93  {
94  mitk::RenderingManager::GetInstance()->RequestUpdate(baseRenderer->GetRenderWindow());
95  }
96 }
97 
98 void QmitkDataNodeOpacityAction::OnActionChanged()
99 {
100  auto dataNode = GetSelectedNode();
101  if (dataNode.IsNull())
102  {
103  return;
104  }
105 
106  InitializeWithDataNode(dataNode);
107 }
mitk::BaseRenderer::Pointer GetBaseRenderer()
Grants access to the base renderer stored for the action. Will return nullptr if renderer was never s...
QmitkDataNodeOpacityAction(QWidget *parent, berry::IWorkbenchPartSite::Pointer workbenchPartSite)
bool GetFloatProperty(const char *propertyKey, float &floatValue, const mitk::BaseRenderer *renderer=nullptr) const
Convenience access method for float properties (instances of FloatProperty)
void InitializeWithDataNode(const mitk::DataNode *dataNode) override
static RenderingManager * GetInstance()
mitk::DataNode::Pointer GetSelectedNode() const
void RequestUpdate(vtkRenderWindow *renderWindow)
void RequestUpdateAll(RequestType type=REQUEST_UPDATE_ALL)
Class for nodes of the DataTree.
Definition: mitkDataNode.h:57