]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistselectionmanager.cpp
Remove unused #include
[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::beginAnchoredSelection(int anchor)
177 {
178 if (anchor >= 0 && m_model && anchor < m_model->count()) {
179 m_isAnchoredSelectionActive = true;
180 m_anchorItem = anchor;
181 }
182 }
183
184 void KItemListSelectionManager::endAnchoredSelection()
185 {
186 if (m_isAnchoredSelectionActive && (m_anchorItem != m_currentItem)) {
187 Q_ASSERT(m_anchorItem >= 0);
188 Q_ASSERT(m_currentItem >= 0);
189 const int from = qMin(m_anchorItem, m_currentItem);
190 const int to = qMax(m_anchorItem, m_currentItem);
191
192 for (int index = from; index <= to; ++index) {
193 m_selectedItems.insert(index);
194 }
195 }
196
197 m_isAnchoredSelectionActive = false;
198 }
199
200 bool KItemListSelectionManager::isAnchoredSelectionActive() const
201 {
202 return m_isAnchoredSelectionActive;
203 }
204
205 KItemModelBase* KItemListSelectionManager::model() const
206 {
207 return m_model;
208 }
209
210 void KItemListSelectionManager::setModel(KItemModelBase* model)
211 {
212 m_model = model;
213 if (model && model->count() > 0) {
214 m_currentItem = 0;
215 }
216 }
217
218 void KItemListSelectionManager::itemsInserted(const KItemRangeList& itemRanges)
219 {
220 // Store the current selection (needed in the selectionChanged() signal)
221 const KItemSet previousSelection = selectedItems();
222
223 // Update the current item
224 if (m_currentItem < 0) {
225 setCurrentItem(0);
226 } else {
227 const int previousCurrent = m_currentItem;
228 int inc = 0;
229 foreach (const KItemRange& itemRange, itemRanges) {
230 if (m_currentItem < itemRange.index) {
231 break;
232 }
233 inc += itemRange.count;
234 }
235 // Calling setCurrentItem would trigger the selectionChanged signal, but we want to
236 // emit it only once in this function -> change the current item manually and emit currentChanged
237 m_currentItem += inc;
238 emit currentChanged(m_currentItem, previousCurrent);
239 }
240
241 // Update the anchor item
242 if (m_anchorItem < 0) {
243 m_anchorItem = 0;
244 } else {
245 int inc = 0;
246 foreach (const KItemRange& itemRange, itemRanges) {
247 if (m_anchorItem < itemRange.index) {
248 break;
249 }
250 inc += itemRange.count;
251 }
252 m_anchorItem += inc;
253 }
254
255 // Update the selections
256 if (!m_selectedItems.isEmpty()) {
257 const KItemSet previous = m_selectedItems;
258 m_selectedItems.clear();
259
260 for (int index: previous) {
261 int inc = 0;
262 foreach (const KItemRange& itemRange, itemRanges) {
263 if (index < itemRange.index) {
264 break;
265 }
266 inc += itemRange.count;
267 }
268 m_selectedItems.insert(index + inc);
269 }
270 }
271
272 const KItemSet selection = selectedItems();
273 if (selection != previousSelection) {
274 emit selectionChanged(selection, previousSelection);
275 }
276 }
277
278 void KItemListSelectionManager::itemsRemoved(const KItemRangeList& itemRanges)
279 {
280 // Store the current selection (needed in the selectionChanged() signal)
281 const KItemSet previousSelection = selectedItems();
282 const int previousCurrent = m_currentItem;
283
284 // Update the current item
285 m_currentItem = indexAfterRangesRemoving(m_currentItem, itemRanges, DiscardRemovedIndex);
286 if (m_currentItem != previousCurrent) {
287 emit currentChanged(m_currentItem, previousCurrent);
288 if (m_currentItem < 0) {
289 // Calling setCurrentItem() would trigger the selectionChanged signal, but we want to
290 // emit it only once in this function -> change the current item manually and emit currentChanged
291 m_currentItem = indexAfterRangesRemoving(previousCurrent, itemRanges, AdjustRemovedIndex);
292 emit currentChanged(m_currentItem, -1);
293 }
294 }
295
296 // Update the anchor item
297 if (m_anchorItem >= 0) {
298 m_anchorItem = indexAfterRangesRemoving(m_anchorItem, itemRanges, DiscardRemovedIndex);
299 if (m_anchorItem < 0) {
300 m_isAnchoredSelectionActive = false;
301 }
302 }
303
304 // Update the selections and the anchor item
305 if (!m_selectedItems.isEmpty()) {
306 const KItemSet previous = m_selectedItems;
307 m_selectedItems.clear();
308
309 for (int oldIndex : previous) {
310 const int index = indexAfterRangesRemoving(oldIndex, itemRanges, DiscardRemovedIndex);
311 if (index >= 0) {
312 m_selectedItems.insert(index);
313 }
314 }
315 }
316
317 const KItemSet selection = selectedItems();
318 if (selection != previousSelection) {
319 emit selectionChanged(selection, previousSelection);
320 }
321
322 Q_ASSERT(m_currentItem < m_model->count());
323 Q_ASSERT(m_anchorItem < m_model->count());
324 }
325
326 void KItemListSelectionManager::itemsMoved(const KItemRange& itemRange, const QList<int>& movedToIndexes)
327 {
328 // Store the current selection (needed in the selectionChanged() signal)
329 const KItemSet previousSelection = selectedItems();
330
331 // endAnchoredSelection() adds all items between m_currentItem and
332 // m_anchorItem to m_selectedItems. They can then be moved
333 // individually later in this function.
334 endAnchoredSelection();
335
336 // Update the current item
337 if (m_currentItem >= itemRange.index && m_currentItem < itemRange.index + itemRange.count) {
338 const int previousCurrentItem = m_currentItem;
339 const int newCurrentItem = movedToIndexes.at(previousCurrentItem - itemRange.index);
340
341 // Calling setCurrentItem would trigger the selectionChanged signal, but we want to
342 // emit it only once in this function -> change the current item manually and emit currentChanged
343 m_currentItem = newCurrentItem;
344 emit currentChanged(newCurrentItem, previousCurrentItem);
345 }
346
347 // Start a new anchored selection.
348 beginAnchoredSelection(m_currentItem);
349
350 // Update the selections
351 if (!m_selectedItems.isEmpty()) {
352 const KItemSet previous = m_selectedItems;
353 m_selectedItems.clear();
354
355 for (int index : previous) {
356 if (index >= itemRange.index && index < itemRange.index + itemRange.count) {
357 m_selectedItems.insert(movedToIndexes.at(index - itemRange.index));
358 }
359 else {
360 m_selectedItems.insert(index);
361 }
362 }
363 }
364
365 const KItemSet selection = selectedItems();
366 if (selection != previousSelection) {
367 emit selectionChanged(selection, previousSelection);
368 }
369 }
370
371 int KItemListSelectionManager::indexAfterRangesRemoving(int index, const KItemRangeList& itemRanges,
372 const RangesRemovingBehaviour behaviour) const
373 {
374 int dec = 0;
375 foreach (const KItemRange& itemRange, itemRanges) {
376 if (index < itemRange.index) {
377 break;
378 }
379
380 dec += itemRange.count;
381
382 const int firstIndexAfterRange = itemRange.index + itemRange.count;
383 if (index < firstIndexAfterRange) {
384 // The index is part of the removed range
385 if (behaviour == DiscardRemovedIndex) {
386 return -1;
387 } else {
388 // Use the first item after the range as new index
389 index = firstIndexAfterRange;
390 break;
391 }
392 }
393 }
394 return qBound(-1, index - dec, m_model->count() - 1);
395 }
396