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
29 KItemListViewLayouter::KItemListViewLayouter(QObject
* parent
) :
32 m_visibleIndexesDirty(true),
33 m_scrollOrientation(Qt::Vertical
),
39 m_sizeHintResolver(0),
41 m_maximumScrollOffset(0),
43 m_maximumItemOffset(0),
44 m_firstVisibleIndex(-1),
45 m_lastVisibleIndex(-1),
50 m_groupHeaderHeight(0),
51 m_groupHeaderMargin(0),
56 KItemListViewLayouter::~KItemListViewLayouter()
60 void KItemListViewLayouter::setScrollOrientation(Qt::Orientation orientation
)
62 if (m_scrollOrientation
!= orientation
) {
63 m_scrollOrientation
= orientation
;
68 Qt::Orientation
KItemListViewLayouter::scrollOrientation() const
70 return m_scrollOrientation
;
73 void KItemListViewLayouter::setSize(const QSizeF
& size
)
81 QSizeF
KItemListViewLayouter::size() const
86 void KItemListViewLayouter::setItemSize(const QSizeF
& size
)
88 if (m_itemSize
!= size
) {
94 QSizeF
KItemListViewLayouter::itemSize() const
99 void KItemListViewLayouter::setItemMargin(const QSizeF
& margin
)
101 if (m_itemMargin
!= margin
) {
102 m_itemMargin
= margin
;
107 QSizeF
KItemListViewLayouter::itemMargin() const
112 void KItemListViewLayouter::setHeaderHeight(qreal height
)
114 if (m_headerHeight
!= height
) {
115 m_headerHeight
= height
;
120 qreal
KItemListViewLayouter::headerHeight() const
122 return m_headerHeight
;
125 void KItemListViewLayouter::setGroupHeaderHeight(qreal height
)
127 if (m_groupHeaderHeight
!= height
) {
128 m_groupHeaderHeight
= height
;
133 qreal
KItemListViewLayouter::groupHeaderHeight() const
135 return m_groupHeaderHeight
;
138 void KItemListViewLayouter::setGroupHeaderMargin(qreal margin
)
140 if (m_groupHeaderMargin
!= margin
) {
141 m_groupHeaderMargin
= margin
;
146 qreal
KItemListViewLayouter::groupHeaderMargin() const
148 return m_groupHeaderMargin
;
151 void KItemListViewLayouter::setScrollOffset(qreal offset
)
153 if (m_scrollOffset
!= offset
) {
154 m_scrollOffset
= offset
;
155 m_visibleIndexesDirty
= true;
159 qreal
KItemListViewLayouter::scrollOffset() const
161 return m_scrollOffset
;
164 qreal
KItemListViewLayouter::maximumScrollOffset() const
166 const_cast<KItemListViewLayouter
*>(this)->doLayout();
167 return m_maximumScrollOffset
;
170 void KItemListViewLayouter::setItemOffset(qreal offset
)
172 if (m_itemOffset
!= offset
) {
173 m_itemOffset
= offset
;
174 m_visibleIndexesDirty
= true;
178 qreal
KItemListViewLayouter::itemOffset() const
183 qreal
KItemListViewLayouter::maximumItemOffset() const
185 const_cast<KItemListViewLayouter
*>(this)->doLayout();
186 return m_maximumItemOffset
;
189 void KItemListViewLayouter::setModel(const KItemModelBase
* model
)
191 if (m_model
!= model
) {
197 const KItemModelBase
* KItemListViewLayouter::model() const
202 void KItemListViewLayouter::setSizeHintResolver(const KItemListSizeHintResolver
* sizeHintResolver
)
204 if (m_sizeHintResolver
!= sizeHintResolver
) {
205 m_sizeHintResolver
= sizeHintResolver
;
210 const KItemListSizeHintResolver
* KItemListViewLayouter::sizeHintResolver() const
212 return m_sizeHintResolver
;
215 int KItemListViewLayouter::firstVisibleIndex() const
217 const_cast<KItemListViewLayouter
*>(this)->doLayout();
218 return m_firstVisibleIndex
;
221 int KItemListViewLayouter::lastVisibleIndex() const
223 const_cast<KItemListViewLayouter
*>(this)->doLayout();
224 return m_lastVisibleIndex
;
227 QRectF
KItemListViewLayouter::itemRect(int index
) const
229 const_cast<KItemListViewLayouter
*>(this)->doLayout();
230 if (index
< 0 || index
>= m_itemRects
.count()) {
234 if (m_scrollOrientation
== Qt::Horizontal
) {
235 // Rotate the logical direction which is always vertical by 90°
236 // to get the physical horizontal direction
237 const QRectF
& b
= m_itemRects
[index
];
238 QRectF
bounds(b
.y(), b
.x(), b
.height(), b
.width());
239 QPointF pos
= bounds
.topLeft();
240 pos
.rx() -= m_scrollOffset
;
245 QRectF bounds
= m_itemRects
[index
];
246 bounds
.moveTo(bounds
.topLeft() - QPointF(m_itemOffset
, m_scrollOffset
));
250 QRectF
KItemListViewLayouter::groupHeaderRect(int index
) const
252 const_cast<KItemListViewLayouter
*>(this)->doLayout();
254 const QRectF firstItemRect
= itemRect(index
);
255 QPointF pos
= firstItemRect
.topLeft();
261 if (m_scrollOrientation
== Qt::Vertical
) {
263 pos
.ry() -= m_groupHeaderHeight
;
264 size
= QSizeF(m_size
.width(), m_groupHeaderHeight
);
266 pos
.rx() -= m_itemMargin
.width();
269 // Determine the maximum width used in the
270 // current column. As the scroll-direction is
271 // Qt::Horizontal and m_itemRects is accessed directly,
272 // the logical height represents the visual width.
273 qreal width
= minimumGroupHeaderWidth();
274 const qreal y
= m_itemRects
[index
].y();
275 const int maxIndex
= m_itemRects
.count() - 1;
276 while (index
<= maxIndex
) {
277 QRectF bounds
= m_itemRects
[index
];
278 if (bounds
.y() != y
) {
282 if (bounds
.height() > width
) {
283 width
= bounds
.height();
289 size
= QSizeF(width
, m_size
.height());
291 return QRectF(pos
, size
);
294 int KItemListViewLayouter::maximumVisibleItems() const
296 const_cast<KItemListViewLayouter
*>(this)->doLayout();
298 const int height
= static_cast<int>(m_size
.height());
299 const int rowHeight
= static_cast<int>(m_itemSize
.height());
300 int rows
= height
/ rowHeight
;
301 if (height
% rowHeight
!= 0) {
305 return rows
* m_columnCount
;
308 bool KItemListViewLayouter::isFirstGroupItem(int itemIndex
) const
310 const_cast<KItemListViewLayouter
*>(this)->doLayout();
311 return m_groupItemIndexes
.contains(itemIndex
);
314 void KItemListViewLayouter::markAsDirty()
319 void KItemListViewLayouter::doLayout()
322 #ifdef KITEMLISTVIEWLAYOUTER_DEBUG
326 m_visibleIndexesDirty
= true;
328 QSizeF itemSize
= m_itemSize
;
329 QSizeF itemMargin
= m_itemMargin
;
330 QSizeF size
= m_size
;
332 const bool grouped
= createGroupHeaders();
334 const bool horizontalScrolling
= (m_scrollOrientation
== Qt::Horizontal
);
335 if (horizontalScrolling
) {
336 // Flip everything so that the layout logically can work like having
337 // a vertical scrolling
338 itemSize
.setWidth(m_itemSize
.height());
339 itemSize
.setHeight(m_itemSize
.width());
340 itemMargin
.setWidth(m_itemMargin
.height());
341 itemMargin
.setHeight(m_itemMargin
.width());
342 size
.setWidth(m_size
.height());
343 size
.setHeight(m_size
.width());
346 // In the horizontal scrolling case all groups are aligned
347 // at the top, which decreases the available height. For the
348 // flipped data this means that the width must be decreased.
349 size
.rwidth() -= m_groupHeaderHeight
;
353 m_columnWidth
= itemSize
.width() + itemMargin
.width();
354 const qreal widthForColumns
= size
.width() - itemMargin
.width();
355 m_columnCount
= qMax(1, int(widthForColumns
/ m_columnWidth
));
356 m_xPosInc
= itemMargin
.width();
358 const int itemCount
= m_model
->count();
359 if (itemCount
> m_columnCount
&& m_columnWidth
>= 32) {
360 // Apply the unused width equally to each column
361 const qreal unusedWidth
= widthForColumns
- m_columnCount
* m_columnWidth
;
362 if (unusedWidth
> 0) {
363 const qreal columnInc
= unusedWidth
/ (m_columnCount
+ 1);
364 m_columnWidth
+= columnInc
;
365 m_xPosInc
+= columnInc
;
369 int rowCount
= itemCount
/ m_columnCount
;
370 if (itemCount
% m_columnCount
!= 0) {
374 m_itemRects
.reserve(itemCount
);
376 qreal y
= m_headerHeight
+ itemMargin
.height();
380 while (index
< itemCount
) {
382 qreal maxItemHeight
= itemSize
.height();
385 if (horizontalScrolling
) {
386 // All group headers will always be aligned on the top and not
387 // flipped like the other properties
388 x
+= m_groupHeaderHeight
;
391 if (m_groupItemIndexes
.contains(index
)) {
392 // The item is the first item of a group.
393 // Increase the y-position to provide space
394 // for the group header.
396 // Only add a margin if there has been added another
397 // group already before
398 y
+= m_groupHeaderMargin
;
399 } else if (!horizontalScrolling
) {
400 // The first group header should be aligned on top
401 y
-= itemMargin
.height();
404 if (!horizontalScrolling
) {
405 y
+= m_groupHeaderHeight
;
411 while (index
< itemCount
&& column
< m_columnCount
) {
412 qreal requiredItemHeight
= itemSize
.height();
413 if (m_sizeHintResolver
) {
414 const QSizeF sizeHint
= m_sizeHintResolver
->sizeHint(index
);
415 const qreal sizeHintHeight
= horizontalScrolling
? sizeHint
.width() : sizeHint
.height();
416 if (sizeHintHeight
> requiredItemHeight
) {
417 requiredItemHeight
= sizeHintHeight
;
421 const QRectF
bounds(x
, y
, itemSize
.width(), requiredItemHeight
);
422 if (index
< m_itemRects
.count()) {
423 m_itemRects
[index
] = bounds
;
425 m_itemRects
.append(bounds
);
428 if (grouped
&& horizontalScrolling
) {
429 // When grouping is enabled in the horizontal mode, the header alignment
431 // Header-1 Header-2 Header-3
432 // Item 1 Item 4 Item 7
433 // Item 2 Item 5 Item 8
434 // Item 3 Item 6 Item 9
435 // In this case 'requiredItemHeight' represents the column-width. We don't
436 // check the content of the header in the layouter to determine the required
437 // width, hence assure that at least a minimal width of 15 characters is given
438 // (in average a character requires the halve width of the font height).
440 // TODO: Let the group headers provide a minimum width and respect this width here
441 const qreal headerWidth
= minimumGroupHeaderWidth();
442 if (requiredItemHeight
< headerWidth
) {
443 requiredItemHeight
= headerWidth
;
447 maxItemHeight
= qMax(maxItemHeight
, requiredItemHeight
);
452 if (grouped
&& m_groupItemIndexes
.contains(index
)) {
453 // The item represents the first index of a group
454 // and must aligned in the first column
459 y
+= maxItemHeight
+ itemMargin
.height();
462 if (m_itemRects
.count() > itemCount
) {
463 m_itemRects
.erase(m_itemRects
.begin() + itemCount
,
468 // Calculate the maximum y-range of the last row for m_maximumScrollOffset
469 m_maximumScrollOffset
= m_itemRects
.last().bottom();
470 const qreal rowY
= m_itemRects
.last().y();
472 int index
= m_itemRects
.count() - 2;
473 while (index
>= 0 && m_itemRects
.at(index
).bottom() >= rowY
) {
474 m_maximumScrollOffset
= qMax(m_maximumScrollOffset
, m_itemRects
.at(index
).bottom());
478 m_maximumScrollOffset
+= itemMargin
.height();
480 m_maximumItemOffset
= m_columnCount
* m_columnWidth
;
482 m_maximumScrollOffset
= 0;
483 m_maximumItemOffset
= 0;
486 #ifdef KITEMLISTVIEWLAYOUTER_DEBUG
487 kDebug() << "[TIME] doLayout() for " << m_model
->count() << "items:" << timer
.elapsed();
492 updateVisibleIndexes();
495 void KItemListViewLayouter::updateVisibleIndexes()
497 if (!m_visibleIndexesDirty
) {
503 if (m_model
->count() <= 0) {
504 m_firstVisibleIndex
= -1;
505 m_lastVisibleIndex
= -1;
506 m_visibleIndexesDirty
= false;
510 const int maxIndex
= m_model
->count() - 1;
512 // Calculate the first visible index that is fully visible
517 mid
= (min
+ max
) / 2;
518 if (m_itemRects
[mid
].top() < m_scrollOffset
) {
523 } while (min
<= max
);
526 // Include the row before the first fully visible index, as it might
528 if (m_itemRects
[mid
].top() >= m_scrollOffset
) {
530 Q_ASSERT(m_itemRects
[mid
].top() < m_scrollOffset
);
533 const qreal rowTop
= m_itemRects
[mid
].top();
534 while (mid
> 0 && m_itemRects
[mid
- 1].top() == rowTop
) {
538 m_firstVisibleIndex
= mid
;
540 // Calculate the last visible index that is (at least partly) visible
541 const int visibleHeight
= (m_scrollOrientation
== Qt::Horizontal
) ? m_size
.width() : m_size
.height();
542 qreal bottom
= m_scrollOffset
+ visibleHeight
;
543 if (m_model
->groupedSorting()) {
544 bottom
+= m_groupHeaderHeight
;
547 min
= m_firstVisibleIndex
;
550 mid
= (min
+ max
) / 2;
551 if (m_itemRects
[mid
].y() <= bottom
) {
556 } while (min
<= max
);
558 while (mid
> 0 && m_itemRects
[mid
].y() > bottom
) {
561 m_lastVisibleIndex
= mid
;
563 m_visibleIndexesDirty
= false;
566 bool KItemListViewLayouter::createGroupHeaders()
568 if (!m_model
->groupedSorting()) {
572 m_groupItemIndexes
.clear();
574 const QList
<QPair
<int, QVariant
> > groups
= m_model
->groups();
575 if (groups
.isEmpty()) {
579 for (int i
= 0; i
< groups
.count(); ++i
) {
580 const int firstItemIndex
= groups
.at(i
).first
;
581 m_groupItemIndexes
.insert(firstItemIndex
);
587 qreal
KItemListViewLayouter::minimumGroupHeaderWidth() const
592 #include "kitemlistviewlayouter_p.moc"