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),
30 m_needsResolving(false)
34 KItemListSizeHintResolver::~KItemListSizeHintResolver()
38 QSizeF
KItemListSizeHintResolver::maxSizeHint()
41 return QSizeF(m_logicalWidthHint
, m_logicalHeightHint
);
44 QSizeF
KItemListSizeHintResolver::minSizeHint()
47 return QSizeF(m_logicalWidthHint
, m_minHeightHint
);
50 QSizeF
KItemListSizeHintResolver::sizeHint(int index
)
53 return QSizeF(m_logicalWidthHint
, m_logicalHeightHintCache
.at(index
));
56 void KItemListSizeHintResolver::itemsInserted(const KItemRangeList
& itemRanges
)
58 int insertedCount
= 0;
59 foreach (const KItemRange
& range
, itemRanges
) {
60 insertedCount
+= range
.count
;
63 const int currentCount
= m_logicalHeightHintCache
.count();
64 m_logicalHeightHintCache
.reserve(currentCount
+ insertedCount
);
66 // We build the new list from the end to the beginning to mimize the
68 m_logicalHeightHintCache
.insert(m_logicalHeightHintCache
.end(), insertedCount
, 0.0);
70 int sourceIndex
= currentCount
- 1;
71 int targetIndex
= m_logicalHeightHintCache
.count() - 1;
72 int itemsToInsertBeforeCurrentRange
= insertedCount
;
74 for (int rangeIndex
= itemRanges
.count() - 1; rangeIndex
>= 0; --rangeIndex
) {
75 const KItemRange
& range
= itemRanges
.at(rangeIndex
);
76 itemsToInsertBeforeCurrentRange
-= range
.count
;
78 // First: move all existing items that must be put behind 'range'.
79 while (targetIndex
>= itemsToInsertBeforeCurrentRange
+ range
.index
+ range
.count
) {
80 m_logicalHeightHintCache
[targetIndex
] = m_logicalHeightHintCache
[sourceIndex
];
85 // Then: insert QSizeF() for the items which are inserted into 'range'.
86 while (targetIndex
>= itemsToInsertBeforeCurrentRange
+ range
.index
) {
87 m_logicalHeightHintCache
[targetIndex
] = 0.0;
92 m_needsResolving
= true;
94 Q_ASSERT(m_logicalHeightHintCache
.count() == m_itemListView
->model()->count());
97 void KItemListSizeHintResolver::itemsRemoved(const KItemRangeList
& itemRanges
)
99 const QVector
<qreal
>::iterator begin
= m_logicalHeightHintCache
.begin();
100 const QVector
<qreal
>::iterator end
= m_logicalHeightHintCache
.end();
102 KItemRangeList::const_iterator rangeIt
= itemRanges
.constBegin();
103 const KItemRangeList::const_iterator rangeEnd
= itemRanges
.constEnd();
105 QVector
<qreal
>::iterator destIt
= begin
+ rangeIt
->index
;
106 QVector
<qreal
>::iterator srcIt
= destIt
+ rangeIt
->count
;
110 while (srcIt
!= end
) {
115 if (rangeIt
!= rangeEnd
&& srcIt
== begin
+ rangeIt
->index
) {
116 // Skip the items in the next removed range.
117 srcIt
+= rangeIt
->count
;
122 m_logicalHeightHintCache
.erase(destIt
, end
);
124 // Note that the cache size might temporarily not match the model size if
125 // this function is called from KItemListView::setModel() to empty the cache.
126 if (!m_logicalHeightHintCache
.isEmpty() && m_itemListView
->model()) {
127 Q_ASSERT(m_logicalHeightHintCache
.count() == m_itemListView
->model()->count());
131 void KItemListSizeHintResolver::itemsMoved(const KItemRange
& range
, const QList
<int>& movedToIndexes
)
133 QVector
<qreal
> newLogicalHeightHintCache(m_logicalHeightHintCache
);
135 const int movedRangeEnd
= range
.index
+ range
.count
;
136 for (int i
= range
.index
; i
< movedRangeEnd
; ++i
) {
137 const int newIndex
= movedToIndexes
.at(i
- range
.index
);
138 newLogicalHeightHintCache
[newIndex
] = m_logicalHeightHintCache
.at(i
);
141 m_logicalHeightHintCache
= newLogicalHeightHintCache
;
144 void KItemListSizeHintResolver::itemsChanged(int index
, int count
, const QSet
<QByteArray
>& roles
)
148 m_logicalHeightHintCache
[index
] = 0.0;
153 m_needsResolving
= true;
156 void KItemListSizeHintResolver::clearCache()
158 m_logicalHeightHintCache
.fill(0.0);
159 m_needsResolving
= true;
162 void KItemListSizeHintResolver::updateCache()
164 if (m_needsResolving
) {
165 m_itemListView
->calculateItemSizeHints(m_logicalHeightHintCache
, m_logicalWidthHint
);
166 // Set logical height as the max cached height (if the cache is not empty).
167 if (m_logicalHeightHintCache
.isEmpty()) {
168 m_logicalHeightHint
= 0.0;
170 m_logicalHeightHint
= *std::max_element(m_logicalHeightHintCache
.begin(), m_logicalHeightHintCache
.end());
171 m_minHeightHint
= *std::min_element(m_logicalHeightHintCache
.begin(), m_logicalHeightHintCache
.end());
173 m_needsResolving
= false;