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"
21 #include "kitemviews/kitemlistview.h"
23 KItemListSizeHintResolver::KItemListSizeHintResolver(const KItemListView
* itemListView
) :
24 m_itemListView(itemListView
),
25 m_logicalHeightHintCache(),
26 m_logicalWidthHint(0.0),
27 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::minSizeHint()
46 return QSizeF(m_logicalWidthHint
, m_minHeightHint
);
49 QSizeF
KItemListSizeHintResolver::sizeHint(int index
)
52 return QSizeF(m_logicalWidthHint
, m_logicalHeightHintCache
.at(index
));
55 void KItemListSizeHintResolver::itemsInserted(const KItemRangeList
& itemRanges
)
57 int insertedCount
= 0;
58 foreach (const KItemRange
& range
, itemRanges
) {
59 insertedCount
+= range
.count
;
62 const int currentCount
= m_logicalHeightHintCache
.count();
63 m_logicalHeightHintCache
.reserve(currentCount
+ insertedCount
);
65 // We build the new list from the end to the beginning to mimize the
67 m_logicalHeightHintCache
.insert(m_logicalHeightHintCache
.end(), insertedCount
, 0.0);
69 int sourceIndex
= currentCount
- 1;
70 int targetIndex
= m_logicalHeightHintCache
.count() - 1;
71 int itemsToInsertBeforeCurrentRange
= insertedCount
;
73 for (int rangeIndex
= itemRanges
.count() - 1; rangeIndex
>= 0; --rangeIndex
) {
74 const KItemRange
& range
= itemRanges
.at(rangeIndex
);
75 itemsToInsertBeforeCurrentRange
-= range
.count
;
77 // First: move all existing items that must be put behind 'range'.
78 while (targetIndex
>= itemsToInsertBeforeCurrentRange
+ range
.index
+ range
.count
) {
79 m_logicalHeightHintCache
[targetIndex
] = m_logicalHeightHintCache
[sourceIndex
];
84 // Then: insert QSizeF() for the items which are inserted into 'range'.
85 while (targetIndex
>= itemsToInsertBeforeCurrentRange
+ range
.index
) {
86 m_logicalHeightHintCache
[targetIndex
] = 0.0;
91 m_needsResolving
= true;
93 Q_ASSERT(m_logicalHeightHintCache
.count() == m_itemListView
->model()->count());
96 void KItemListSizeHintResolver::itemsRemoved(const KItemRangeList
& itemRanges
)
98 const QVector
<qreal
>::iterator begin
= m_logicalHeightHintCache
.begin();
99 const QVector
<qreal
>::iterator end
= m_logicalHeightHintCache
.end();
101 KItemRangeList::const_iterator rangeIt
= itemRanges
.constBegin();
102 const KItemRangeList::const_iterator rangeEnd
= itemRanges
.constEnd();
104 QVector
<qreal
>::iterator destIt
= begin
+ rangeIt
->index
;
105 QVector
<qreal
>::iterator srcIt
= destIt
+ rangeIt
->count
;
109 while (srcIt
!= end
) {
114 if (rangeIt
!= rangeEnd
&& srcIt
== begin
+ rangeIt
->index
) {
115 // Skip the items in the next removed range.
116 srcIt
+= rangeIt
->count
;
121 m_logicalHeightHintCache
.erase(destIt
, end
);
123 // Note that the cache size might temporarily not match the model size if
124 // this function is called from KItemListView::setModel() to empty the cache.
125 if (!m_logicalHeightHintCache
.isEmpty() && m_itemListView
->model()) {
126 Q_ASSERT(m_logicalHeightHintCache
.count() == m_itemListView
->model()->count());
130 void KItemListSizeHintResolver::itemsMoved(const KItemRange
& range
, const QList
<int>& movedToIndexes
)
132 QVector
<qreal
> newLogicalHeightHintCache(m_logicalHeightHintCache
);
134 const int movedRangeEnd
= range
.index
+ range
.count
;
135 for (int i
= range
.index
; i
< movedRangeEnd
; ++i
) {
136 const int newIndex
= movedToIndexes
.at(i
- range
.index
);
137 newLogicalHeightHintCache
[newIndex
] = m_logicalHeightHintCache
.at(i
);
140 m_logicalHeightHintCache
= newLogicalHeightHintCache
;
143 void KItemListSizeHintResolver::itemsChanged(int index
, int count
, const QSet
<QByteArray
>& roles
)
147 m_logicalHeightHintCache
[index
] = 0.0;
152 m_needsResolving
= true;
155 void KItemListSizeHintResolver::clearCache()
157 m_logicalHeightHintCache
.fill(0.0);
158 m_needsResolving
= true;
161 void KItemListSizeHintResolver::updateCache()
163 if (m_needsResolving
) {
164 m_itemListView
->calculateItemSizeHints(m_logicalHeightHintCache
, m_logicalWidthHint
);
165 // Set logical height as the max cached height (if the cache is not empty).
166 if (m_logicalHeightHintCache
.isEmpty()) {
167 m_logicalHeightHint
= 0.0;
169 m_logicalHeightHint
= *std::max_element(m_logicalHeightHintCache
.begin(), m_logicalHeightHintCache
.end());
170 m_minHeightHint
= *std::min_element(m_logicalHeightHintCache
.begin(), m_logicalHeightHintCache
.end());
172 m_needsResolving
= false;