1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "kitemlistsizehintresolver.h"
22 #include <kitemviews/kitemlistview.h>
24 KItemListSizeHintResolver::KItemListSizeHintResolver(const KItemListView
* itemListView
) :
25 m_itemListView(itemListView
),
27 m_needsResolving(false)
31 KItemListSizeHintResolver::~KItemListSizeHintResolver()
35 QSizeF
KItemListSizeHintResolver::sizeHint(int index
)
38 return m_sizeHintCache
.at(index
);
41 void KItemListSizeHintResolver::itemsInserted(const KItemRangeList
& itemRanges
)
43 int insertedCount
= 0;
44 foreach (const KItemRange
& range
, itemRanges
) {
45 insertedCount
+= range
.count
;
48 const int currentCount
= m_sizeHintCache
.count();
49 m_sizeHintCache
.reserve(currentCount
+ insertedCount
);
51 // We build the new list from the end to the beginning to mimize the
53 m_sizeHintCache
.insert(m_sizeHintCache
.end(), insertedCount
, QSizeF());
55 int sourceIndex
= currentCount
- 1;
56 int targetIndex
= m_sizeHintCache
.count() - 1;
57 int itemsToInsertBeforeCurrentRange
= insertedCount
;
59 for (int rangeIndex
= itemRanges
.count() - 1; rangeIndex
>= 0; --rangeIndex
) {
60 const KItemRange
& range
= itemRanges
.at(rangeIndex
);
61 itemsToInsertBeforeCurrentRange
-= range
.count
;
63 // First: move all existing items that must be put behind 'range'.
64 while (targetIndex
>= itemsToInsertBeforeCurrentRange
+ range
.index
+ range
.count
) {
65 m_sizeHintCache
[targetIndex
] = m_sizeHintCache
[sourceIndex
];
70 // Then: insert QSizeF() for the items which are inserted into 'range'.
71 while (targetIndex
>= itemsToInsertBeforeCurrentRange
+ range
.index
) {
72 m_sizeHintCache
[targetIndex
] = QSizeF();
77 m_needsResolving
= true;
79 Q_ASSERT(m_sizeHintCache
.count() == m_itemListView
->model()->count());
82 void KItemListSizeHintResolver::itemsRemoved(const KItemRangeList
& itemRanges
)
84 const QVector
<QSizeF
>::iterator begin
= m_sizeHintCache
.begin();
85 const QVector
<QSizeF
>::iterator end
= m_sizeHintCache
.end();
87 KItemRangeList::const_iterator rangeIt
= itemRanges
.constBegin();
88 const KItemRangeList::const_iterator rangeEnd
= itemRanges
.constEnd();
90 QVector
<QSizeF
>::iterator destIt
= begin
+ rangeIt
->index
;
91 QVector
<QSizeF
>::iterator srcIt
= destIt
+ rangeIt
->count
;
95 while (srcIt
!= end
) {
100 if (rangeIt
!= rangeEnd
&& srcIt
== begin
+ rangeIt
->index
) {
101 // Skip the items in the next removed range.
102 srcIt
+= rangeIt
->count
;
107 m_sizeHintCache
.erase(destIt
, end
);
109 // Note that the cache size might temporarily not match the model size if
110 // this function is called from KItemListView::setModel() to empty the cache.
111 if (!m_sizeHintCache
.isEmpty() && m_itemListView
->model()) {
112 Q_ASSERT(m_sizeHintCache
.count() == m_itemListView
->model()->count());
116 void KItemListSizeHintResolver::itemsMoved(const KItemRange
& range
, const QList
<int>& movedToIndexes
)
118 QVector
<QSizeF
> newSizeHintCache(m_sizeHintCache
);
120 const int movedRangeEnd
= range
.index
+ range
.count
;
121 for (int i
= range
.index
; i
< movedRangeEnd
; ++i
) {
122 const int newIndex
= movedToIndexes
.at(i
- range
.index
);
123 newSizeHintCache
[newIndex
] = m_sizeHintCache
.at(i
);
126 m_sizeHintCache
= newSizeHintCache
;
129 void KItemListSizeHintResolver::itemsChanged(int index
, int count
, const QSet
<QByteArray
>& roles
)
133 m_sizeHintCache
[index
] = QSizeF();
138 m_needsResolving
= true;
141 void KItemListSizeHintResolver::clearCache()
143 m_sizeHintCache
.fill(QSizeF());
144 m_needsResolving
= true;
147 void KItemListSizeHintResolver::updateCache()
149 if (m_needsResolving
) {
150 m_itemListView
->calculateItemSizeHints(m_sizeHintCache
);
151 m_needsResolving
= false;