]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistselectionmanager.cpp
Update the anchor item when items are added or removed
[dolphin.git] / src / kitemviews / kitemlistselectionmanager.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * Based on the Itemviews NG project from Trolltech Labs: *
5 * http://qt.gitorious.org/qt-labs/itemviews-ng *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
21 ***************************************************************************/
22
23 #include "kitemlistselectionmanager.h"
24
25 #include "kitemmodelbase.h"
26 #include <KDebug>
27
28 KItemListSelectionManager::KItemListSelectionManager(QObject* parent) :
29 QObject(parent),
30 m_currentItem(-1),
31 m_anchorItem(-1),
32 m_selectedItems(),
33 m_model(0)
34 {
35 }
36
37 KItemListSelectionManager::~KItemListSelectionManager()
38 {
39 }
40
41 void KItemListSelectionManager::setCurrentItem(int current)
42 {
43 const int previous = m_currentItem;
44 if (m_model && current >= 0 && current < m_model->count()) {
45 m_currentItem = current;
46 } else {
47 m_currentItem = -1;
48 }
49
50 if (m_currentItem != previous) {
51 emit currentChanged(m_currentItem, previous);
52 }
53 }
54
55 int KItemListSelectionManager::currentItem() const
56 {
57 return m_currentItem;
58 }
59
60 void KItemListSelectionManager::setSelectedItems(const QSet<int>& items)
61 {
62 if (m_selectedItems != items) {
63 const QSet<int> previous = m_selectedItems;
64 m_selectedItems = items;
65 emit selectionChanged(m_selectedItems, previous);
66 }
67 }
68
69 QSet<int> KItemListSelectionManager::selectedItems() const
70 {
71 return m_selectedItems;
72 }
73
74 bool KItemListSelectionManager::hasSelection() const
75 {
76 return !m_selectedItems.isEmpty();
77 }
78
79 void KItemListSelectionManager::setSelected(int index, int count, SelectionMode mode)
80 {
81 if (index < 0 || count < 1 || !m_model || index >= m_model->count()) {
82 return;
83 }
84
85 const QSet<int> previous = m_selectedItems;
86
87 count = qMin(count, m_model->count() - index);
88
89 const int endIndex = index + count -1;
90 switch (mode) {
91 case Select:
92 for (int i = index; i <= endIndex; ++i) {
93 m_selectedItems.insert(i);
94 }
95 break;
96
97 case Deselect:
98 for (int i = index; i <= endIndex; ++i) {
99 m_selectedItems.remove(i);
100 }
101 break;
102
103 case Toggle:
104 for (int i = index; i <= endIndex; ++i) {
105 if (m_selectedItems.contains(i)) {
106 m_selectedItems.remove(i);
107 } else {
108 m_selectedItems.insert(i);
109 }
110 }
111 break;
112
113 default:
114 Q_ASSERT(false);
115 break;
116 }
117
118 if (m_selectedItems != previous) {
119 emit selectionChanged(m_selectedItems, previous);
120 }
121 }
122
123 void KItemListSelectionManager::clearSelection()
124 {
125 if (!m_selectedItems.isEmpty()) {
126 const QSet<int> previous = m_selectedItems;
127 m_selectedItems.clear();
128 emit selectionChanged(m_selectedItems, previous);
129 }
130 }
131
132 void KItemListSelectionManager::beginAnchoredSelection(int anchor, SelectionMode mode)
133 {
134 Q_UNUSED(anchor);
135 Q_UNUSED(mode);
136 }
137
138 void KItemListSelectionManager::endAnchoredSelection()
139 {
140 }
141
142 void KItemListSelectionManager::setAnchorItem(int anchor)
143 {
144 const int previous = m_anchorItem;
145 if (m_model && anchor < m_model->count()) {
146 m_anchorItem = anchor;
147 } else {
148 m_anchorItem = -1;
149 }
150
151 if (m_anchorItem != previous) {
152 emit anchorChanged(m_anchorItem, previous);
153 }
154 }
155
156 int KItemListSelectionManager::anchorItem() const
157 {
158 return m_anchorItem;
159 }
160
161 KItemModelBase* KItemListSelectionManager::model() const
162 {
163 return m_model;
164 }
165
166 void KItemListSelectionManager::setModel(KItemModelBase* model)
167 {
168 m_model = model;
169 if (model && model->count() > 0) {
170 m_currentItem = 0;
171 }
172 }
173
174 void KItemListSelectionManager::itemsInserted(const KItemRangeList& itemRanges)
175 {
176 // Update the current item
177 if (m_currentItem < 0) {
178 setCurrentItem(0);
179 } else {
180 int inc = 0;
181 foreach (const KItemRange& itemRange, itemRanges) {
182 if (m_currentItem < itemRange.index) {
183 break;
184 }
185 inc += itemRange.count;
186 }
187 setCurrentItem(m_currentItem + inc);
188 }
189
190 // Update the anchor item
191 if (m_anchorItem < 0) {
192 setAnchorItem(0);
193 } else {
194 int inc = 0;
195 foreach (const KItemRange& itemRange, itemRanges) {
196 if (m_anchorItem < itemRange.index) {
197 break;
198 }
199 inc += itemRange.count;
200 }
201 setAnchorItem(m_anchorItem + inc);
202 }
203
204 // Update the selections
205 if (!m_selectedItems.isEmpty()) {
206 const QSet<int> previous = m_selectedItems;
207
208 QSet<int> current;
209 current.reserve(m_selectedItems.count());
210 QSetIterator<int> it(m_selectedItems);
211 while (it.hasNext()) {
212 const int index = it.next();
213 int inc = 0;
214 foreach (const KItemRange& itemRange, itemRanges) {
215 if (index < itemRange.index) {
216 break;
217 }
218 inc += itemRange.count;
219 }
220 current.insert(index + inc);
221 }
222
223 if (current != previous) {
224 m_selectedItems = current;
225 emit selectionChanged(current, previous);
226 }
227 }
228 }
229
230 void KItemListSelectionManager::itemsRemoved(const KItemRangeList& itemRanges)
231 {
232 // Update the current item
233 if (m_currentItem >= 0) {
234 int currentItem = m_currentItem;
235 foreach (const KItemRange& itemRange, itemRanges) {
236 if (currentItem < itemRange.index) {
237 break;
238 }
239 if (currentItem >= itemRange.index + itemRange.count) {
240 currentItem -= itemRange.count;
241 } else if (currentItem >= m_model->count()) {
242 currentItem = m_model->count() - 1;
243 }
244 }
245 setCurrentItem(currentItem);
246 }
247
248 // Update the anchor item
249 if (m_anchorItem >= 0) {
250 int anchorItem = m_anchorItem;
251 foreach (const KItemRange& itemRange, itemRanges) {
252 if (anchorItem < itemRange.index) {
253 break;
254 }
255 if (anchorItem >= itemRange.index + itemRange.count) {
256 anchorItem -= itemRange.count;
257 } else if (anchorItem >= m_model->count()) {
258 anchorItem = m_model->count() - 1;
259 }
260 }
261 setAnchorItem(anchorItem);
262 }
263
264 // Update the selections
265 if (!m_selectedItems.isEmpty()) {
266 const QSet<int> previous = m_selectedItems;
267
268 QSet<int> current;
269 current.reserve(m_selectedItems.count());
270 QSetIterator<int> it(m_selectedItems);
271 while (it.hasNext()) {
272 int index = it.next();
273 int dec = 0;
274 foreach (const KItemRange& itemRange, itemRanges) {
275 if (index < itemRange.index) {
276 break;
277 }
278
279 if (index < itemRange.index + itemRange.count) {
280 // The selection is part of the removed range
281 // and will get deleted
282 index = -1;
283 break;
284 }
285
286 dec += itemRange.count;
287 }
288 index -= dec;
289 if (index >= 0) {
290 current.insert(index);
291 }
292 }
293
294 if (current != previous) {
295 m_selectedItems = current;
296 emit selectionChanged(current, previous);
297 }
298 }
299 }
300
301 #include "kitemlistselectionmanager.moc"