| 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 |
| 21 | \inmodule KItemModels |
| 22 | \brief Selects the parents of selected items to create breadcrumbs. |
| 23 | |
| 24 | For example, if the tree is |
| 25 | \code |
| 26 | - A |
| 27 | - B |
| 28 | - - C |
| 29 | - - D |
| 30 | - - - E |
| 31 | - - - - F |
| 32 | \endcode |
| 33 | |
| 34 | and E is selected, the selection can contain |
| 35 | |
| 36 | \code |
| 37 | - B |
| 38 | - D |
| 39 | \endcode |
| 40 | |
| 41 | or |
| 42 | |
| 43 | \code |
| 44 | - B |
| 45 | - D |
| 46 | - E |
| 47 | \endcode |
| 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 | \code |
| 54 | - D |
| 55 | - E |
| 56 | \endcode |
| 57 | |
| 58 | And if breadcrumbLength is 2: |
| 59 | |
| 60 | \code |
| 61 | - B |
| 62 | - D |
| 63 | - E |
| 64 | \endcode |
| 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 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 this. |
| 92 | |
| 93 | \since 4.5 |
| 94 | |
| 95 | */ |
| 96 | class KITEMMODELS_EXPORT KBreadcrumbSelectionModel : public QItemSelectionModel |
| 97 | { |
| 98 | Q_OBJECT |
| 99 | public: |
| 100 | /*! |
| 101 | * \value MakeBreadcrumbSelectionInOther |
| 102 | * \value MakeBreadcrumbSelectionInSelf |
| 103 | */ |
| 104 | enum BreadcrumbTarget { |
| 105 | MakeBreadcrumbSelectionInOther, |
| 106 | MakeBreadcrumbSelectionInSelf, |
| 107 | }; |
| 108 | |
| 109 | /*! |
| 110 | * |
| 111 | */ |
| 112 | explicit KBreadcrumbSelectionModel(QItemSelectionModel *selectionModel, QObject *parent = nullptr); |
| 113 | |
| 114 | /*! |
| 115 | * |
| 116 | */ |
| 117 | KBreadcrumbSelectionModel(QItemSelectionModel *selectionModel, BreadcrumbTarget target, QObject *parent = nullptr); |
| 118 | |
| 119 | ~KBreadcrumbSelectionModel() override; |
| 120 | |
| 121 | /*! |
| 122 | Returns whether the actual selection in included in the proxy. |
| 123 | |
| 124 | The default is true. |
| 125 | */ |
| 126 | bool isActualSelectionIncluded() const; |
| 127 | |
| 128 | /*! |
| 129 | Set whether the actual selection in included in the proxy to \a isActualSelectionIncluded. |
| 130 | */ |
| 131 | void setActualSelectionIncluded(bool isActualSelectionIncluded); |
| 132 | |
| 133 | /*! |
| 134 | Returns the depth that the breadcrumb selection should go to. |
| 135 | */ |
| 136 | int breadcrumbLength() const; |
| 137 | |
| 138 | /*! |
| 139 | Sets the depth that the breadcrumb selection should go to. |
| 140 | |
| 141 | If the \a breadcrumbLength is -1, all breadcrumbs are selected. |
| 142 | The default is -1 |
| 143 | */ |
| 144 | void setBreadcrumbLength(int breadcrumbLength); |
| 145 | |
| 146 | void select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) override; |
| 147 | |
| 148 | void select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command) override; |
| 149 | |
| 150 | protected: |
| 151 | std::unique_ptr<KBreadcrumbSelectionModelPrivate> const d_ptr; |
| 152 | |
| 153 | private: |
| 154 | Q_DECLARE_PRIVATE(KBreadcrumbSelectionModel) |
| 155 | }; |
| 156 | |
| 157 | #endif |
| 158 | |