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