]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistwidget.h
Add name-only tooltip and make logicalHeightHints a pair of qreal and bool
[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,
41 int index,
42 const KItemListView* view) const = 0;
43 };
44
45 /**
46 * @brief Widget that shows a visible item from the model.
47 *
48 * For showing an item from a custom model it is required to at least overwrite KItemListWidget::paint().
49 * All properties are set by KItemListView, for each property there is a corresponding
50 * virtual protected method that allows to react on property changes.
51 */
52 class DOLPHIN_EXPORT KItemListWidget : public QGraphicsWidget
53 {
54 Q_OBJECT
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 setStyleOption(const KItemListStyleOption& option);
84 const KItemListStyleOption& styleOption() const;
85
86 // TODO: Hides QGraphicsItem::setSelected()/isSelected(). Replace
87 // this by using the default mechanism.
88 void setSelected(bool selected);
89 bool isSelected() const;
90
91 void setCurrent(bool current);
92 bool isCurrent() const;
93
94 void setHovered(bool hovered);
95 bool isHovered() const;
96
97 void setHoverPosition(const QPointF& pos);
98
99 void setAlternateBackground(bool enable);
100 bool alternateBackground() const;
101
102 void setEnabledSelectionToggle(bool enabled);
103 bool enabledSelectionToggle() const;
104
105 /**
106 * Sets the sibling information for the item and all of its parents.
107 * The sibling information of the upper most parent is represented by
108 * the first bit, the sibling information of the item by the last bit.
109 * The sibling information is useful for drawing the branches in
110 * tree views.
111 */
112 void setSiblingsInformation(const QBitArray& siblings);
113 QBitArray siblingsInformation() const;
114
115 /**
116 * Allows the user to edit the role \a role. The signals
117 * roleEditingCanceled() or roleEditingFinished() will be
118 * emitted after editing. An ongoing editing gets canceled if
119 * the role is empty. Derived classes must implement
120 * editedRoleChanged().
121 */
122 void setEditedRole(const QByteArray& role);
123 QByteArray editedRole() const;
124
125 /**
126 * @return True if \a point is inside KItemListWidget::hoverRect(),
127 * KItemListWidget::textRect(), KItemListWidget::selectionToggleRect()
128 * or KItemListWidget::expansionToggleRect().
129 * @reimp
130 */
131 bool contains(const QPointF& point) const override;
132
133 /**
134 * @return Rectangle for the area that shows the icon.
135 */
136 virtual QRectF iconRect() const = 0;
137
138 /**
139 * @return Rectangle for the area that contains the text-properties.
140 */
141 virtual QRectF textRect() const = 0;
142
143 /**
144 * @return Focus rectangle for indicating the current item. Per default
145 * textRect() will be returned. Overwrite this method if textRect()
146 * provides a larger rectangle than the actual text (e.g. to
147 * be aligned with the iconRect()). The textFocusRect() may not be
148 * outside the boundaries of textRect().
149 */
150 virtual QRectF textFocusRect() const;
151
152 /**
153 * @return Rectangle around which a selection box should be drawn if the item is selected.
154 */
155 virtual QRectF selectionRect() const = 0;
156
157 /**
158 * @return Rectangle for the selection-toggle that is used to select or deselect an item.
159 * Per default an empty rectangle is returned which means that no selection-toggle
160 * is available.
161 */
162 virtual QRectF selectionToggleRect() const;
163
164 /**
165 * @return Rectangle for the expansion-toggle that is used to open a sub-tree of the model.
166 * Per default an empty rectangle is returned which means that no opening of sub-trees
167 * is supported.
168 */
169 virtual QRectF expansionToggleRect() const;
170
171 /**
172 * @return Pixmap that is used when dragging an item. Per default the current state of the
173 * widget is returned as pixmap.
174 */
175 virtual QPixmap createDragPixmap(const QStyleOptionGraphicsItem* option, QWidget* widget = nullptr);
176
177 Q_SIGNALS:
178 void roleEditingCanceled(int index, const QByteArray& role, const QVariant& value);
179 void roleEditingFinished(int index, const QByteArray& role, const QVariant& value);
180
181 protected:
182 virtual void dataChanged(const QHash<QByteArray, QVariant>& current, const QSet<QByteArray>& roles = QSet<QByteArray>());
183 virtual void visibleRolesChanged(const QList<QByteArray>& current, const QList<QByteArray>& previous);
184 virtual void columnWidthChanged(const QByteArray& role, qreal current, qreal previous);
185 virtual void styleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous);
186 virtual void currentChanged(bool current);
187 virtual void selectedChanged(bool selected);
188 virtual void hoveredChanged(bool hovered);
189 virtual void alternateBackgroundChanged(bool enabled);
190 virtual void siblingsInformationChanged(const QBitArray& current, const QBitArray& previous);
191 virtual void editedRoleChanged(const QByteArray& current, const QByteArray& previous);
192 void resizeEvent(QGraphicsSceneResizeEvent* event) override;
193
194 /**
195 * Called when the user starts hovering this item.
196 */
197 virtual void hoverSequenceStarted();
198
199 /**
200 * Called in regular intervals while the user is hovering this item.
201 *
202 * @param sequenceIndex An index that increases over time while the user hovers.
203 */
204 virtual void hoverSequenceIndexChanged(int sequenceIndex);
205
206 /**
207 * Called when the user stops hovering this item.
208 */
209 virtual void hoverSequenceEnded();
210
211 /**
212 * @return The current opacity of the hover-animation. When implementing a custom painting-code for a hover-state
213 * this opacity value should be respected.
214 */
215 qreal hoverOpacity() const;
216
217 int hoverSequenceIndex() const;
218
219 const KItemListWidgetInformant* informant() const;
220
221 private Q_SLOTS:
222 void slotHoverAnimationFinished();
223 void slotHoverSequenceTimerTimeout();
224
225 private:
226 void initializeSelectionToggle();
227 void setHoverOpacity(qreal opacity);
228 void clearHoverCache();
229 void drawItemStyleOption(QPainter* painter, QWidget* widget, QStyle::State styleState);
230
231 private:
232 Q_PROPERTY(qreal hoverOpacity READ hoverOpacity WRITE setHoverOpacity)
233
234 KItemListWidgetInformant* m_informant;
235 int m_index;
236 bool m_selected;
237 bool m_current;
238 bool m_hovered;
239 bool m_alternateBackground;
240 bool m_enabledSelectionToggle;
241 QHash<QByteArray, QVariant> m_data;
242 QList<QByteArray> m_visibleRoles;
243 QHash<QByteArray, qreal> m_columnWidths;
244 KItemListStyleOption m_styleOption;
245 QBitArray m_siblingsInfo;
246
247 qreal m_hoverOpacity;
248 mutable QPixmap* m_hoverCache;
249 QPropertyAnimation* m_hoverAnimation;
250
251 int m_hoverSequenceIndex;
252 QTimer m_hoverSequenceTimer;
253
254 KItemListSelectionToggle* m_selectionToggle;
255
256 QByteArray m_editedRole;
257 };
258
259 inline const KItemListWidgetInformant* KItemListWidget::informant() const
260 {
261 return m_informant;
262 }
263
264 #endif
265
266