Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkUGCombinedRepresentationPropertyWidget.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 
17 
18 #include <mitkProperties.h>
19 #include <mitkPropertyObserver.h>
20 
21 class _UGCombinedBoolPropEditor : public mitk::PropertyEditor
22 {
23 public:
24  _UGCombinedBoolPropEditor(mitk::BoolProperty *boolProp, QmitkUGCombinedRepresentationPropertyWidget *combo)
25  : PropertyEditor(boolProp), m_BoolProperty(boolProp), m_ComboBox(combo)
26  {
28  }
29 
30  ~_UGCombinedBoolPropEditor() override {}
31  bool IsEnabled() const { return enabled; }
32  void SetEnabled(bool enable)
33  {
34  this->BeginModifyProperty();
35  m_BoolProperty->SetValue(enable);
36  this->EndModifyProperty();
37  }
38 
39 protected:
40  void PropertyChanged() override
41  {
42  if (m_BoolProperty)
43  enabled = m_BoolProperty->GetValue();
44  else
45  enabled = false;
46 
47  m_ComboBox->IsVolumeChanged(enabled);
48  }
49 
50  void PropertyRemoved() override
51  {
52  m_Property = nullptr;
53  m_BoolProperty = nullptr;
54  enabled = false;
55  }
56 
57  mitk::BoolProperty *m_BoolProperty;
59  bool enabled;
60 };
61 
62 class _UGCombinedEnumPropEditor : public mitk::PropertyEditor
63 {
64 public:
65  _UGCombinedEnumPropEditor(mitk::EnumerationProperty *property,
67  bool isVolumeProp)
68  : PropertyEditor(property), m_EnumerationProperty(property), m_ComboBox(combo), m_IsVolumeProp(isVolumeProp)
69  {
70  }
71 
72  ~_UGCombinedEnumPropEditor() override { m_EnumerationProperty = nullptr; }
73  void IndexChanged(int enumId)
74  {
75  this->BeginModifyProperty();
76  m_EnumerationProperty->SetValue(enumId);
77  this->EndModifyProperty();
78  }
79 
80  void PropertyChanged() override
81  {
82  if (m_EnumerationProperty)
83  {
84  if (m_IsVolumeProp)
85  {
86  m_ComboBox->SetGridVolumeId(m_EnumerationProperty->GetValueAsId());
87  }
88  else
89  {
90  m_ComboBox->SetGridRepresentationId(m_EnumerationProperty->GetValueAsId());
91  }
92  }
93  }
94 
95  void PropertyRemoved() override
96  {
97  m_Property = nullptr;
98  m_EnumerationProperty = nullptr;
99  }
100 
101 protected:
102  mitk::EnumerationProperty *m_EnumerationProperty;
104  QHash<int, int> m_EnumIdToItemIndex;
105  bool m_IsVolumeProp;
106 };
107 
109  : QComboBox(parent),
110  gridRepPropEditor(nullptr),
111  volumeMapperPropEditor(nullptr),
112  volumePropEditor(nullptr),
113  m_GridRepIndex(0),
114  m_GridVolIndex(0),
115  m_FirstVolumeRepId(0)
116 {
117  connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(OnIndexChanged(int)));
118 }
119 
121 {
122  delete gridRepPropEditor;
123  delete volumeMapperPropEditor;
124  delete volumePropEditor;
125 }
126 
128  mitk::GridVolumeMapperProperty *gridVolProp,
129  mitk::BoolProperty *volumeProp)
130 {
131  if (gridRepPropEditor)
132  {
133  delete gridRepPropEditor;
134  gridRepPropEditor = nullptr;
135  }
136 
138  {
139  delete volumeMapperPropEditor;
140  volumeMapperPropEditor = nullptr;
141  }
142 
143  if (volumePropEditor)
144  {
145  delete volumePropEditor;
146  volumePropEditor = nullptr;
147  }
148 
149  this->clear();
150  this->setEnabled(true);
151  m_FirstVolumeRepId = 0;
152  m_MapRepEnumToIndex.clear();
153  m_MapVolEnumToIndex.clear();
154 
155  if (!gridRepProp && !gridVolProp)
156  return;
157 
158  int i = 0;
159  if (gridRepProp)
160  {
161  const mitk::EnumerationProperty::EnumStringsContainerType &repStrings = gridRepProp->GetEnumStrings();
162 
163  for (auto it = repStrings.begin(); it != repStrings.end(); ++it, ++i)
164  {
165  m_MapRepEnumToIndex.insert(it->second, i);
166  this->addItem(QString::fromStdString(it->first), it->second);
167  }
168  m_FirstVolumeRepId = i;
169  }
170  if (gridVolProp)
171  {
172  const mitk::EnumerationProperty::EnumStringsContainerType &volStrings = gridVolProp->GetEnumStrings();
173 
174  for (auto it = volStrings.begin(); it != volStrings.end(); ++it, ++i)
175  {
176  m_MapVolEnumToIndex.insert(it->second, i);
177  this->addItem(QString("Volume (") + QString::fromStdString(it->first) + ")", it->second);
178  }
179  }
180 
181  if (gridRepProp)
182  {
183  gridRepPropEditor = new _UGCombinedEnumPropEditor(gridRepProp, this, false);
184  }
185  if (gridVolProp)
186  {
187  volumeMapperPropEditor = new _UGCombinedEnumPropEditor(gridVolProp, this, true);
188  }
189  if (volumeProp)
190  {
191  volumePropEditor = new _UGCombinedBoolPropEditor(volumeProp, this);
192  }
193 
194  if (gridRepProp)
195  {
196  this->SetGridRepresentationId(gridRepProp->GetValueAsId());
197  }
198  if (gridVolProp)
199  {
200  this->SetGridVolumeId(gridVolProp->GetValueAsId());
201  }
202 }
203 
205 {
206  int enumIndex = this->itemData(index, Qt::UserRole).toInt();
207  if (index < m_FirstVolumeRepId && gridRepPropEditor)
208  {
209  gridRepPropEditor->IndexChanged(enumIndex);
210  if (volumePropEditor)
211  {
212  volumePropEditor->SetEnabled(false);
213  }
214  }
215  else if (volumeMapperPropEditor)
216  {
217  volumeMapperPropEditor->IndexChanged(enumIndex);
218  if (volumePropEditor)
219  {
220  volumePropEditor->SetEnabled(true);
221  }
222  }
223 }
224 
226 {
227  m_GridRepIndex = enumId;
228  if (volumePropEditor && volumePropEditor->IsEnabled())
229  {
230  return;
231  }
232  else
233  {
234  this->setCurrentIndex(m_MapRepEnumToIndex[enumId]);
235  }
236 }
237 
239 {
240  m_GridVolIndex = enumId;
241  if (volumePropEditor && volumePropEditor->IsEnabled())
242  {
243  this->setCurrentIndex(m_MapVolEnumToIndex[enumId]);
244  }
245  else
246  {
247  return;
248  }
249 }
250 
252 {
253  if (volume)
254  {
256  }
257  else
258  {
260  }
261 }
virtual IdType GetValueAsId() const
virtual void PropertyRemoved()=0
const EnumStringsContainerType & GetEnumStrings() const
mitk::BaseProperty * m_Property
PropertyEditor(mitk::BaseProperty *)
virtual void PropertyChanged()=0
void SetProperty(mitk::GridRepresentationProperty *gridRepresentation, mitk::GridVolumeMapperProperty *volumeMapper, mitk::BoolProperty *volumeProp)
std::map< std::string, IdType > EnumStringsContainerType