File indexing completed on 2024-07-14 03:54:52

0001 /*
0002     This file is part of the proxy model test suite.
0003 
0004     SPDX-FileCopyrightText: 2009 Stephen Kelly <steveire@gmail.com>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-or-later
0007 */
0008 
0009 #include "selectionpmwidget.h"
0010 
0011 #include <QHBoxLayout>
0012 #include <QLabel>
0013 #include <QSplitter>
0014 #include <QTreeView>
0015 
0016 #include "dynamictreemodel.h"
0017 #include "dynamictreewidget.h"
0018 #include "kselectionproxymodel.h"
0019 
0020 SelectionProxyWidget::SelectionProxyWidget(QWidget *parent)
0021     : QWidget(parent)
0022 {
0023     QHBoxLayout *layout = new QHBoxLayout(this);
0024     QSplitter *splitter = new QSplitter(this);
0025     layout->addWidget(splitter);
0026 
0027     m_rootModel = new DynamicTreeModel(this);
0028 
0029     DynamicTreeWidget *dynTreeWidget = new DynamicTreeWidget(m_rootModel, splitter);
0030 
0031     dynTreeWidget->setInitialTree(
0032         QLatin1String("- 1"
0033                       "- 2"
0034                       "- - 3"
0035                       "- - 3"
0036                       "- - - 4"
0037                       "- - - 4"
0038                       "- - - - 4"
0039                       "- - 4"
0040                       "- - 5"
0041                       "- - - 4"
0042                       "- - - - 4"
0043                       "- - 5"
0044                       "- 6"
0045                       "- 7"
0046                       "- - 8"
0047                       "- - - 9"
0048                       "- - - 10"
0049                       "- - - - 9"
0050                       "- - - - - 10"
0051                       "- - - - - - 9"
0052                       "- - - - - - 10"
0053                       "- - - - - - - 9"
0054                       "- - - - - - - - 10"
0055                       "- - - - - - - - 9"
0056                       "- - - - - - - 10"
0057                       "- - - - - 9"
0058                       "- - - - - 9"
0059                       "- - - - - 9"
0060                       "- - - - - 10"
0061                       "- - - - - - 9"
0062                       "- - - - - - 10"
0063                       "- - - - - 9"
0064                       "- - - - - 9"
0065                       "- - - - - 9"
0066                       "- - - - - 10"
0067                       "- - - - - - 9"
0068                       "- - - - - - 10"
0069                       "- - - - 10"
0070                       "- - 11"
0071                       "- - 12"
0072                       "- 13"
0073                       "- 14"
0074                       "- 15"
0075                       "- - 16"
0076                       "- - - 17"
0077                       "- - - 18"
0078                       "- 19"
0079                       "- 20"
0080                       "- 21"));
0081 
0082     QTreeView *selectionTree = createLabelledView(QStringLiteral("Selection"), splitter);
0083     selectionTree->setSelectionMode(QAbstractItemView::ExtendedSelection);
0084     selectionTree->setModel(m_rootModel);
0085     selectionTree->expandAll();
0086 
0087 #define SUBTREES
0088 #define SUBTREEROOTS
0089 #define SUBTREESWITHOUTROOTS
0090 #define EXACTSELECTION
0091 #define CHILDRENOFEXACTSELECTION
0092 
0093 #ifdef SUBTREES
0094     KSelectionProxyModel *selectedBranchesModel = new KSelectionProxyModel(selectionTree->selectionModel(), this);
0095     selectedBranchesModel->setSourceModel(m_rootModel);
0096     selectedBranchesModel->setFilterBehavior(KSelectionProxyModel::SubTrees);
0097 
0098     QTreeView *selectedBranchesView = createLabelledView(QStringLiteral("SubTrees"), splitter);
0099     selectedBranchesView->setModel(selectedBranchesModel);
0100 #endif
0101 
0102 #ifdef SUBTREEROOTS
0103     KSelectionProxyModel *selectedBranchesRootsModel = new KSelectionProxyModel(selectionTree->selectionModel(), this);
0104     selectedBranchesRootsModel->setSourceModel(m_rootModel);
0105     selectedBranchesRootsModel->setFilterBehavior(KSelectionProxyModel::SubTreeRoots);
0106 
0107     QTreeView *selectedBranchesRootsView = createLabelledView(QStringLiteral("SubTreeRoots"), splitter);
0108     selectedBranchesRootsView->setModel(selectedBranchesRootsModel);
0109 #endif
0110 
0111 #ifdef SUBTREESWITHOUTROOTS
0112     KSelectionProxyModel *selectedBranchesChildrenModel = new KSelectionProxyModel(selectionTree->selectionModel(), this);
0113     selectedBranchesChildrenModel->setSourceModel(m_rootModel);
0114     selectedBranchesChildrenModel->setFilterBehavior(KSelectionProxyModel::SubTreesWithoutRoots);
0115 
0116     QTreeView *selectedBranchesChildrenView = createLabelledView(QStringLiteral("SubTreesWithoutRoots"), splitter);
0117     selectedBranchesChildrenView->setModel(selectedBranchesChildrenModel);
0118 #endif
0119 
0120 #ifdef EXACTSELECTION
0121     KSelectionProxyModel *onlySelectedModel = new KSelectionProxyModel(selectionTree->selectionModel(), this);
0122     onlySelectedModel->setSourceModel(m_rootModel);
0123     onlySelectedModel->setFilterBehavior(KSelectionProxyModel::ExactSelection);
0124 
0125     QTreeView *onlySelectedView = createLabelledView(QStringLiteral("ExactSelection"), splitter);
0126     onlySelectedView->setModel(onlySelectedModel);
0127 #endif
0128 
0129 #ifdef CHILDRENOFEXACTSELECTION
0130     KSelectionProxyModel *onlySelectedChildrenModel = new KSelectionProxyModel(selectionTree->selectionModel(), this);
0131     onlySelectedChildrenModel->setSourceModel(m_rootModel);
0132     onlySelectedChildrenModel->setFilterBehavior(KSelectionProxyModel::ChildrenOfExactSelection);
0133 
0134     QTreeView *onlySelectedChildrenView = createLabelledView(QStringLiteral("ChildrenOfExactSelection"), splitter);
0135     onlySelectedChildrenView->setModel(onlySelectedChildrenModel);
0136 #endif
0137 }
0138 
0139 QTreeView *SelectionProxyWidget::createLabelledView(const QString &labelText, QWidget *parent)
0140 {
0141     QWidget *labelledTreeWidget = new QWidget(parent);
0142     QVBoxLayout *layout = new QVBoxLayout(labelledTreeWidget);
0143 
0144     QLabel *label = new QLabel(labelText, labelledTreeWidget);
0145     QTreeView *treeview = new QTreeView(labelledTreeWidget);
0146 
0147     layout->addWidget(label);
0148     layout->addWidget(treeview);
0149     return treeview;
0150 }