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