]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistwidget.h
New selection effects
[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 public:
56 KItemListWidget(KItemListWidgetInformant *informant, QGraphicsItem *parent);
57 ~KItemListWidget() override;
58
59 void setIndex(int index);
60 int index() const;
61
62 void setData(const QHash<QByteArray, QVariant> &data, const QSet<QByteArray> &roles = QSet<QByteArray>());
63 QHash<QByteArray, QVariant> data() const;
64 QVariant value(const QByteArray &key) 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 leftPaddingWidth, qreal rightPaddingWidth);
84 qreal leftPadding() const;
85 qreal rightPadding() const;
86
87 void setStyleOption(const KItemListStyleOption &option);
88 const KItemListStyleOption &styleOption() const;
89
90 // TODO: Hides QGraphicsItem::setSelected()/isSelected(). Replace
91 // this by using the default mechanism.
92 void setSelected(bool selected);
93 bool isSelected() const;
94
95 void setCurrent(bool current);
96 bool isCurrent() const;
97
98 void setHovered(bool hovered);
99 bool isHovered() const;
100
101 /** Sets a purely visual pressed highlight effect. */
102 void setPressed(bool enabled);
103 bool isPressed() const;
104
105 void setExpansionAreaHovered(bool hover);
106 bool expansionAreaHovered() const;
107
108 void setHoverPosition(const QPointF &pos);
109
110 void setAlternateBackground(bool enable);
111 bool alternateBackground() const;
112
113 void setEnabledSelectionToggle(bool enabled);
114 bool enabledSelectionToggle() const;
115
116 /**
117 * Sets the sibling information for the item and all of its parents.
118 * The sibling information of the upper most parent is represented by
119 * the first bit, the sibling information of the item by the last bit.
120 * The sibling information is useful for drawing the branches in
121 * tree views.
122 */
123 void setSiblingsInformation(const QBitArray &siblings);
124 QBitArray siblingsInformation() const;
125
126 /**
127 * Allows the user to edit the role \a role. The signals
128 * roleEditingCanceled() or roleEditingFinished() will be
129 * emitted after editing. An ongoing editing gets canceled if
130 * the role is empty. Derived classes must implement
131 * editedRoleChanged().
132 */
133 void setEditedRole(const QByteArray &role);
134 QByteArray editedRole() const;
135
136 /**
137 * Contains the actual icon size used to draw the icon.
138 * Also used during icon resizing animation.
139 */
140 void setIconSize(int iconSize);
141 int iconSize() const;
142
143 /**
144 * @return True if \a point is inside KItemListWidget::selectionRectFull(),
145 * KItemListWidget::selectionToggleRect()
146 * or KItemListWidget::expansionToggleRect().
147 * @reimp
148 */
149 bool contains(const QPointF &point) const override;
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 * Used for drawing the visuals, and situations where we want the behavior of the
167 * selection to match the visuals.
168 *
169 * @return The rectangle around selection.
170 */
171 virtual QRectF selectionRectFull() const = 0;
172
173 /**
174 * @return The core area of the item. All of it reacts exactly the same way to mouse clicks.
175 */
176 virtual QRectF selectionRectCore() const = 0;
177
178 /**
179 * @return Rectangle for the selection-toggle that is used to select or deselect an item.
180 * Per default an empty rectangle is returned which means that no selection-toggle
181 * is available.
182 */
183 virtual QRectF selectionToggleRect() const;
184
185 /**
186 * @return Rectangle for the expansion-toggle that is used to open a sub-tree of the model.
187 * Per default an empty rectangle is returned which means that no opening of sub-trees
188 * is supported.
189 */
190 virtual QRectF expansionToggleRect() const;
191
192 /**
193 * @return Pixmap that is used when dragging an item. Per default the current state of the
194 * widget is returned as pixmap.
195 */
196 virtual QPixmap createDragPixmap(const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr);
197
198 /**
199 * Starts an animation that makes clear that the item will be activated soon.
200 * @param timeUntilActivation time in milliseconds until the item will activate
201 */
202 virtual void startActivateSoonAnimation(int timeUntilActivation);
203
204 Q_SIGNALS:
205 void roleEditingCanceled(int index, const QByteArray &role, const QVariant &value);
206 void roleEditingFinished(int index, const QByteArray &role, const QVariant &value);
207
208 protected:
209 virtual void dataChanged(const QHash<QByteArray, QVariant> &current, const QSet<QByteArray> &roles = QSet<QByteArray>());
210 virtual void visibleRolesChanged(const QList<QByteArray> &current, const QList<QByteArray> &previous);
211 virtual void columnWidthChanged(const QByteArray &role, qreal current, qreal previous);
212 virtual void sidePaddingChanged(qreal leftPaddingWidth, qreal rightPaddingWidth);
213 virtual void styleOptionChanged(const KItemListStyleOption &current, const KItemListStyleOption &previous);
214 virtual void currentChanged(bool current);
215 virtual void selectedChanged(bool selected);
216 virtual void hoveredChanged(bool hovered);
217 virtual void alternateBackgroundChanged(bool enabled);
218 virtual void siblingsInformationChanged(const QBitArray &current, const QBitArray &previous);
219 virtual void editedRoleChanged(const QByteArray &current, const QByteArray &previous);
220 virtual void iconSizeChanged(int current, int previous);
221 void resizeEvent(QGraphicsSceneResizeEvent *event) override;
222 void clearHoverCache();
223
224 /**
225 * Called when the user starts hovering this item.
226 */
227 virtual void hoverSequenceStarted();
228
229 /**
230 * Called in regular intervals while the user is hovering this item.
231 *
232 * @param sequenceIndex An index that increases over time while the user hovers.
233 */
234 virtual void hoverSequenceIndexChanged(int sequenceIndex);
235
236 /**
237 * Called when the user stops hovering this item.
238 */
239 virtual void hoverSequenceEnded();
240
241 /**
242 * @return The current opacity of the hover-animation. When implementing a custom painting-code for a hover-state
243 * this opacity value should be respected.
244 */
245 qreal hoverOpacity() const;
246
247 int hoverSequenceIndex() const;
248
249 const KItemListWidgetInformant *informant() const;
250
251 private Q_SLOTS:
252 void slotHoverSequenceTimerTimeout();
253
254 private:
255 void initializeSelectionToggle();
256 void setHoverOpacity(qreal opacity);
257 void drawItemStyleOption(QPainter *painter, QWidget *widget, QStyle::State styleState);
258
259 private:
260 KItemListWidgetInformant *m_informant;
261 int m_index;
262 bool m_selected;
263 bool m_current;
264 bool m_hovered;
265 bool m_expansionAreaHovered;
266 bool m_alternateBackground;
267 bool m_enabledSelectionToggle;
268 bool m_clickHighlighted;
269 QHash<QByteArray, QVariant> m_data;
270 QList<QByteArray> m_visibleRoles;
271 QHash<QByteArray, qreal> m_columnWidths;
272 qreal m_leftPadding;
273 qreal m_rightPadding;
274 KItemListStyleOption m_styleOption;
275 QBitArray m_siblingsInfo;
276
277 qreal m_hoverOpacity;
278 mutable QPixmap *m_hoverCache;
279
280 int m_hoverSequenceIndex;
281 QTimer m_hoverSequenceTimer;
282
283 KItemListSelectionToggle *m_selectionToggle;
284
285 QByteArray m_editedRole;
286 int m_iconSize;
287 };
288
289 inline const KItemListWidgetInformant *KItemListWidget::informant() const
290 {
291 return m_informant;
292 }
293
294 #endif