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