1 | /* |
2 | SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net> |
3 | SPDX-FileContributor: Stephen Kelly <stephen@kdab.com> |
4 | |
5 | SPDX-License-Identifier: LGPL-2.0-or-later |
6 | */ |
7 | |
8 | #ifndef KBREADCRUMBSELECTIONMODEL_H |
9 | #define KBREADCRUMBSELECTIONMODEL_H |
10 | |
11 | #include <QItemSelectionModel> |
12 | |
13 | #include "kitemmodels_export.h" |
14 | |
15 | #include <memory> |
16 | |
17 | class KBreadcrumbSelectionModelPrivate; |
18 | |
19 | /** |
20 | @class KBreadcrumbSelectionModel kbreadcrumbselectionmodel.h KBreadcrumbSelectionModel |
21 | |
22 | @brief Selects the parents of selected items to create breadcrumbs |
23 | |
24 | For example, if the tree is |
25 | @verbatim |
26 | - A |
27 | - B |
28 | - - C |
29 | - - D |
30 | - - - E |
31 | - - - - F |
32 | @endverbatim |
33 | |
34 | and E is selected, the selection can contain |
35 | |
36 | @verbatim |
37 | - B |
38 | - D |
39 | @endverbatim |
40 | |
41 | or |
42 | |
43 | @verbatim |
44 | - B |
45 | - D |
46 | - E |
47 | @endverbatim |
48 | |
49 | if isActualSelectionIncluded is true. |
50 | |
51 | The depth of the selection may also be set. For example if the breadcrumbLength is 1: |
52 | |
53 | @verbatim |
54 | - D |
55 | - E |
56 | @endverbatim |
57 | |
58 | And if breadcrumbLength is 2: |
59 | |
60 | @verbatim |
61 | - B |
62 | - D |
63 | - E |
64 | @endverbatim |
65 | |
66 | A KBreadcrumbsSelectionModel with a breadcrumbLength of 0 and including the actual selection is |
67 | the same as a KSelectionProxyModel in the KSelectionProxyModel::ExactSelection configuration. |
68 | |
69 | @code |
70 | view1->setModel(rootModel); |
71 | |
72 | QItemSelectionModel *breadcrumbSelectionModel = new QItemSelectionModel(rootModel, this); |
73 | |
74 | KBreadcrumbSelectionModel *breadcrumbProxySelector = new KBreadcrumbSelectionModel(breadcrumbSelectionModel, rootModel, this); |
75 | |
76 | view1->setSelectionModel(breadcrumbProxySelector); |
77 | |
78 | KSelectionProxyModel *breadcrumbSelectionProxyModel = new KSelectionProxyModel( breadcrumbSelectionModel, this); |
79 | breadcrumbSelectionProxyModel->setSourceModel( rootModel ); |
80 | breadcrumbSelectionProxyModel->setFilterBehavior( KSelectionProxyModel::ExactSelection ); |
81 | |
82 | view2->setModel(breadcrumbSelectionProxyModel); |
83 | @endcode |
84 | |
85 | @image html kbreadcrumbselectionmodel.png "KBreadcrumbSelectionModel in several configurations" |
86 | |
87 | This can work in two directions. One option is for a single selection in the KBreadcrumbSelectionModel to invoke |
88 | the breadcrumb selection in its constructor argument. |
89 | |
90 | The other is for a selection in the itemselectionmodel in the constructor argument to cause a breadcrumb selection |
91 | in @p this. |
92 | |
93 | @since 4.5 |
94 | |
95 | */ |
96 | class KITEMMODELS_EXPORT KBreadcrumbSelectionModel : public QItemSelectionModel |
97 | { |
98 | Q_OBJECT |
99 | public: |
100 | enum BreadcrumbTarget { |
101 | MakeBreadcrumbSelectionInOther, |
102 | MakeBreadcrumbSelectionInSelf, |
103 | }; |
104 | |
105 | explicit KBreadcrumbSelectionModel(QItemSelectionModel *selectionModel, QObject *parent = nullptr); |
106 | KBreadcrumbSelectionModel(QItemSelectionModel *selectionModel, BreadcrumbTarget target, QObject *parent = nullptr); |
107 | ~KBreadcrumbSelectionModel() override; |
108 | |
109 | /** |
110 | Returns whether the actual selection in included in the proxy. |
111 | |
112 | The default is true. |
113 | */ |
114 | bool isActualSelectionIncluded() const; |
115 | |
116 | /** |
117 | Set whether the actual selection in included in the proxy to @p isActualSelectionIncluded. |
118 | */ |
119 | void setActualSelectionIncluded(bool isActualSelectionIncluded); |
120 | |
121 | /** |
122 | Returns the depth that the breadcrumb selection should go to. |
123 | */ |
124 | int breadcrumbLength() const; |
125 | |
126 | /** |
127 | Sets the depth that the breadcrumb selection should go to. |
128 | |
129 | If the @p breadcrumbLength is -1, all breadcrumbs are selected. |
130 | The default is -1 |
131 | */ |
132 | void setBreadcrumbLength(int breadcrumbLength); |
133 | |
134 | void select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) override; |
135 | |
136 | void select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command) override; |
137 | |
138 | protected: |
139 | std::unique_ptr<KBreadcrumbSelectionModelPrivate> const d_ptr; |
140 | |
141 | private: |
142 | //@cond PRIVATE |
143 | Q_DECLARE_PRIVATE(KBreadcrumbSelectionModel) |
144 | //@cond PRIVATE |
145 | }; |
146 | |
147 | #endif |
148 | |