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 return QRectF(pos
, sizeHint
);
245 if (sizeHint
.width() <= 0) {
246 // In Details View, a size hint with negative width is used internally.
247 sizeHint
.rwidth() = m_itemSize
.width();
250 const QPointF
pos(x
- m_itemOffset
, y
- m_scrollOffset
);
251 return QRectF(pos
, sizeHint
);
254 QRectF
KItemListViewLayouter::groupHeaderRect(int index
) const
256 const_cast<KItemListViewLayouter
*>(this)->doLayout();
258 const QRectF firstItemRect
= itemRect(index
);
259 QPointF pos
= firstItemRect
.topLeft();
265 if (m_scrollOrientation
== Qt::Vertical
) {
267 pos
.ry() -= m_groupHeaderHeight
;
268 size
= QSizeF(m_size
.width(), m_groupHeaderHeight
);
270 pos
.rx() -= m_itemMargin
.width();
273 // Determine the maximum width used in the current column. As the
274 // scroll-direction is Qt::Horizontal and m_itemRects is accessed
275 // directly, the logical height represents the visual width, and
276 // the logical row represents the column.
277 qreal headerWidth
= minimumGroupHeaderWidth();
278 const int row
= m_itemInfos
[index
].row
;
279 const int maxIndex
= m_itemInfos
.count() - 1;
280 while (index
<= maxIndex
) {
281 if (m_itemInfos
[index
].row
!= row
) {
285 const qreal itemWidth
= m_sizeHintResolver
->sizeHint(index
).width();
287 if (itemWidth
> headerWidth
) {
288 headerWidth
= itemWidth
;
294 size
= QSizeF(headerWidth
, m_size
.height());
296 return QRectF(pos
, size
);
299 int KItemListViewLayouter::itemColumn(int index
) const
301 const_cast<KItemListViewLayouter
*>(this)->doLayout();
302 if (index
< 0 || index
>= m_itemInfos
.count()) {
306 return (m_scrollOrientation
== Qt::Vertical
)
307 ? m_itemInfos
[index
].column
308 : m_itemInfos
[index
].row
;
311 int KItemListViewLayouter::itemRow(int index
) const
313 const_cast<KItemListViewLayouter
*>(this)->doLayout();
314 if (index
< 0 || index
>= m_itemInfos
.count()) {
318 return (m_scrollOrientation
== Qt::Vertical
)
319 ? m_itemInfos
[index
].row
320 : m_itemInfos
[index
].column
;
323 int KItemListViewLayouter::maximumVisibleItems() const
325 const_cast<KItemListViewLayouter
*>(this)->doLayout();
327 const int height
= static_cast<int>(m_size
.height());
328 const int rowHeight
= static_cast<int>(m_itemSize
.height());
329 int rows
= height
/ rowHeight
;
330 if (height
% rowHeight
!= 0) {
334 return rows
* m_columnCount
;
337 bool KItemListViewLayouter::isFirstGroupItem(int itemIndex
) const
339 const_cast<KItemListViewLayouter
*>(this)->doLayout();
340 return m_groupItemIndexes
.contains(itemIndex
);
343 void KItemListViewLayouter::markAsDirty()
350 bool KItemListViewLayouter::isDirty()
356 void KItemListViewLayouter::doLayout()
359 #ifdef KITEMLISTVIEWLAYOUTER_DEBUG
363 m_visibleIndexesDirty
= true;
365 QSizeF itemSize
= m_itemSize
;
366 QSizeF itemMargin
= m_itemMargin
;
367 QSizeF size
= m_size
;
369 const bool grouped
= createGroupHeaders();
371 const bool horizontalScrolling
= (m_scrollOrientation
== Qt::Horizontal
);
372 if (horizontalScrolling
) {
373 // Flip everything so that the layout logically can work like having
374 // a vertical scrolling
375 itemSize
.transpose();
376 itemMargin
.transpose();
380 // In the horizontal scrolling case all groups are aligned
381 // at the top, which decreases the available height. For the
382 // flipped data this means that the width must be decreased.
383 size
.rwidth() -= m_groupHeaderHeight
;
387 m_columnWidth
= itemSize
.width() + itemMargin
.width();
388 const qreal widthForColumns
= size
.width() - itemMargin
.width();
389 m_columnCount
= qMax(1, int(widthForColumns
/ m_columnWidth
));
390 m_xPosInc
= itemMargin
.width();
392 const int itemCount
= m_model
->count();
393 if (itemCount
> m_columnCount
&& m_columnWidth
>= 32) {
394 // Apply the unused width equally to each column
395 const qreal unusedWidth
= widthForColumns
- m_columnCount
* m_columnWidth
;
396 if (unusedWidth
> 0) {
397 const qreal columnInc
= unusedWidth
/ (m_columnCount
+ 1);
398 m_columnWidth
+= columnInc
;
399 m_xPosInc
+= columnInc
;
403 m_itemInfos
.resize(itemCount
);
405 // Calculate the offset of each column, i.e., the x-coordinate where the column starts.
406 m_columnOffsets
.resize(m_columnCount
);
407 qreal currentOffset
= m_xPosInc
;
409 if (grouped
&& horizontalScrolling
) {
410 // All group headers will always be aligned on the top and not
411 // flipped like the other properties.
412 currentOffset
+= m_groupHeaderHeight
;
415 for (int column
= 0; column
< m_columnCount
; ++column
) {
416 m_columnOffsets
[column
] = currentOffset
;
417 currentOffset
+= m_columnWidth
;
420 // Prepare the QVector which stores the y-coordinate for each new row.
421 int numberOfRows
= (itemCount
+ m_columnCount
- 1) / m_columnCount
;
422 if (grouped
&& m_columnCount
> 1) {
423 // In the worst case, a new row will be started for every group.
424 // We could calculate the exact number of rows now to prevent that we reserve
425 // too much memory, but the code required to do that might need much more
426 // memory than it would save in the average case.
427 numberOfRows
+= m_groupItemIndexes
.count();
429 m_rowOffsets
.resize(numberOfRows
);
431 qreal y
= m_headerHeight
+ itemMargin
.height();
435 while (index
< itemCount
) {
436 qreal maxItemHeight
= itemSize
.height();
439 if (m_groupItemIndexes
.contains(index
)) {
440 // The item is the first item of a group.
441 // Increase the y-position to provide space
442 // for the group header.
444 // Only add a margin if there has been added another
445 // group already before
446 y
+= m_groupHeaderMargin
;
447 } else if (!horizontalScrolling
) {
448 // The first group header should be aligned on top
449 y
-= itemMargin
.height();
452 if (!horizontalScrolling
) {
453 y
+= m_groupHeaderHeight
;
458 m_rowOffsets
[row
] = y
;
461 while (index
< itemCount
&& column
< m_columnCount
) {
462 qreal requiredItemHeight
= itemSize
.height();
463 const QSizeF sizeHint
= m_sizeHintResolver
->sizeHint(index
);
464 const qreal sizeHintHeight
= horizontalScrolling
? sizeHint
.width() : sizeHint
.height();
465 if (sizeHintHeight
> requiredItemHeight
) {
466 requiredItemHeight
= sizeHintHeight
;
469 ItemInfo
& itemInfo
= m_itemInfos
[index
];
470 itemInfo
.column
= column
;
473 if (grouped
&& horizontalScrolling
) {
474 // When grouping is enabled in the horizontal mode, the header alignment
476 // Header-1 Header-2 Header-3
477 // Item 1 Item 4 Item 7
478 // Item 2 Item 5 Item 8
479 // Item 3 Item 6 Item 9
480 // In this case 'requiredItemHeight' represents the column-width. We don't
481 // check the content of the header in the layouter to determine the required
482 // width, hence assure that at least a minimal width of 15 characters is given
483 // (in average a character requires the halve width of the font height).
485 // TODO: Let the group headers provide a minimum width and respect this width here
486 const qreal headerWidth
= minimumGroupHeaderWidth();
487 if (requiredItemHeight
< headerWidth
) {
488 requiredItemHeight
= headerWidth
;
492 maxItemHeight
= qMax(maxItemHeight
, requiredItemHeight
);
496 if (grouped
&& m_groupItemIndexes
.contains(index
)) {
497 // The item represents the first index of a group
498 // and must aligned in the first column
503 y
+= maxItemHeight
+ itemMargin
.height();
508 m_maximumScrollOffset
= y
;
509 m_maximumItemOffset
= m_columnCount
* m_columnWidth
;
511 m_maximumScrollOffset
= 0;
512 m_maximumItemOffset
= 0;
515 #ifdef KITEMLISTVIEWLAYOUTER_DEBUG
516 kDebug() << "[TIME] doLayout() for " << m_model
->count() << "items:" << timer
.elapsed();
521 updateVisibleIndexes();
524 void KItemListViewLayouter::updateVisibleIndexes()
526 if (!m_visibleIndexesDirty
) {
532 if (m_model
->count() <= 0) {
533 m_firstVisibleIndex
= -1;
534 m_lastVisibleIndex
= -1;
535 m_visibleIndexesDirty
= false;
539 const int maxIndex
= m_model
->count() - 1;
541 // Calculate the first visible index that is fully visible
546 mid
= (min
+ max
) / 2;
547 if (m_rowOffsets
.at(m_itemInfos
[mid
].row
) < m_scrollOffset
) {
552 } while (min
<= max
);
555 // Include the row before the first fully visible index, as it might
557 if (m_rowOffsets
.at(m_itemInfos
[mid
].row
) >= m_scrollOffset
) {
559 Q_ASSERT(m_rowOffsets
.at(m_itemInfos
[mid
].row
) < m_scrollOffset
);
562 const int firstVisibleRow
= m_itemInfos
[mid
].row
;
563 while (mid
> 0 && m_itemInfos
[mid
- 1].row
== firstVisibleRow
) {
567 m_firstVisibleIndex
= mid
;
569 // Calculate the last visible index that is (at least partly) visible
570 const int visibleHeight
= (m_scrollOrientation
== Qt::Horizontal
) ? m_size
.width() : m_size
.height();
571 qreal bottom
= m_scrollOffset
+ visibleHeight
;
572 if (m_model
->groupedSorting()) {
573 bottom
+= m_groupHeaderHeight
;
576 min
= m_firstVisibleIndex
;
579 mid
= (min
+ max
) / 2;
580 if (m_rowOffsets
.at(m_itemInfos
[mid
].row
) <= bottom
) {
585 } while (min
<= max
);
587 while (mid
> 0 && m_rowOffsets
.at(m_itemInfos
[mid
].row
) > bottom
) {
590 m_lastVisibleIndex
= mid
;
592 m_visibleIndexesDirty
= false;
595 bool KItemListViewLayouter::createGroupHeaders()
597 if (!m_model
->groupedSorting()) {
601 m_groupItemIndexes
.clear();
603 const QList
<QPair
<int, QVariant
> > groups
= m_model
->groups();
604 if (groups
.isEmpty()) {
608 for (int i
= 0; i
< groups
.count(); ++i
) {
609 const int firstItemIndex
= groups
.at(i
).first
;
610 m_groupItemIndexes
.insert(firstItemIndex
);
616 qreal
KItemListViewLayouter::minimumGroupHeaderWidth() const
621 #include "kitemlistviewlayouter.moc"