1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
/*
SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
SPDX-FileContributor: Stephen Kelly <stephen@kdab.com>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "breadcrumbnavigationwidget.h"
#include "dynamictreemodel.h"
#include "dynamictreewidget.h"
#include <QHBoxLayout>
#include <QListView>
#include <QSplitter>
#include <QTreeView>
#include "kbreadcrumbselectionmodel.h"
#include "kselectionproxymodel.h"
#define SON(object) object->setObjectName(QStringLiteral(#object))
CurrentItemLabel::CurrentItemLabel(QAbstractItemModel *model, QWidget *parent, Qt::WindowFlags f)
: QLabel(parent, f)
, m_model(model)
{
connect(model, SIGNAL(dataChanged(QModelIndex, QModelIndex)), SLOT(dataChanged(QModelIndex, QModelIndex)));
connect(model, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(rowsInserted(QModelIndex, int, int)));
connect(model, SIGNAL(rowsRemoved(QModelIndex, int, int)), SLOT(rowsRemoved(QModelIndex, int, int)));
connect(model, SIGNAL(modelReset()), SLOT(modelReset()));
if (!m_model->hasChildren()) {
setText(QStringLiteral("No selection"));
}
}
void CurrentItemLabel::dataChanged(const QModelIndex &, const QModelIndex &)
{
setText(m_model->index(0, 0).data().toString());
}
void CurrentItemLabel::rowsInserted(const QModelIndex &, int, int)
{
setText(m_model->index(0, 0).data().toString());
}
void CurrentItemLabel::rowsRemoved(const QModelIndex &, int, int)
{
if (!m_model->hasChildren()) {
setText(QStringLiteral("No selection"));
return;
}
setText(m_model->index(0, 0).data().toString());
}
void CurrentItemLabel::modelReset()
{
if (!m_model->hasChildren()) {
setText(QStringLiteral("No selection"));
}
setText(m_model->index(0, 0).data().toString());
}
KBreadcrumbNavigationProxyModel::KBreadcrumbNavigationProxyModel(QItemSelectionModel *selectionModel, QObject *parent)
: KSelectionProxyModel(selectionModel, parent)
{
}
QVariant KBreadcrumbNavigationProxyModel::data(const QModelIndex &index, int role) const
{
if (rowCount() > 2 && index.row() == 0 && role == Qt::DisplayRole) {
QModelIndex sourceIndex = mapToSource(index);
QStringList dataList;
while (sourceIndex.isValid()) {
dataList.prepend(sourceIndex.data().toString());
sourceIndex = sourceIndex.parent();
}
return dataList.join(QLatin1String(" > "));
}
return KSelectionProxyModel::data(index, role);
}
void KBreadcrumbNavigationProxyModel::setShowHiddenAscendantData(bool showHiddenAscendantData)
{
m_showHiddenAscendantData = showHiddenAscendantData;
}
bool KBreadcrumbNavigationProxyModel::showHiddenAscendantData() const
{
return m_showHiddenAscendantData;
}
KNavigatingProxyModel::KNavigatingProxyModel(QItemSelectionModel *selectionModel, QObject *parent)
: KSelectionProxyModel(selectionModel, parent)
, m_selectionModel(selectionModel)
{
}
void KNavigatingProxyModel::setSourceModel(QAbstractItemModel *sourceModel)
{
connect(m_selectionModel, SIGNAL(selectionChanged(QItemSelection, QItemSelection)), SLOT(navigationSelectionChanged(QItemSelection, QItemSelection)));
KSelectionProxyModel::setSourceModel(sourceModel);
updateNavigation();
}
void KNavigatingProxyModel::navigationSelectionChanged(const QItemSelection &, const QItemSelection &)
{
updateNavigation();
}
void KNavigatingProxyModel::updateNavigation()
{
if (!sourceModel()) {
return;
}
if (m_selectionModel->selection().isEmpty()) {
setFilterBehavior(KSelectionProxyModel::ExactSelection);
QModelIndex top = sourceModel()->index(0, 0);
QModelIndex bottom = sourceModel()->index(sourceModel()->rowCount() - 1, 0);
disconnect(m_selectionModel,
SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
this,
SLOT(navigationSelectionChanged(QItemSelection, QItemSelection)));
m_selectionModel->select(QItemSelection(top, bottom), QItemSelectionModel::Select);
connect(m_selectionModel, SIGNAL(selectionChanged(QItemSelection, QItemSelection)), SLOT(navigationSelectionChanged(QItemSelection, QItemSelection)));
} else if (filterBehavior() != KSelectionProxyModel::ChildrenOfExactSelection) {
setFilterBehavior(KSelectionProxyModel::ChildrenOfExactSelection);
}
}
void KNavigatingProxyModel::modelReset()
{
updateNavigation();
}
QVariant KNavigatingProxyModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::DisplayRole && sourceModel()->hasChildren(mapToSource(index))) {
return QString("+ " + KSelectionProxyModel::data(index, role).toString());
}
return KSelectionProxyModel::data(index, role);
}
KForwardingItemSelectionModel::KForwardingItemSelectionModel(QAbstractItemModel *model, QItemSelectionModel *selectionModel, QObject *parent)
: QItemSelectionModel(model, parent)
, m_selectionModel(selectionModel)
, m_direction(Forward)
{
Q_ASSERT(model == selectionModel->model());
connect(selectionModel, SIGNAL(selectionChanged(QItemSelection, QItemSelection)), SLOT(navigationSelectionChanged(QItemSelection, QItemSelection)));
}
KForwardingItemSelectionModel::KForwardingItemSelectionModel(QAbstractItemModel *model,
QItemSelectionModel *selectionModel,
Direction direction,
QObject *parent)
: QItemSelectionModel(model, parent)
, m_selectionModel(selectionModel)
, m_direction(direction)
{
Q_ASSERT(model == selectionModel->model());
if (m_direction == Forward) {
connect(selectionModel, SIGNAL(selectionChanged(QItemSelection, QItemSelection)), SLOT(navigationSelectionChanged(QItemSelection, QItemSelection)));
}
}
void KForwardingItemSelectionModel::select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
{
if (m_direction == Reverse) {
m_selectionModel->select(index, command);
} else {
QItemSelectionModel::select(index, command);
}
}
void KForwardingItemSelectionModel::select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command)
{
if (m_direction == Reverse) {
m_selectionModel->select(selection, command);
} else {
QItemSelectionModel::select(selection, command);
}
}
void KForwardingItemSelectionModel::navigationSelectionChanged(const QItemSelection &selected, const QItemSelection &)
{
select(selected, ClearAndSelect);
}
BreadcrumbNavigationWidget::BreadcrumbNavigationWidget(QWidget *parent, Qt::WindowFlags f)
: QWidget(parent, f)
{
DynamicTreeModel *rootModel = new DynamicTreeModel(this);
QSplitter *splitter = new QSplitter(this);
QHBoxLayout *layout = new QHBoxLayout(this);
layout->addWidget(splitter);
DynamicTreeWidget *dynamicTree = new DynamicTreeWidget(rootModel, splitter);
dynamicTree->treeView()->setSelectionMode(QAbstractItemView::SingleSelection);
dynamicTree->setInitialTree(
QLatin1String("- 1"
"- - 2"
"- - 2"
"- - - 3"
"- - - - 4"
"- - - - - 5"
"- - 2"
"- 6"
"- 6"
"- 6"
"- - 7"
"- - - 8"
"- - - 8"
"- - - - 9"
"- - - - - 10"
"- - - 8"
"- - - 8"
"- - 8"
"- 16"
"- - 17"
"- - - 18"
"- - - - 19"
"- - - - - 20"));
QList<QItemSelectionModel *> selectionModelList;
QSplitter *vSplitter = new QSplitter(Qt::Vertical, splitter);
QItemSelectionModel *rootSelectionModel = new QItemSelectionModel(rootModel, this);
SON(rootSelectionModel);
dynamicTree->treeView()->setSelectionModel(rootSelectionModel);
KBreadcrumbSelectionModel *breadcrumbOnlyProxySelector2 =
new KBreadcrumbSelectionModel(rootSelectionModel, KBreadcrumbSelectionModel::MakeBreadcrumbSelectionInOther, this);
SON(breadcrumbOnlyProxySelector2);
breadcrumbOnlyProxySelector2->setActualSelectionIncluded(false);
KBreadcrumbNavigationProxyModel *breadcrumbNavigationModel = new KBreadcrumbNavigationProxyModel(breadcrumbOnlyProxySelector2, this);
SON(breadcrumbNavigationModel);
breadcrumbNavigationModel->setSourceModel(rootModel);
breadcrumbNavigationModel->setFilterBehavior(KSelectionProxyModel::ExactSelection);
QListView *breadcrumbView = new QListView(vSplitter);
// SON(breadcrumbNavigationModel);
breadcrumbView->setModel(breadcrumbNavigationModel);
// This shouldn't operate on rootSelectionModel. It should operate on oneway instead?
KLinkItemSelectionModel *breadcrumbViewSelectionModel = new KLinkItemSelectionModel(breadcrumbNavigationModel, rootSelectionModel, this);
SON(breadcrumbViewSelectionModel);
KForwardingItemSelectionModel *oneway2 =
new KForwardingItemSelectionModel(breadcrumbNavigationModel, breadcrumbViewSelectionModel, KForwardingItemSelectionModel::Reverse);
SON(oneway2);
breadcrumbView->setSelectionModel(oneway2);
KSelectionProxyModel *currentItemSelectionModel = new KSelectionProxyModel(rootSelectionModel, this);
currentItemSelectionModel->setFilterBehavior(KSelectionProxyModel::ExactSelection);
currentItemSelectionModel->setSourceModel(rootModel);
SON(currentItemSelectionModel);
new CurrentItemLabel(currentItemSelectionModel, vSplitter);
QListView *selectionView = new QListView(vSplitter);
// Need a one-way connection from rootSelectionModel to rootSelectionModel2
KForwardingItemSelectionModel *oneway = new KForwardingItemSelectionModel(rootModel, rootSelectionModel);
KNavigatingProxyModel *navigatingProxyModel = new KNavigatingProxyModel(oneway, this);
SON(navigatingProxyModel);
navigatingProxyModel->setSourceModel(rootModel);
selectionView->setModel(navigatingProxyModel);
KLinkItemSelectionModel *selectedChildrenSelectionModel = new KLinkItemSelectionModel(navigatingProxyModel, rootSelectionModel, this);
selectionView->setSelectionModel(selectedChildrenSelectionModel);
}
#include "moc_breadcrumbnavigationwidget.cpp"
|