2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #ifndef KITEMLISTVIEWLAYOUTER_H
8 #define KITEMLISTVIEWLAYOUTER_H
10 #include "dolphin_export.h"
19 class KItemListSizeHintResolver
;
22 * @brief Internal helper class for KItemListView to layout the items.
24 * The layouter is capable to align the items within a grid. If the
25 * scroll-direction is horizontal the column-width of the grid can be
26 * variable. If the scroll-direction is vertical the row-height of
27 * the grid can be variable.
29 * The layouter is implemented in a way that it postpones the expensive
30 * layout operation until a property is read the first time after
31 * marking the layouter as dirty (see markAsDirty()). This means that
32 * changing properties of the layouter is not expensive, only the
33 * first read of a property can get expensive.
35 class DOLPHIN_EXPORT KItemListViewLayouter
: public QObject
40 explicit KItemListViewLayouter(KItemListSizeHintResolver
* sizeHintResolver
, QObject
* parent
= nullptr);
41 ~KItemListViewLayouter() override
;
43 void setScrollOrientation(Qt::Orientation orientation
);
44 Qt::Orientation
scrollOrientation() const;
46 void setSize(const QSizeF
& size
);
49 void setItemSize(const QSizeF
& size
);
50 QSizeF
itemSize() const;
53 * Margin between the rows and columns of items.
55 void setItemMargin(const QSizeF
& margin
);
56 QSizeF
itemMargin() const;
59 * Sets the height of the header that is always aligned
60 * at the top. A height of <= 0.0 means that no header is
63 void setHeaderHeight(qreal height
);
64 qreal
headerHeight() const;
67 * Sets the height of the group header that is used
68 * to indicate a new item group.
70 void setGroupHeaderHeight(qreal height
);
71 qreal
groupHeaderHeight() const;
74 * Sets the margin between the last items of the group n and
75 * the group header for the group n + 1.
77 void setGroupHeaderMargin(qreal margin
);
78 qreal
groupHeaderMargin() const;
80 void setScrollOffset(qreal scrollOffset
);
81 qreal
scrollOffset() const;
83 qreal
maximumScrollOffset() const;
85 void setItemOffset(qreal scrollOffset
);
86 qreal
itemOffset() const;
88 qreal
maximumItemOffset() const;
90 void setModel(const KItemModelBase
* model
);
91 const KItemModelBase
* model() const;
94 * @return The first (at least partly) visible index. -1 is returned
95 * if the item count is 0.
97 int firstVisibleIndex() const;
100 * @return The last (at least partly) visible index. -1 is returned
101 * if the item count is 0.
103 int lastVisibleIndex() const;
106 * @return Rectangle of the item with the index \a index.
107 * The top/left of the bounding rectangle is related to
108 * the top/left of the KItemListView. An empty rectangle
109 * is returned if an invalid index is given.
111 QRectF
itemRect(int index
) const;
114 * @return Rectangle of the group header for the item with the
115 * index \a index. Note that the layouter does not check
116 * whether the item really has a header: Usually only
117 * the first item of a group gets a header (see
118 * isFirstGroupItem()).
120 QRectF
groupHeaderRect(int index
) const;
123 * @return Column of the item with the index \a index.
124 * -1 is returned if an invalid index is given.
126 int itemColumn(int index
) const;
129 * @return Row of the item with the index \a index.
130 * -1 is returned if an invalid index is given.
132 int itemRow(int index
) const;
135 * @return Maximum number of (at least partly) visible items for
138 int maximumVisibleItems() const;
141 * @return True if the item with the index \p itemIndex
142 * is the first item within a group.
144 bool isFirstGroupItem(int itemIndex
) const;
147 * Marks the layouter as dirty. This means as soon as a property of
148 * the layouter gets read, an expensive relayout will be done.
152 inline int columnCount() const
154 return m_columnCount
;
159 * @return True if the layouter has been marked as dirty and hence has
160 * not called yet doLayout(). Is enabled only in the debugging
161 * mode, as it is not useful to check the dirty state otherwise.
168 void updateVisibleIndexes();
169 bool createGroupHeaders();
172 * @return Minimum width of group headers when grouping is enabled in the horizontal
173 * alignment mode. The header alignment is done like this:
174 * Header-1 Header-2 Header-3
175 * Item 1 Item 4 Item 7
176 * Item 2 Item 5 Item 8
177 * Item 3 Item 6 Item 9
179 qreal
minimumGroupHeaderWidth() const;
183 bool m_visibleIndexesDirty
;
185 Qt::Orientation m_scrollOrientation
;
190 qreal m_headerHeight
;
191 const KItemModelBase
* m_model
;
192 KItemListSizeHintResolver
* m_sizeHintResolver
;
194 qreal m_scrollOffset
;
195 qreal m_maximumScrollOffset
;
198 qreal m_maximumItemOffset
;
200 int m_firstVisibleIndex
;
201 int m_lastVisibleIndex
;
207 QVector
<qreal
> m_rowOffsets
;
208 QVector
<qreal
> m_columnOffsets
;
210 // Stores all item indexes that are the first item of a group.
211 // Assures fast access for KItemListViewLayouter::isFirstGroupItem().
212 QSet
<int> m_groupItemIndexes
;
213 qreal m_groupHeaderHeight
;
214 qreal m_groupHeaderMargin
;
220 QVector
<ItemInfo
> m_itemInfos
;
222 friend class KItemListControllerTest
;