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