2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "kitemlistviewlayouter.h"
8 #include "dolphindebug.h"
9 #include "kitemlistsizehintresolver.h"
10 #include "kitemviews/kitemmodelbase.h"
12 #include <QGuiApplication>
13 #include <QScopeGuard>
15 // #define KITEMLISTVIEWLAYOUTER_DEBUG
17 KItemListViewLayouter::KItemListViewLayouter(KItemListSizeHintResolver
* sizeHintResolver
, QObject
* parent
) :
20 m_visibleIndexesDirty(true),
21 m_scrollOrientation(Qt::Vertical
),
27 m_sizeHintResolver(sizeHintResolver
),
29 m_maximumScrollOffset(0),
31 m_maximumItemOffset(0),
32 m_firstVisibleIndex(-1),
33 m_lastVisibleIndex(-1),
40 m_groupHeaderHeight(0),
41 m_groupHeaderMargin(0),
44 Q_ASSERT(m_sizeHintResolver
);
47 KItemListViewLayouter::~KItemListViewLayouter()
51 void KItemListViewLayouter::setScrollOrientation(Qt::Orientation orientation
)
53 if (m_scrollOrientation
!= orientation
) {
54 m_scrollOrientation
= orientation
;
59 Qt::Orientation
KItemListViewLayouter::scrollOrientation() const
61 return m_scrollOrientation
;
64 void KItemListViewLayouter::setSize(const QSizeF
& size
)
67 if (m_scrollOrientation
== Qt::Vertical
) {
68 if (m_size
.width() != size
.width()) {
71 } else if (m_size
.height() != size
.height()) {
76 m_visibleIndexesDirty
= true;
80 QSizeF
KItemListViewLayouter::size() const
85 void KItemListViewLayouter::setItemSize(const QSizeF
& size
)
87 if (m_itemSize
!= size
) {
93 QSizeF
KItemListViewLayouter::itemSize() const
98 void KItemListViewLayouter::setItemMargin(const QSizeF
& margin
)
100 if (m_itemMargin
!= margin
) {
101 m_itemMargin
= margin
;
106 QSizeF
KItemListViewLayouter::itemMargin() const
111 void KItemListViewLayouter::setHeaderHeight(qreal height
)
113 if (m_headerHeight
!= height
) {
114 m_headerHeight
= height
;
119 qreal
KItemListViewLayouter::headerHeight() const
121 return m_headerHeight
;
124 void KItemListViewLayouter::setGroupHeaderHeight(qreal height
)
126 if (m_groupHeaderHeight
!= height
) {
127 m_groupHeaderHeight
= height
;
132 qreal
KItemListViewLayouter::groupHeaderHeight() const
134 return m_groupHeaderHeight
;
137 void KItemListViewLayouter::setGroupHeaderMargin(qreal margin
)
139 if (m_groupHeaderMargin
!= margin
) {
140 m_groupHeaderMargin
= margin
;
145 qreal
KItemListViewLayouter::groupHeaderMargin() const
147 return m_groupHeaderMargin
;
150 void KItemListViewLayouter::setScrollOffset(qreal offset
)
152 if (m_scrollOffset
!= offset
) {
153 m_scrollOffset
= offset
;
154 m_visibleIndexesDirty
= true;
158 qreal
KItemListViewLayouter::scrollOffset() const
160 return m_scrollOffset
;
163 qreal
KItemListViewLayouter::maximumScrollOffset() const
165 const_cast<KItemListViewLayouter
*>(this)->doLayout();
166 return m_maximumScrollOffset
;
169 void KItemListViewLayouter::setItemOffset(qreal offset
)
171 if (m_itemOffset
!= offset
) {
172 m_itemOffset
= offset
;
173 m_visibleIndexesDirty
= true;
177 qreal
KItemListViewLayouter::itemOffset() const
182 qreal
KItemListViewLayouter::maximumItemOffset() const
184 const_cast<KItemListViewLayouter
*>(this)->doLayout();
185 return m_maximumItemOffset
;
188 void KItemListViewLayouter::setModel(const KItemModelBase
* model
)
190 if (m_model
!= model
) {
196 const KItemModelBase
* KItemListViewLayouter::model() const
201 int KItemListViewLayouter::firstVisibleIndex() const
203 const_cast<KItemListViewLayouter
*>(this)->doLayout();
204 return m_firstVisibleIndex
;
207 int KItemListViewLayouter::lastVisibleIndex() const
209 const_cast<KItemListViewLayouter
*>(this)->doLayout();
210 return m_lastVisibleIndex
;
213 QRectF
KItemListViewLayouter::itemRect(int index
) const
215 const_cast<KItemListViewLayouter
*>(this)->doLayout();
216 if (index
< 0 || index
>= m_itemInfos
.count()) {
220 QSizeF sizeHint
= m_sizeHintResolver
->sizeHint(index
);
222 const qreal x
= m_columnOffsets
.at(m_itemInfos
.at(index
).column
);
223 const qreal y
= m_rowOffsets
.at(m_itemInfos
.at(index
).row
);
225 if (m_scrollOrientation
== Qt::Horizontal
) {
226 // Rotate the logical direction which is always vertical by 90°
227 // to get the physical horizontal direction
229 pos
.rx() -= m_scrollOffset
;
230 sizeHint
.transpose();
231 return QRectF(pos
, sizeHint
);
234 if (sizeHint
.width() <= 0) {
235 // In Details View, a size hint with negative width is used internally.
236 sizeHint
.rwidth() = m_itemSize
.width();
239 const QPointF
pos(x
- m_itemOffset
, y
- m_scrollOffset
);
240 return QRectF(pos
, sizeHint
);
243 QRectF
KItemListViewLayouter::groupHeaderRect(int index
) const
245 const_cast<KItemListViewLayouter
*>(this)->doLayout();
247 const QRectF firstItemRect
= itemRect(index
);
248 QPointF pos
= firstItemRect
.topLeft();
254 if (m_scrollOrientation
== Qt::Vertical
) {
256 pos
.ry() -= m_groupHeaderHeight
;
257 size
= QSizeF(m_size
.width(), m_groupHeaderHeight
);
259 pos
.rx() -= m_itemMargin
.width();
262 // Determine the maximum width used in the current column. As the
263 // scroll-direction is Qt::Horizontal and m_itemRects is accessed
264 // directly, the logical height represents the visual width, and
265 // the logical row represents the column.
266 qreal headerWidth
= minimumGroupHeaderWidth();
267 const int row
= m_itemInfos
[index
].row
;
268 const int maxIndex
= m_itemInfos
.count() - 1;
269 while (index
<= maxIndex
) {
270 if (m_itemInfos
[index
].row
!= row
) {
274 const qreal itemWidth
= (m_scrollOrientation
== Qt::Vertical
)
275 ? m_sizeHintResolver
->sizeHint(index
).width()
276 : m_sizeHintResolver
->sizeHint(index
).height();
278 if (itemWidth
> headerWidth
) {
279 headerWidth
= itemWidth
;
285 size
= QSizeF(headerWidth
, m_size
.height());
287 return QRectF(pos
, size
);
290 int KItemListViewLayouter::itemColumn(int index
) const
292 const_cast<KItemListViewLayouter
*>(this)->doLayout();
293 if (index
< 0 || index
>= m_itemInfos
.count()) {
297 return (m_scrollOrientation
== Qt::Vertical
)
298 ? m_itemInfos
[index
].column
299 : m_itemInfos
[index
].row
;
302 int KItemListViewLayouter::itemRow(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
].row
311 : m_itemInfos
[index
].column
;
314 int KItemListViewLayouter::maximumVisibleItems() const
316 const_cast<KItemListViewLayouter
*>(this)->doLayout();
318 const int height
= static_cast<int>(m_size
.height());
319 const int rowHeight
= static_cast<int>(m_itemSize
.height());
320 int rows
= height
/ rowHeight
;
321 if (height
% rowHeight
!= 0) {
325 return rows
* m_columnCount
;
328 bool KItemListViewLayouter::isFirstGroupItem(int itemIndex
) const
330 const_cast<KItemListViewLayouter
*>(this)->doLayout();
331 return m_groupItemIndexes
.contains(itemIndex
);
334 void KItemListViewLayouter::markAsDirty()
341 bool KItemListViewLayouter::isDirty()
347 void KItemListViewLayouter::doLayout()
349 // we always want to update visible indexes after performing a layout
350 auto qsg
= qScopeGuard([this] { updateVisibleIndexes(); });
356 #ifdef KITEMLISTVIEWLAYOUTER_DEBUG
360 m_visibleIndexesDirty
= true;
362 QSizeF itemSize
= m_itemSize
;
363 QSizeF itemMargin
= m_itemMargin
;
364 QSizeF size
= m_size
;
366 const bool grouped
= createGroupHeaders();
368 const bool horizontalScrolling
= (m_scrollOrientation
== Qt::Horizontal
);
369 if (horizontalScrolling
) {
370 // Flip everything so that the layout logically can work like having
371 // a vertical scrolling
372 itemSize
.transpose();
373 itemMargin
.transpose();
377 // In the horizontal scrolling case all groups are aligned
378 // at the top, which decreases the available height. For the
379 // flipped data this means that the width must be decreased.
380 size
.rwidth() -= m_groupHeaderHeight
;
384 m_columnWidth
= itemSize
.width() + itemMargin
.width();
385 const qreal widthForColumns
= size
.width() - itemMargin
.width();
386 m_columnCount
= qMax(1, int(widthForColumns
/ m_columnWidth
));
387 m_xPosInc
= itemMargin
.width();
389 const int itemCount
= m_model
->count();
390 if (itemCount
> m_columnCount
&& m_columnWidth
>= 32) {
391 // Apply the unused width equally to each column
392 const qreal unusedWidth
= widthForColumns
- m_columnCount
* m_columnWidth
;
393 if (unusedWidth
> 0) {
394 const qreal columnInc
= unusedWidth
/ (m_columnCount
+ 1);
395 m_columnWidth
+= columnInc
;
396 m_xPosInc
+= columnInc
;
400 m_itemInfos
.resize(itemCount
);
402 // Calculate the offset of each column, i.e., the x-coordinate where the column starts.
403 m_columnOffsets
.resize(m_columnCount
);
404 qreal currentOffset
= QGuiApplication::isRightToLeft() ? widthForColumns
: m_xPosInc
;
406 if (grouped
&& horizontalScrolling
) {
407 // All group headers will always be aligned on the top and not
408 // flipped like the other properties.
409 currentOffset
+= m_groupHeaderHeight
;
412 if (QGuiApplication::isLeftToRight()) for (int column
= 0; column
< m_columnCount
; ++column
) {
413 m_columnOffsets
[column
] = currentOffset
;
414 currentOffset
+= m_columnWidth
;
416 else for (int column
= 0; column
< m_columnCount
; ++column
) {
417 m_columnOffsets
[column
] = currentOffset
- m_columnWidth
;
418 currentOffset
-= m_columnWidth
;
421 // Prepare the QVector which stores the y-coordinate for each new row.
422 int numberOfRows
= (itemCount
+ m_columnCount
- 1) / m_columnCount
;
423 if (grouped
&& m_columnCount
> 1) {
424 // In the worst case, a new row will be started for every group.
425 // We could calculate the exact number of rows now to prevent that we reserve
426 // too much memory, but the code required to do that might need much more
427 // memory than it would save in the average case.
428 numberOfRows
+= m_groupItemIndexes
.count();
430 m_rowOffsets
.resize(numberOfRows
);
432 qreal y
= m_headerHeight
+ itemMargin
.height();
436 while (index
< itemCount
) {
437 qreal maxItemHeight
= itemSize
.height();
440 if (m_groupItemIndexes
.contains(index
)) {
441 // The item is the first item of a group.
442 // Increase the y-position to provide space
443 // for the group header.
445 // Only add a margin if there has been added another
446 // group already before
447 y
+= m_groupHeaderMargin
;
448 } else if (!horizontalScrolling
) {
449 // The first group header should be aligned on top
450 y
-= itemMargin
.height();
453 if (!horizontalScrolling
) {
454 y
+= m_groupHeaderHeight
;
459 m_rowOffsets
[row
] = y
;
462 while (index
< itemCount
&& column
< m_columnCount
) {
463 qreal requiredItemHeight
= itemSize
.height();
464 const QSizeF sizeHint
= m_sizeHintResolver
->sizeHint(index
);
465 const qreal sizeHintHeight
= sizeHint
.height();
466 if (sizeHintHeight
> requiredItemHeight
) {
467 requiredItemHeight
= sizeHintHeight
;
470 ItemInfo
& itemInfo
= m_itemInfos
[index
];
471 itemInfo
.column
= column
;
474 if (grouped
&& horizontalScrolling
) {
475 // When grouping is enabled in the horizontal mode, the header alignment
477 // Header-1 Header-2 Header-3
478 // Item 1 Item 4 Item 7
479 // Item 2 Item 5 Item 8
480 // Item 3 Item 6 Item 9
481 // In this case 'requiredItemHeight' represents the column-width. We don't
482 // check the content of the header in the layouter to determine the required
483 // width, hence assure that at least a minimal width of 15 characters is given
484 // (in average a character requires the halve width of the font height).
486 // TODO: Let the group headers provide a minimum width and respect this width here
487 const qreal headerWidth
= minimumGroupHeaderWidth();
488 if (requiredItemHeight
< headerWidth
) {
489 requiredItemHeight
= headerWidth
;
493 maxItemHeight
= qMax(maxItemHeight
, requiredItemHeight
);
497 if (grouped
&& m_groupItemIndexes
.contains(index
)) {
498 // The item represents the first index of a group
499 // and must aligned in the first column
504 y
+= maxItemHeight
+ itemMargin
.height();
509 m_maximumScrollOffset
= y
;
510 m_maximumItemOffset
= m_columnCount
* m_columnWidth
;
512 m_maximumScrollOffset
= 0;
513 m_maximumItemOffset
= 0;
516 #ifdef KITEMLISTVIEWLAYOUTER_DEBUG
517 qCDebug(DolphinDebug
) << "[TIME] doLayout() for " << m_model
->count() << "items:" << timer
.elapsed();
522 void KItemListViewLayouter::updateVisibleIndexes()
524 if (!m_visibleIndexesDirty
) {
530 if (m_model
->count() <= 0) {
531 m_firstVisibleIndex
= -1;
532 m_lastVisibleIndex
= -1;
533 m_visibleIndexesDirty
= false;
537 const int maxIndex
= m_model
->count() - 1;
539 // Calculate the first visible index that is fully visible
544 mid
= (min
+ max
) / 2;
545 if (m_rowOffsets
.at(m_itemInfos
[mid
].row
) < m_scrollOffset
) {
550 } while (min
<= max
);
553 // Include the row before the first fully visible index, as it might
555 if (m_rowOffsets
.at(m_itemInfos
[mid
].row
) >= m_scrollOffset
) {
557 Q_ASSERT(m_rowOffsets
.at(m_itemInfos
[mid
].row
) < m_scrollOffset
);
560 const int firstVisibleRow
= m_itemInfos
[mid
].row
;
561 while (mid
> 0 && m_itemInfos
[mid
- 1].row
== firstVisibleRow
) {
565 m_firstVisibleIndex
= mid
;
567 // Calculate the last visible index that is (at least partly) visible
568 const int visibleHeight
= (m_scrollOrientation
== Qt::Horizontal
) ? m_size
.width() : m_size
.height();
569 qreal bottom
= m_scrollOffset
+ visibleHeight
;
570 if (m_model
->groupedSorting()) {
571 bottom
+= m_groupHeaderHeight
;
574 min
= m_firstVisibleIndex
;
577 mid
= (min
+ max
) / 2;
578 if (m_rowOffsets
.at(m_itemInfos
[mid
].row
) <= bottom
) {
583 } while (min
<= max
);
585 while (mid
> 0 && m_rowOffsets
.at(m_itemInfos
[mid
].row
) > bottom
) {
588 m_lastVisibleIndex
= mid
;
590 m_visibleIndexesDirty
= false;
593 bool KItemListViewLayouter::createGroupHeaders()
595 if (!m_model
->groupedSorting()) {
599 m_groupItemIndexes
.clear();
601 const QList
<QPair
<int, QVariant
> > groups
= m_model
->groups();
602 if (groups
.isEmpty()) {
606 for (int i
= 0; i
< groups
.count(); ++i
) {
607 const int firstItemIndex
= groups
.at(i
).first
;
608 m_groupItemIndexes
.insert(firstItemIndex
);
614 qreal
KItemListViewLayouter::minimumGroupHeaderWidth() const