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
),
26 m_logicalHeightHintCache(),
27 m_logicalWidthHint(0.0),
28 m_logicalHeightHint(0.0),
29 m_needsResolving(false)
33 KItemListSizeHintResolver::~KItemListSizeHintResolver()
37 QSizeF
KItemListSizeHintResolver::maxSizeHint()
40 return QSizeF(m_logicalWidthHint
, m_logicalHeightHint
);
43 QSizeF
KItemListSizeHintResolver::sizeHint(int index
)
46 return QSizeF(m_logicalWidthHint
, m_logicalHeightHintCache
.at(index
));
49 void KItemListSizeHintResolver::itemsInserted(const KItemRangeList
& itemRanges
)
51 int insertedCount
= 0;
52 foreach (const KItemRange
& range
, itemRanges
) {
53 insertedCount
+= range
.count
;
56 const int currentCount
= m_logicalHeightHintCache
.count();
57 m_logicalHeightHintCache
.reserve(currentCount
+ insertedCount
);
59 // We build the new list from the end to the beginning to mimize the
61 m_logicalHeightHintCache
.insert(m_logicalHeightHintCache
.end(), insertedCount
, 0.0);
63 int sourceIndex
= currentCount
- 1;
64 int targetIndex
= m_logicalHeightHintCache
.count() - 1;
65 int itemsToInsertBeforeCurrentRange
= insertedCount
;
67 for (int rangeIndex
= itemRanges
.count() - 1; rangeIndex
>= 0; --rangeIndex
) {
68 const KItemRange
& range
= itemRanges
.at(rangeIndex
);
69 itemsToInsertBeforeCurrentRange
-= range
.count
;
71 // First: move all existing items that must be put behind 'range'.
72 while (targetIndex
>= itemsToInsertBeforeCurrentRange
+ range
.index
+ range
.count
) {
73 m_logicalHeightHintCache
[targetIndex
] = m_logicalHeightHintCache
[sourceIndex
];
78 // Then: insert QSizeF() for the items which are inserted into 'range'.
79 while (targetIndex
>= itemsToInsertBeforeCurrentRange
+ range
.index
) {
80 m_logicalHeightHintCache
[targetIndex
] = 0.0;
85 m_needsResolving
= true;
87 Q_ASSERT(m_logicalHeightHintCache
.count() == m_itemListView
->model()->count());
90 void KItemListSizeHintResolver::itemsRemoved(const KItemRangeList
& itemRanges
)
92 const QVector
<qreal
>::iterator begin
= m_logicalHeightHintCache
.begin();
93 const QVector
<qreal
>::iterator end
= m_logicalHeightHintCache
.end();
95 KItemRangeList::const_iterator rangeIt
= itemRanges
.constBegin();
96 const KItemRangeList::const_iterator rangeEnd
= itemRanges
.constEnd();
98 QVector
<qreal
>::iterator destIt
= begin
+ rangeIt
->index
;
99 QVector
<qreal
>::iterator srcIt
= destIt
+ rangeIt
->count
;
103 while (srcIt
!= end
) {
108 if (rangeIt
!= rangeEnd
&& srcIt
== begin
+ rangeIt
->index
) {
109 // Skip the items in the next removed range.
110 srcIt
+= rangeIt
->count
;
115 m_logicalHeightHintCache
.erase(destIt
, end
);
117 // Note that the cache size might temporarily not match the model size if
118 // this function is called from KItemListView::setModel() to empty the cache.
119 if (!m_logicalHeightHintCache
.isEmpty() && m_itemListView
->model()) {
120 Q_ASSERT(m_logicalHeightHintCache
.count() == m_itemListView
->model()->count());
124 void KItemListSizeHintResolver::itemsMoved(const KItemRange
& range
, const QList
<int>& movedToIndexes
)
126 QVector
<qreal
> newLogicalHeightHintCache(m_logicalHeightHintCache
);
128 const int movedRangeEnd
= range
.index
+ range
.count
;
129 for (int i
= range
.index
; i
< movedRangeEnd
; ++i
) {
130 const int newIndex
= movedToIndexes
.at(i
- range
.index
);
131 newLogicalHeightHintCache
[newIndex
] = m_logicalHeightHintCache
.at(i
);
134 m_logicalHeightHintCache
= newLogicalHeightHintCache
;
137 void KItemListSizeHintResolver::itemsChanged(int index
, int count
, const QSet
<QByteArray
>& roles
)
141 m_logicalHeightHintCache
[index
] = 0.0;
146 m_needsResolving
= true;
149 void KItemListSizeHintResolver::clearCache()
151 m_logicalHeightHintCache
.fill(0.0);
152 m_needsResolving
= true;
155 void KItemListSizeHintResolver::updateCache()
157 if (m_needsResolving
) {
158 m_itemListView
->calculateItemSizeHints(m_logicalHeightHintCache
, m_logicalWidthHint
);
159 // Set logical height as the max cached height (if the cache is not empty).
160 if (m_logicalHeightHintCache
.isEmpty()) {
161 m_logicalHeightHint
= 0.0;
163 m_logicalHeightHint
= *std::max_element(m_logicalHeightHintCache
.begin(), m_logicalHeightHintCache
.end());
165 m_needsResolving
= false;