23 , m_SelectOnlyVisibleNodes(false)
30 if (
nullptr != m_View)
32 disconnect(m_View->selectionModel(), SIGNAL(selectionChanged(
const QItemSelection&,
const QItemSelection&)),
this, SLOT(ChangeModelSelection(
const QItemSelection&,
const QItemSelection&)));
39 mitkThrow() <<
"Invalid item view. To use the model-view selection connector please specify a valid 'QAbstractItemView'.";
44 if (storageModel ==
nullptr)
46 mitkThrow() <<
"Invalid data model. To use the model-view selection connector please set a valid 'QmitkAbstractDataStorageModel' for the given item view.";
51 m_Model = storageModel;
52 connect(m_View->selectionModel(), SIGNAL(selectionChanged(
const QItemSelection&,
const QItemSelection&)), SLOT(ChangeModelSelection(
const QItemSelection&,
const QItemSelection&)));
57 m_SelectOnlyVisibleNodes = selectOnlyVisibleNodes;
62 if (
nullptr == m_Model ||
nullptr == m_View)
68 QList<mitk::DataNode::Pointer> filteredNodes = FilterNodeList(selectedNodes);
76 if (!m_SelectOnlyVisibleNodes)
79 m_NonVisibleSelection = selectedNodes;
84 m_NonVisibleSelection.erase(std::remove_if(m_NonVisibleSelection.begin(), m_NonVisibleSelection.end(), lambda), m_NonVisibleSelection.end());
88 QItemSelection newCurrentSelection;
89 for (
const auto& node : filteredNodes)
91 QModelIndexList matched = m_Model->match(m_Model->index(0, 0),
QmitkDataNodeRole, QVariant::fromValue<mitk::DataNode::Pointer>(node), 1, Qt::MatchRecursive);
94 newCurrentSelection.select(matched.front(), matched.front());
98 m_View->selectionModel()->select(newCurrentSelection, QItemSelectionModel::ClearAndSelect);
101 void QmitkModelViewSelectionConnector::ChangeModelSelection(
const QItemSelection& ,
const QItemSelection& )
108 return m_SelectOnlyVisibleNodes;
113 auto nodes = GetInternalSelectedNodes();
115 if (!m_SelectOnlyVisibleNodes)
118 nodes.append(m_NonVisibleSelection);
124 QList<mitk::DataNode::Pointer> QmitkModelViewSelectionConnector::GetInternalSelectedNodes()
const 126 if (
nullptr == m_Model ||
nullptr == m_View)
128 return QList<mitk::DataNode::Pointer>();
131 QList<mitk::DataNode::Pointer> nodes;
132 QModelIndexList selectedIndexes = m_View->selectionModel()->selectedIndexes();
133 for (
const auto& index : selectedIndexes)
144 QList<mitk::DataNode::Pointer> QmitkModelViewSelectionConnector::FilterNodeList(
const QList<mitk::DataNode::Pointer>& nodes)
const 148 return QList<mitk::DataNode::Pointer>();
151 if (
nullptr == m_Model)
157 if (
nullptr == nodePredicate)
163 QList<mitk::DataNode::Pointer> result;
164 for (
const auto& node : nodes)
166 if (
true == nodePredicate->CheckNode(node))
168 result.push_back(node);
177 if (selection1.size() == selection2.size())
181 return std::is_permutation(selection1.begin(), selection1.end(), selection2.begin(), selection2.end(), lambda);
void SetView(QAbstractItemView *view)
Set the view whose selection model is used to propagate or receive selection changes. Use the view's data model to transform selected nodes into model indexes and vice versa.
QList< mitk::DataNode::Pointer > GetSelectedNodes() const
Retrieve the currently selected nodes (equals the last CurrentSelectionChanged values).
bool GetSelectOnlyVisibleNodes() const
void SetSelectOnlyVisibleNodes(bool selectOnlyVisibleNodes)
Change the selection mode of the item view's selection model.
QmitkModelViewSelectionConnector()
void CurrentSelectionChanged(QList< mitk::DataNode::Pointer > nodes)
A signal that will be emitted by the 'ChangeModelSelection'-function. This happens if the selection m...
const mitk::NodePredicateBase * GetNodePredicate() const
void SetCurrentSelection(QList< mitk::DataNode::Pointer > selectedNodes)
Transform a list of data nodes into a model selection and set this as a new selection of the selectio...
bool MITKQTWIDGETS_EXPORT EqualNodeSelections(const QList< mitk::DataNode::Pointer > &selection1, const QList< mitk::DataNode::Pointer > &selection2)