]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistselectionmanager.cpp
Keep current item and selection when resorting, part 1
[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::hasSelection() const
99 {
100 return !m_selectedItems.isEmpty() || (m_isAnchoredSelectionActive && m_anchorItem != m_currentItem);
101 }
102
103 void KItemListSelectionManager::setSelected(int index, int count, SelectionMode mode)
104 {
105 if (index < 0 || count < 1 || !m_model || index >= m_model->count()) {
106 return;
107 }
108
109 endAnchoredSelection();
110 const QSet<int> previous = selectedItems();
111
112 count = qMin(count, m_model->count() - index);
113
114 const int endIndex = index + count -1;
115 switch (mode) {
116 case Select:
117 for (int i = index; i <= endIndex; ++i) {
118 m_selectedItems.insert(i);
119 }
120 break;
121
122 case Deselect:
123 for (int i = index; i <= endIndex; ++i) {
124 m_selectedItems.remove(i);
125 }
126 break;
127
128 case Toggle:
129 for (int i = index; i <= endIndex; ++i) {
130 if (m_selectedItems.contains(i)) {
131 m_selectedItems.remove(i);
132 } else {
133 m_selectedItems.insert(i);
134 }
135 }
136 break;
137
138 default:
139 Q_ASSERT(false);
140 break;
141 }
142
143 const QSet<int> selection = selectedItems();
144 if (selection != previous) {
145 emit selectionChanged(selection, previous);
146 }
147 }
148
149 void KItemListSelectionManager::clearSelection()
150 {
151 const QSet<int> previous = selectedItems();
152 if (!previous.isEmpty()) {
153 m_selectedItems.clear();
154 m_isAnchoredSelectionActive = false;
155 emit selectionChanged(QSet<int>(), previous);
156 }
157 }
158
159 void KItemListSelectionManager::beginAnchoredSelection(int anchor)
160 {
161 if (anchor >= 0 && m_model && anchor < m_model->count()) {
162 m_isAnchoredSelectionActive = true;
163 m_anchorItem = anchor;
164 }
165 }
166
167 void KItemListSelectionManager::endAnchoredSelection()
168 {
169 if (m_isAnchoredSelectionActive && (m_anchorItem != m_currentItem)) {
170 Q_ASSERT(m_anchorItem >= 0);
171 Q_ASSERT(m_currentItem >= 0);
172 const int from = qMin(m_anchorItem, m_currentItem);
173 const int to = qMax(m_anchorItem, m_currentItem);
174
175 for (int index = from; index <= to; ++index) {
176 m_selectedItems.insert(index);
177 }
178 }
179
180 m_isAnchoredSelectionActive = false;
181 }
182
183 bool KItemListSelectionManager::isAnchoredSelectionActive() const
184 {
185 return m_isAnchoredSelectionActive;
186 }
187
188 KItemModelBase* KItemListSelectionManager::model() const
189 {
190 return m_model;
191 }
192
193 void KItemListSelectionManager::setModel(KItemModelBase* model)
194 {
195 m_model = model;
196 if (model && model->count() > 0) {
197 m_currentItem = 0;
198 }
199 }
200
201 void KItemListSelectionManager::itemsInserted(const KItemRangeList& itemRanges)
202 {
203 // Store the current selection (needed in the selectionChanged() signal)
204 const QSet<int> previousSelection = selectedItems();
205
206 // Update the current item
207 if (m_currentItem < 0) {
208 setCurrentItem(0);
209 } else {
210 const int previousCurrent = m_currentItem;
211 int inc = 0;
212 foreach (const KItemRange& itemRange, itemRanges) {
213 if (m_currentItem < itemRange.index) {
214 break;
215 }
216 inc += itemRange.count;
217 }
218 // Calling setCurrentItem would trigger the selectionChanged signal, but we want to
219 // emit it only once in this function -> change the current item manually and emit currentChanged
220 m_currentItem += inc;
221 emit currentChanged(m_currentItem, previousCurrent);
222 }
223
224 // Update the anchor item
225 if (m_anchorItem < 0) {
226 m_anchorItem = 0;
227 } else {
228 int inc = 0;
229 foreach (const KItemRange& itemRange, itemRanges) {
230 if (m_anchorItem < itemRange.index) {
231 break;
232 }
233 inc += itemRange.count;
234 }
235 m_anchorItem += inc;
236 }
237
238 // Update the selections
239 if (!m_selectedItems.isEmpty()) {
240 const QSet<int> previous = m_selectedItems;
241 m_selectedItems.clear();
242 m_selectedItems.reserve(previous.count());
243 QSetIterator<int> it(previous);
244 while (it.hasNext()) {
245 const int index = it.next();
246 int inc = 0;
247 foreach (const KItemRange& itemRange, itemRanges) {
248 if (index < itemRange.index) {
249 break;
250 }
251 inc += itemRange.count;
252 }
253 m_selectedItems.insert(index + inc);
254 }
255 }
256
257 const QSet<int> selection = selectedItems();
258 if (selection != previousSelection) {
259 emit selectionChanged(selection, previousSelection);
260 }
261 }
262
263 void KItemListSelectionManager::itemsRemoved(const KItemRangeList& itemRanges)
264 {
265 // Store the current selection (needed in the selectionChanged() signal)
266 const QSet<int> previousSelection = selectedItems();
267
268 // Update the current item
269 if (m_currentItem >= 0) {
270 const int previousCurrent = m_currentItem;
271 int currentItem = m_currentItem;
272 foreach (const KItemRange& itemRange, itemRanges) {
273 if (currentItem < itemRange.index) {
274 break;
275 }
276 if (currentItem >= itemRange.index + itemRange.count) {
277 currentItem -= itemRange.count;
278 } else if (currentItem >= m_model->count()) {
279 currentItem = m_model->count() - 1;
280 }
281 }
282 // Calling setCurrentItem would trigger the selectionChanged signal, but we want to
283 // emit it only once in this function -> change the current item manually and emit currentChanged
284 m_currentItem = currentItem;
285 emit currentChanged(m_currentItem, previousCurrent);
286 }
287
288 // Update the anchor item
289 if (m_anchorItem >= 0) {
290 int anchorItem = m_anchorItem;
291 foreach (const KItemRange& itemRange, itemRanges) {
292 if (anchorItem < itemRange.index) {
293 break;
294 }
295 if (anchorItem >= itemRange.index + itemRange.count) {
296 anchorItem -= itemRange.count;
297 } else if (anchorItem >= m_model->count()) {
298 anchorItem = m_model->count() - 1;
299 }
300 }
301 m_anchorItem = anchorItem;
302 if (m_anchorItem < 0) {
303 m_isAnchoredSelectionActive = false;
304 }
305 }
306
307 // Update the selections
308 if (!m_selectedItems.isEmpty()) {
309 const QSet<int> previous = m_selectedItems;
310 m_selectedItems.clear();
311 m_selectedItems.reserve(previous.count());
312 QSetIterator<int> it(previous);
313 while (it.hasNext()) {
314 int index = it.next();
315 int dec = 0;
316 foreach (const KItemRange& itemRange, itemRanges) {
317 if (index < itemRange.index) {
318 break;
319 }
320
321 if (index < itemRange.index + itemRange.count) {
322 // The selection is part of the removed range
323 // and will get deleted
324 index = -1;
325 break;
326 }
327
328 dec += itemRange.count;
329 }
330 index -= dec;
331 if (index >= 0) {
332 m_selectedItems.insert(index);
333 }
334 }
335 }
336
337 const QSet<int> selection = selectedItems();
338 if (selection != previousSelection) {
339 emit selectionChanged(selection, previousSelection);
340 }
341 }
342
343 void KItemListSelectionManager::itemsMoved(const KItemRange& itemRange, const QList<int>& movedToIndexes)
344 {
345 // Store the current selection (needed in the selectionChanged() signal)
346 const QSet<int> previousSelection = selectedItems();
347
348 // Update the current item
349 if (m_currentItem >= itemRange.index && m_currentItem < itemRange.index + itemRange.count) {
350 const int previousCurrentItem = m_currentItem;
351 const int newCurrentItem = movedToIndexes.at(previousCurrentItem - itemRange.index);
352
353 // Calling setCurrentItem would trigger the selectionChanged signal, but we want to
354 // emit it only once in this function -> change the current item manually and emit currentChanged
355 m_currentItem = newCurrentItem;
356 emit currentChanged(newCurrentItem, previousCurrentItem);
357 }
358
359 // Update the anchor item
360 if (m_anchorItem >= itemRange.index && m_anchorItem < itemRange.index + itemRange.count) {
361 m_anchorItem = movedToIndexes.at(m_anchorItem - itemRange.index);
362 }
363
364 // Update the selections
365 if (!m_selectedItems.isEmpty()) {
366 const QSet<int> previous = m_selectedItems;
367 m_selectedItems.clear();
368 m_selectedItems.reserve(previous.count());
369 QSetIterator<int> it(previous);
370 while (it.hasNext()) {
371 const int index = it.next();
372 if (index >= itemRange.index && index < itemRange.index + itemRange.count) {
373 m_selectedItems.insert(movedToIndexes.at(index - itemRange.index));
374 }
375 else {
376 m_selectedItems.insert(index);
377 }
378 }
379 }
380
381 const QSet<int> selection = selectedItems();
382 if (selection != previousSelection) {
383 emit selectionChanged(selection, previousSelection);
384 }
385 }
386
387 #include "kitemlistselectionmanager.moc"