diff --git a/Plugins/org.mitk.gui.qt.common/src/QmitkSelectionServiceConnector.cpp b/Plugins/org.mitk.gui.qt.common/src/QmitkSelectionServiceConnector.cpp index 2b3fee05a1..10a89c0be3 100644 --- a/Plugins/org.mitk.gui.qt.common/src/QmitkSelectionServiceConnector.cpp +++ b/Plugins/org.mitk.gui.qt.common/src/QmitkSelectionServiceConnector.cpp @@ -1,130 +1,130 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical Image Computing. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ // mitk gui qt common plugin #include "QmitkSelectionServiceConnector.h" #include "internal/QmitkDataNodeSelection.h" // qt widgets module #include "QmitkCustomVariants.h" #include "QmitkEnums.h" // blueberry ui qt plugin #include QmitkSelectionServiceConnector::QmitkSelectionServiceConnector() : m_SelectionService(nullptr) , m_SelectionProvider(nullptr) { m_DataNodeItemModel = std::make_shared(); m_DataNodeSelectionModel = std::make_shared(m_DataNodeItemModel.get()); } QmitkSelectionServiceConnector::~QmitkSelectionServiceConnector() { RemovePostSelectionListener(); RemoveAsSelectionProvider(); } void QmitkSelectionServiceConnector::AddPostSelectionListener(berry::ISelectionService* selectionService) { if (nullptr == selectionService) { return; } m_SelectionService = selectionService; - m_BerrySelectionListener.reset(new berry::NullSelectionChangedAdapter(this, &QmitkSelectionServiceConnector::ServiceSelectionChanged)); + m_BerrySelectionListener.reset(new berry::NullSelectionChangedAdapter(this, &QmitkSelectionServiceConnector::OnServiceSelectionChanged)); m_SelectionService->AddPostSelectionListener(m_BerrySelectionListener.get()); } void QmitkSelectionServiceConnector::RemovePostSelectionListener() { if (nullptr == m_SelectionService) { return; } m_SelectionService->RemovePostSelectionListener(m_BerrySelectionListener.get()); m_SelectionService = nullptr; } void QmitkSelectionServiceConnector::SetAsSelectionProvider(QmitkDataNodeSelectionProvider* selectionProvider) { m_SelectionProvider = selectionProvider; } void QmitkSelectionServiceConnector::RemoveAsSelectionProvider() { m_SelectionProvider = nullptr; } void QmitkSelectionServiceConnector::ChangeServiceSelection(QList nodes) { if (nullptr == m_SelectionProvider) { return; } m_SelectionProvider->SetItemSelectionModel(m_DataNodeSelectionModel.get()); if (nodes.empty()) { m_DataNodeSelectionModel->clearSelection(); m_DataNodeItemModel->clear(); } else { m_DataNodeItemModel->clear(); // fill the temporary helper data node item model with the nodes to select for (const auto& node : nodes) { m_DataNodeItemModel->AddDataNode(node); } m_DataNodeSelectionModel->select(QItemSelection(m_DataNodeItemModel->index(0, 0), m_DataNodeItemModel->index(nodes.size() - 1, 0)), QItemSelectionModel::ClearAndSelect); } } -void QmitkSelectionServiceConnector::ServiceSelectionChanged(const berry::IWorkbenchPart::Pointer& sourcePart, const berry::ISelection::ConstPointer& selection) +void QmitkSelectionServiceConnector::OnServiceSelectionChanged(const berry::IWorkbenchPart::Pointer& sourcePart, const berry::ISelection::ConstPointer& selection) { if (sourcePart.IsNull()) { return; } QList nodes; if (selection.IsNull()) { // propagate an empty list nodes = QList(); } // transform valid selection to DataNodeSelection, which allows to retrieve the selected nodes mitk::DataNodeSelection::ConstPointer dataNodeSelection = selection.Cast(); if (dataNodeSelection.IsNull()) { // propagate an empty list nodes = QList(); } else { nodes = QList::fromStdList(dataNodeSelection->GetSelectedDataNodes()); } // send new (possibly empty) list of selected nodes emit ServiceSelectionChanged(nodes); } diff --git a/Plugins/org.mitk.gui.qt.common/src/QmitkSelectionServiceConnector.h b/Plugins/org.mitk.gui.qt.common/src/QmitkSelectionServiceConnector.h index 6ad01065e0..21c1f8f7f1 100644 --- a/Plugins/org.mitk.gui.qt.common/src/QmitkSelectionServiceConnector.h +++ b/Plugins/org.mitk.gui.qt.common/src/QmitkSelectionServiceConnector.h @@ -1,133 +1,133 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical Image Computing. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef QMITKSELECTIONSERVICECONNECTOR_H #define QMITKSELECTIONSERVICECONNECTOR_H #include // qt widgets module #include // mitk gui qt common plugin #include "QmitkDataNodeSelectionProvider.h" #include "internal/QmitkDataNodeItemModel.h" // blueberry ui qt plugin #include /* * @brief The 'QmitkSelectionServiceConnector' is used to handle the selections of the global selection bus (selection service). * * The selection service connector can listen to a selection service. This should be done by using 'AddPostSelectionListener' * with the existing selection service of the surrounding 'QmitkAbstractView'. * The selection service connector can provide selections. This should be done by using 'SetAsSelectionProvider' * with the existing selection provider of the surrounding 'QmitkAbstractView'. * * The 'QmitkSelectionServiceConnector' offers a public slot and signal that can be used to propagate the selected * nodes from or to the global selection bus: * The 'ChangeServiceSelection'-slot transforms the given list of selected nodes into a QItemSelection of a temporary * data node selection model. This data node selection model is set as the item selection model of the member selection provider. * So by temporary adding a new data node selection model and changing its selection the selection provider sends a new selection * that can be received at any place in the workbench. * * The 'ServiceSelectionChanged'-signal sends a list of selected nodes to it's local environment (e.g. containing widget). * The 'ServiceSelectionChanged'-signal is emitted by the 'ServiceSelectionChanged'-function, which transforms the * berry selection of the selection into a data node list. The 'ServiceSelectionChanged'-function is called whenever * the selection service sends a selection changed event. * * In order to connect the 'QmitkSelectionServiceConnector' with a model-view pair, a 'QmitkModelViewSelectionConnector' needs to be used: * The 'QmitkModelViewSelectionConnector' offers a 'SetCurrentSelection'-slot that can be connected with the * 'ServiceSelectionChanged'-signal of this class. * The 'QmitkModelViewSelectionConnector' offers a 'CurrentSelectionChanged'-signal that can be connected with the * 'ChangeServiceSelection'-slot of this class. */ class MITK_QT_COMMON QmitkSelectionServiceConnector : public QObject { Q_OBJECT public: QmitkSelectionServiceConnector(); ~QmitkSelectionServiceConnector(); /* * @brief Create a selection listener and add it to the list of selection listener of the given selection service. * * The selection listener is connected to the 'ServiceSelectionChanged' member function, which is * called if a berry selection is changed in the workbench. */ void AddPostSelectionListener(berry::ISelectionService* selectionService); /* * @brief Remove a selection listener from the list of selection listener of the selection service member. */ void RemovePostSelectionListener(); /* * @brief Store the given selection provider as a private member. * In order to use the public slot 'ChangeServiceSelection'-function, the selection provider member had to be * previously set. */ void SetAsSelectionProvider(QmitkDataNodeSelectionProvider* selectionProvider); /* * @brief Set the selection provider member to a nullptr. This will prevent the public slot * 'ChangeServiceSelection'-function from working. */ void RemoveAsSelectionProvider(); Q_SIGNALS: /* * @brief A signal that will be emitted by the private 'ServiceSelectionChanged'-function. This happens if a selection is changed * via the selection service. * * @par nodes A list of data nodes that are newly selected. */ void ServiceSelectionChanged(QList nodes); public Q_SLOTS: /* * @brief Send new selections to the selection service via the private selection provider member. * * This slot-function is called whenever a local selection is changed in the surrounding widget and a selection provider was set. * The newly selected data nodes are added temporary to a 'QmitkDataNodeItemModel', which is then used to define * the indices to select. * The 'QItemSelectionModel' is set as the item selection model of the selection provider member and its items are * selected by the indices previously defined by the 'QmitkDataNodeItemModel'. */ void ChangeServiceSelection(QList nodes); private: std::unique_ptr m_BerrySelectionListener; berry::ISelectionService* m_SelectionService; QmitkDataNodeSelectionProvider* m_SelectionProvider; std::shared_ptr m_DataNodeItemModel; std::shared_ptr m_DataNodeSelectionModel; /* * @brief Handle a selection received from the selection service. * * This function is called whenever a berry selection of the selection service is changed in the workbench. * The new selection is transformed into a data node selection and the contained data nodes are propagated * as the new current selection of the item view member. * * @par sourcePart The workbench part containing the selection. * @par selection The current selection. */ - void ServiceSelectionChanged(const berry::IWorkbenchPart::Pointer& sourcePart, const berry::ISelection::ConstPointer& selection); + void OnServiceSelectionChanged(const berry::IWorkbenchPart::Pointer& sourcePart, const berry::ISelection::ConstPointer& selection); }; #endif // QMITKSELECTIONSERVICECONNECTOR_H