]>
cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistselectionmanager.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
4 * Based on the Itemviews NG project from Trolltech Labs: *
5 * http://qt.gitorious.org/qt-labs/itemviews-ng *
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. *
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. *
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 ***************************************************************************/
23 #include "kitemlistselectionmanager.h"
25 #include "kitemmodelbase.h"
28 KItemListSelectionManager::KItemListSelectionManager(QObject
* parent
) :
37 KItemListSelectionManager::~KItemListSelectionManager()
41 void KItemListSelectionManager::setCurrentItem(int current
)
43 const int previous
= m_currentItem
;
44 if (m_model
&& current
>= 0 && current
< m_model
->count()) {
45 m_currentItem
= current
;
50 if (m_currentItem
!= previous
) {
51 emit
currentChanged(m_currentItem
, previous
);
55 int KItemListSelectionManager::currentItem() const
60 void KItemListSelectionManager::setSelectedItems(const QSet
<int>& items
)
62 if (m_selectedItems
!= items
) {
63 const QSet
<int> previous
= m_selectedItems
;
64 m_selectedItems
= items
;
65 emit
selectionChanged(m_selectedItems
, previous
);
69 QSet
<int> KItemListSelectionManager::selectedItems() const
71 return m_selectedItems
;
74 bool KItemListSelectionManager::hasSelection() const
76 return !m_selectedItems
.isEmpty();
79 void KItemListSelectionManager::setSelected(int index
, int count
, SelectionMode mode
)
81 if (index
< 0 || count
< 1 || !m_model
|| index
>= m_model
->count()) {
85 const QSet
<int> previous
= m_selectedItems
;
87 count
= qMin(count
, m_model
->count() - index
);
89 const int endIndex
= index
+ count
-1;
92 for (int i
= index
; i
<= endIndex
; ++i
) {
93 m_selectedItems
.insert(i
);
98 for (int i
= index
; i
<= endIndex
; ++i
) {
99 m_selectedItems
.remove(i
);
104 for (int i
= index
; i
<= endIndex
; ++i
) {
105 if (m_selectedItems
.contains(i
)) {
106 m_selectedItems
.remove(i
);
108 m_selectedItems
.insert(i
);
118 if (m_selectedItems
!= previous
) {
119 emit
selectionChanged(m_selectedItems
, previous
);
123 void KItemListSelectionManager::clearSelection()
125 if (!m_selectedItems
.isEmpty()) {
126 const QSet
<int> previous
= m_selectedItems
;
127 m_selectedItems
.clear();
128 emit
selectionChanged(m_selectedItems
, previous
);
132 void KItemListSelectionManager::beginAnchoredSelection(int anchor
, SelectionMode mode
)
138 void KItemListSelectionManager::endAnchoredSelection()
142 void KItemListSelectionManager::setAnchorItem(int anchor
)
144 const int previous
= m_anchorItem
;
145 if (m_model
&& anchor
< m_model
->count()) {
146 m_anchorItem
= anchor
;
151 if (m_anchorItem
!= previous
) {
152 emit
anchorChanged(m_anchorItem
, previous
);
156 int KItemListSelectionManager::anchorItem() const
161 KItemModelBase
* KItemListSelectionManager::model() const
166 void KItemListSelectionManager::setModel(KItemModelBase
* model
)
169 if (model
&& model
->count() > 0) {
174 void KItemListSelectionManager::itemsInserted(const KItemRangeList
& itemRanges
)
176 // Update the current item
177 if (m_currentItem
< 0) {
181 foreach (const KItemRange
& itemRange
, itemRanges
) {
182 if (m_currentItem
< itemRange
.index
) {
185 inc
+= itemRange
.count
;
187 setCurrentItem(m_currentItem
+ inc
);
190 // Update the anchor item
191 if (m_anchorItem
< 0) {
195 foreach (const KItemRange
& itemRange
, itemRanges
) {
196 if (m_anchorItem
< itemRange
.index
) {
199 inc
+= itemRange
.count
;
201 setAnchorItem(m_anchorItem
+ inc
);
204 // Update the selections
205 if (!m_selectedItems
.isEmpty()) {
206 const QSet
<int> previous
= m_selectedItems
;
209 current
.reserve(m_selectedItems
.count());
210 QSetIterator
<int> it(m_selectedItems
);
211 while (it
.hasNext()) {
212 const int index
= it
.next();
214 foreach (const KItemRange
& itemRange
, itemRanges
) {
215 if (index
< itemRange
.index
) {
218 inc
+= itemRange
.count
;
220 current
.insert(index
+ inc
);
223 if (current
!= previous
) {
224 m_selectedItems
= current
;
225 emit
selectionChanged(current
, previous
);
230 void KItemListSelectionManager::itemsRemoved(const KItemRangeList
& itemRanges
)
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
) {
239 if (currentItem
>= itemRange
.index
+ itemRange
.count
) {
240 currentItem
-= itemRange
.count
;
241 } else if (currentItem
>= m_model
->count()) {
242 currentItem
= m_model
->count() - 1;
245 setCurrentItem(currentItem
);
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
) {
255 if (anchorItem
>= itemRange
.index
+ itemRange
.count
) {
256 anchorItem
-= itemRange
.count
;
257 } else if (anchorItem
>= m_model
->count()) {
258 anchorItem
= m_model
->count() - 1;
261 setAnchorItem(anchorItem
);
264 // Update the selections
265 if (!m_selectedItems
.isEmpty()) {
266 const QSet
<int> previous
= m_selectedItems
;
269 current
.reserve(m_selectedItems
.count());
270 QSetIterator
<int> it(m_selectedItems
);
271 while (it
.hasNext()) {
272 int index
= it
.next();
274 foreach (const KItemRange
& itemRange
, itemRanges
) {
275 if (index
< itemRange
.index
) {
279 if (index
< itemRange
.index
+ itemRange
.count
) {
280 // The selection is part of the removed range
281 // and will get deleted
286 dec
+= itemRange
.count
;
290 current
.insert(index
);
294 if (current
!= previous
) {
295 m_selectedItems
= current
;
296 emit
selectionChanged(current
, previous
);
301 #include "kitemlistselectionmanager.moc"