]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistselectionmanager.cpp
Hide tooltip instantly on filter change
[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 KItemListSelectionManager::KItemListSelectionManager(QObject* parent) :
27 QObject(parent),
28 m_currentItem(-1),
29 m_anchorItem(-1),
30 m_selectedItems(),
31 m_isAnchoredSelectionActive(false),
32 m_model(nullptr)
33 {
34 }
35
36 KItemListSelectionManager::~KItemListSelectionManager()
37 {
38 }
39
40 void KItemListSelectionManager::setCurrentItem(int current)
41 {
42 const int previous = m_currentItem;
43 const KItemSet previousSelection = selectedItems();
44
45 if (m_model && current >= 0 && current < m_model->count()) {
46 m_currentItem = current;
47 } else {
48 m_currentItem = -1;
49 }
50
51 if (m_currentItem != previous) {
52 emit currentChanged(m_currentItem, previous);
53
54 if (m_isAnchoredSelectionActive) {
55 const KItemSet selection = selectedItems();
56 if (selection != previousSelection) {
57 emit selectionChanged(selection, previousSelection);
58 }
59 }
60 }
61 }
62
63 int KItemListSelectionManager::currentItem() const
64 {
65 return m_currentItem;
66 }
67
68 void KItemListSelectionManager::setSelectedItems(const KItemSet& items)
69 {
70 if (m_selectedItems != items) {
71 const KItemSet previous = m_selectedItems;
72 m_selectedItems = items;
73 emit selectionChanged(m_selectedItems, previous);
74 }
75 }
76
77 KItemSet KItemListSelectionManager::selectedItems() const
78 {
79 KItemSet selectedItems = m_selectedItems;
80
81 if (m_isAnchoredSelectionActive && m_anchorItem != m_currentItem) {
82 Q_ASSERT(m_anchorItem >= 0);
83 Q_ASSERT(m_currentItem >= 0);
84 const int from = qMin(m_anchorItem, m_currentItem);
85 const int to = qMax(m_anchorItem, m_currentItem);
86
87 for (int index = from; index <= to; ++index) {
88 selectedItems.insert(index);
89 }
90 }
91
92 return selectedItems;
93 }
94
95 bool KItemListSelectionManager::isSelected(int index) const
96 {
97 if (m_selectedItems.contains(index)) {
98 return true;
99 }
100
101 if (m_isAnchoredSelectionActive && m_anchorItem != m_currentItem) {
102 Q_ASSERT(m_anchorItem >= 0);
103 Q_ASSERT(m_currentItem >= 0);
104 const int from = qMin(m_anchorItem, m_currentItem);
105 const int to = qMax(m_anchorItem, m_currentItem);
106
107 if (from <= index && index <= to) {
108 return true;
109 }
110 }
111
112 return false;
113 }
114
115 bool KItemListSelectionManager::hasSelection() const
116 {
117 return !m_selectedItems.isEmpty() || (m_isAnchoredSelectionActive && m_anchorItem != m_currentItem);
118 }
119
120 void KItemListSelectionManager::setSelected(int index, int count, SelectionMode mode)
121 {
122 if (index < 0 || count < 1 || !m_model || index >= m_model->count()) {
123 return;
124 }
125
126 endAnchoredSelection();
127 const KItemSet previous = selectedItems();
128
129 count = qMin(count, m_model->count() - index);
130
131 const int endIndex = index + count -1;
132 switch (mode) {
133 case Select:
134 for (int i = index; i <= endIndex; ++i) {
135 m_selectedItems.insert(i);
136 }
137 break;
138
139 case Deselect:
140 for (int i = index; i <= endIndex; ++i) {
141 m_selectedItems.remove(i);
142 }
143 break;
144
145 case Toggle:
146 for (int i = index; i <= endIndex; ++i) {
147 if (m_selectedItems.contains(i)) {
148 m_selectedItems.remove(i);
149 } else {
150 m_selectedItems.insert(i);
151 }
152 }
153 break;
154
155 default:
156 Q_ASSERT(false);
157 break;
158 }
159
160 const KItemSet selection = selectedItems();
161 if (selection != previous) {
162 emit selectionChanged(selection, previous);
163 }
164 }
165
166 void KItemListSelectionManager::clearSelection()
167 {
168 const KItemSet previous = selectedItems();
169 if (!previous.isEmpty()) {
170 m_selectedItems.clear();
171 m_isAnchoredSelectionActive = false;
172 emit selectionChanged(KItemSet(), previous);
173 }
174 }
175
176 void KItemListSelectionManager::replaceSelection(int index, int count)
177 {
178 const KItemSet previous = selectedItems();
179 if (!previous.isEmpty()) {
180 m_selectedItems.clear();
181 m_isAnchoredSelectionActive = false;
182 }
183 setSelected(index, count);
184 }
185
186 void KItemListSelectionManager::beginAnchoredSelection(int anchor)
187 {
188 if (anchor >= 0 && m_model && anchor < m_model->count()) {
189 m_isAnchoredSelectionActive = true;
190 m_anchorItem = anchor;
191 }
192 }
193
194 void KItemListSelectionManager::endAnchoredSelection()
195 {
196 if (m_isAnchoredSelectionActive && (m_anchorItem != m_currentItem)) {
197 Q_ASSERT(m_anchorItem >= 0);
198 Q_ASSERT(m_currentItem >= 0);
199 const int from = qMin(m_anchorItem, m_currentItem);
200 const int to = qMax(m_anchorItem, m_currentItem);
201
202 for (int index = from; index <= to; ++index) {
203 m_selectedItems.insert(index);
204 }
205 }
206
207 m_isAnchoredSelectionActive = false;
208 }
209
210 bool KItemListSelectionManager::isAnchoredSelectionActive() const
211 {
212 return m_isAnchoredSelectionActive;
213 }
214
215 KItemModelBase* KItemListSelectionManager::model() const
216 {
217 return m_model;
218 }
219
220 void KItemListSelectionManager::setModel(KItemModelBase* model)
221 {
222 m_model = model;
223 if (model && model->count() > 0) {
224 m_currentItem = 0;
225 }
226 }
227
228 void KItemListSelectionManager::itemsInserted(const KItemRangeList& itemRanges)
229 {
230 // Store the current selection (needed in the selectionChanged() signal)
231 const KItemSet previousSelection = selectedItems();
232
233 // Update the current item
234 if (m_currentItem < 0) {
235 setCurrentItem(0);
236 } else {
237 const int previousCurrent = m_currentItem;
238 int inc = 0;
239 foreach (const KItemRange& itemRange, itemRanges) {
240 if (m_currentItem < itemRange.index) {
241 break;
242 }
243 inc += itemRange.count;
244 }
245 // Calling setCurrentItem would trigger the selectionChanged signal, but we want to
246 // emit it only once in this function -> change the current item manually and emit currentChanged
247 m_currentItem += inc;
248 if (m_currentItem >= m_model->count()) {
249 m_currentItem = -1;
250 }
251 emit currentChanged(m_currentItem, previousCurrent);
252 }
253
254 // Update the anchor item
255 if (m_anchorItem < 0) {
256 m_anchorItem = 0;
257 } else {
258 int inc = 0;
259 foreach (const KItemRange& itemRange, itemRanges) {
260 if (m_anchorItem < itemRange.index) {
261 break;
262 }
263 inc += itemRange.count;
264 }
265 m_anchorItem += inc;
266 }
267
268 // Update the selections
269 if (!m_selectedItems.isEmpty()) {
270 const KItemSet previous = m_selectedItems;
271 m_selectedItems.clear();
272
273 for (int index: previous) {
274 int inc = 0;
275 foreach (const KItemRange& itemRange, itemRanges) {
276 if (index < itemRange.index) {
277 break;
278 }
279 inc += itemRange.count;
280 }
281 m_selectedItems.insert(index + inc);
282 }
283 }
284
285 const KItemSet selection = selectedItems();
286 if (selection != previousSelection) {
287 emit selectionChanged(selection, previousSelection);
288 }
289 }
290
291 void KItemListSelectionManager::itemsRemoved(const KItemRangeList& itemRanges)
292 {
293 // Store the current selection (needed in the selectionChanged() signal)
294 const KItemSet previousSelection = selectedItems();
295 const int previousCurrent = m_currentItem;
296
297 // Update the current item
298 m_currentItem = indexAfterRangesRemoving(m_currentItem, itemRanges, DiscardRemovedIndex);
299 if (m_currentItem != previousCurrent) {
300 emit currentChanged(m_currentItem, previousCurrent);
301 if (m_currentItem < 0) {
302 // Calling setCurrentItem() would trigger the selectionChanged signal, but we want to
303 // emit it only once in this function -> change the current item manually and emit currentChanged
304 m_currentItem = indexAfterRangesRemoving(previousCurrent, itemRanges, AdjustRemovedIndex);
305 emit currentChanged(m_currentItem, -1);
306 }
307 }
308
309 // Update the anchor item
310 if (m_anchorItem >= 0) {
311 m_anchorItem = indexAfterRangesRemoving(m_anchorItem, itemRanges, DiscardRemovedIndex);
312 if (m_anchorItem < 0) {
313 m_isAnchoredSelectionActive = false;
314 }
315 }
316
317 // Update the selections and the anchor item
318 if (!m_selectedItems.isEmpty()) {
319 const KItemSet previous = m_selectedItems;
320 m_selectedItems.clear();
321
322 for (int oldIndex : previous) {
323 const int index = indexAfterRangesRemoving(oldIndex, itemRanges, DiscardRemovedIndex);
324 if (index >= 0) {
325 m_selectedItems.insert(index);
326 }
327 }
328 }
329
330 const KItemSet selection = selectedItems();
331 if (selection != previousSelection) {
332 emit selectionChanged(selection, previousSelection);
333 }
334
335 Q_ASSERT(m_currentItem < m_model->count());
336 Q_ASSERT(m_anchorItem < m_model->count());
337 }
338
339 void KItemListSelectionManager::itemsMoved(const KItemRange& itemRange, const QList<int>& movedToIndexes)
340 {
341 // Store the current selection (needed in the selectionChanged() signal)
342 const KItemSet previousSelection = selectedItems();
343
344 // Store whether we were doing an anchored selection
345 const bool wasInAnchoredSelection = isAnchoredSelectionActive();
346
347 // endAnchoredSelection() adds all items between m_currentItem and
348 // m_anchorItem to m_selectedItems. They can then be moved
349 // individually later in this function.
350 endAnchoredSelection();
351
352 // Update the current item
353 if (m_currentItem >= itemRange.index && m_currentItem < itemRange.index + itemRange.count) {
354 const int previousCurrentItem = m_currentItem;
355 const int newCurrentItem = movedToIndexes.at(previousCurrentItem - itemRange.index);
356
357 // Calling setCurrentItem would trigger the selectionChanged signal, but we want to
358 // emit it only once in this function -> change the current item manually and emit currentChanged
359 m_currentItem = newCurrentItem;
360 emit currentChanged(newCurrentItem, previousCurrentItem);
361 }
362
363 // Start a new anchored selection.
364 if (wasInAnchoredSelection) {
365 beginAnchoredSelection(m_currentItem);
366 }
367
368 // Update the selections
369 if (!m_selectedItems.isEmpty()) {
370 const KItemSet previous = m_selectedItems;
371 m_selectedItems.clear();
372
373 for (int index : previous) {
374 if (index >= itemRange.index && index < itemRange.index + itemRange.count) {
375 m_selectedItems.insert(movedToIndexes.at(index - itemRange.index));
376 }
377 else {
378 m_selectedItems.insert(index);
379 }
380 }
381 }
382
383 const KItemSet selection = selectedItems();
384 if (selection != previousSelection) {
385 emit selectionChanged(selection, previousSelection);
386 }
387 }
388
389 int KItemListSelectionManager::indexAfterRangesRemoving(int index, const KItemRangeList& itemRanges,
390 const RangesRemovingBehaviour behaviour) const
391 {
392 int dec = 0;
393 foreach (const KItemRange& itemRange, itemRanges) {
394 if (index < itemRange.index) {
395 break;
396 }
397
398 dec += itemRange.count;
399
400 const int firstIndexAfterRange = itemRange.index + itemRange.count;
401 if (index < firstIndexAfterRange) {
402 // The index is part of the removed range
403 if (behaviour == DiscardRemovedIndex) {
404 return -1;
405 } else {
406 // Use the first item after the range as new index
407 index = firstIndexAfterRange;
408 break;
409 }
410 }
411 }
412 return qBound(-1, index - dec, m_model->count() - 1);
413 }
414