Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
QmitkToolWorkingDataSelectionBox.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 
19 
21  : QListWidget(parent),
22  m_SelfCall(false),
23  m_LastSelectedReferenceData(NULL),
24  m_ToolGroupsForFiltering("default"),
25  m_DisplayOnlyDerivedNodes(true)
26 {
27  m_ToolManager = mitk::ToolManagerProvider::GetInstance()->GetToolManager(); // this widget should be placeable from
28  // designer so it can't take other than
29  // the defaul parameters
30 
31  QListWidget::setSelectionMode(QListWidget::MultiSelection);
32  QListWidget::setDragDropMode(QListWidget::InternalMove);
33 
34  connect(this, SIGNAL(itemSelectionChanged()), this, SLOT(OnWorkingDataSelectionChanged()));
35 
40 }
41 
43 {
44 }
45 
47 {
48  return m_ToolManager->GetDataStorage();
49 }
50 
52 {
53  m_ToolManager->SetDataStorage(storage);
54 }
55 
57 {
58  return m_ToolManager;
59 }
60 
62  mitk::ToolManager &newManager) // no NULL pointer allowed here, a manager is required
63 {
68 
69  m_ToolManager = &newManager;
70 
71  m_ToolManager->ReferenceDataChanged += mitk::MessageDelegate<QmitkToolWorkingDataSelectionBox>(
73  m_ToolManager->WorkingDataChanged += mitk::MessageDelegate<QmitkToolWorkingDataSelectionBox>(
75 
77 }
78 
80 {
81  static mitk::ToolManager::DataVectorType previouslySelectedNodes;
82 
84 
85  previouslySelectedNodes = selection;
86 
87  if (selection.size() > 0)
88  {
89  const mitk::DataNode *node = selection[0];
90  emit WorkingNodeSelected(node);
91  }
92  else
93  {
94  emit WorkingNodeSelected(NULL);
95  }
96 
97  m_SelfCall = true;
98  m_ToolManager->SetWorkingData(selection); // maybe empty
99  m_SelfCall = false;
100 }
101 
103 {
104  if (m_SelfCall)
105  return;
106 
107  const mitk::DataNode *node = m_ToolManager->GetWorkingData(0);
108  emit WorkingNodeSelected(node);
109 
111 }
112 
114 {
115  if (m_ToolManager->GetReferenceData(0) != m_LastSelectedReferenceData)
116  {
117  m_ToolManager->SetWorkingData(NULL);
119 
120  m_LastSelectedReferenceData = m_ToolManager->GetReferenceData(0);
121  }
122 }
123 
125 {
126  // clear all
127  QListWidget::clear();
128  m_Node.clear();
129 
130  // rebuild contents
132  for (mitk::ToolManager::DataVectorType::const_iterator objectIter = allObjects.begin();
133  objectIter != allObjects.end();
134  ++objectIter)
135  {
136  mitk::DataNode *node = *objectIter;
137 
138  if (node) // delete this check when datastorage is really used
139  {
140  // get name and color
141  std::string name = node->GetName();
142  float rgb[3];
143  rgb[0] = 1.0;
144  rgb[1] = 0.0;
145  rgb[2] = 0.0;
146  node->GetColor(rgb);
147  QRgb qrgb = qRgb((int)(rgb[0] * 255.0), (int)(rgb[1] * 255.0), (int)(rgb[2] * 255.0));
148 
149  QPixmap pixmap(25, 18);
150  pixmap.fill(QColor(qrgb));
151 
152  // create a list item
153  QListWidgetItem *newItem = new QListWidgetItem();
154  QString qname = QString::fromLocal8Bit(name.c_str());
155 
156  // set name and color
157  newItem->setText(qname);
158  newItem->setIcon(QIcon(pixmap));
159 
160  this->addItem(newItem);
161 
162  m_Node.insert(std::make_pair(newItem, node));
163  }
164  }
165 }
166 
168 {
170 
171  QList<QListWidgetItem *> items;
172  for (int j = 0; j < this->count(); j++)
173  {
174  if (this->item(j)->isSelected())
175  items.append(this->item(j));
176  }
177 
178  for (int i = 0; i < items.size(); ++i)
179  {
180  QListWidgetItem *item = items.at(i);
181  if (item)
182  {
183  ItemNodeMapType::iterator it = m_Node.find(item);
184  if (it != m_Node.end())
185  {
186  mitk::DataNode::Pointer node = it->second;
187  if (node)
188  result.push_back(node);
189  }
190  }
191  }
192 
193  return result;
194 }
195 
197 {
198  QListWidgetItem *item = QListWidget::selectedItems().first();
199  if (item)
200  {
201  ItemNodeMapType::iterator iter = m_Node.find(item);
202  if (iter != m_Node.end())
203  {
204  return iter->second;
205  }
206  }
207 
208  return NULL;
209 }
210 
212 {
213  mitk::DataStorage *dataStorage = m_ToolManager->GetDataStorage();
214  if (!dataStorage)
215  {
217  }
218 
227  std::vector<mitk::NodePredicateBase::ConstPointer> m_Predicates;
228  mitk::NodePredicateBase::ConstPointer completePredicate = NULL;
229  bool rebuildNeeded = true;
230  if (rebuildNeeded)
231  {
232  m_Predicates.clear();
233 
234  const mitk::ToolManager::ToolVectorTypeConst allTools = m_ToolManager->GetTools();
235 
236  for (mitk::ToolManager::ToolVectorTypeConst::const_iterator iter = allTools.begin(); iter != allTools.end(); ++iter)
237  {
238  const mitk::Tool *tool = *iter;
239 
240  if ((m_ToolGroupsForFiltering.empty()) ||
241  (m_ToolGroupsForFiltering.find(tool->GetGroup()) != std::string::npos) ||
242  (m_ToolGroupsForFiltering.find(tool->GetName()) != std::string::npos))
243  {
244  if (completePredicate.IsNotNull())
245  {
246  m_Predicates.push_back(
247  mitk::NodePredicateOr::New(completePredicate, tool->GetWorkingDataPreference()).GetPointer());
248 
249  completePredicate = m_Predicates.back();
250  }
251  else
252  {
253  completePredicate = tool->GetWorkingDataPreference();
254  }
255  }
256  }
257  }
258 
259  // TODO delete all m_Predicates
261 
268  if (onlyDerivedFromOriginal)
269  {
270  mitk::DataNode *sourceNode(m_ToolManager->GetReferenceData(0));
271  if (sourceNode)
272  {
273  allObjects = dataStorage->GetDerivations(sourceNode, completePredicate, false);
274  }
275  else
276  {
278  }
279  }
280  else
281  {
282  if (completePredicate)
283  {
284  allObjects = dataStorage->GetSubset(completePredicate);
285  }
286  else
287  {
288  allObjects = dataStorage->GetAll();
289  }
290  }
291 
292  m_Predicates.clear();
293  completePredicate = NULL;
294 
296 
297  for (mitk::DataStorage::SetOfObjects::const_iterator objectIter = allObjects->begin();
298  objectIter != allObjects->end();
299  ++objectIter)
300  {
301  mitk::DataNode *node = (*objectIter).GetPointer();
302  resultVector.push_back(node);
303  }
304 
305  return resultVector;
306 }
virtual const char * GetName() const =0
Returns the name of this tool. Make it short!
std::vector< Tool::ConstPointer > ToolVectorTypeConst
Base class of all tools used by mitk::ToolManager.
Definition: mitkTool.h:92
Data management class that handles 'was created by' relations.
mitk::DataNode * GetSelectedNode()
Like GetSelectedNodes(), but will only return one object. Will only return what QListView gives as se...
virtual SetOfObjects::ConstPointer GetDerivations(const mitk::DataNode *node, const NodePredicateBase *condition=nullptr, bool onlyDirectDerivations=true) const =0
returns a set of derived objects for a given node.
void OnToolManagerReferenceDataModified()
Callback function, no need to call it. This is used to observe and react to changes in the mitk::Tool...
std::vector< DataNode * > DataVectorType
bool GetName(std::string &nodeName, const mitk::BaseRenderer *renderer=nullptr, const char *propertyKey="name") const
Convenience access method for accessing the name of an object (instance of StringProperty with proper...
Definition: mitkDataNode.h:366
static Pointer New()
virtual mitk::ToolManager * GetToolManager()
Returns ToolManager object.
virtual NodePredicateBase::ConstPointer GetWorkingDataPreference() const
Definition: mitkTool.cpp:193
itk::SmartPointer< const Self > ConstPointer
void SetToolManager(mitk::ToolManager &)
Tell this object to listen to another ToolManager.
void UpdateDataDisplay()
Can be called to trigger an update of the list contents.
virtual SetOfObjects::ConstPointer GetAll() const =0
returns a set of all data objects that are stored in the data storage
SetOfObjects::ConstPointer GetSubset(const NodePredicateBase *condition) const
returns a set of data objects that meet the given condition(s)
void WorkingNodeSelected(const mitk::DataNode *)
void OnToolManagerWorkingDataModified()
Callback function, no need to call it. This is used to observe and react to changes in the mitk::Tool...
mitk::ToolManager * GetToolManager()
Returns the associated mitk::ToolManager.
virtual const char * GetGroup() const
Name of a group.
Definition: mitkTool.cpp:106
void SetDataStorage(mitk::DataStorage &storage)
mitk::ToolManager::DataVectorType GetAllNodes(bool onlyDerivedFromOriginal=true)
A list of all displayed DataNode objects. This method might be convenient for program modules that wa...
bool GetColor(float rgb[3], const mitk::BaseRenderer *renderer=nullptr, const char *propertyKey="color") const
Convenience access method for color properties (instances of ColorProperty)
mitk::ToolManager::DataVectorType GetSelectedNodes()
A list of all selected DataNode objects. This method might be convenient for program modules that wan...
Class for nodes of the DataTree.
Definition: mitkDataNode.h:66
static mitk::ToolManagerProvider * GetInstance()
Returns an instance of ToolManagerProvider service.
Manages and coordinates instances of mitk::Tool.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.