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"
21 #include "dolphindebug.h"
22 #include "kitemlistsizehintresolver.h"
23 #include "kitemviews/kitemmodelbase.h"
25 // #define KITEMLISTVIEWLAYOUTER_DEBUG
27 KItemListViewLayouter::KItemListViewLayouter(KItemListSizeHintResolver
* sizeHintResolver
, QObject
* parent
) :
30 m_visibleIndexesDirty(true),
31 m_scrollOrientation(Qt::Vertical
),
37 m_sizeHintResolver(sizeHintResolver
),
39 m_maximumScrollOffset(0),
41 m_maximumItemOffset(0),
42 m_firstVisibleIndex(-1),
43 m_lastVisibleIndex(-1),
50 m_groupHeaderHeight(0),
51 m_groupHeaderMargin(0),
54 Q_ASSERT(m_sizeHintResolver
);
57 KItemListViewLayouter::~KItemListViewLayouter()
61 void KItemListViewLayouter::setScrollOrientation(Qt::Orientation orientation
)
63 if (m_scrollOrientation
!= orientation
) {
64 m_scrollOrientation
= orientation
;
69 Qt::Orientation
KItemListViewLayouter::scrollOrientation() const
71 return m_scrollOrientation
;
74 void KItemListViewLayouter::setSize(const QSizeF
& size
)
77 if (m_scrollOrientation
== Qt::Vertical
) {
78 if (m_size
.width() != size
.width()) {
81 } else if (m_size
.height() != size
.height()) {
86 m_visibleIndexesDirty
= true;
90 QSizeF
KItemListViewLayouter::size() const
95 void KItemListViewLayouter::setItemSize(const QSizeF
& size
)
97 if (m_itemSize
!= size
) {
103 QSizeF
KItemListViewLayouter::itemSize() const
108 void KItemListViewLayouter::setItemMargin(const QSizeF
& margin
)
110 if (m_itemMargin
!= margin
) {
111 m_itemMargin
= margin
;
116 QSizeF
KItemListViewLayouter::itemMargin() const
121 void KItemListViewLayouter::setHeaderHeight(qreal height
)
123 if (m_headerHeight
!= height
) {
124 m_headerHeight
= height
;
129 qreal
KItemListViewLayouter::headerHeight() const
131 return m_headerHeight
;
134 void KItemListViewLayouter::setGroupHeaderHeight(qreal height
)
136 if (m_groupHeaderHeight
!= height
) {
137 m_groupHeaderHeight
= height
;
142 qreal
KItemListViewLayouter::groupHeaderHeight() const
144 return m_groupHeaderHeight
;
147 void KItemListViewLayouter::setGroupHeaderMargin(qreal margin
)
149 if (m_groupHeaderMargin
!= margin
) {
150 m_groupHeaderMargin
= margin
;
155 qreal
KItemListViewLayouter::groupHeaderMargin() const
157 return m_groupHeaderMargin
;
160 void KItemListViewLayouter::setScrollOffset(qreal offset
)
162 if (m_scrollOffset
!= offset
) {
163 m_scrollOffset
= offset
;
164 m_visibleIndexesDirty
= true;
168 qreal
KItemListViewLayouter::scrollOffset() const
170 return m_scrollOffset
;
173 qreal
KItemListViewLayouter::maximumScrollOffset() const
175 const_cast<KItemListViewLayouter
*>(this)->doLayout();
176 return m_maximumScrollOffset
;
179 void KItemListViewLayouter::setItemOffset(qreal offset
)
181 if (m_itemOffset
!= offset
) {
182 m_itemOffset
= offset
;
183 m_visibleIndexesDirty
= true;
187 qreal
KItemListViewLayouter::itemOffset() const
192 qreal
KItemListViewLayouter::maximumItemOffset() const
194 const_cast<KItemListViewLayouter
*>(this)->doLayout();
195 return m_maximumItemOffset
;
198 void KItemListViewLayouter::setModel(const KItemModelBase
* model
)
200 if (m_model
!= model
) {
206 const KItemModelBase
* KItemListViewLayouter::model() const
211 int KItemListViewLayouter::firstVisibleIndex() const
213 const_cast<KItemListViewLayouter
*>(this)->doLayout();
214 return m_firstVisibleIndex
;
217 int KItemListViewLayouter::lastVisibleIndex() const
219 const_cast<KItemListViewLayouter
*>(this)->doLayout();
220 return m_lastVisibleIndex
;
223 QRectF
KItemListViewLayouter::itemRect(int index
) const
225 const_cast<KItemListViewLayouter
*>(this)->doLayout();
226 if (index
< 0 || index
>= m_itemInfos
.count()) {
230 QSizeF sizeHint
= m_sizeHintResolver
->sizeHint(index
);
232 const qreal x
= m_columnOffsets
.at(m_itemInfos
.at(index
).column
);
233 const qreal y
= m_rowOffsets
.at(m_itemInfos
.at(index
).row
);
235 if (m_scrollOrientation
== Qt::Horizontal
) {
236 // Rotate the logical direction which is always vertical by 90°
237 // to get the physical horizontal direction
239 pos
.rx() -= m_scrollOffset
;
240 sizeHint
.transpose();
241 return QRectF(pos
, sizeHint
);
244 if (sizeHint
.width() <= 0) {
245 // In Details View, a size hint with negative width is used internally.
246 sizeHint
.rwidth() = m_itemSize
.width();
249 const QPointF
pos(x
- m_itemOffset
, y
- m_scrollOffset
);
250 return QRectF(pos
, sizeHint
);
253 QRectF
KItemListViewLayouter::groupHeaderRect(int index
) const
255 const_cast<KItemListViewLayouter
*>(this)->doLayout();
257 const QRectF firstItemRect
= itemRect(index
);
258 QPointF pos
= firstItemRect
.topLeft();
264 if (m_scrollOrientation
== Qt::Vertical
) {
266 pos
.ry() -= m_groupHeaderHeight
;
267 size
= QSizeF(m_size
.width(), m_groupHeaderHeight
);
269 pos
.rx() -= m_itemMargin
.width();
272 // Determine the maximum width used in the current column. As the
273 // scroll-direction is Qt::Horizontal and m_itemRects is accessed
274 // directly, the logical height represents the visual width, and
275 // the logical row represents the column.
276 qreal headerWidth
= minimumGroupHeaderWidth();
277 const int row
= m_itemInfos
[index
].row
;
278 const int maxIndex
= m_itemInfos
.count() - 1;
279 while (index
<= maxIndex
) {
280 if (m_itemInfos
[index
].row
!= row
) {
284 const qreal itemWidth
= (m_scrollOrientation
== Qt::Vertical
)
285 ? m_sizeHintResolver
->sizeHint(index
).width()
286 : m_sizeHintResolver
->sizeHint(index
).height();
288 if (itemWidth
> headerWidth
) {
289 headerWidth
= itemWidth
;
295 size
= QSizeF(headerWidth
, m_size
.height());
297 return QRectF(pos
, size
);
300 int KItemListViewLayouter::itemColumn(int index
) const
302 const_cast<KItemListViewLayouter
*>(this)->doLayout();
303 if (index
< 0 || index
>= m_itemInfos
.count()) {
307 return (m_scrollOrientation
== Qt::Vertical
)
308 ? m_itemInfos
[index
].column
309 : m_itemInfos
[index
].row
;
312 int KItemListViewLayouter::itemRow(int index
) const
314 const_cast<KItemListViewLayouter
*>(this)->doLayout();
315 if (index
< 0 || index
>= m_itemInfos
.count()) {
319 return (m_scrollOrientation
== Qt::Vertical
)
320 ? m_itemInfos
[index
].row
321 : m_itemInfos
[index
].column
;
324 int KItemListViewLayouter::maximumVisibleItems() const
326 const_cast<KItemListViewLayouter
*>(this)->doLayout();
328 const int height
= static_cast<int>(m_size
.height());
329 const int rowHeight
= static_cast<int>(m_itemSize
.height());
330 int rows
= height
/ rowHeight
;
331 if (height
% rowHeight
!= 0) {
335 return rows
* m_columnCount
;
338 bool KItemListViewLayouter::isFirstGroupItem(int itemIndex
) const
340 const_cast<KItemListViewLayouter
*>(this)->doLayout();
341 return m_groupItemIndexes
.contains(itemIndex
);
344 void KItemListViewLayouter::markAsDirty()
351 bool KItemListViewLayouter::isDirty()
357 void KItemListViewLayouter::doLayout()
360 #ifdef KITEMLISTVIEWLAYOUTER_DEBUG
364 m_visibleIndexesDirty
= true;
366 QSizeF itemSize
= m_itemSize
;
367 QSizeF itemMargin
= m_itemMargin
;
368 QSizeF size
= m_size
;
370 const bool grouped
= createGroupHeaders();
372 const bool horizontalScrolling
= (m_scrollOrientation
== Qt::Horizontal
);
373 if (horizontalScrolling
) {
374 // Flip everything so that the layout logically can work like having
375 // a vertical scrolling
376 itemSize
.transpose();
377 itemMargin
.transpose();
381 // In the horizontal scrolling case all groups are aligned
382 // at the top, which decreases the available height. For the
383 // flipped data this means that the width must be decreased.
384 size
.rwidth() -= m_groupHeaderHeight
;
388 m_columnWidth
= itemSize
.width() + itemMargin
.width();
389 const qreal widthForColumns
= size
.width() - itemMargin
.width();
390 m_columnCount
= qMax(1, int(widthForColumns
/ m_columnWidth
));
391 m_xPosInc
= itemMargin
.width();
393 const int itemCount
= m_model
->count();
394 if (itemCount
> m_columnCount
&& m_columnWidth
>= 32) {
395 // Apply the unused width equally to each column
396 const qreal unusedWidth
= widthForColumns
- m_columnCount
* m_columnWidth
;
397 if (unusedWidth
> 0) {
398 const qreal columnInc
= unusedWidth
/ (m_columnCount
+ 1);
399 m_columnWidth
+= columnInc
;
400 m_xPosInc
+= columnInc
;
404 m_itemInfos
.resize(itemCount
);
406 // Calculate the offset of each column, i.e., the x-coordinate where the column starts.
407 m_columnOffsets
.resize(m_columnCount
);
408 qreal currentOffset
= m_xPosInc
;
410 if (grouped
&& horizontalScrolling
) {
411 // All group headers will always be aligned on the top and not
412 // flipped like the other properties.
413 currentOffset
+= m_groupHeaderHeight
;
416 for (int column
= 0; column
< m_columnCount
; ++column
) {
417 m_columnOffsets
[column
] = currentOffset
;
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 updateVisibleIndexes();
525 void KItemListViewLayouter::updateVisibleIndexes()
527 if (!m_visibleIndexesDirty
) {
533 if (m_model
->count() <= 0) {
534 m_firstVisibleIndex
= -1;
535 m_lastVisibleIndex
= -1;
536 m_visibleIndexesDirty
= false;
540 const int maxIndex
= m_model
->count() - 1;
542 // Calculate the first visible index that is fully visible
547 mid
= (min
+ max
) / 2;
548 if (m_rowOffsets
.at(m_itemInfos
[mid
].row
) < m_scrollOffset
) {
553 } while (min
<= max
);
556 // Include the row before the first fully visible index, as it might
558 if (m_rowOffsets
.at(m_itemInfos
[mid
].row
) >= m_scrollOffset
) {
560 Q_ASSERT(m_rowOffsets
.at(m_itemInfos
[mid
].row
) < m_scrollOffset
);
563 const int firstVisibleRow
= m_itemInfos
[mid
].row
;
564 while (mid
> 0 && m_itemInfos
[mid
- 1].row
== firstVisibleRow
) {
568 m_firstVisibleIndex
= mid
;
570 // Calculate the last visible index that is (at least partly) visible
571 const int visibleHeight
= (m_scrollOrientation
== Qt::Horizontal
) ? m_size
.width() : m_size
.height();
572 qreal bottom
= m_scrollOffset
+ visibleHeight
;
573 if (m_model
->groupedSorting()) {
574 bottom
+= m_groupHeaderHeight
;
577 min
= m_firstVisibleIndex
;
580 mid
= (min
+ max
) / 2;
581 if (m_rowOffsets
.at(m_itemInfos
[mid
].row
) <= bottom
) {
586 } while (min
<= max
);
588 while (mid
> 0 && m_rowOffsets
.at(m_itemInfos
[mid
].row
) > bottom
) {
591 m_lastVisibleIndex
= mid
;
593 m_visibleIndexesDirty
= false;
596 bool KItemListViewLayouter::createGroupHeaders()
598 if (!m_model
->groupedSorting()) {
602 m_groupItemIndexes
.clear();
604 const QList
<QPair
<int, QVariant
> > groups
= m_model
->groups();
605 if (groups
.isEmpty()) {
609 for (int i
= 0; i
< groups
.count(); ++i
) {
610 const int firstItemIndex
= groups
.at(i
).first
;
611 m_groupItemIndexes
.insert(firstItemIndex
);
617 qreal
KItemListViewLayouter::minimumGroupHeaderWidth() const