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 KItemSet 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 KItemSet selection
= selectedItems();
59 if (selection
!= previousSelection
) {
60 emit
selectionChanged(selection
, previousSelection
);
66 int KItemListSelectionManager::currentItem() const
71 void KItemListSelectionManager::setSelectedItems(const KItemSet
& items
)
73 if (m_selectedItems
!= items
) {
74 const KItemSet previous
= m_selectedItems
;
75 m_selectedItems
= items
;
76 emit
selectionChanged(m_selectedItems
, previous
);
80 KItemSet
KItemListSelectionManager::selectedItems() const
82 KItemSet selectedItems
= m_selectedItems
;
84 if (m_isAnchoredSelectionActive
&& m_anchorItem
!= m_currentItem
) {
85 Q_ASSERT(m_anchorItem
>= 0);
86 Q_ASSERT(m_currentItem
>= 0);
87 const int from
= qMin(m_anchorItem
, m_currentItem
);
88 const int to
= qMax(m_anchorItem
, m_currentItem
);
90 for (int index
= from
; index
<= to
; ++index
) {
91 selectedItems
.insert(index
);
98 bool KItemListSelectionManager::isSelected(int index
) const
100 if (m_selectedItems
.contains(index
)) {
104 if (m_isAnchoredSelectionActive
&& m_anchorItem
!= m_currentItem
) {
105 Q_ASSERT(m_anchorItem
>= 0);
106 Q_ASSERT(m_currentItem
>= 0);
107 const int from
= qMin(m_anchorItem
, m_currentItem
);
108 const int to
= qMax(m_anchorItem
, m_currentItem
);
110 if (from
<= index
&& index
<= to
) {
118 bool KItemListSelectionManager::hasSelection() const
120 return !m_selectedItems
.isEmpty() || (m_isAnchoredSelectionActive
&& m_anchorItem
!= m_currentItem
);
123 void KItemListSelectionManager::setSelected(int index
, int count
, SelectionMode mode
)
125 if (index
< 0 || count
< 1 || !m_model
|| index
>= m_model
->count()) {
129 endAnchoredSelection();
130 const KItemSet previous
= selectedItems();
132 count
= qMin(count
, m_model
->count() - index
);
134 const int endIndex
= index
+ count
-1;
137 for (int i
= index
; i
<= endIndex
; ++i
) {
138 m_selectedItems
.insert(i
);
143 for (int i
= index
; i
<= endIndex
; ++i
) {
144 m_selectedItems
.remove(i
);
149 for (int i
= index
; i
<= endIndex
; ++i
) {
150 if (m_selectedItems
.contains(i
)) {
151 m_selectedItems
.remove(i
);
153 m_selectedItems
.insert(i
);
163 const KItemSet selection
= selectedItems();
164 if (selection
!= previous
) {
165 emit
selectionChanged(selection
, previous
);
169 void KItemListSelectionManager::clearSelection()
171 const KItemSet previous
= selectedItems();
172 if (!previous
.isEmpty()) {
173 m_selectedItems
.clear();
174 m_isAnchoredSelectionActive
= false;
175 emit
selectionChanged(KItemSet(), previous
);
179 void KItemListSelectionManager::beginAnchoredSelection(int anchor
)
181 if (anchor
>= 0 && m_model
&& anchor
< m_model
->count()) {
182 m_isAnchoredSelectionActive
= true;
183 m_anchorItem
= anchor
;
187 void KItemListSelectionManager::endAnchoredSelection()
189 if (m_isAnchoredSelectionActive
&& (m_anchorItem
!= m_currentItem
)) {
190 Q_ASSERT(m_anchorItem
>= 0);
191 Q_ASSERT(m_currentItem
>= 0);
192 const int from
= qMin(m_anchorItem
, m_currentItem
);
193 const int to
= qMax(m_anchorItem
, m_currentItem
);
195 for (int index
= from
; index
<= to
; ++index
) {
196 m_selectedItems
.insert(index
);
200 m_isAnchoredSelectionActive
= false;
203 bool KItemListSelectionManager::isAnchoredSelectionActive() const
205 return m_isAnchoredSelectionActive
;
208 KItemModelBase
* KItemListSelectionManager::model() const
213 void KItemListSelectionManager::setModel(KItemModelBase
* model
)
216 if (model
&& model
->count() > 0) {
221 void KItemListSelectionManager::itemsInserted(const KItemRangeList
& itemRanges
)
223 // Store the current selection (needed in the selectionChanged() signal)
224 const KItemSet previousSelection
= selectedItems();
226 // Update the current item
227 if (m_currentItem
< 0) {
230 const int previousCurrent
= m_currentItem
;
232 foreach (const KItemRange
& itemRange
, itemRanges
) {
233 if (m_currentItem
< itemRange
.index
) {
236 inc
+= itemRange
.count
;
238 // Calling setCurrentItem would trigger the selectionChanged signal, but we want to
239 // emit it only once in this function -> change the current item manually and emit currentChanged
240 m_currentItem
+= inc
;
241 emit
currentChanged(m_currentItem
, previousCurrent
);
244 // Update the anchor item
245 if (m_anchorItem
< 0) {
249 foreach (const KItemRange
& itemRange
, itemRanges
) {
250 if (m_anchorItem
< itemRange
.index
) {
253 inc
+= itemRange
.count
;
258 // Update the selections
259 if (!m_selectedItems
.isEmpty()) {
260 const KItemSet previous
= m_selectedItems
;
261 m_selectedItems
.clear();
263 foreach (int index
, previous
) {
265 foreach (const KItemRange
& itemRange
, itemRanges
) {
266 if (index
< itemRange
.index
) {
269 inc
+= itemRange
.count
;
271 m_selectedItems
.insert(index
+ inc
);
275 const KItemSet selection
= selectedItems();
276 if (selection
!= previousSelection
) {
277 emit
selectionChanged(selection
, previousSelection
);
281 void KItemListSelectionManager::itemsRemoved(const KItemRangeList
& itemRanges
)
283 // Store the current selection (needed in the selectionChanged() signal)
284 const KItemSet previousSelection
= selectedItems();
285 const int previousCurrent
= m_currentItem
;
287 // Update the current item
288 m_currentItem
= indexAfterRangesRemoving(m_currentItem
, itemRanges
, DiscardRemovedIndex
);
289 if (m_currentItem
!= previousCurrent
) {
290 emit
currentChanged(m_currentItem
, previousCurrent
);
291 if (m_currentItem
< 0) {
292 // Calling setCurrentItem() would trigger the selectionChanged signal, but we want to
293 // emit it only once in this function -> change the current item manually and emit currentChanged
294 m_currentItem
= indexAfterRangesRemoving(previousCurrent
, itemRanges
, AdjustRemovedIndex
);
295 emit
currentChanged(m_currentItem
, -1);
299 // Update the anchor item
300 if (m_anchorItem
>= 0) {
301 m_anchorItem
= indexAfterRangesRemoving(m_anchorItem
, itemRanges
, DiscardRemovedIndex
);
302 if (m_anchorItem
< 0) {
303 m_isAnchoredSelectionActive
= false;
307 // Update the selections and the anchor item
308 if (!m_selectedItems
.isEmpty()) {
309 const KItemSet previous
= m_selectedItems
;
310 m_selectedItems
.clear();
312 foreach (int oldIndex
, previous
) {
313 const int index
= indexAfterRangesRemoving(oldIndex
, itemRanges
, DiscardRemovedIndex
);
315 m_selectedItems
.insert(index
);
320 const KItemSet selection
= selectedItems();
321 if (selection
!= previousSelection
) {
322 emit
selectionChanged(selection
, previousSelection
);
325 Q_ASSERT(m_currentItem
< m_model
->count());
326 Q_ASSERT(m_anchorItem
< m_model
->count());
329 void KItemListSelectionManager::itemsMoved(const KItemRange
& itemRange
, const QList
<int>& movedToIndexes
)
331 // Store the current selection (needed in the selectionChanged() signal)
332 const KItemSet previousSelection
= selectedItems();
334 // endAnchoredSelection() adds all items between m_currentItem and
335 // m_anchorItem to m_selectedItems. They can then be moved
336 // individually later in this function.
337 endAnchoredSelection();
339 // Update the current item
340 if (m_currentItem
>= itemRange
.index
&& m_currentItem
< itemRange
.index
+ itemRange
.count
) {
341 const int previousCurrentItem
= m_currentItem
;
342 const int newCurrentItem
= movedToIndexes
.at(previousCurrentItem
- itemRange
.index
);
344 // Calling setCurrentItem would trigger the selectionChanged signal, but we want to
345 // emit it only once in this function -> change the current item manually and emit currentChanged
346 m_currentItem
= newCurrentItem
;
347 emit
currentChanged(newCurrentItem
, previousCurrentItem
);
350 // Start a new anchored selection.
351 beginAnchoredSelection(m_currentItem
);
353 // Update the selections
354 if (!m_selectedItems
.isEmpty()) {
355 const KItemSet previous
= m_selectedItems
;
356 m_selectedItems
.clear();
358 foreach (int index
, previous
) {
359 if (index
>= itemRange
.index
&& index
< itemRange
.index
+ itemRange
.count
) {
360 m_selectedItems
.insert(movedToIndexes
.at(index
- itemRange
.index
));
363 m_selectedItems
.insert(index
);
368 const KItemSet selection
= selectedItems();
369 if (selection
!= previousSelection
) {
370 emit
selectionChanged(selection
, previousSelection
);
374 int KItemListSelectionManager::indexAfterRangesRemoving(int index
, const KItemRangeList
& itemRanges
,
375 const RangesRemovingBehaviour behaviour
) const
378 foreach (const KItemRange
& itemRange
, itemRanges
) {
379 if (index
< itemRange
.index
) {
383 dec
+= itemRange
.count
;
385 const int firstIndexAfterRange
= itemRange
.index
+ itemRange
.count
;
386 if (index
< firstIndexAfterRange
) {
387 // The index is part of the removed range
388 if (behaviour
== DiscardRemovedIndex
) {
391 // Use the first item after the range as new index
392 index
= firstIndexAfterRange
;
397 return qBound(-1, index
- dec
, m_model
->count() - 1);