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.h"
22 #include <kitemviews/kitemmodelbase.h>
23 #include "kitemlistsizehintresolver.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
)
76 if (m_scrollOrientation
== Qt::Vertical
) {
77 if (m_size
.width() != size
.width()) {
80 } else if (m_size
.height() != size
.height()) {
85 m_visibleIndexesDirty
= true;
89 QSizeF
KItemListViewLayouter::size() const
94 void KItemListViewLayouter::setItemSize(const QSizeF
& size
)
96 if (m_itemSize
!= size
) {
102 QSizeF
KItemListViewLayouter::itemSize() const
107 void KItemListViewLayouter::setItemMargin(const QSizeF
& margin
)
109 if (m_itemMargin
!= margin
) {
110 m_itemMargin
= margin
;
115 QSizeF
KItemListViewLayouter::itemMargin() const
120 void KItemListViewLayouter::setHeaderHeight(qreal height
)
122 if (m_headerHeight
!= height
) {
123 m_headerHeight
= height
;
128 qreal
KItemListViewLayouter::headerHeight() const
130 return m_headerHeight
;
133 void KItemListViewLayouter::setGroupHeaderHeight(qreal height
)
135 if (m_groupHeaderHeight
!= height
) {
136 m_groupHeaderHeight
= height
;
141 qreal
KItemListViewLayouter::groupHeaderHeight() const
143 return m_groupHeaderHeight
;
146 void KItemListViewLayouter::setGroupHeaderMargin(qreal margin
)
148 if (m_groupHeaderMargin
!= margin
) {
149 m_groupHeaderMargin
= margin
;
154 qreal
KItemListViewLayouter::groupHeaderMargin() const
156 return m_groupHeaderMargin
;
159 void KItemListViewLayouter::setScrollOffset(qreal offset
)
161 if (m_scrollOffset
!= offset
) {
162 m_scrollOffset
= offset
;
163 m_visibleIndexesDirty
= true;
167 qreal
KItemListViewLayouter::scrollOffset() const
169 return m_scrollOffset
;
172 qreal
KItemListViewLayouter::maximumScrollOffset() const
174 const_cast<KItemListViewLayouter
*>(this)->doLayout();
175 return m_maximumScrollOffset
;
178 void KItemListViewLayouter::setItemOffset(qreal offset
)
180 if (m_itemOffset
!= offset
) {
181 m_itemOffset
= offset
;
182 m_visibleIndexesDirty
= true;
186 qreal
KItemListViewLayouter::itemOffset() const
191 qreal
KItemListViewLayouter::maximumItemOffset() const
193 const_cast<KItemListViewLayouter
*>(this)->doLayout();
194 return m_maximumItemOffset
;
197 void KItemListViewLayouter::setModel(const KItemModelBase
* model
)
199 if (m_model
!= model
) {
205 const KItemModelBase
* KItemListViewLayouter::model() const
210 void KItemListViewLayouter::setSizeHintResolver(const KItemListSizeHintResolver
* sizeHintResolver
)
212 if (m_sizeHintResolver
!= sizeHintResolver
) {
213 m_sizeHintResolver
= sizeHintResolver
;
218 const KItemListSizeHintResolver
* KItemListViewLayouter::sizeHintResolver() const
220 return m_sizeHintResolver
;
223 int KItemListViewLayouter::firstVisibleIndex() const
225 const_cast<KItemListViewLayouter
*>(this)->doLayout();
226 return m_firstVisibleIndex
;
229 int KItemListViewLayouter::lastVisibleIndex() const
231 const_cast<KItemListViewLayouter
*>(this)->doLayout();
232 return m_lastVisibleIndex
;
235 QRectF
KItemListViewLayouter::itemRect(int index
) const
237 const_cast<KItemListViewLayouter
*>(this)->doLayout();
238 if (index
< 0 || index
>= m_itemInfos
.count()) {
242 if (m_scrollOrientation
== Qt::Horizontal
) {
243 // Rotate the logical direction which is always vertical by 90°
244 // to get the physical horizontal direction
245 const QRectF
& b
= m_itemInfos
[index
].rect
;
246 QRectF
bounds(b
.y(), b
.x(), b
.height(), b
.width());
247 QPointF pos
= bounds
.topLeft();
248 pos
.rx() -= m_scrollOffset
;
253 QRectF bounds
= m_itemInfos
[index
].rect
;
254 bounds
.moveTo(bounds
.topLeft() - QPointF(m_itemOffset
, m_scrollOffset
));
258 QRectF
KItemListViewLayouter::groupHeaderRect(int index
) const
260 const_cast<KItemListViewLayouter
*>(this)->doLayout();
262 const QRectF firstItemRect
= itemRect(index
);
263 QPointF pos
= firstItemRect
.topLeft();
269 if (m_scrollOrientation
== Qt::Vertical
) {
271 pos
.ry() -= m_groupHeaderHeight
;
272 size
= QSizeF(m_size
.width(), m_groupHeaderHeight
);
274 pos
.rx() -= m_itemMargin
.width();
277 // Determine the maximum width used in the
278 // current column. As the scroll-direction is
279 // Qt::Horizontal and m_itemRects is accessed directly,
280 // the logical height represents the visual width.
281 qreal width
= minimumGroupHeaderWidth();
282 const qreal y
= m_itemInfos
[index
].rect
.y();
283 const int maxIndex
= m_itemInfos
.count() - 1;
284 while (index
<= maxIndex
) {
285 QRectF bounds
= m_itemInfos
[index
].rect
;
286 if (bounds
.y() != y
) {
290 if (bounds
.height() > width
) {
291 width
= bounds
.height();
297 size
= QSizeF(width
, m_size
.height());
299 return QRectF(pos
, size
);
302 int KItemListViewLayouter::itemColumn(int index
) const
304 const_cast<KItemListViewLayouter
*>(this)->doLayout();
305 if (index
< 0 || index
>= m_itemInfos
.count()) {
309 return (m_scrollOrientation
== Qt::Vertical
)
310 ? m_itemInfos
[index
].column
311 : m_itemInfos
[index
].row
;
314 int KItemListViewLayouter::itemRow(int index
) const
316 const_cast<KItemListViewLayouter
*>(this)->doLayout();
317 if (index
< 0 || index
>= m_itemInfos
.count()) {
321 return (m_scrollOrientation
== Qt::Vertical
)
322 ? m_itemInfos
[index
].row
323 : m_itemInfos
[index
].column
;
326 int KItemListViewLayouter::maximumVisibleItems() const
328 const_cast<KItemListViewLayouter
*>(this)->doLayout();
330 const int height
= static_cast<int>(m_size
.height());
331 const int rowHeight
= static_cast<int>(m_itemSize
.height());
332 int rows
= height
/ rowHeight
;
333 if (height
% rowHeight
!= 0) {
337 return rows
* m_columnCount
;
340 bool KItemListViewLayouter::isFirstGroupItem(int itemIndex
) const
342 const_cast<KItemListViewLayouter
*>(this)->doLayout();
343 return m_groupItemIndexes
.contains(itemIndex
);
346 void KItemListViewLayouter::markAsDirty()
353 bool KItemListViewLayouter::isDirty()
359 void KItemListViewLayouter::doLayout()
362 #ifdef KITEMLISTVIEWLAYOUTER_DEBUG
366 m_visibleIndexesDirty
= true;
368 QSizeF itemSize
= m_itemSize
;
369 QSizeF itemMargin
= m_itemMargin
;
370 QSizeF size
= m_size
;
372 const bool grouped
= createGroupHeaders();
374 const bool horizontalScrolling
= (m_scrollOrientation
== Qt::Horizontal
);
375 if (horizontalScrolling
) {
376 // Flip everything so that the layout logically can work like having
377 // a vertical scrolling
378 itemSize
.transpose();
379 itemMargin
.transpose();
383 // In the horizontal scrolling case all groups are aligned
384 // at the top, which decreases the available height. For the
385 // flipped data this means that the width must be decreased.
386 size
.rwidth() -= m_groupHeaderHeight
;
390 m_columnWidth
= itemSize
.width() + itemMargin
.width();
391 const qreal widthForColumns
= size
.width() - itemMargin
.width();
392 m_columnCount
= qMax(1, int(widthForColumns
/ m_columnWidth
));
393 m_xPosInc
= itemMargin
.width();
395 const int itemCount
= m_model
->count();
396 if (itemCount
> m_columnCount
&& m_columnWidth
>= 32) {
397 // Apply the unused width equally to each column
398 const qreal unusedWidth
= widthForColumns
- m_columnCount
* m_columnWidth
;
399 if (unusedWidth
> 0) {
400 const qreal columnInc
= unusedWidth
/ (m_columnCount
+ 1);
401 m_columnWidth
+= columnInc
;
402 m_xPosInc
+= columnInc
;
406 int rowCount
= itemCount
/ m_columnCount
;
407 if (itemCount
% m_columnCount
!= 0) {
411 m_itemInfos
.resize(itemCount
);
413 qreal y
= m_headerHeight
+ itemMargin
.height();
417 while (index
< itemCount
) {
419 qreal maxItemHeight
= itemSize
.height();
422 if (horizontalScrolling
) {
423 // All group headers will always be aligned on the top and not
424 // flipped like the other properties
425 x
+= m_groupHeaderHeight
;
428 if (m_groupItemIndexes
.contains(index
)) {
429 // The item is the first item of a group.
430 // Increase the y-position to provide space
431 // for the group header.
433 // Only add a margin if there has been added another
434 // group already before
435 y
+= m_groupHeaderMargin
;
436 } else if (!horizontalScrolling
) {
437 // The first group header should be aligned on top
438 y
-= itemMargin
.height();
441 if (!horizontalScrolling
) {
442 y
+= m_groupHeaderHeight
;
448 while (index
< itemCount
&& column
< m_columnCount
) {
449 qreal requiredItemHeight
= itemSize
.height();
450 if (m_sizeHintResolver
) {
451 const QSizeF sizeHint
= m_sizeHintResolver
->sizeHint(index
);
452 const qreal sizeHintHeight
= horizontalScrolling
? sizeHint
.width() : sizeHint
.height();
453 if (sizeHintHeight
> requiredItemHeight
) {
454 requiredItemHeight
= sizeHintHeight
;
458 ItemInfo
& itemInfo
= m_itemInfos
[index
];
459 itemInfo
.rect
= QRectF(x
, y
, itemSize
.width(), requiredItemHeight
);
460 itemInfo
.column
= column
;
463 if (grouped
&& horizontalScrolling
) {
464 // When grouping is enabled in the horizontal mode, the header alignment
466 // Header-1 Header-2 Header-3
467 // Item 1 Item 4 Item 7
468 // Item 2 Item 5 Item 8
469 // Item 3 Item 6 Item 9
470 // In this case 'requiredItemHeight' represents the column-width. We don't
471 // check the content of the header in the layouter to determine the required
472 // width, hence assure that at least a minimal width of 15 characters is given
473 // (in average a character requires the halve width of the font height).
475 // TODO: Let the group headers provide a minimum width and respect this width here
476 const qreal headerWidth
= minimumGroupHeaderWidth();
477 if (requiredItemHeight
< headerWidth
) {
478 requiredItemHeight
= headerWidth
;
482 maxItemHeight
= qMax(maxItemHeight
, requiredItemHeight
);
487 if (grouped
&& m_groupItemIndexes
.contains(index
)) {
488 // The item represents the first index of a group
489 // and must aligned in the first column
494 y
+= maxItemHeight
+ itemMargin
.height();
499 // Calculate the maximum y-range of the last row for m_maximumScrollOffset
500 m_maximumScrollOffset
= m_itemInfos
.last().rect
.bottom();
501 const qreal rowY
= m_itemInfos
.last().rect
.y();
503 int index
= m_itemInfos
.count() - 2;
504 while (index
>= 0 && m_itemInfos
[index
].rect
.bottom() >= rowY
) {
505 m_maximumScrollOffset
= qMax(m_maximumScrollOffset
, m_itemInfos
[index
].rect
.bottom());
509 m_maximumScrollOffset
+= itemMargin
.height();
511 m_maximumItemOffset
= m_columnCount
* m_columnWidth
;
513 m_maximumScrollOffset
= 0;
514 m_maximumItemOffset
= 0;
517 #ifdef KITEMLISTVIEWLAYOUTER_DEBUG
518 kDebug() << "[TIME] doLayout() for " << m_model
->count() << "items:" << timer
.elapsed();
523 updateVisibleIndexes();
526 void KItemListViewLayouter::updateVisibleIndexes()
528 if (!m_visibleIndexesDirty
) {
534 if (m_model
->count() <= 0) {
535 m_firstVisibleIndex
= -1;
536 m_lastVisibleIndex
= -1;
537 m_visibleIndexesDirty
= false;
541 const int maxIndex
= m_model
->count() - 1;
543 // Calculate the first visible index that is fully visible
548 mid
= (min
+ max
) / 2;
549 if (m_itemInfos
[mid
].rect
.top() < m_scrollOffset
) {
554 } while (min
<= max
);
557 // Include the row before the first fully visible index, as it might
559 if (m_itemInfos
[mid
].rect
.top() >= m_scrollOffset
) {
561 Q_ASSERT(m_itemInfos
[mid
].rect
.top() < m_scrollOffset
);
564 const qreal rowTop
= m_itemInfos
[mid
].rect
.top();
565 while (mid
> 0 && m_itemInfos
[mid
- 1].rect
.top() == rowTop
) {
569 m_firstVisibleIndex
= mid
;
571 // Calculate the last visible index that is (at least partly) visible
572 const int visibleHeight
= (m_scrollOrientation
== Qt::Horizontal
) ? m_size
.width() : m_size
.height();
573 qreal bottom
= m_scrollOffset
+ visibleHeight
;
574 if (m_model
->groupedSorting()) {
575 bottom
+= m_groupHeaderHeight
;
578 min
= m_firstVisibleIndex
;
581 mid
= (min
+ max
) / 2;
582 if (m_itemInfos
[mid
].rect
.y() <= bottom
) {
587 } while (min
<= max
);
589 while (mid
> 0 && m_itemInfos
[mid
].rect
.y() > bottom
) {
592 m_lastVisibleIndex
= mid
;
594 m_visibleIndexesDirty
= false;
597 bool KItemListViewLayouter::createGroupHeaders()
599 if (!m_model
->groupedSorting()) {
603 m_groupItemIndexes
.clear();
605 const QList
<QPair
<int, QVariant
> > groups
= m_model
->groups();
606 if (groups
.isEmpty()) {
610 for (int i
= 0; i
< groups
.count(); ++i
) {
611 const int firstItemIndex
= groups
.at(i
).first
;
612 m_groupItemIndexes
.insert(firstItemIndex
);
618 qreal
KItemListViewLayouter::minimumGroupHeaderWidth() const
623 #include "kitemlistviewlayouter.moc"