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(KItemListSizeHintResolver
* sizeHintResolver
, QObject
* parent
) :
32 m_visibleIndexesDirty(true),
33 m_scrollOrientation(Qt::Vertical
),
39 m_sizeHintResolver(sizeHintResolver
),
41 m_maximumScrollOffset(0),
43 m_maximumItemOffset(0),
44 m_firstVisibleIndex(-1),
45 m_lastVisibleIndex(-1),
52 m_groupHeaderHeight(0),
53 m_groupHeaderMargin(0),
56 Q_ASSERT(m_sizeHintResolver
);
59 KItemListViewLayouter::~KItemListViewLayouter()
63 void KItemListViewLayouter::setScrollOrientation(Qt::Orientation orientation
)
65 if (m_scrollOrientation
!= orientation
) {
66 m_scrollOrientation
= orientation
;
71 Qt::Orientation
KItemListViewLayouter::scrollOrientation() const
73 return m_scrollOrientation
;
76 void KItemListViewLayouter::setSize(const QSizeF
& size
)
79 if (m_scrollOrientation
== Qt::Vertical
) {
80 if (m_size
.width() != size
.width()) {
83 } else if (m_size
.height() != size
.height()) {
88 m_visibleIndexesDirty
= true;
92 QSizeF
KItemListViewLayouter::size() const
97 void KItemListViewLayouter::setItemSize(const QSizeF
& size
)
99 if (m_itemSize
!= size
) {
105 QSizeF
KItemListViewLayouter::itemSize() const
110 void KItemListViewLayouter::setItemMargin(const QSizeF
& margin
)
112 if (m_itemMargin
!= margin
) {
113 m_itemMargin
= margin
;
118 QSizeF
KItemListViewLayouter::itemMargin() const
123 void KItemListViewLayouter::setHeaderHeight(qreal height
)
125 if (m_headerHeight
!= height
) {
126 m_headerHeight
= height
;
131 qreal
KItemListViewLayouter::headerHeight() const
133 return m_headerHeight
;
136 void KItemListViewLayouter::setGroupHeaderHeight(qreal height
)
138 if (m_groupHeaderHeight
!= height
) {
139 m_groupHeaderHeight
= height
;
144 qreal
KItemListViewLayouter::groupHeaderHeight() const
146 return m_groupHeaderHeight
;
149 void KItemListViewLayouter::setGroupHeaderMargin(qreal margin
)
151 if (m_groupHeaderMargin
!= margin
) {
152 m_groupHeaderMargin
= margin
;
157 qreal
KItemListViewLayouter::groupHeaderMargin() const
159 return m_groupHeaderMargin
;
162 void KItemListViewLayouter::setScrollOffset(qreal offset
)
164 if (m_scrollOffset
!= offset
) {
165 m_scrollOffset
= offset
;
166 m_visibleIndexesDirty
= true;
170 qreal
KItemListViewLayouter::scrollOffset() const
172 return m_scrollOffset
;
175 qreal
KItemListViewLayouter::maximumScrollOffset() const
177 const_cast<KItemListViewLayouter
*>(this)->doLayout();
178 return m_maximumScrollOffset
;
181 void KItemListViewLayouter::setItemOffset(qreal offset
)
183 if (m_itemOffset
!= offset
) {
184 m_itemOffset
= offset
;
185 m_visibleIndexesDirty
= true;
189 qreal
KItemListViewLayouter::itemOffset() const
194 qreal
KItemListViewLayouter::maximumItemOffset() const
196 const_cast<KItemListViewLayouter
*>(this)->doLayout();
197 return m_maximumItemOffset
;
200 void KItemListViewLayouter::setModel(const KItemModelBase
* model
)
202 if (m_model
!= model
) {
208 const KItemModelBase
* KItemListViewLayouter::model() const
213 int KItemListViewLayouter::firstVisibleIndex() const
215 const_cast<KItemListViewLayouter
*>(this)->doLayout();
216 return m_firstVisibleIndex
;
219 int KItemListViewLayouter::lastVisibleIndex() const
221 const_cast<KItemListViewLayouter
*>(this)->doLayout();
222 return m_lastVisibleIndex
;
225 QRectF
KItemListViewLayouter::itemRect(int index
) const
227 const_cast<KItemListViewLayouter
*>(this)->doLayout();
228 if (index
< 0 || index
>= m_itemInfos
.count()) {
232 QSizeF sizeHint
= m_sizeHintResolver
->sizeHint(index
);
234 const qreal x
= m_columnOffsets
.at(m_itemInfos
.at(index
).column
);
235 const qreal y
= m_rowOffsets
.at(m_itemInfos
.at(index
).row
);
237 if (m_scrollOrientation
== Qt::Horizontal
) {
238 // Rotate the logical direction which is always vertical by 90°
239 // to get the physical horizontal direction
241 pos
.rx() -= m_scrollOffset
;
242 sizeHint
.transpose();
243 return QRectF(pos
, sizeHint
);
246 if (sizeHint
.width() <= 0) {
247 // In Details View, a size hint with negative width is used internally.
248 sizeHint
.rwidth() = m_itemSize
.width();
251 const QPointF
pos(x
- m_itemOffset
, y
- m_scrollOffset
);
252 return QRectF(pos
, sizeHint
);
255 QRectF
KItemListViewLayouter::groupHeaderRect(int index
) const
257 const_cast<KItemListViewLayouter
*>(this)->doLayout();
259 const QRectF firstItemRect
= itemRect(index
);
260 QPointF pos
= firstItemRect
.topLeft();
266 if (m_scrollOrientation
== Qt::Vertical
) {
268 pos
.ry() -= m_groupHeaderHeight
;
269 size
= QSizeF(m_size
.width(), m_groupHeaderHeight
);
271 pos
.rx() -= m_itemMargin
.width();
274 // Determine the maximum width used in the current column. As the
275 // scroll-direction is Qt::Horizontal and m_itemRects is accessed
276 // directly, the logical height represents the visual width, and
277 // the logical row represents the column.
278 qreal headerWidth
= minimumGroupHeaderWidth();
279 const int row
= m_itemInfos
[index
].row
;
280 const int maxIndex
= m_itemInfos
.count() - 1;
281 while (index
<= maxIndex
) {
282 if (m_itemInfos
[index
].row
!= row
) {
286 const qreal itemWidth
= (m_scrollOrientation
== Qt::Vertical
)
287 ? m_sizeHintResolver
->sizeHint(index
).width()
288 : m_sizeHintResolver
->sizeHint(index
).height();
290 if (itemWidth
> headerWidth
) {
291 headerWidth
= itemWidth
;
297 size
= QSizeF(headerWidth
, 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 m_itemInfos
.resize(itemCount
);
408 // Calculate the offset of each column, i.e., the x-coordinate where the column starts.
409 m_columnOffsets
.resize(m_columnCount
);
410 qreal currentOffset
= m_xPosInc
;
412 if (grouped
&& horizontalScrolling
) {
413 // All group headers will always be aligned on the top and not
414 // flipped like the other properties.
415 currentOffset
+= m_groupHeaderHeight
;
418 for (int column
= 0; column
< m_columnCount
; ++column
) {
419 m_columnOffsets
[column
] = currentOffset
;
420 currentOffset
+= m_columnWidth
;
423 // Prepare the QVector which stores the y-coordinate for each new row.
424 int numberOfRows
= (itemCount
+ m_columnCount
- 1) / m_columnCount
;
425 if (grouped
&& m_columnCount
> 1) {
426 // In the worst case, a new row will be started for every group.
427 // We could calculate the exact number of rows now to prevent that we reserve
428 // too much memory, but the code required to do that might need much more
429 // memory than it would save in the average case.
430 numberOfRows
+= m_groupItemIndexes
.count();
432 m_rowOffsets
.resize(numberOfRows
);
434 qreal y
= m_headerHeight
+ itemMargin
.height();
438 while (index
< itemCount
) {
439 qreal maxItemHeight
= itemSize
.height();
442 if (m_groupItemIndexes
.contains(index
)) {
443 // The item is the first item of a group.
444 // Increase the y-position to provide space
445 // for the group header.
447 // Only add a margin if there has been added another
448 // group already before
449 y
+= m_groupHeaderMargin
;
450 } else if (!horizontalScrolling
) {
451 // The first group header should be aligned on top
452 y
-= itemMargin
.height();
455 if (!horizontalScrolling
) {
456 y
+= m_groupHeaderHeight
;
461 m_rowOffsets
[row
] = y
;
464 while (index
< itemCount
&& column
< m_columnCount
) {
465 qreal requiredItemHeight
= itemSize
.height();
466 const QSizeF sizeHint
= m_sizeHintResolver
->sizeHint(index
);
467 const qreal sizeHintHeight
= sizeHint
.height();
468 if (sizeHintHeight
> requiredItemHeight
) {
469 requiredItemHeight
= sizeHintHeight
;
472 ItemInfo
& itemInfo
= m_itemInfos
[index
];
473 itemInfo
.column
= column
;
476 if (grouped
&& horizontalScrolling
) {
477 // When grouping is enabled in the horizontal mode, the header alignment
479 // Header-1 Header-2 Header-3
480 // Item 1 Item 4 Item 7
481 // Item 2 Item 5 Item 8
482 // Item 3 Item 6 Item 9
483 // In this case 'requiredItemHeight' represents the column-width. We don't
484 // check the content of the header in the layouter to determine the required
485 // width, hence assure that at least a minimal width of 15 characters is given
486 // (in average a character requires the halve width of the font height).
488 // TODO: Let the group headers provide a minimum width and respect this width here
489 const qreal headerWidth
= minimumGroupHeaderWidth();
490 if (requiredItemHeight
< headerWidth
) {
491 requiredItemHeight
= headerWidth
;
495 maxItemHeight
= qMax(maxItemHeight
, requiredItemHeight
);
499 if (grouped
&& m_groupItemIndexes
.contains(index
)) {
500 // The item represents the first index of a group
501 // and must aligned in the first column
506 y
+= maxItemHeight
+ itemMargin
.height();
511 m_maximumScrollOffset
= y
;
512 m_maximumItemOffset
= m_columnCount
* m_columnWidth
;
514 m_maximumScrollOffset
= 0;
515 m_maximumItemOffset
= 0;
518 #ifdef KITEMLISTVIEWLAYOUTER_DEBUG
519 kDebug() << "[TIME] doLayout() for " << m_model
->count() << "items:" << timer
.elapsed();
524 updateVisibleIndexes();
527 void KItemListViewLayouter::updateVisibleIndexes()
529 if (!m_visibleIndexesDirty
) {
535 if (m_model
->count() <= 0) {
536 m_firstVisibleIndex
= -1;
537 m_lastVisibleIndex
= -1;
538 m_visibleIndexesDirty
= false;
542 const int maxIndex
= m_model
->count() - 1;
544 // Calculate the first visible index that is fully visible
549 mid
= (min
+ max
) / 2;
550 if (m_rowOffsets
.at(m_itemInfos
[mid
].row
) < m_scrollOffset
) {
555 } while (min
<= max
);
558 // Include the row before the first fully visible index, as it might
560 if (m_rowOffsets
.at(m_itemInfos
[mid
].row
) >= m_scrollOffset
) {
562 Q_ASSERT(m_rowOffsets
.at(m_itemInfos
[mid
].row
) < m_scrollOffset
);
565 const int firstVisibleRow
= m_itemInfos
[mid
].row
;
566 while (mid
> 0 && m_itemInfos
[mid
- 1].row
== firstVisibleRow
) {
570 m_firstVisibleIndex
= mid
;
572 // Calculate the last visible index that is (at least partly) visible
573 const int visibleHeight
= (m_scrollOrientation
== Qt::Horizontal
) ? m_size
.width() : m_size
.height();
574 qreal bottom
= m_scrollOffset
+ visibleHeight
;
575 if (m_model
->groupedSorting()) {
576 bottom
+= m_groupHeaderHeight
;
579 min
= m_firstVisibleIndex
;
582 mid
= (min
+ max
) / 2;
583 if (m_rowOffsets
.at(m_itemInfos
[mid
].row
) <= bottom
) {
588 } while (min
<= max
);
590 while (mid
> 0 && m_rowOffsets
.at(m_itemInfos
[mid
].row
) > bottom
) {
593 m_lastVisibleIndex
= mid
;
595 m_visibleIndexesDirty
= false;
598 bool KItemListViewLayouter::createGroupHeaders()
600 if (!m_model
->groupedSorting()) {
604 m_groupItemIndexes
.clear();
606 const QList
<QPair
<int, QVariant
> > groups
= m_model
->groups();
607 if (groups
.isEmpty()) {
611 for (int i
= 0; i
< groups
.count(); ++i
) {
612 const int firstItemIndex
= groups
.at(i
).first
;
613 m_groupItemIndexes
.insert(firstItemIndex
);
619 qreal
KItemListViewLayouter::minimumGroupHeaderWidth() const