]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistselectionmanager.cpp
Merge branch 'KDE/4.10'
[dolphin.git] / src / kitemviews / kitemlistselectionmanager.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2011 by Frank Reininghaus <frank78ac@googlemail.com> *
4 * *
5 * Based on the Itemviews NG project from Trolltech Labs: *
6 * http://qt.gitorious.org/qt-labs/itemviews-ng *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
22 ***************************************************************************/
23
24 #include "kitemlistselectionmanager.h"
25
26 #include "kitemmodelbase.h"
27 #include <KDebug>
28
29 KItemListSelectionManager::KItemListSelectionManager(QObject* parent) :
30 QObject(parent),
31 m_currentItem(-1),
32 m_anchorItem(-1),
33 m_selectedItems(),
34 m_isAnchoredSelectionActive(false),
35 m_model(0)
36 {
37 }
38
39 KItemListSelectionManager::~KItemListSelectionManager()
40 {
41 }
42
43 void KItemListSelectionManager::setCurrentItem(int current)
44 {
45 const int previous = m_currentItem;
46 const QSet<int> previousSelection = selectedItems();
47
48 if (m_model && current >= 0 && current < m_model->count()) {
49 m_currentItem = current;
50 } else {
51 m_currentItem = -1;
52 }
53
54 if (m_currentItem != previous) {
55 emit currentChanged(m_currentItem, previous);
56
57 if (m_isAnchoredSelectionActive) {
58 const QSet<int> selection = selectedItems();
59 if (selection != previousSelection) {
60 emit selectionChanged(selection, previousSelection);
61 }
62 }
63 }
64 }
65
66 int KItemListSelectionManager::currentItem() const
67 {
68 return m_currentItem;
69 }
70
71 void KItemListSelectionManager::setSelectedItems(const QSet<int>& items)
72 {
73 if (m_selectedItems != items) {
74 const QSet<int> previous = m_selectedItems;
75 m_selectedItems = items;
76 emit selectionChanged(m_selectedItems, previous);
77 }
78 }
79
80 QSet<int> KItemListSelectionManager::selectedItems() const
81 {
82 QSet<int> selectedItems = m_selectedItems;
83
84 if (m_isAnchoredSelectionActive && m_anchorItem != m_currentItem) {
85 Q_ASSERT(m_anchorItem >= 0);
86 Q_ASSERT(m_currentItem >= 0);
87 const int from = qMin(m_anchorItem, m_currentItem);
88 const int to = qMax(m_anchorItem, m_currentItem);
89
90 for (int index = from; index <= to; ++index) {
91 selectedItems.insert(index);
92 }
93 }
94
95 return selectedItems;
96 }
97
98 bool KItemListSelectionManager::isSelected(int index) const
99 {
100 if (m_selectedItems.contains(index)) {
101 return true;
102 }
103
104 if (m_isAnchoredSelectionActive && m_anchorItem != m_currentItem) {
105 Q_ASSERT(m_anchorItem >= 0);
106 Q_ASSERT(m_currentItem >= 0);
107 const int from = qMin(m_anchorItem, m_currentItem);
108 const int to = qMax(m_anchorItem, m_currentItem);
109
110 if (from <= index && index <= to) {
111 return true;
112 }
113 }
114
115 return false;
116 }
117
118 bool KItemListSelectionManager::hasSelection() const
119 {
120 return !m_selectedItems.isEmpty() || (m_isAnchoredSelectionActive && m_anchorItem != m_currentItem);
121 }
122
123 void KItemListSelectionManager::setSelected(int index, int count, SelectionMode mode)
124 {
125 if (index < 0 || count < 1 || !m_model || index >= m_model->count()) {
126 return;
127 }
128
129 endAnchoredSelection();
130 const QSet<int> previous = selectedItems();
131
132 count = qMin(count, m_model->count() - index);
133
134 const int endIndex = index + count -1;
135 switch (mode) {
136 case Select:
137 for (int i = index; i <= endIndex; ++i) {
138 m_selectedItems.insert(i);
139 }
140 break;
141
142 case Deselect:
143 for (int i = index; i <= endIndex; ++i) {
144 m_selectedItems.remove(i);
145 }
146 break;
147
148 case Toggle:
149 for (int i = index; i <= endIndex; ++i) {
150 if (m_selectedItems.contains(i)) {
151 m_selectedItems.remove(i);
152 } else {
153 m_selectedItems.insert(i);
154 }
155 }
156 break;
157
158 default:
159 Q_ASSERT(false);
160 break;
161 }
162
163 const QSet<int> selection = selectedItems();
164 if (selection != previous) {
165 emit selectionChanged(selection, previous);
166 }
167 }
168
169 void KItemListSelectionManager::clearSelection()
170 {
171 const QSet<int> previous = selectedItems();
172 if (!previous.isEmpty()) {
173 m_selectedItems.clear();
174 m_isAnchoredSelectionActive = false;
175 emit selectionChanged(QSet<int>(), previous);
176 }
177 }
178
179 void KItemListSelectionManager::beginAnchoredSelection(int anchor)
180 {
181 if (anchor >= 0 && m_model && anchor < m_model->count()) {
182 m_isAnchoredSelectionActive = true;
183 m_anchorItem = anchor;
184 }
185 }
186
187 void KItemListSelectionManager::endAnchoredSelection()
188 {
189 if (m_isAnchoredSelectionActive && (m_anchorItem != m_currentItem)) {
190 Q_ASSERT(m_anchorItem >= 0);
191 Q_ASSERT(m_currentItem >= 0);
192 const int from = qMin(m_anchorItem, m_currentItem);
193 const int to = qMax(m_anchorItem, m_currentItem);
194
195 for (int index = from; index <= to; ++index) {
196 m_selectedItems.insert(index);
197 }
198 }
199
200 m_isAnchoredSelectionActive = false;
201 }
202
203 bool KItemListSelectionManager::isAnchoredSelectionActive() const
204 {
205 return m_isAnchoredSelectionActive;
206 }
207
208 KItemModelBase* KItemListSelectionManager::model() const
209 {
210 return m_model;
211 }
212
213 void KItemListSelectionManager::setModel(KItemModelBase* model)
214 {
215 m_model = model;
216 if (model && model->count() > 0) {
217 m_currentItem = 0;
218 }
219 }
220
221 void KItemListSelectionManager::itemsInserted(const KItemRangeList& itemRanges)
222 {
223 // Store the current selection (needed in the selectionChanged() signal)
224 const QSet<int> previousSelection = selectedItems();
225
226 // Update the current item
227 if (m_currentItem < 0) {
228 setCurrentItem(0);
229 } else {
230 const int previousCurrent = m_currentItem;
231 int inc = 0;
232 foreach (const KItemRange& itemRange, itemRanges) {
233 if (m_currentItem < itemRange.index) {
234 break;
235 }
236 inc += itemRange.count;
237 }
238 // Calling setCurrentItem would trigger the selectionChanged signal, but we want to
239 // emit it only once in this function -> change the current item manually and emit currentChanged
240 m_currentItem += inc;
241 emit currentChanged(m_currentItem, previousCurrent);
242 }
243
244 // Update the anchor item
245 if (m_anchorItem < 0) {
246 m_anchorItem = 0;
247 } else {
248 int inc = 0;
249 foreach (const KItemRange& itemRange, itemRanges) {
250 if (m_anchorItem < itemRange.index) {
251 break;
252 }
253 inc += itemRange.count;
254 }
255 m_anchorItem += inc;
256 }
257
258 // Update the selections
259 if (!m_selectedItems.isEmpty()) {
260 const QSet<int> previous = m_selectedItems;
261 m_selectedItems.clear();
262 m_selectedItems.reserve(previous.count());
263 QSetIterator<int> it(previous);
264 while (it.hasNext()) {
265 const int index = it.next();
266 int inc = 0;
267 foreach (const KItemRange& itemRange, itemRanges) {
268 if (index < itemRange.index) {
269 break;
270 }
271 inc += itemRange.count;
272 }
273 m_selectedItems.insert(index + inc);
274 }
275 }
276
277 const QSet<int> selection = selectedItems();
278 if (selection != previousSelection) {
279 emit selectionChanged(selection, previousSelection);
280 }
281 }
282
283 void KItemListSelectionManager::itemsRemoved(const KItemRangeList& itemRanges)
284 {
285 // Store the current selection (needed in the selectionChanged() signal)
286 const QSet<int> previousSelection = selectedItems();
287 const int previousCurrent = m_currentItem;
288
289 // Update the current item
290 m_currentItem = indexAfterRangesRemoving(m_currentItem, itemRanges, DiscardRemovedIndex);
291 if (m_currentItem != previousCurrent) {
292 emit currentChanged(m_currentItem, previousCurrent);
293 if (m_currentItem < 0) {
294 // Calling setCurrentItem() would trigger the selectionChanged signal, but we want to
295 // emit it only once in this function -> change the current item manually and emit currentChanged
296 m_currentItem = indexAfterRangesRemoving(previousCurrent, itemRanges, AdjustRemovedIndex);
297 emit currentChanged(m_currentItem, -1);
298 }
299 }
300
301 // Update the anchor item
302 if (m_anchorItem >= 0) {
303 m_anchorItem = indexAfterRangesRemoving(m_anchorItem, itemRanges, DiscardRemovedIndex);
304 if (m_anchorItem < 0) {
305 m_isAnchoredSelectionActive = false;
306 }
307 }
308
309 // Update the selections and the anchor item
310 if (!m_selectedItems.isEmpty()) {
311 const QSet<int> previous = m_selectedItems;
312 m_selectedItems.clear();
313 m_selectedItems.reserve(previous.count());
314 QSetIterator<int> it(previous);
315 while (it.hasNext()) {
316 const int index = indexAfterRangesRemoving(it.next(), itemRanges, DiscardRemovedIndex);
317 if (index >= 0) {
318 m_selectedItems.insert(index);
319 }
320 }
321 }
322
323 const QSet<int> selection = selectedItems();
324 if (selection != previousSelection) {
325 emit selectionChanged(selection, previousSelection);
326 }
327
328 Q_ASSERT(m_currentItem < m_model->count());
329 Q_ASSERT(m_anchorItem < m_model->count());
330 }
331
332 void KItemListSelectionManager::itemsMoved(const KItemRange& itemRange, const QList<int>& movedToIndexes)
333 {
334 // Store the current selection (needed in the selectionChanged() signal)
335 const QSet<int> previousSelection = selectedItems();
336
337 // Update the current item
338 if (m_currentItem >= itemRange.index && m_currentItem < itemRange.index + itemRange.count) {
339 const int previousCurrentItem = m_currentItem;
340 const int newCurrentItem = movedToIndexes.at(previousCurrentItem - itemRange.index);
341
342 // Calling setCurrentItem would trigger the selectionChanged signal, but we want to
343 // emit it only once in this function -> change the current item manually and emit currentChanged
344 m_currentItem = newCurrentItem;
345 emit currentChanged(newCurrentItem, previousCurrentItem);
346 }
347
348 // Update the anchor item
349 if (m_anchorItem >= itemRange.index && m_anchorItem < itemRange.index + itemRange.count) {
350 m_anchorItem = movedToIndexes.at(m_anchorItem - itemRange.index);
351 }
352
353 // Update the selections
354 if (!m_selectedItems.isEmpty()) {
355 const QSet<int> previous = m_selectedItems;
356 m_selectedItems.clear();
357 m_selectedItems.reserve(previous.count());
358 QSetIterator<int> it(previous);
359 while (it.hasNext()) {
360 const int index = it.next();
361 if (index >= itemRange.index && index < itemRange.index + itemRange.count) {
362 m_selectedItems.insert(movedToIndexes.at(index - itemRange.index));
363 }
364 else {
365 m_selectedItems.insert(index);
366 }
367 }
368 }
369
370 const QSet<int> selection = selectedItems();
371 if (selection != previousSelection) {
372 emit selectionChanged(selection, previousSelection);
373 }
374 }
375
376 int KItemListSelectionManager::indexAfterRangesRemoving(int index, const KItemRangeList& itemRanges,
377 const RangesRemovingBehaviour behaviour) const
378 {
379 int dec = 0;
380 foreach (const KItemRange& itemRange, itemRanges) {
381 if (index < itemRange.index) {
382 break;
383 }
384
385 dec += itemRange.count;
386
387 const int firstIndexAfterRange = itemRange.index + itemRange.count;
388 if (index < firstIndexAfterRange) {
389 // The index is part of the removed range
390 if (behaviour == DiscardRemovedIndex) {
391 return -1;
392 } else {
393 // Use the first item after the range as new index
394 index = firstIndexAfterRange;
395 break;
396 }
397 }
398 }
399 return qBound(-1, index - dec, m_model->count() - 1);
400 }
401 #include "kitemlistselectionmanager.moc"