]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kitemlistsizehintresolver.cpp
Make sure that KItemListSizeHintResolver is always consistent
[dolphin.git] / src / kitemviews / private / kitemlistsizehintresolver.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "kitemlistsizehintresolver.h"
21
22 #include <kitemviews/kitemlistview.h>
23
24 KItemListSizeHintResolver::KItemListSizeHintResolver(const KItemListView* itemListView) :
25 m_itemListView(itemListView),
26 m_sizeHintCache()
27 {
28 }
29
30 KItemListSizeHintResolver::~KItemListSizeHintResolver()
31 {
32 }
33
34 QSizeF KItemListSizeHintResolver::sizeHint(int index) const
35 {
36 QSizeF size = m_sizeHintCache.at(index);
37 if (size.isEmpty()) {
38 size = m_itemListView->itemSizeHint(index);
39 m_sizeHintCache[index] = size;
40 }
41 return size;
42 }
43
44 void KItemListSizeHintResolver::itemsInserted(const KItemRangeList& itemRanges)
45 {
46 int insertedCount = 0;
47 foreach (const KItemRange& range, itemRanges) {
48 insertedCount += range.count;
49 }
50
51 const int currentCount = m_sizeHintCache.count();
52 m_sizeHintCache.reserve(currentCount + insertedCount);
53
54 // We build the new list from the end to the beginning to mimize the
55 // number of moves.
56 m_sizeHintCache.insert(m_sizeHintCache.end(), insertedCount, QSizeF());
57
58 int sourceIndex = currentCount - 1;
59 int targetIndex = m_sizeHintCache.count() - 1;
60 int itemsToInsertBeforeCurrentRange = insertedCount;
61
62 for (int rangeIndex = itemRanges.count() - 1; rangeIndex >= 0; --rangeIndex) {
63 const KItemRange& range = itemRanges.at(rangeIndex);
64 itemsToInsertBeforeCurrentRange -= range.count;
65
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];
69 --sourceIndex;
70 --targetIndex;
71 }
72
73 // Then: insert QSizeF() for the items which are inserted into 'range'.
74 while (targetIndex >= itemsToInsertBeforeCurrentRange + range.index) {
75 m_sizeHintCache[targetIndex] = QSizeF();
76 --targetIndex;
77 }
78 }
79
80 Q_ASSERT(m_sizeHintCache.count() == m_itemListView->model()->count());
81 }
82
83 void KItemListSizeHintResolver::itemsRemoved(const KItemRangeList& itemRanges)
84 {
85 const QVector<QSizeF>::iterator begin = m_sizeHintCache.begin();
86 const QVector<QSizeF>::iterator end = m_sizeHintCache.end();
87
88 KItemRangeList::const_iterator rangeIt = itemRanges.constBegin();
89 const KItemRangeList::const_iterator rangeEnd = itemRanges.constEnd();
90
91 QVector<QSizeF>::iterator destIt = begin + rangeIt->index;
92 QVector<QSizeF>::iterator srcIt = destIt + rangeIt->count;
93
94 ++rangeIt;
95
96 while (srcIt != end) {
97 *destIt = *srcIt;
98 ++destIt;
99 ++srcIt;
100
101 if (rangeIt != rangeEnd && srcIt == begin + rangeIt->index) {
102 // Skip the items in the next removed range.
103 srcIt += rangeIt->count;
104 ++rangeIt;
105 }
106 }
107
108 m_sizeHintCache.erase(destIt, end);
109
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());
114 }
115 }
116
117 void KItemListSizeHintResolver::itemsMoved(int index, int count)
118 {
119 while (count) {
120 m_sizeHintCache[index] = QSizeF();
121 ++index;
122 --count;
123 }
124 }
125
126 void KItemListSizeHintResolver::itemsChanged(int index, int count, const QSet<QByteArray>& roles)
127 {
128 Q_UNUSED(roles);
129 while (count) {
130 m_sizeHintCache[index] = QSizeF();
131 ++index;
132 --count;
133 }
134 }
135
136 void KItemListSizeHintResolver::clearCache()
137 {
138 const int count = m_sizeHintCache.count();
139 for (int i = 0; i < count; ++i) {
140 m_sizeHintCache[i] = QSizeF();
141 }
142 }