]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistselectionmanager.cpp
Improvements for selections, smooth scrolling, tooltips and info-panel
[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 selections
191 if (!m_selectedItems.isEmpty()) {
192 const QSet<int> previous = m_selectedItems;
193
194 QSet<int> current;
195 current.reserve(m_selectedItems.count());
196 QSetIterator<int> it(m_selectedItems);
197 while (it.hasNext()) {
198 const int index = it.next();
199 int inc = 0;
200 foreach (const KItemRange& itemRange, itemRanges) {
201 if (index < itemRange.index) {
202 break;
203 }
204 inc += itemRange.count;
205 }
206 current.insert(index + inc);
207 }
208
209 if (current != previous) {
210 m_selectedItems = current;
211 emit selectionChanged(current, previous);
212 }
213 }
214 }
215
216 void KItemListSelectionManager::itemsRemoved(const KItemRangeList& itemRanges)
217 {
218 // Update the current item
219 if (m_currentItem >= 0) {
220 int currentItem = m_currentItem;
221 foreach (const KItemRange& itemRange, itemRanges) {
222 if (currentItem < itemRange.index) {
223 break;
224 }
225 if (currentItem >= itemRange.index + itemRange.count) {
226 currentItem -= itemRange.count;
227 } else if (currentItem >= m_model->count()) {
228 currentItem = m_model->count() - 1;
229 }
230 }
231 setCurrentItem(currentItem);
232 }
233
234 // Update the selections
235 if (!m_selectedItems.isEmpty()) {
236 const QSet<int> previous = m_selectedItems;
237
238 QSet<int> current;
239 current.reserve(m_selectedItems.count());
240 QSetIterator<int> it(m_selectedItems);
241 while (it.hasNext()) {
242 int index = it.next();
243 int dec = 0;
244 foreach (const KItemRange& itemRange, itemRanges) {
245 if (index < itemRange.index) {
246 break;
247 }
248
249 if (index < itemRange.index + itemRange.count) {
250 // The selection is part of the removed range
251 // and will get deleted
252 index = -1;
253 break;
254 }
255
256 dec += itemRange.count;
257 }
258 index -= dec;
259 if (index >= 0) {
260 current.insert(index);
261 }
262 }
263
264 if (current != previous) {
265 m_selectedItems = current;
266 emit selectionChanged(current, previous);
267 }
268 }
269 }
270
271 #include "kitemlistselectionmanager.moc"