Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkSingleNodeSelectionWidget.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 
13 
15 
16 #include <berryQtStyleManager.h>
17 
19 #include "mitkNodePredicateAnd.h"
20 
21 #include <QMouseEvent>
22 
24 #include "QmitkNodeDetailsDialog.h"
25 
27 {
28  m_Controls.setupUi(this);
29 
30  m_Controls.btnSelect->installEventFilter(this);
31  m_Controls.btnSelect->setVisible(true);
32  m_Controls.btnClear->setVisible(false);
33 
34  m_Controls.btnClear->setIcon(berry::QtStyleManager::ThemeIcon(QStringLiteral(":/org.mitk.gui.qt.common/times.svg")));
35 
36  this->UpdateInfo();
37 
38  connect(m_Controls.btnClear, SIGNAL(clicked(bool)), this, SLOT(OnClearSelection()));
39 }
40 
41 void QmitkSingleNodeSelectionWidget::ReviseSelectionChanged(const NodeList& oldInternalSelection, NodeList& newInternalSelection)
42 {
43  if (newInternalSelection.empty())
44  {
46  {
47  auto autoSelectedNode = this->DetermineAutoSelectNode(oldInternalSelection);
48 
49  if (autoSelectedNode.IsNotNull())
50  {
51  newInternalSelection.append(autoSelectedNode);
52  }
53  }
54  }
55  else if (newInternalSelection.size()>1)
56  {
57  //this widget only allows one internal selected node.
58  newInternalSelection = { newInternalSelection.front() };
59  }
60 }
61 
63 {
64  if (m_IsOptional)
65  {
66  this->SetCurrentSelection({});
67  }
68 
69  this->UpdateInfo();
70 }
71 
73 {
75 
76  auto selection = GetCurrentInternalSelection();
77  if (!selection.empty())
78  {
79  result = selection.front();
80  }
81  return result;
82 }
83 
84 bool QmitkSingleNodeSelectionWidget::eventFilter(QObject *obj, QEvent *ev)
85 {
86  if (obj == m_Controls.btnSelect)
87  {
88  if (ev->type() == QEvent::MouseButtonRelease)
89  {
90  auto mouseEv = dynamic_cast<QMouseEvent*>(ev);
91  if (!mouseEv)
92  {
93  return false;
94  }
95 
96  if (mouseEv->button() == Qt::LeftButton)
97  {
98  if (this->isEnabled())
99  {
100  this->EditSelection();
101  return true;
102  }
103  }
104  else
105  {
106  auto selection = this->CompileEmitSelection();
107  if (!selection.empty())
108  {
109  QmitkNodeDetailsDialog infoDialog(selection, this);
110  infoDialog.exec();
111  return true;
112  }
113  }
114  }
115  }
116 
117  return false;
118 }
119 
121 {
123 
124  dialog->SetDataStorage(m_DataStorage.Lock());
128  dialog->SetSelectionMode(QAbstractItemView::SingleSelection);
129 
130  m_Controls.btnSelect->setChecked(true);
131 
132  if (dialog->exec())
133  {
135  }
136 
137  m_Controls.btnSelect->setChecked(false);
138 
139  delete dialog;
140 }
141 
143 {
144  if (this->GetSelectedNode().IsNull())
145  {
146  if (m_IsOptional)
147  {
148  m_Controls.btnSelect->SetNodeInfo(m_EmptyInfo);
149  }
150  else
151  {
152  m_Controls.btnSelect->SetNodeInfo(m_InvalidInfo);
153  }
154  m_Controls.btnSelect->SetSelectionIsOptional(m_IsOptional);
155  m_Controls.btnClear->setVisible(false);
156  }
157  else
158  {
159  m_Controls.btnClear->setVisible(m_IsOptional);
160  }
161 
162  m_Controls.btnSelect->SetSelectedNode(this->GetSelectedNode());
163 }
164 
166 {
168  auto storage = m_DataStorage.Lock();
169  if (storage.IsNotNull())
170  {
171  auto ignoreCheck = [ignoreNodes](const mitk::DataNode * node)
172  {
173  bool result = true;
174  for (const auto& ignoreNode : ignoreNodes)
175  {
176  if (node == ignoreNode)
177  {
178  result = false;
179  break;
180  }
181  }
182  return result;
183  };
184 
186  mitk::NodePredicateBase::Pointer predicate = isNotIgnoredNode.GetPointer();
187 
188  if (m_NodePredicate.IsNotNull())
189  {
190  predicate = mitk::NodePredicateAnd::New(m_NodePredicate.GetPointer(), predicate.GetPointer()).GetPointer();
191  }
192 
193  result = storage->GetNode(predicate);
194  }
195  return result;
196 }
197 
199 {
200  NodeList selection;
201  if (selectedNode)
202  {
203  selection.append(selectedNode);
204  }
205  this->SetCurrentSelection(selection);
206 };
207 
209 {
210  if (this->GetSelectedNode().IsNull() && m_AutoSelectNewNodes)
211  {
212  auto autoNode = this->DetermineAutoSelectNode();
213 
214  if (autoNode.IsNotNull())
215  {
216  this->HandleChangeOfInternalSelection({ autoNode });
217  }
218  }
219 }
220 
222 {
223  return m_AutoSelectNewNodes;
224 }
225 
227 {
228  m_AutoSelectNewNodes = autoSelect;
229  this->OnNodeAddedToStorage(nullptr);
230 }
void OnNodeAddedToStorage(const mitk::DataNode *node) override
itk::SmartPointer< T > Lock() const
static Pointer New()
Ui_QmitkSingleNodeSelectionWidget m_Controls
void SetCurrentSelectedNode(mitk::DataNode *selectedNode)
static QIcon ThemeIcon(const QByteArray &originalSVG)
QList< mitk::DataNode::Pointer > NodeList
void SetSelectOnlyVisibleNodes(bool selectOnlyVisibleNodes)
static Pointer New(const FunctionType &_arg)
bool eventFilter(QObject *obj, QEvent *ev) override
void ReviseSelectionChanged(const NodeList &oldInternalSelection, NodeList &newInternalSelection) override
mitk::WeakPointer< mitk::DataStorage > m_DataStorage
void SetSelectionMode(SelectionMode mode)
mitk::DataNode::Pointer DetermineAutoSelectNode(const NodeList &ignoreNodes={})
Widget that allows to show and edit the content of an mitk::IsoDoseLevel instance.
void SetDataStorage(mitk::DataStorage *dataStorage)
Sets the data storage that will be used /monitored by widget.
mitk::DataNode::Pointer GetSelectedNode() const
mitk::NodePredicateBase::ConstPointer m_NodePredicate
QmitkSingleNodeSelectionWidget(QWidget *parent=nullptr)
virtual void SetNodePredicate(const mitk::NodePredicateBase *nodePredicate)
Sets the node predicate and updates the widget, according to the node predicate.
void SetCurrentSelection(NodeList selectedNodes)
Abstract base class for the selection of data from a data storage.
Class for nodes of the DataTree.
Definition: mitkDataNode.h:57
void HandleChangeOfInternalSelection(NodeList newInternalSelection)