]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistwidget.h
Add clang-format and format code as in Frameworks
[dolphin.git] / src / kitemviews / kitemlistwidget.h
1 /*
2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
3 *
4 * Based on the Itemviews NG project from Trolltech Labs
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9 #ifndef KITEMLISTWIDGET_H
10 #define KITEMLISTWIDGET_H
11
12 #include "dolphin_export.h"
13 #include "kitemviews/kitemliststyleoption.h"
14
15 #include <QBitArray>
16 #include <QGraphicsWidget>
17 #include <QStyle>
18 #include <QTimer>
19
20 class KItemListSelectionToggle;
21 class KItemListView;
22 class QPropertyAnimation;
23
24 /**
25 * @brief Provides information for creating an instance of KItemListWidget.
26 *
27 * KItemListView only creates KItemListWidget instances for the visible
28 * area. For calculating the required size of all items the expected
29 * size for the invisible items must be accessible. KItemListWidgetInformant
30 * provides this information.
31 */
32 class DOLPHIN_EXPORT KItemListWidgetInformant
33 {
34 public:
35 KItemListWidgetInformant();
36 virtual ~KItemListWidgetInformant();
37
38 virtual void calculateItemSizeHints(QVector<std::pair<qreal, bool>> &logicalHeightHints, qreal &logicalWidthHint, const KItemListView *view) const = 0;
39
40 virtual qreal preferredRoleColumnWidth(const QByteArray &role, int index, const KItemListView *view) const = 0;
41 };
42
43 /**
44 * @brief Widget that shows a visible item from the model.
45 *
46 * For showing an item from a custom model it is required to at least overwrite KItemListWidget::paint().
47 * All properties are set by KItemListView, for each property there is a corresponding
48 * virtual protected method that allows to react on property changes.
49 */
50 class DOLPHIN_EXPORT KItemListWidget : public QGraphicsWidget
51 {
52 Q_OBJECT
53
54 Q_PROPERTY(int iconSize READ iconSize WRITE setIconSize)
55
56 public:
57 KItemListWidget(KItemListWidgetInformant *informant, QGraphicsItem *parent);
58 ~KItemListWidget() override;
59
60 void setIndex(int index);
61 int index() const;
62
63 void setData(const QHash<QByteArray, QVariant> &data, const QSet<QByteArray> &roles = QSet<QByteArray>());
64 QHash<QByteArray, QVariant> data() const;
65
66 /**
67 * Draws the hover-rectangle if the item is hovered. Overwrite this method
68 * to show the data of the custom model provided by KItemListWidget::data().
69 * @reimp
70 */
71 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
72
73 void setVisibleRoles(const QList<QByteArray> &roles);
74 QList<QByteArray> visibleRoles() const;
75
76 /**
77 * Sets the width of a role that should be used if the alignment of the content
78 * should be done in columns.
79 */
80 void setColumnWidth(const QByteArray &role, qreal width);
81 qreal columnWidth(const QByteArray &role) const;
82
83 void setSidePadding(qreal width);
84 qreal sidePadding() const;
85
86 void setStyleOption(const KItemListStyleOption &option);
87 const KItemListStyleOption &styleOption() const;
88
89 // TODO: Hides QGraphicsItem::setSelected()/isSelected(). Replace
90 // this by using the default mechanism.
91 void setSelected(bool selected);
92 bool isSelected() const;
93
94 void setCurrent(bool current);
95 bool isCurrent() const;
96
97 void setHovered(bool hovered);
98 bool isHovered() const;
99
100 void setExpansionAreaHovered(bool hover);
101 bool expansionAreaHovered() const;
102
103 void setHoverPosition(const QPointF &pos);
104
105 void setAlternateBackground(bool enable);
106 bool alternateBackground() const;
107
108 void setEnabledSelectionToggle(bool enabled);
109 bool enabledSelectionToggle() const;
110
111 /**
112 * Sets the sibling information for the item and all of its parents.
113 * The sibling information of the upper most parent is represented by
114 * the first bit, the sibling information of the item by the last bit.
115 * The sibling information is useful for drawing the branches in
116 * tree views.
117 */
118 void setSiblingsInformation(const QBitArray &siblings);
119 QBitArray siblingsInformation() const;
120
121 /**
122 * Allows the user to edit the role \a role. The signals
123 * roleEditingCanceled() or roleEditingFinished() will be
124 * emitted after editing. An ongoing editing gets canceled if
125 * the role is empty. Derived classes must implement
126 * editedRoleChanged().
127 */
128 void setEditedRole(const QByteArray &role);
129 QByteArray editedRole() const;
130
131 /**
132 * Contains the actual icon size used to draw the icon.
133 * Also used during icon resizing animation.
134 */
135 void setIconSize(int iconSize);
136 int iconSize() const;
137
138 /**
139 * @return True if \a point is inside KItemListWidget::hoverRect(),
140 * KItemListWidget::textRect(), KItemListWidget::selectionToggleRect()
141 * or KItemListWidget::expansionToggleRect().
142 * @reimp
143 */
144 bool contains(const QPointF &point) const override;
145
146 /**
147 * @return Rectangle for the area that shows the icon.
148 */
149 virtual QRectF iconRect() const = 0;
150
151 /**
152 * @return Rectangle for the area that contains the text-properties.
153 */
154 virtual QRectF textRect() const = 0;
155
156 /**
157 * @return Focus rectangle for indicating the current item. Per default
158 * textRect() will be returned. Overwrite this method if textRect()
159 * provides a larger rectangle than the actual text (e.g. to
160 * be aligned with the iconRect()). The textFocusRect() may not be
161 * outside the boundaries of textRect().
162 */
163 virtual QRectF textFocusRect() const;
164
165 /**
166 * @return Rectangle around which a selection box should be drawn if the item is selected.
167 */
168 virtual QRectF selectionRect() const = 0;
169
170 /**
171 * @return Rectangle for the selection-toggle that is used to select or deselect an item.
172 * Per default an empty rectangle is returned which means that no selection-toggle
173 * is available.
174 */
175 virtual QRectF selectionToggleRect() const;
176
177 /**
178 * @return Rectangle for the expansion-toggle that is used to open a sub-tree of the model.
179 * Per default an empty rectangle is returned which means that no opening of sub-trees
180 * is supported.
181 */
182 virtual QRectF expansionToggleRect() const;
183
184 /**
185 * @return Pixmap that is used when dragging an item. Per default the current state of the
186 * widget is returned as pixmap.
187 */
188 virtual QPixmap createDragPixmap(const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr);
189
190 Q_SIGNALS:
191 void roleEditingCanceled(int index, const QByteArray &role, const QVariant &value);
192 void roleEditingFinished(int index, const QByteArray &role, const QVariant &value);
193
194 protected:
195 virtual void dataChanged(const QHash<QByteArray, QVariant> &current, const QSet<QByteArray> &roles = QSet<QByteArray>());
196 virtual void visibleRolesChanged(const QList<QByteArray> &current, const QList<QByteArray> &previous);
197 virtual void columnWidthChanged(const QByteArray &role, qreal current, qreal previous);
198 virtual void sidePaddingChanged(qreal width);
199 virtual void styleOptionChanged(const KItemListStyleOption &current, const KItemListStyleOption &previous);
200 virtual void currentChanged(bool current);
201 virtual void selectedChanged(bool selected);
202 virtual void hoveredChanged(bool hovered);
203 virtual void alternateBackgroundChanged(bool enabled);
204 virtual void siblingsInformationChanged(const QBitArray &current, const QBitArray &previous);
205 virtual void editedRoleChanged(const QByteArray &current, const QByteArray &previous);
206 virtual void iconSizeChanged(int current, int previous);
207 void resizeEvent(QGraphicsSceneResizeEvent *event) override;
208 void clearHoverCache();
209
210 /**
211 * Called when the user starts hovering this item.
212 */
213 virtual void hoverSequenceStarted();
214
215 /**
216 * Called in regular intervals while the user is hovering this item.
217 *
218 * @param sequenceIndex An index that increases over time while the user hovers.
219 */
220 virtual void hoverSequenceIndexChanged(int sequenceIndex);
221
222 /**
223 * Called when the user stops hovering this item.
224 */
225 virtual void hoverSequenceEnded();
226
227 /**
228 * @return The current opacity of the hover-animation. When implementing a custom painting-code for a hover-state
229 * this opacity value should be respected.
230 */
231 qreal hoverOpacity() const;
232
233 int hoverSequenceIndex() const;
234
235 const KItemListWidgetInformant *informant() const;
236
237 private Q_SLOTS:
238 void slotHoverAnimationFinished();
239 void slotHoverSequenceTimerTimeout();
240
241 private:
242 void initializeSelectionToggle();
243 void setHoverOpacity(qreal opacity);
244 void drawItemStyleOption(QPainter *painter, QWidget *widget, QStyle::State styleState);
245
246 private:
247 Q_PROPERTY(qreal hoverOpacity READ hoverOpacity WRITE setHoverOpacity)
248
249 KItemListWidgetInformant *m_informant;
250 int m_index;
251 bool m_selected;
252 bool m_current;
253 bool m_hovered;
254 bool m_expansionAreaHovered;
255 bool m_alternateBackground;
256 bool m_enabledSelectionToggle;
257 QHash<QByteArray, QVariant> m_data;
258 QList<QByteArray> m_visibleRoles;
259 QHash<QByteArray, qreal> m_columnWidths;
260 qreal m_sidePadding;
261 KItemListStyleOption m_styleOption;
262 QBitArray m_siblingsInfo;
263
264 qreal m_hoverOpacity;
265 mutable QPixmap *m_hoverCache;
266 QPropertyAnimation *m_hoverAnimation;
267
268 int m_hoverSequenceIndex;
269 QTimer m_hoverSequenceTimer;
270
271 KItemListSelectionToggle *m_selectionToggle;
272
273 QByteArray m_editedRole;
274 int m_iconSize;
275 };
276
277 inline const KItemListWidgetInformant *KItemListWidget::informant() const
278 {
279 return m_informant;
280 }
281
282 #endif