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