Medical Imaging Interaction Toolkit  2022.04.99-01b95b17
Medical Imaging Interaction Toolkit
QmitkSelectionServiceConnector.h
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 #ifndef QMITKSELECTIONSERVICECONNECTOR_H
14 #define QMITKSELECTIONSERVICECONNECTOR_H
15 
17 
18 // mitk gui qt common plugin
20 #include "internal/QmitkDataNodeItemModel.h"
21 
22  // blueberry ui qt plugin
23 #include <berryISelectionService.h>
24 
25 /*
26 * @brief The 'QmitkSelectionServiceConnector' is used to handle the selections of the global selection bus (selection service).
27 *
28 * The selection service connector can listen to a selection service. This should be done by using 'AddPostSelectionListener'
29 * with the existing selection service of the surrounding 'QmitkAbstractView'.
30 * The selection service connector can provide selections. This should be done by using 'SetAsSelectionProvider'
31 * with the existing selection provider of the surrounding 'QmitkAbstractView'.
32 *
33 * The 'QmitkSelectionServiceConnector' offers a public slot and signal that can be used to propagate the selected
34 * nodes from or to the global selection bus:
35 * The 'ChangeServiceSelection'-slot transforms the given list of selected nodes into a QItemSelection of a temporary
36 * data node selection model. This data node selection model is set as the item selection model of the member selection provider.
37 * So by temporary adding a new data node selection model and changing its selection the selection provider sends a new selection
38 * that can be received at any place in the workbench.
39 *
40 * The 'ServiceSelectionChanged'-signal sends a list of selected nodes to it's local environment (e.g. containing widget).
41 * The 'ServiceSelectionChanged'-signal is emitted by the 'ServiceSelectionChanged'-function, which transforms the
42 * berry selection of the selection into a data node list. The 'ServiceSelectionChanged'-function is called whenever
43 * the selection service sends a selection changed event.
44 *
45 * In order to connect the 'QmitkSelectionServiceConnector' with a model-view pair, a 'QmitkModelViewSelectionConnector' needs to be used:
46 * The 'QmitkModelViewSelectionConnector' offers a 'SetCurrentSelection'-slot that can be connected with the
47 * 'ServiceSelectionChanged'-signal of this class.
48 * The 'QmitkModelViewSelectionConnector' offers a 'CurrentSelectionChanged'-signal that can be connected with the
49 * 'ChangeServiceSelection'-slot of this class.
50 */
52 {
53  Q_OBJECT
54 
55 public:
56 
59 
60  /*
61  * @brief Create a selection listener and add it to the list of selection listener of the given selection service.
62  *
63  * The selection listener is connected to the 'ServiceSelectionChanged' member function, which is
64  * called if a berry selection is changed in the workbench.
65  */
66  void AddPostSelectionListener(berry::ISelectionService* selectionService);
67  /*
68  * @brief Remove a selection listener from the list of selection listener of the selection service member.
69  */
70  void RemovePostSelectionListener();
71  /*
72  * @brief Store the given selection provider as a private member.
73  * In order to use the public slot 'ChangeServiceSelection'-function, the selection provider member had to be
74  * previously set.
75  */
76  void SetAsSelectionProvider(QmitkDataNodeSelectionProvider* selectionProvider);
77  /*
78  * @brief Set the selection provider member to a nullptr. This will prevent the public slot
79  * 'ChangeServiceSelection'-function from working.
80  */
81  void RemoveAsSelectionProvider();
82 
83 Q_SIGNALS:
84  /*
85  * @brief A signal that will be emitted by the private 'ServiceSelectionChanged'-function. This happens if a selection is changed
86  * via the selection service.
87  *
88  * @par nodes A list of data nodes that are newly selected.
89  */
90  void ServiceSelectionChanged(QList<mitk::DataNode::Pointer> nodes);
91 
92  /*
93  * @brief A signal that will be emitted by the private 'ServiceSelectionChanged'-function. If sourcePart has send an invalid selection
94  * (selection pointer was Null).
95  * @par sourcePart Part that sent the null selection.
96  */
97  void ServiceNullSelection(const berry::IWorkbenchPart::Pointer& sourcePart);
98 
99 public Q_SLOTS:
100  /*
101  * @brief Send new selections to the selection service via the private selection provider member.
102  *
103  * This slot-function is called whenever a local selection is changed in the surrounding widget and a selection provider was set.
104  * The newly selected data nodes are added temporary to a 'QmitkDataNodeItemModel', which is then used to define
105  * the indices to select.
106  * The 'QItemSelectionModel' is set as the item selection model of the selection provider member and its items are
107  * selected by the indices previously defined by the 'QmitkDataNodeItemModel'.
108  */
109  void ChangeServiceSelection(QList<mitk::DataNode::Pointer> nodes);
110 
111 private:
112 
113  std::unique_ptr<berry::ISelectionListener> m_BerrySelectionListener;
114  berry::ISelectionService* m_SelectionService;
115  QmitkDataNodeSelectionProvider* m_SelectionProvider;
116  std::shared_ptr<QmitkDataNodeItemModel> m_DataNodeItemModel;
117  std::shared_ptr<QItemSelectionModel> m_DataNodeSelectionModel;
118 
119  /*
120  * @brief Handle a selection received from the selection service.
121  *
122  * This function is called whenever a berry selection of the selection service is changed in the workbench.
123  * The new selection is transformed into a data node selection and the contained data nodes are propagated
124  * as the new current selection of the item view member.
125  *
126  * @par sourcePart The workbench part containing the selection.
127  * @par selection The current selection.
128  */
129  void OnServiceSelectionChanged(const berry::IWorkbenchPart::Pointer& sourcePart, const berry::ISelection::ConstPointer& selection);
130 
131 };
132 
133 #endif // QMITKSELECTIONSERVICECONNECTOR_H
A BlueBerry selection provider for mitk::DataNode selections.
#define MITK_QT_COMMON