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