File indexing completed on 2024-04-21 03:56:13

0001 /*
0002     SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
0003     SPDX-FileContributor: Stephen Kelly <stephen@kdab.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "breadcrumbswidget.h"
0009 
0010 #include "dynamictreemodel.h"
0011 #include "dynamictreewidget.h"
0012 
0013 #include <QHBoxLayout>
0014 #include <QListView>
0015 #include <QSplitter>
0016 #include <QTreeView>
0017 
0018 #include "kbreadcrumbselectionmodel.h"
0019 #include "kselectionproxymodel.h"
0020 
0021 MultiSelectionModel::MultiSelectionModel(QAbstractItemModel *model, QList<QItemSelectionModel *> selectionModels, QObject *parent)
0022     : QItemSelectionModel(model, parent)
0023     , m_selectionModels(selectionModels)
0024 {
0025 }
0026 
0027 void MultiSelectionModel::select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
0028 {
0029     for (QItemSelectionModel *selectionModel : std::as_const(m_selectionModels)) {
0030         selectionModel->select(index, command);
0031     }
0032     QItemSelectionModel::select(index, command);
0033 }
0034 
0035 void MultiSelectionModel::select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command)
0036 {
0037     for (QItemSelectionModel *selectionModel : std::as_const(m_selectionModels)) {
0038         selectionModel->select(selection, command);
0039     }
0040     QItemSelectionModel::select(selection, command);
0041 }
0042 
0043 BreadcrumbsWidget::BreadcrumbsWidget(QWidget *parent, Qt::WindowFlags f)
0044     : QWidget(parent, f)
0045 {
0046     DynamicTreeModel *rootModel = new DynamicTreeModel(this);
0047     QSplitter *splitter = new QSplitter(this);
0048     QHBoxLayout *layout = new QHBoxLayout(this);
0049     layout->addWidget(splitter);
0050 
0051     DynamicTreeWidget *dynamicTree = new DynamicTreeWidget(rootModel, splitter);
0052     dynamicTree->treeView()->setSelectionMode(QAbstractItemView::SingleSelection);
0053     dynamicTree->setInitialTree(
0054         QLatin1String("- 1"
0055                       "- - 2"
0056                       "- - 2"
0057                       "- - - 3"
0058                       "- - - - 4"
0059                       "- - - - - 5"
0060                       "- - 2"
0061                       "- 6"
0062                       "- 6"
0063                       "- 6"
0064                       "- - 7"
0065                       "- - - 8"
0066                       "- - - 8"
0067                       "- - - - 9"
0068                       "- - - - - 10"
0069                       "- - - 8"
0070                       "- - - 8"
0071                       "- - 8"
0072                       "- 16"
0073                       "- - 17"
0074                       "- - - 18"
0075                       "- - - - 19"
0076                       "- - - - - 20"));
0077 
0078     QList<QItemSelectionModel *> selectionModelList;
0079     QItemSelectionModel *fullBreadcrumbSelectionModel = new QItemSelectionModel(rootModel, this);
0080 
0081     KBreadcrumbSelectionModel *fullBreadcrumbProxySelector = new KBreadcrumbSelectionModel(fullBreadcrumbSelectionModel, this);
0082     selectionModelList << fullBreadcrumbProxySelector;
0083 
0084     KSelectionProxyModel *fullBreadCrumbSelectionProxyModel = new KSelectionProxyModel(fullBreadcrumbSelectionModel, this);
0085     fullBreadCrumbSelectionProxyModel->setSourceModel(rootModel);
0086     fullBreadCrumbSelectionProxyModel->setFilterBehavior(KSelectionProxyModel::ExactSelection);
0087 
0088     QListView *fullBreadcrumbProxyView = new QListView(splitter);
0089     fullBreadcrumbProxyView->setModel(fullBreadCrumbSelectionProxyModel);
0090 
0091     QItemSelectionModel *breadcrumbOnlySelectionModel = new QItemSelectionModel(rootModel, this);
0092 
0093     KBreadcrumbSelectionModel *breadcrumbOnlyProxySelector = new KBreadcrumbSelectionModel(breadcrumbOnlySelectionModel, this);
0094     breadcrumbOnlyProxySelector->setActualSelectionIncluded(false);
0095     selectionModelList << breadcrumbOnlyProxySelector;
0096 
0097     KSelectionProxyModel *breadcrumbOnlySelectionProxyModel = new KSelectionProxyModel(breadcrumbOnlySelectionModel, this);
0098     breadcrumbOnlySelectionProxyModel->setSourceModel(rootModel);
0099     breadcrumbOnlySelectionProxyModel->setFilterBehavior(KSelectionProxyModel::ExactSelection);
0100 
0101     QListView *breadcrumbOnlyProxyView = new QListView(splitter);
0102     breadcrumbOnlyProxyView->setModel(breadcrumbOnlySelectionProxyModel);
0103 
0104     int selectionDepth = 2;
0105 
0106     QItemSelectionModel *thisAndAscendantsSelectionModel = new QItemSelectionModel(rootModel, this);
0107 
0108     KBreadcrumbSelectionModel *thisAndAscendantsProxySelector = new KBreadcrumbSelectionModel(thisAndAscendantsSelectionModel, this);
0109     thisAndAscendantsProxySelector->setBreadcrumbLength(selectionDepth);
0110     selectionModelList << thisAndAscendantsProxySelector;
0111 
0112     KSelectionProxyModel *thisAndAscendantsSelectionProxyModel = new KSelectionProxyModel(thisAndAscendantsSelectionModel, this);
0113     thisAndAscendantsSelectionProxyModel->setSourceModel(rootModel);
0114     thisAndAscendantsSelectionProxyModel->setFilterBehavior(KSelectionProxyModel::ExactSelection);
0115 
0116     QListView *thisAndAscendantsProxyView = new QListView(splitter);
0117     thisAndAscendantsProxyView->setModel(thisAndAscendantsSelectionProxyModel);
0118 
0119     QItemSelectionModel *ascendantsOnlySelectionModel = new QItemSelectionModel(rootModel, this);
0120 
0121     KBreadcrumbSelectionModel *ascendantsOnlyProxySelector = new KBreadcrumbSelectionModel(ascendantsOnlySelectionModel, this);
0122     ascendantsOnlyProxySelector->setActualSelectionIncluded(false);
0123     ascendantsOnlyProxySelector->setBreadcrumbLength(selectionDepth);
0124     selectionModelList << ascendantsOnlyProxySelector;
0125 
0126     KSelectionProxyModel *ascendantsOnlySelectionProxyModel = new KSelectionProxyModel(ascendantsOnlySelectionModel, this);
0127     ascendantsOnlySelectionProxyModel->setSourceModel(rootModel);
0128     ascendantsOnlySelectionProxyModel->setFilterBehavior(KSelectionProxyModel::ExactSelection);
0129 
0130     QListView *ascendantsOnlyProxyView = new QListView(splitter);
0131     ascendantsOnlyProxyView->setModel(ascendantsOnlySelectionProxyModel);
0132 
0133     MultiSelectionModel *multiSelectionModel = new MultiSelectionModel(rootModel, selectionModelList, this);
0134     dynamicTree->treeView()->setSelectionModel(multiSelectionModel);
0135 }
0136 
0137 #include "moc_breadcrumbswidget.cpp"