Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
QmitkDataStorageComboBoxWithSelectNone.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) University College London (UCL).
6 All rights reserved.
7 
8 This software is distributed WITHOUT ANY WARRANTY; without
9 even the implied warranty of MERCHANTABILITY or FITNESS FOR
10 A PARTICULAR PURPOSE.
11 
12 See LICENSE.txt or http://www.mitk.org for details.
13 
14 ===================================================================*/
15 
17 #include <QDebug>
18 
19 const QString QmitkDataStorageComboBoxWithSelectNone::ZERO_ENTRY_STRING = "please select";
20 
21 //-----------------------------------------------------------------------------
23  QWidget* parent,
24  bool autoSelectNewNodes )
25 : QmitkDataStorageComboBox(parent, autoSelectNewNodes)
26 , m_CurrentPath("")
27 {
28 }
29 
30 
31 //-----------------------------------------------------------------------------
33  mitk::DataStorage* dataStorage,
34  const mitk::NodePredicateBase* predicate,
35  QWidget* parent, bool autoSelectNewNodes )
36 : QmitkDataStorageComboBox(dataStorage, predicate, parent, autoSelectNewNodes)
37 {
38 }
39 
40 
41 //-----------------------------------------------------------------------------
43 {
44 }
45 
46 
47 //-----------------------------------------------------------------------------
49 {
50  int index = QmitkDataStorageComboBox::Find(dataNode);
51  if (index != -1)
52  {
53  index += 1;
54  }
55  return index;
56 }
57 
58 
59 //-----------------------------------------------------------------------------
61 {
62  mitk::DataNode::Pointer result = nullptr;
63 
64  if (this->HasIndex(index))
65  {
66  if (index != 0)
67  {
68  result = m_Nodes.at(index - 1);
69  }
70  }
71  return result;
72 }
73 
74 
75 //-----------------------------------------------------------------------------
77 {
78  return this->GetNode(this->currentIndex());
79 }
80 
81 
82 //-----------------------------------------------------------------------------
84 {
85  int currentIndex = -1;
86  for (int i = 0; i < static_cast<int>(m_Nodes.size()); i++)
87  {
88  if (m_Nodes[i] == node.GetPointer())
89  {
90  currentIndex = i;
91  break;
92  }
93  }
94  if (currentIndex == -1)
95  {
96  // didn't find it, so set the value to 0.
97  currentIndex = 0;
98  }
99  else
100  {
101  currentIndex += 1; // because the combo box contains "please select" at position zero.
102  }
103  this->setCurrentIndex(currentIndex);
104 }
105 
106 //-----------------------------------------------------------------------------
108 {
109  if(index > 0 && this->HasIndex(index))
110  {
111 
112  // remove itk::Event observer
113  mitk::DataNode* dataNode = m_Nodes.at(index - 1);
114 
115  // get name property first
116  mitk::BaseProperty* nameProperty = dataNode->GetProperty("name");
117 
118  // if prop exists remove modified listener
119  if(nameProperty)
120  {
121  nameProperty->RemoveObserver(m_NodesModifiedObserverTags[index-1]);
122 
123  // remove name property map
124  m_PropertyToNode.erase(dataNode);
125  }
126 
127  // then remove delete listener on the node itself
128  dataNode->RemoveObserver(m_NodesDeleteObserverTags[index-1]);
129 
130  // remove observer tags from lists
133 
134  // remove node name from combobox
135  this->removeItem(index);
136 
137  // remove node from node vector
138  m_Nodes.erase(m_Nodes.begin()+index-1);
139  }
140 }
141 
142 
143 //-----------------------------------------------------------------------------
145 {
146  if(index > 0 && this->HasIndex(index))
147  {
148  // if node identical, we only update the name in the QComboBoxItem
149  if( dataNode == this->m_Nodes.at(index-1 ) )
150  {
151  mitk::BaseProperty* nameProperty = dataNode->GetProperty("name");
152  std::string dataNodeNameStr = nameProperty->GetValueAsString();
153 
154  this->setItemText(index, QString::fromStdString( dataNodeNameStr) );
155  }
156  else
157  QmitkDataStorageComboBox::InsertNode(index - 1, dataNode);
158  }
159 }
160 
161 
162 //-----------------------------------------------------------------------------
164 {
165  return (m_Nodes.size() > 0 && index <= m_Nodes.size());
166 }
167 
168 
169 //-----------------------------------------------------------------------------
171 {
172  if (index != 0)
173  {
174  QmitkDataStorageComboBox::InsertNode(index - 1, dataNode);
175  }
176 }
177 
178 
179 //-----------------------------------------------------------------------------
181 {
183  this->insertItem(0, ZERO_ENTRY_STRING);
184 }
185 
186 
187 //-----------------------------------------------------------------------------
189 {
190  this->setItemText(0, zeroEntryString);
191 }
192 
193 
194 //-----------------------------------------------------------------------------
196 {
197  return m_CurrentPath;
198 }
199 
200 
201 //-----------------------------------------------------------------------------
203 {
204  m_CurrentPath = path;
205 }
virtual void SetSelectedNode(const mitk::DataNode::Pointer &node)
Sets the combo box to the index that contains the specified node, or 0 if the node cannot be found...
virtual void SetNode(int index, const mitk::DataNode *dataNode) override
Set a DataNode in the ComboBox at the specified index (if the index exists). Internally the method ju...
std::map< mitk::DataNode *, const mitk::BaseProperty * > m_PropertyToNode
Maps a a specific node to (Name-)property. This is needed because we have to find the assiociated nod...
Data management class that handles 'was created by' relations.
virtual mitk::DataNode::Pointer GetSelectedNode() const override
Returns the selected DataNode or NULL if there is none, or the current index is zero.
std::vector< long > m_NodesModifiedObserverTags
Holds the tags of the node-modified observers. (must be updated everytime m_Nodes changes) ...
Displays all or a subset (defined by a predicate) of nodes of the Data Storage.
std::vector< mitk::DataNode * > m_Nodes
bool HasIndex(unsigned int index) const
Checks if the given index is within range.
virtual void InsertNode(int index, const mitk::DataNode *_DataNode)
Inserts a new node at the given index. If the index does not exist, the _DataNode is simply appended ...
static const QString ZERO_ENTRY_STRING
Stores the string that will be present on index 0, currently equal to "please select".
mitk::BaseProperty * GetProperty(const char *propertyKey, const mitk::BaseRenderer *renderer=nullptr) const
Get the property (instance of BaseProperty) with key propertyKey from the PropertyList of the rendere...
virtual int Find(const mitk::DataNode *_DataNode) const
Seaches for a given node and returns a valid index or -1 if the node was not found.
virtual QString currentValue() const
Get the current file path.
virtual void RemoveNode(int index) override
Removes a node from the ComboBox at a specified index (if the index exists). Gets called when a DataS...
Abstract base class for properties.
virtual std::string GetValueAsString() const
virtual mitk::DataNode::Pointer GetNode(int index) const override
Retrieves the node at a given index, where if index is zero, will always return NULL.
virtual void setCurrentValue(const QString &path)
Set the current file path.
virtual void Reset() override
Reset function whenever datastorage or predicate changes.
std::vector< long > m_NodesDeleteObserverTags
Holds the tags of the node-modified observers. (must be updated everytime m_Nodes changes) ...
virtual void InsertNode(int index, const mitk::DataNode *dataNode) override
Inserts a new node at the given index, unless index is 0, which is silently ignored.
virtual void Reset()
Reset function whenever datastorage or predicate changes.
void SetZeroEntryText(const QString &zeroEntryString)
Set the string that will be present on index 0.
Interface for evaluation conditions used in the DataStorage class GetSubset() method.
QmitkDataStorageComboBoxWithSelectNone(QWidget *parent=nullptr, bool autoSelectNewNodes=false)
Calls base class constructor.
Class for nodes of the DataTree.
Definition: mitkDataNode.h:66
virtual int Find(const mitk::DataNode *dataNode) const override
Searches for a given node, returning the index if found.