Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkEnumerationPropertyWidget.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 
16 #include <mitkPropertyObserver.h>
17 
18 class _EnumPropEditorImpl : public mitk::PropertyEditor
19 {
20 public:
21  _EnumPropEditorImpl(mitk::EnumerationProperty *property, QComboBox *combo, const QHash<int, int> &enumIdToItemIndex)
22  : PropertyEditor(property),
23  m_EnumerationProperty(property),
24  m_ComboBox(combo),
25  m_EnumIdToItemIndex(enumIdToItemIndex)
26  {
27  }
28 
29  ~_EnumPropEditorImpl() override { m_EnumerationProperty = nullptr; }
30  void IndexChanged(int enumId)
31  {
32  this->BeginModifyProperty();
33  m_EnumerationProperty->SetValue(enumId);
34  this->EndModifyProperty();
35  }
36 
37  void PropertyChanged() override
38  {
39  if (m_EnumerationProperty)
40  {
41  m_ComboBox->setCurrentIndex(m_EnumIdToItemIndex[m_EnumerationProperty->GetValueAsId()]);
42  }
43  }
44 
45  void PropertyRemoved() override
46  {
47  m_Property = nullptr;
48  m_EnumerationProperty = nullptr;
49  m_ComboBox->setEnabled(false);
50  }
51 
52 protected:
53  mitk::EnumerationProperty *m_EnumerationProperty;
54  QComboBox *m_ComboBox;
55  QHash<int, int> m_EnumIdToItemIndex;
56 };
57 
58 QmitkEnumerationPropertyWidget::QmitkEnumerationPropertyWidget(QWidget *parent) : QComboBox(parent), propView(nullptr)
59 {
60  connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(OnIndexChanged(int)));
61 }
62 
64 {
65  delete propView;
66 }
67 
69 {
70  if (propView)
71  {
72  delete propView;
73  propView = nullptr;
74  }
75 
76  this->clear();
77 
78  if (!property)
79  {
80  return;
81  }
82 
83  this->setEnabled(true);
84 
85  QHash<int, int> enumIdToItemIndex;
86 
87  const mitk::EnumerationProperty::EnumStringsContainerType &strings = property->GetEnumStrings();
88  int index = 0;
89  for (auto it = strings.begin(); it != strings.end(); ++it, ++index)
90  {
91  enumIdToItemIndex.insert(it->second, index);
92  this->addItem(QString::fromStdString(it->first), it->second);
93  }
94 
95  propView = new _EnumPropEditorImpl(property, this, enumIdToItemIndex);
96  propView->PropertyChanged();
97 }
98 
100 {
101  if (propView)
102  {
103  int enumIndex = this->itemData(index, Qt::UserRole).toInt();
104  propView->IndexChanged(enumIndex);
105  }
106 }
virtual void PropertyRemoved()=0
void SetProperty(mitk::EnumerationProperty *property)
mitk::BaseProperty * m_Property
QmitkEnumerationPropertyWidget(QWidget *parent=nullptr)
PropertyEditor(mitk::BaseProperty *)
virtual void PropertyChanged()=0
std::map< std::string, IdType > EnumStringsContainerType