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 "kitemlistviewlayouter_p.h"
22 #include "kitemmodelbase.h"
23 #include "kitemlistsizehintresolver_p.h"
27 #define KITEMLISTVIEWLAYOUTER_DEBUG
31 const int HeaderHeight
= 50;
34 KItemListViewLayouter::KItemListViewLayouter(QObject
* parent
) :
37 m_visibleIndexesDirty(true),
39 m_scrollOrientation(Qt::Vertical
),
43 m_sizeHintResolver(0),
46 m_firstVisibleIndex(-1),
47 m_lastVisibleIndex(-1),
48 m_firstVisibleGroupIndex(-1),
58 KItemListViewLayouter::~KItemListViewLayouter()
62 void KItemListViewLayouter::setScrollOrientation(Qt::Orientation orientation
)
64 if (m_scrollOrientation
!= orientation
) {
65 m_scrollOrientation
= orientation
;
70 Qt::Orientation
KItemListViewLayouter::scrollOrientation() const
72 return m_scrollOrientation
;
75 void KItemListViewLayouter::setSize(const QSizeF
& size
)
83 QSizeF
KItemListViewLayouter::size() const
88 void KItemListViewLayouter::setItemSize(const QSizeF
& size
)
90 if (m_itemSize
!= size
) {
96 QSizeF
KItemListViewLayouter::itemSize() const
101 void KItemListViewLayouter::setOffset(qreal offset
)
103 if (m_offset
!= offset
) {
105 m_visibleIndexesDirty
= true;
109 qreal
KItemListViewLayouter::offset() const
114 void KItemListViewLayouter::setModel(const KItemModelBase
* model
)
116 if (m_model
!= model
) {
122 const KItemModelBase
* KItemListViewLayouter::model() const
127 void KItemListViewLayouter::setSizeHintResolver(const KItemListSizeHintResolver
* sizeHintResolver
)
129 if (m_sizeHintResolver
!= sizeHintResolver
) {
130 m_sizeHintResolver
= sizeHintResolver
;
135 const KItemListSizeHintResolver
* KItemListViewLayouter::sizeHintResolver() const
137 return m_sizeHintResolver
;
140 qreal
KItemListViewLayouter::maximumOffset() const
142 const_cast<KItemListViewLayouter
*>(this)->doLayout();
143 return m_maximumOffset
;
146 int KItemListViewLayouter::firstVisibleIndex() const
148 const_cast<KItemListViewLayouter
*>(this)->doLayout();
149 return m_firstVisibleIndex
;
152 int KItemListViewLayouter::lastVisibleIndex() const
154 const_cast<KItemListViewLayouter
*>(this)->doLayout();
155 return m_lastVisibleIndex
;
158 QRectF
KItemListViewLayouter::itemBoundingRect(int index
) const
160 const_cast<KItemListViewLayouter
*>(this)->doLayout();
161 if (index
< 0 || index
>= m_itemBoundingRects
.count()) {
165 if (m_scrollOrientation
== Qt::Horizontal
) {
166 // Rotate the logical direction which is always vertical by 90°
167 // to get the physical horizontal direction
168 const QRectF
& b
= m_itemBoundingRects
[index
];
169 QRectF
bounds(b
.y(), b
.x(), b
.height(), b
.width());
170 QPointF pos
= bounds
.topLeft();
171 pos
.rx() -= m_offset
;
176 QRectF bounds
= m_itemBoundingRects
[index
];
177 QPointF pos
= bounds
.topLeft();
178 pos
.ry() -= m_offset
;
183 int KItemListViewLayouter::maximumVisibleItems() const
185 const_cast<KItemListViewLayouter
*>(this)->doLayout();
187 const int height
= static_cast<int>(m_size
.height());
188 const int rowHeight
= static_cast<int>(m_itemSize
.height());
189 int rows
= height
/ rowHeight
;
190 if (height
% rowHeight
!= 0) {
194 return rows
* m_columnCount
;
197 bool KItemListViewLayouter::isFirstGroupItem(int itemIndex
) const
199 return m_groupIndexes
.contains(itemIndex
);
202 void KItemListViewLayouter::markAsDirty()
207 void KItemListViewLayouter::doLayout()
210 #ifdef KITEMLISTVIEWLAYOUTER_DEBUG
215 m_visibleIndexesDirty
= true;
217 QSizeF itemSize
= m_itemSize
;
218 QSizeF size
= m_size
;
220 const bool horizontalScrolling
= (m_scrollOrientation
== Qt::Horizontal
);
221 if (horizontalScrolling
) {
222 itemSize
.setWidth(m_itemSize
.height());
223 itemSize
.setHeight(m_itemSize
.width());
224 size
.setWidth(m_size
.height());
225 size
.setHeight(m_size
.width());
228 m_columnWidth
= itemSize
.width();
229 m_columnCount
= qMax(1, int(size
.width() / m_columnWidth
));
232 const int itemCount
= m_model
->count();
233 if (itemCount
> m_columnCount
) {
234 // Apply the unused width equally to each column
235 const qreal unusedWidth
= size
.width() - m_columnCount
* m_columnWidth
;
236 const qreal columnInc
= unusedWidth
/ (m_columnCount
+ 1);
237 m_columnWidth
+= columnInc
;
238 m_xPosInc
+= columnInc
;
241 int rowCount
= itemCount
/ m_columnCount
;
242 if (itemCount
% m_columnCount
!= 0) {
246 m_itemBoundingRects
.reserve(itemCount
);
252 while (index
< itemCount
) {
254 qreal maxItemHeight
= itemSize
.height();
257 while (index
< itemCount
&& column
< m_columnCount
) {
258 qreal requiredItemHeight
= itemSize
.height();
259 if (m_sizeHintResolver
) {
260 const QSizeF sizeHint
= m_sizeHintResolver
->sizeHint(index
);
261 const qreal sizeHintHeight
= horizontalScrolling
? sizeHint
.width() : sizeHint
.height();
262 if (sizeHintHeight
> requiredItemHeight
) {
263 requiredItemHeight
= sizeHintHeight
;
267 const QRectF
bounds(x
, y
, itemSize
.width(), requiredItemHeight
);
268 if (index
< m_itemBoundingRects
.count()) {
269 m_itemBoundingRects
[index
] = bounds
;
271 m_itemBoundingRects
.append(bounds
);
274 maxItemHeight
= qMax(maxItemHeight
, requiredItemHeight
);
283 if (m_itemBoundingRects
.count() > itemCount
) {
284 m_itemBoundingRects
.erase(m_itemBoundingRects
.begin() + itemCount
,
285 m_itemBoundingRects
.end());
288 m_maximumOffset
= (itemCount
> 0) ? m_itemBoundingRects
.last().bottom() : 0;
290 m_grouped
= !m_model
->groupRole().isEmpty();
292 createGroupHeaders();
294 const int lastGroupItemCount = m_model->count() - m_groups.last().firstItemIndex;
295 m_maximumOffset = m_groups.last().y + (lastGroupItemCount / m_columnCount) * m_rowHeight;
296 if (lastGroupItemCount % m_columnCount != 0) {
297 m_maximumOffset += m_rowHeight;
300 // m_maximumOffset = m_minimumRowHeight * rowCount;
303 #ifdef KITEMLISTVIEWLAYOUTER_DEBUG
304 kDebug() << "[TIME] doLayout() for " << m_model
->count() << "items:" << timer
.elapsed();
310 updateGroupedVisibleIndexes();
312 updateVisibleIndexes();
316 void KItemListViewLayouter::updateVisibleIndexes()
318 if (!m_visibleIndexesDirty
) {
322 Q_ASSERT(!m_grouped
);
325 if (m_model
->count() <= 0) {
326 m_firstVisibleIndex
= -1;
327 m_lastVisibleIndex
= -1;
328 m_visibleIndexesDirty
= false;
332 const bool horizontalScrolling
= (m_scrollOrientation
== Qt::Horizontal
);
333 const int minimumHeight
= horizontalScrolling
? m_itemSize
.width()
334 : m_itemSize
.height();
336 // Calculate the first visible index:
337 // 1. Guess the index by using the minimum row height
338 const int maxIndex
= m_model
->count() - 1;
339 m_firstVisibleIndex
= int(m_offset
/ minimumHeight
) * m_columnCount
;
341 // 2. Decrease the index by checking the real row heights
342 int prevRowIndex
= m_firstVisibleIndex
- m_columnCount
;
343 while (prevRowIndex
> maxIndex
) {
344 prevRowIndex
-= m_columnCount
;
347 while (prevRowIndex
>= 0 && m_itemBoundingRects
[prevRowIndex
].bottom() >= m_offset
) {
348 m_firstVisibleIndex
= prevRowIndex
;
349 prevRowIndex
-= m_columnCount
;
351 m_firstVisibleIndex
= qBound(0, m_firstVisibleIndex
, maxIndex
);
353 // Calculate the last visible index
354 const int visibleHeight
= horizontalScrolling
? m_size
.width() : m_size
.height();
355 const qreal bottom
= m_offset
+ visibleHeight
;
356 m_lastVisibleIndex
= m_firstVisibleIndex
; // first visible row, first column
357 int nextRowIndex
= m_lastVisibleIndex
+ m_columnCount
;
358 while (nextRowIndex
<= maxIndex
&& m_itemBoundingRects
[nextRowIndex
].y() <= bottom
) {
359 m_lastVisibleIndex
= nextRowIndex
;
360 nextRowIndex
+= m_columnCount
;
362 m_lastVisibleIndex
+= m_columnCount
- 1; // move it to the last column
363 m_lastVisibleIndex
= qBound(0, m_lastVisibleIndex
, maxIndex
);
365 m_visibleIndexesDirty
= false;
368 void KItemListViewLayouter::updateGroupedVisibleIndexes()
370 if (!m_visibleIndexesDirty
) {
377 if (m_model
->count() <= 0) {
378 m_firstVisibleIndex
= -1;
379 m_lastVisibleIndex
= -1;
380 m_visibleIndexesDirty
= false;
384 // Find the first visible group
385 const int lastGroupIndex
= m_groups
.count() - 1;
386 int groupIndex
= lastGroupIndex
;
387 for (int i
= 1; i
< m_groups
.count(); ++i
) {
388 if (m_groups
[i
].y
>= m_offset
) {
394 // Calculate the first visible index
395 qreal groupY
= m_groups
[groupIndex
].y
;
396 m_firstVisibleIndex
= m_groups
[groupIndex
].firstItemIndex
;
397 const int invisibleRowCount
= int(m_offset
- groupY
) / int(m_itemSize
.height());
398 m_firstVisibleIndex
+= invisibleRowCount
* m_columnCount
;
399 if (groupIndex
+ 1 <= lastGroupIndex
) {
400 // Check whether the calculated first visible index remains inside the current
401 // group. If this is not the case let the first element of the next group be the first
403 const int nextGroupIndex
= m_groups
[groupIndex
+ 1].firstItemIndex
;
404 if (m_firstVisibleIndex
> nextGroupIndex
) {
405 m_firstVisibleIndex
= nextGroupIndex
;
409 m_firstVisibleGroupIndex
= groupIndex
;
411 const int maxIndex
= m_model
->count() - 1;
412 m_firstVisibleIndex
= qBound(0, m_firstVisibleIndex
, maxIndex
);
414 // Calculate the last visible index: Find group where the last visible item is shown.
415 const qreal visibleBottom
= m_offset
+ m_size
.height(); // TODO: respect Qt::Horizontal alignment
416 while ((groupIndex
< lastGroupIndex
) && (m_groups
[groupIndex
+ 1].y
< visibleBottom
)) {
420 groupY
= m_groups
[groupIndex
].y
;
421 m_lastVisibleIndex
= m_groups
[groupIndex
].firstItemIndex
;
422 const int availableHeight
= static_cast<int>(visibleBottom
- groupY
);
423 int visibleRowCount
= availableHeight
/ int(m_itemSize
.height());
424 if (availableHeight
% int(m_itemSize
.height()) != 0) {
427 m_lastVisibleIndex
+= visibleRowCount
* m_columnCount
- 1;
429 if (groupIndex
+ 1 <= lastGroupIndex
) {
430 // Check whether the calculate last visible index remains inside the current group.
431 // If this is not the case let the last element of this group be the last visible index.
432 const int nextGroupIndex
= m_groups
[groupIndex
+ 1].firstItemIndex
;
433 if (m_lastVisibleIndex
>= nextGroupIndex
) {
434 m_lastVisibleIndex
= nextGroupIndex
- 1;
437 //Q_ASSERT(m_lastVisibleIndex < m_model->count());
438 m_lastVisibleIndex
= qBound(0, m_lastVisibleIndex
, maxIndex
);
440 m_visibleIndexesDirty
= false;
443 void KItemListViewLayouter::createGroupHeaders()
446 m_groupIndexes
.clear();
450 numbers
<< 0 << 5 << 6 << 13 << 20 << 25 << 30 << 35 << 50;
453 for (int i
= 0; i
< numbers
.count(); ++i
) {
455 const int previousGroupItemCount
= numbers
[i
] - m_groups
.last().firstItemIndex
;
456 int previousGroupRowCount
= previousGroupItemCount
/ m_columnCount
;
457 if (previousGroupItemCount
% m_columnCount
!= 0) {
458 ++previousGroupRowCount
;
460 const qreal previousGroupHeight
= previousGroupRowCount
* m_itemSize
.height();
461 y
+= previousGroupHeight
;
466 itemGroup
.firstItemIndex
= numbers
[i
];
469 m_groups
.append(itemGroup
);
470 m_groupIndexes
.insert(itemGroup
.firstItemIndex
);
474 #include "kitemlistviewlayouter_p.moc"