]>
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 selections
191 if (!m_selectedItems
.isEmpty()) {
192 const QSet
<int> previous
= m_selectedItems
;
195 current
.reserve(m_selectedItems
.count());
196 QSetIterator
<int> it(m_selectedItems
);
197 while (it
.hasNext()) {
198 const int index
= it
.next();
200 foreach (const KItemRange
& itemRange
, itemRanges
) {
201 if (index
< itemRange
.index
) {
204 inc
+= itemRange
.count
;
206 current
.insert(index
+ inc
);
209 if (current
!= previous
) {
210 m_selectedItems
= current
;
211 emit
selectionChanged(current
, previous
);
216 void KItemListSelectionManager::itemsRemoved(const KItemRangeList
& itemRanges
)
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
) {
225 if (currentItem
>= itemRange
.index
+ itemRange
.count
) {
226 currentItem
-= itemRange
.count
;
227 } else if (currentItem
>= m_model
->count()) {
228 currentItem
= m_model
->count() - 1;
231 setCurrentItem(currentItem
);
234 // Update the selections
235 if (!m_selectedItems
.isEmpty()) {
236 const QSet
<int> previous
= m_selectedItems
;
239 current
.reserve(m_selectedItems
.count());
240 QSetIterator
<int> it(m_selectedItems
);
241 while (it
.hasNext()) {
242 int index
= it
.next();
244 foreach (const KItemRange
& itemRange
, itemRanges
) {
245 if (index
< itemRange
.index
) {
249 if (index
< itemRange
.index
+ itemRange
.count
) {
250 // The selection is part of the removed range
251 // and will get deleted
256 dec
+= itemRange
.count
;
260 current
.insert(index
);
264 if (current
!= previous
) {
265 m_selectedItems
= current
;
266 emit
selectionChanged(current
, previous
);
271 #include "kitemlistselectionmanager.moc"