• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • itemviews
kbreadcrumbselectionmodel.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2010 Klarälvdalens Datakonsult AB,
3 a KDAB Group company, info@kdab.net,
4 author Stephen Kelly <stephen@kdab.com>
5
6 This library is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Library General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at your
9 option) any later version.
10
11 This library is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14 License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA.
20*/
21
22
23#include "kbreadcrumbselectionmodel.h"
24
25#include "kdebug.h"
26
27class KBreadcrumbSelectionModelPrivate
28{
29 Q_DECLARE_PUBLIC(KBreadcrumbSelectionModel)
30 KBreadcrumbSelectionModel * const q_ptr;
31public:
32 KBreadcrumbSelectionModelPrivate(KBreadcrumbSelectionModel *breadcrumbSelector, QItemSelectionModel *selectionModel, KBreadcrumbSelectionModel::BreadcrumbTarget direction)
33 : q_ptr(breadcrumbSelector),
34 m_includeActualSelection(true),
35 m_selectionDepth(-1),
36 m_showHiddenAscendantData(false),
37 m_selectionModel(selectionModel),
38 m_direction(direction),
39 m_ignoreCurrentChanged(false)
40 {
41
42 }
43
47 QItemSelection getBreadcrumbSelection(const QModelIndex &index);
48
52 QItemSelection getBreadcrumbSelection(const QItemSelection &selection);
53
54 void sourceSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
55
56 void init();
57 void syncBreadcrumbs();
58
59 bool m_includeActualSelection;
60 int m_selectionDepth;
61 bool m_showHiddenAscendantData;
62 QItemSelectionModel *m_selectionModel;
63 KBreadcrumbSelectionModel::BreadcrumbTarget m_direction;
64 bool m_ignoreCurrentChanged;
65};
66
67KBreadcrumbSelectionModel::KBreadcrumbSelectionModel(QItemSelectionModel *selectionModel, QObject* parent)
68 : QItemSelectionModel(const_cast<QAbstractItemModel *>(selectionModel->model()), parent),
69 d_ptr(new KBreadcrumbSelectionModelPrivate(this, selectionModel, MakeBreadcrumbSelectionInSelf))
70{
71 d_ptr->init();
72}
73
74KBreadcrumbSelectionModel::KBreadcrumbSelectionModel(QItemSelectionModel *selectionModel, BreadcrumbTarget direction, QObject* parent)
75 : QItemSelectionModel(const_cast<QAbstractItemModel *>(selectionModel->model()), parent),
76 d_ptr(new KBreadcrumbSelectionModelPrivate(this, selectionModel, direction))
77{
78 if ( direction != MakeBreadcrumbSelectionInSelf)
79 connect(selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
80 this, SLOT(sourceSelectionChanged(QItemSelection,QItemSelection)));
81
82 d_ptr->init();
83}
84
85KBreadcrumbSelectionModel::~KBreadcrumbSelectionModel()
86{
87 delete d_ptr;
88}
89
90bool KBreadcrumbSelectionModel::isActualSelectionIncluded() const
91{
92 Q_D(const KBreadcrumbSelectionModel);
93 return d->m_includeActualSelection;
94}
95
96void KBreadcrumbSelectionModel::setActualSelectionIncluded(bool includeActualSelection)
97{
98 Q_D(KBreadcrumbSelectionModel);
99 d->m_includeActualSelection = includeActualSelection;
100}
101
102int KBreadcrumbSelectionModel::breadcrumbLength() const
103{
104 Q_D(const KBreadcrumbSelectionModel);
105 return d->m_selectionDepth;
106}
107
108void KBreadcrumbSelectionModel::setBreadcrumbLength(int breadcrumbLength)
109{
110 Q_D(KBreadcrumbSelectionModel);
111 d->m_selectionDepth = breadcrumbLength;
112}
113
114QItemSelection KBreadcrumbSelectionModelPrivate::getBreadcrumbSelection(const QModelIndex& index)
115{
116 QItemSelection breadcrumbSelection;
117
118 if (m_includeActualSelection)
119 breadcrumbSelection.append(QItemSelectionRange(index));
120
121 QModelIndex parent = index.parent();
122 int sumBreadcrumbs = 0;
123 bool includeAll = m_selectionDepth < 0;
124 while (parent.isValid() && (includeAll || sumBreadcrumbs < m_selectionDepth)) {
125 breadcrumbSelection.append(QItemSelectionRange(parent));
126 parent = parent.parent();
127 }
128 return breadcrumbSelection;
129}
130
131QItemSelection KBreadcrumbSelectionModelPrivate::getBreadcrumbSelection(const QItemSelection& selection)
132{
133 QItemSelection breadcrumbSelection;
134
135 if (m_includeActualSelection)
136 breadcrumbSelection = selection;
137
138 QItemSelection::const_iterator it = selection.constBegin();
139 const QItemSelection::const_iterator end = selection.constEnd();
140
141 for ( ; it != end; ++it)
142 {
143 QModelIndex parent = it->parent();
144
145 if (breadcrumbSelection.contains(parent))
146 continue;
147
148 int sumBreadcrumbs = 0;
149 bool includeAll = m_selectionDepth < 0;
150
151 while (parent.isValid() && (includeAll || sumBreadcrumbs < m_selectionDepth))
152 {
153 breadcrumbSelection.append(QItemSelectionRange(parent));
154 parent = parent.parent();
155
156 if (breadcrumbSelection.contains(parent))
157 break;
158
159 ++sumBreadcrumbs;
160 }
161 }
162 return breadcrumbSelection;
163}
164
165void KBreadcrumbSelectionModelPrivate::sourceSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
166{
167 Q_Q(KBreadcrumbSelectionModel);
168 QItemSelection deselectedCrumbs = getBreadcrumbSelection(deselected);
169 QItemSelection selectedCrumbs = getBreadcrumbSelection(selected);
170
171 QItemSelection removed = deselectedCrumbs;
172 foreach(const QItemSelectionRange &range, selectedCrumbs)
173 {
174 removed.removeAll(range);
175 }
176
177 QItemSelection added = selectedCrumbs;
178 foreach(const QItemSelectionRange &range, deselectedCrumbs)
179 {
180 added.removeAll(range);
181 }
182
183 if (!removed.isEmpty())
184 {
185 q->QItemSelectionModel::select(removed, QItemSelectionModel::Deselect);
186 }
187 if (!added.isEmpty())
188 {
189 q->QItemSelectionModel::select(added, QItemSelectionModel::Select);
190 }
191}
192
193void KBreadcrumbSelectionModel::select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
194{
195 Q_D(KBreadcrumbSelectionModel);
196 // When an item is removed, the current index is set to the top index in the model.
197 // That causes a selectionChanged signal with a selection which we do not want.
198 if ( d->m_ignoreCurrentChanged )
199 {
200 d->m_ignoreCurrentChanged = false;
201 return;
202 }
203 if ( d->m_direction == MakeBreadcrumbSelectionInOther )
204 {
205 d->m_selectionModel->select(d->getBreadcrumbSelection(index), command);
206 QItemSelectionModel::select(index, command);
207 } else {
208 d->m_selectionModel->select(index, command);
209 QItemSelectionModel::select(d->getBreadcrumbSelection(index), command);
210 }
211}
212
213void KBreadcrumbSelectionModel::select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command)
214{
215 Q_D(KBreadcrumbSelectionModel);
216 QItemSelection bcc = d->getBreadcrumbSelection(selection);
217 if ( d->m_direction == MakeBreadcrumbSelectionInOther )
218 {
219 d->m_selectionModel->select(selection, command);
220 QItemSelectionModel::select(bcc, command);
221 } else {
222 d->m_selectionModel->select(bcc, command);
223 QItemSelectionModel::select(selection, command);
224 }
225}
226
227void KBreadcrumbSelectionModelPrivate::init()
228{
229 Q_Q(KBreadcrumbSelectionModel);
230 q->connect(m_selectionModel->model(), SIGNAL(layoutChanged()), SLOT(syncBreadcrumbs()));
231 q->connect(m_selectionModel->model(), SIGNAL(modelReset()), SLOT(syncBreadcrumbs()));
232 q->connect(m_selectionModel->model(), SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)), SLOT(syncBreadcrumbs()));
233 // Don't need to handle insert & remove because they can't change the breadcrumbs on their own.
234}
235
236void KBreadcrumbSelectionModelPrivate::syncBreadcrumbs()
237{
238 Q_Q(KBreadcrumbSelectionModel);
239 q->select(m_selectionModel->selection(), QItemSelectionModel::ClearAndSelect);
240}
241
242
243#include "kbreadcrumbselectionmodel.moc"
KBreadcrumbSelectionModel
Selects the parents of selected items to create breadcrumbs.
QAbstractItemModel
QItemSelectionModel
QObject
kbreadcrumbselectionmodel.h
kdebug.h
KStandardShortcut::end
const KShortcut & end()
Goto end of the document.
Definition: kstandardshortcut.cpp:348
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal