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
),
30 KItemListSizeHintResolver::~KItemListSizeHintResolver()
34 QSizeF
KItemListSizeHintResolver::sizeHint(int index
) const
36 QSizeF size
= m_sizeHintCache
.at(index
);
38 size
= m_itemListView
->itemSizeHint(index
);
39 m_sizeHintCache
[index
] = size
;
44 void KItemListSizeHintResolver::itemsInserted(const KItemRangeList
& itemRanges
)
46 int insertedCount
= 0;
47 foreach (const KItemRange
& range
, itemRanges
) {
48 insertedCount
+= range
.count
;
51 const int currentCount
= m_sizeHintCache
.count();
52 m_sizeHintCache
.reserve(currentCount
+ insertedCount
);
54 // We build the new list from the end to the beginning to mimize the
56 m_sizeHintCache
.insert(m_sizeHintCache
.end(), insertedCount
, QSizeF());
58 int sourceIndex
= currentCount
- 1;
59 int targetIndex
= m_sizeHintCache
.count() - 1;
60 int itemsToInsertBeforeCurrentRange
= insertedCount
;
62 for (int rangeIndex
= itemRanges
.count() - 1; rangeIndex
>= 0; --rangeIndex
) {
63 const KItemRange
& range
= itemRanges
.at(rangeIndex
);
64 itemsToInsertBeforeCurrentRange
-= range
.count
;
66 // First: move all existing items that must be put behind 'range'.
67 while (targetIndex
>= itemsToInsertBeforeCurrentRange
+ range
.index
+ range
.count
) {
68 m_sizeHintCache
[targetIndex
] = m_sizeHintCache
[sourceIndex
];
73 // Then: insert QSizeF() for the items which are inserted into 'range'.
74 while (targetIndex
>= itemsToInsertBeforeCurrentRange
+ range
.index
) {
75 m_sizeHintCache
[targetIndex
] = QSizeF();
80 Q_ASSERT(m_sizeHintCache
.count() == m_itemListView
->model()->count());
83 void KItemListSizeHintResolver::itemsRemoved(const KItemRangeList
& itemRanges
)
85 const QVector
<QSizeF
>::iterator begin
= m_sizeHintCache
.begin();
86 const QVector
<QSizeF
>::iterator end
= m_sizeHintCache
.end();
88 KItemRangeList::const_iterator rangeIt
= itemRanges
.constBegin();
89 const KItemRangeList::const_iterator rangeEnd
= itemRanges
.constEnd();
91 QVector
<QSizeF
>::iterator destIt
= begin
+ rangeIt
->index
;
92 QVector
<QSizeF
>::iterator srcIt
= destIt
+ rangeIt
->count
;
96 while (srcIt
!= end
) {
101 if (rangeIt
!= rangeEnd
&& srcIt
== begin
+ rangeIt
->index
) {
102 // Skip the items in the next removed range.
103 srcIt
+= rangeIt
->count
;
108 m_sizeHintCache
.erase(destIt
, end
);
110 // Note that the cache size might temporarily not match the model size if
111 // this function is called from KItemListView::setModel() to empty the cache.
112 if (!m_sizeHintCache
.isEmpty() && m_itemListView
->model()) {
113 Q_ASSERT(m_sizeHintCache
.count() == m_itemListView
->model()->count());
117 void KItemListSizeHintResolver::itemsMoved(int index
, int count
)
120 m_sizeHintCache
[index
] = QSizeF();
126 void KItemListSizeHintResolver::itemsChanged(int index
, int count
, const QSet
<QByteArray
>& roles
)
130 m_sizeHintCache
[index
] = QSizeF();
136 void KItemListSizeHintResolver::clearCache()
138 const int count
= m_sizeHintCache
.count();
139 for (int i
= 0; i
< count
; ++i
) {
140 m_sizeHintCache
[i
] = QSizeF();