1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2011 by Frank Reininghaus <frank78ac@googlemail.com> *
5 * Based on the Itemviews NG project from Trolltech Labs: *
6 * http://qt.gitorious.org/qt-labs/itemviews-ng *
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. *
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. *
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 ***************************************************************************/
24 #include "kitemlistselectionmanager.h"
26 #include "kitemmodelbase.h"
29 KItemListSelectionManager::KItemListSelectionManager(QObject
* parent
) :
34 m_isAnchoredSelectionActive(false),
39 KItemListSelectionManager::~KItemListSelectionManager()
43 void KItemListSelectionManager::setCurrentItem(int current
)
45 const int previous
= m_currentItem
;
46 const QSet
<int> previousSelection
= selectedItems();
48 if (m_model
&& current
>= 0 && current
< m_model
->count()) {
49 m_currentItem
= current
;
54 if (m_currentItem
!= previous
) {
55 emit
currentChanged(m_currentItem
, previous
);
57 if (m_isAnchoredSelectionActive
) {
58 const QSet
<int> selection
= selectedItems();
59 if (selection
!= previousSelection
) {
60 emit
selectionChanged(selection
, previousSelection
);
66 int KItemListSelectionManager::currentItem() const
71 void KItemListSelectionManager::setSelectedItems(const QSet
<int>& items
)
73 if (m_selectedItems
!= items
) {
74 const QSet
<int> previous
= m_selectedItems
;
75 m_selectedItems
= items
;
76 emit
selectionChanged(m_selectedItems
, previous
);
80 QSet
<int> KItemListSelectionManager::selectedItems() const
82 QSet
<int> selectedItems
= m_selectedItems
;
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
);
88 for (int index
= from
; index
<= to
; index
++) {
89 selectedItems
.insert(index
);
96 bool KItemListSelectionManager::hasSelection() const
98 return !m_selectedItems
.isEmpty() || (m_isAnchoredSelectionActive
&& (m_anchorItem
!= m_currentItem
));
101 void KItemListSelectionManager::setSelected(int index
, int count
, SelectionMode mode
)
103 if (index
< 0 || count
< 1 || !m_model
|| index
>= m_model
->count()) {
107 endAnchoredSelection();
108 const QSet
<int> previous
= selectedItems();
110 count
= qMin(count
, m_model
->count() - index
);
112 const int endIndex
= index
+ count
-1;
115 for (int i
= index
; i
<= endIndex
; ++i
) {
116 m_selectedItems
.insert(i
);
121 for (int i
= index
; i
<= endIndex
; ++i
) {
122 m_selectedItems
.remove(i
);
127 for (int i
= index
; i
<= endIndex
; ++i
) {
128 if (m_selectedItems
.contains(i
)) {
129 m_selectedItems
.remove(i
);
131 m_selectedItems
.insert(i
);
141 const QSet
<int> selection
= selectedItems();
142 if (selection
!= previous
) {
143 emit
selectionChanged(selection
, previous
);
147 void KItemListSelectionManager::clearSelection()
149 const QSet
<int> previous
= selectedItems();
150 if (!previous
.isEmpty()) {
151 m_selectedItems
.clear();
152 m_isAnchoredSelectionActive
= false;
153 emit
selectionChanged(QSet
<int>(), previous
);
157 void KItemListSelectionManager::beginAnchoredSelection(int anchor
)
159 m_isAnchoredSelectionActive
= true;
160 setAnchorItem(anchor
);
163 void KItemListSelectionManager::endAnchoredSelection()
165 if (m_isAnchoredSelectionActive
&& (m_anchorItem
!= m_currentItem
)) {
166 const int from
= qMin(m_anchorItem
, m_currentItem
);
167 const int to
= qMax(m_anchorItem
, m_currentItem
);
169 for (int index
= from
; index
<= to
; index
++) {
170 m_selectedItems
.insert(index
);
174 m_isAnchoredSelectionActive
= false;
177 void KItemListSelectionManager::setAnchorItem(int anchor
)
179 const int previous
= m_anchorItem
;
180 if (m_model
&& anchor
< m_model
->count()) {
181 m_anchorItem
= anchor
;
186 if (m_anchorItem
!= previous
) {
187 emit
anchorChanged(m_anchorItem
, previous
);
191 int KItemListSelectionManager::anchorItem() const
196 bool KItemListSelectionManager::isAnchoredSelectionActive() const
198 return m_isAnchoredSelectionActive
;
201 void KItemListSelectionManager::setAnchoredSelectionActive(bool active
)
203 m_isAnchoredSelectionActive
= active
;
206 KItemModelBase
* KItemListSelectionManager::model() const
211 void KItemListSelectionManager::setModel(KItemModelBase
* model
)
214 if (model
&& model
->count() > 0) {
219 void KItemListSelectionManager::itemsInserted(const KItemRangeList
& itemRanges
)
221 // Store the current selection (needed in the selectionChanged() signal)
222 const QSet
<int> previousSelection
= selectedItems();
224 // Update the current item
225 if (m_currentItem
< 0) {
228 const int previousCurrent
= m_currentItem
;
230 foreach (const KItemRange
& itemRange
, itemRanges
) {
231 if (m_currentItem
< itemRange
.index
) {
234 inc
+= itemRange
.count
;
236 // Calling setCurrentItem would trigger the selectionChanged signal, but we want to
237 // emit it only once in this function -> change the current item manually and emit currentChanged
238 m_currentItem
+= inc
;
239 emit
currentChanged(m_currentItem
, previousCurrent
);
242 // Update the anchor item
243 if (m_anchorItem
< 0) {
246 const int previousAnchor
= m_anchorItem
;
248 foreach (const KItemRange
& itemRange
, itemRanges
) {
249 if (m_anchorItem
< itemRange
.index
) {
252 inc
+= itemRange
.count
;
255 emit
anchorChanged(m_anchorItem
, previousAnchor
);
258 // Update the selections
259 if (!m_selectedItems
.isEmpty()) {
260 const QSet
<int> previous
= m_selectedItems
;
261 m_selectedItems
.clear();
262 QSetIterator
<int> it(previous
);
263 while (it
.hasNext()) {
264 const int index
= it
.next();
266 foreach (const KItemRange
& itemRange
, itemRanges
) {
267 if (index
< itemRange
.index
) {
270 inc
+= itemRange
.count
;
272 m_selectedItems
.insert(index
+ inc
);
276 const QSet
<int> selection
= selectedItems();
277 if (selection
!= previousSelection
) {
278 emit
selectionChanged(selection
, previousSelection
);
282 void KItemListSelectionManager::itemsRemoved(const KItemRangeList
& itemRanges
)
284 // Store the current selection (needed in the selectionChanged() signal)
285 const QSet
<int> previousSelection
= selectedItems();
287 // Update the current item
288 if (m_currentItem
>= 0) {
289 const int previousCurrent
= m_currentItem
;
290 int currentItem
= m_currentItem
;
291 foreach (const KItemRange
& itemRange
, itemRanges
) {
292 if (currentItem
< itemRange
.index
) {
295 if (currentItem
>= itemRange
.index
+ itemRange
.count
) {
296 currentItem
-= itemRange
.count
;
297 } else if (currentItem
>= m_model
->count()) {
298 currentItem
= m_model
->count() - 1;
301 // Calling setCurrentItem would trigger the selectionChanged signal, but we want to
302 // emit it only once in this function -> change the current item manually and emit currentChanged
303 m_currentItem
= currentItem
;
304 emit
currentChanged(m_currentItem
, previousCurrent
);
307 // Update the anchor item
308 if (m_anchorItem
>= 0) {
309 const int previousAnchor
= m_anchorItem
;
310 int anchorItem
= m_anchorItem
;
311 foreach (const KItemRange
& itemRange
, itemRanges
) {
312 if (anchorItem
< itemRange
.index
) {
315 if (anchorItem
>= itemRange
.index
+ itemRange
.count
) {
316 anchorItem
-= itemRange
.count
;
317 } else if (anchorItem
>= m_model
->count()) {
318 anchorItem
= m_model
->count() - 1;
321 m_anchorItem
= anchorItem
;
322 emit
anchorChanged(m_anchorItem
, previousAnchor
);
325 // Update the selections
326 if (!m_selectedItems
.isEmpty()) {
327 const QSet
<int> previous
= m_selectedItems
;
328 m_selectedItems
.clear();
329 QSetIterator
<int> it(previous
);
330 while (it
.hasNext()) {
331 int index
= it
.next();
333 foreach (const KItemRange
& itemRange
, itemRanges
) {
334 if (index
< itemRange
.index
) {
338 if (index
< itemRange
.index
+ itemRange
.count
) {
339 // The selection is part of the removed range
340 // and will get deleted
345 dec
+= itemRange
.count
;
349 m_selectedItems
.insert(index
);
354 const QSet
<int> selection
= selectedItems();
355 if (selection
!= previousSelection
) {
356 emit
selectionChanged(selection
, previousSelection
);
360 #include "kitemlistselectionmanager.moc"