]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistwidget.h
Output of licensedigger + manual cleanup afterwards.
[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
19 class KItemListSelectionToggle;
20 class KItemListView;
21 class QPropertyAnimation;
22
23 /**
24 * @brief Provides information for creating an instance of KItemListWidget.
25 *
26 * KItemListView only creates KItemListWidget instances for the visible
27 * area. For calculating the required size of all items the expected
28 * size for the invisible items must be accessible. KItemListWidgetInformant
29 * provides this information.
30 */
31 class DOLPHIN_EXPORT KItemListWidgetInformant
32 {
33 public:
34 KItemListWidgetInformant();
35 virtual ~KItemListWidgetInformant();
36
37 virtual void calculateItemSizeHints(QVector<qreal>& logicalHeightHints, qreal& logicalWidthHint, const KItemListView* view) const = 0;
38
39 virtual qreal preferredRoleColumnWidth(const QByteArray& role,
40 int index,
41 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
65 /**
66 * Draws the hover-rectangle if the item is hovered. Overwrite this method
67 * to show the data of the custom model provided by KItemListWidget::data().
68 * @reimp
69 */
70 void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = nullptr) override;
71
72 void setVisibleRoles(const QList<QByteArray>& roles);
73 QList<QByteArray> visibleRoles() const;
74
75 /**
76 * Sets the width of a role that should be used if the alignment of the content
77 * should be done in columns.
78 */
79 void setColumnWidth(const QByteArray& role, qreal width);
80 qreal columnWidth(const QByteArray& role) const;
81
82 void setStyleOption(const KItemListStyleOption& option);
83 const KItemListStyleOption& styleOption() const;
84
85 // TODO: Hides QGraphicsItem::setSelected()/isSelected(). Replace
86 // this by using the default mechanism.
87 void setSelected(bool selected);
88 bool isSelected() const;
89
90 void setCurrent(bool current);
91 bool isCurrent() const;
92
93 void setHovered(bool hovered);
94 bool isHovered() const;
95
96 void setHoverPosition(const QPointF& pos);
97
98 void setAlternateBackground(bool enable);
99 bool alternateBackground() const;
100
101 void setEnabledSelectionToggle(bool enabled);
102 bool enabledSelectionToggle() const;
103
104 /**
105 * Sets the sibling information for the item and all of its parents.
106 * The sibling information of the upper most parent is represented by
107 * the first bit, the sibling information of the item by the last bit.
108 * The sibling information is useful for drawing the branches in
109 * tree views.
110 */
111 void setSiblingsInformation(const QBitArray& siblings);
112 QBitArray siblingsInformation() const;
113
114 /**
115 * Allows the user to edit the role \a role. The signals
116 * roleEditingCanceled() or roleEditingFinished() will be
117 * emitted after editing. An ongoing editing gets canceled if
118 * the role is empty. Derived classes must implement
119 * editedRoleChanged().
120 */
121 void setEditedRole(const QByteArray& role);
122 QByteArray editedRole() const;
123
124 /**
125 * @return True if \a point is inside KItemListWidget::hoverRect(),
126 * KItemListWidget::textRect(), KItemListWidget::selectionToggleRect()
127 * or KItemListWidget::expansionToggleRect().
128 * @reimp
129 */
130 bool contains(const QPointF& point) const override;
131
132 /**
133 * @return Rectangle for the area that shows the icon.
134 */
135 virtual QRectF iconRect() const = 0;
136
137 /**
138 * @return Rectangle for the area that contains the text-properties.
139 */
140 virtual QRectF textRect() const = 0;
141
142 /**
143 * @return Focus rectangle for indicating the current item. Per default
144 * textRect() will be returned. Overwrite this method if textRect()
145 * provides a larger rectangle than the actual text (e.g. to
146 * be aligned with the iconRect()). The textFocusRect() may not be
147 * outside the boundaries of textRect().
148 */
149 virtual QRectF textFocusRect() const;
150
151 /**
152 * @return Rectangle around which a selection box should be drawn if the item is selected.
153 */
154 virtual QRectF selectionRect() const = 0;
155
156 /**
157 * @return Rectangle for the selection-toggle that is used to select or deselect an item.
158 * Per default an empty rectangle is returned which means that no selection-toggle
159 * is available.
160 */
161 virtual QRectF selectionToggleRect() const;
162
163 /**
164 * @return Rectangle for the expansion-toggle that is used to open a sub-tree of the model.
165 * Per default an empty rectangle is returned which means that no opening of sub-trees
166 * is supported.
167 */
168 virtual QRectF expansionToggleRect() const;
169
170 /**
171 * @return Pixmap that is used when dragging an item. Per default the current state of the
172 * widget is returned as pixmap.
173 */
174 virtual QPixmap createDragPixmap(const QStyleOptionGraphicsItem* option, QWidget* widget = nullptr);
175
176 signals:
177 void roleEditingCanceled(int index, const QByteArray& role, const QVariant& value);
178 void roleEditingFinished(int index, const QByteArray& role, const QVariant& value);
179
180 protected:
181 virtual void dataChanged(const QHash<QByteArray, QVariant>& current, const QSet<QByteArray>& roles = QSet<QByteArray>());
182 virtual void visibleRolesChanged(const QList<QByteArray>& current, const QList<QByteArray>& previous);
183 virtual void columnWidthChanged(const QByteArray& role, qreal current, qreal previous);
184 virtual void styleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous);
185 virtual void currentChanged(bool current);
186 virtual void selectedChanged(bool selected);
187 virtual void hoveredChanged(bool hovered);
188 virtual void alternateBackgroundChanged(bool enabled);
189 virtual void siblingsInformationChanged(const QBitArray& current, const QBitArray& previous);
190 virtual void editedRoleChanged(const QByteArray& current, const QByteArray& previous);
191 void resizeEvent(QGraphicsSceneResizeEvent* event) override;
192
193 /**
194 * @return The current opacity of the hover-animation. When implementing a custom painting-code for a hover-state
195 * this opacity value should be respected.
196 */
197 qreal hoverOpacity() const;
198
199 const KItemListWidgetInformant* informant() const;
200
201 private slots:
202 void slotHoverAnimationFinished();
203
204 private:
205 void initializeSelectionToggle();
206 void setHoverOpacity(qreal opacity);
207 void clearHoverCache();
208 void drawItemStyleOption(QPainter* painter, QWidget* widget, QStyle::State styleState);
209
210 private:
211 Q_PROPERTY(qreal hoverOpacity READ hoverOpacity WRITE setHoverOpacity)
212
213 KItemListWidgetInformant* m_informant;
214 int m_index;
215 bool m_selected;
216 bool m_current;
217 bool m_hovered;
218 bool m_alternateBackground;
219 bool m_enabledSelectionToggle;
220 QHash<QByteArray, QVariant> m_data;
221 QList<QByteArray> m_visibleRoles;
222 QHash<QByteArray, qreal> m_columnWidths;
223 KItemListStyleOption m_styleOption;
224 QBitArray m_siblingsInfo;
225
226 qreal m_hoverOpacity;
227 mutable QPixmap* m_hoverCache;
228 QPropertyAnimation* m_hoverAnimation;
229
230 KItemListSelectionToggle* m_selectionToggle;
231
232 QByteArray m_editedRole;
233 };
234
235 inline const KItemListWidgetInformant* KItemListWidget::informant() const
236 {
237 return m_informant;
238 }
239
240 #endif
241
242