2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
4 * Based on the Itemviews NG project from Trolltech Labs
6 * SPDX-License-Identifier: GPL-2.0-or-later
9 #ifndef KITEMLISTWIDGET_H
10 #define KITEMLISTWIDGET_H
12 #include "dolphin_export.h"
13 #include "kitemviews/kitemliststyleoption.h"
16 #include <QGraphicsWidget>
20 class KItemListSelectionToggle
;
22 class QPropertyAnimation
;
25 * @brief Provides generic information for all KItemListWidgets
26 * for which the construction of any specific KItemListWidget isn't required.
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.
33 class DOLPHIN_EXPORT KItemListWidgetInformant
36 KItemListWidgetInformant();
37 virtual ~KItemListWidgetInformant();
39 virtual void calculateItemSizeHints(QVector
<std::pair
<qreal
, bool>> &logicalHeightHints
, qreal
&logicalWidthHint
, const KItemListView
*view
) const = 0;
41 virtual qreal
preferredRoleColumnWidth(const QByteArray
&role
, int index
, const KItemListView
*view
) const = 0;
45 * @brief Widget that shows a visible item from the model.
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.
51 class DOLPHIN_EXPORT KItemListWidget
: public QGraphicsWidget
56 KItemListWidget(KItemListWidgetInformant
*informant
, QGraphicsItem
*parent
);
57 ~KItemListWidget() override
;
59 void setIndex(int index
);
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;
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().
71 void paint(QPainter
*painter
, const QStyleOptionGraphicsItem
*option
, QWidget
*widget
= nullptr) override
;
73 void setVisibleRoles(const QList
<QByteArray
> &roles
);
74 QList
<QByteArray
> visibleRoles() const;
77 * Sets the width of a role that should be used if the alignment of the content
78 * should be done in columns.
80 void setColumnWidth(const QByteArray
&role
, qreal width
);
81 qreal
columnWidth(const QByteArray
&role
) const;
83 void setSidePadding(qreal leftPaddingWidth
, qreal rightPaddingWidth
);
84 qreal
leftPadding() const;
85 qreal
rightPadding() const;
87 void setStyleOption(const KItemListStyleOption
&option
);
88 const KItemListStyleOption
&styleOption() const;
90 // TODO: Hides QGraphicsItem::setSelected()/isSelected(). Replace
91 // this by using the default mechanism.
92 void setSelected(bool selected
);
93 bool isSelected() const;
95 void setCurrent(bool current
);
96 bool isCurrent() const;
98 void setHovered(bool hovered
);
99 bool isHovered() const;
101 void setExpansionAreaHovered(bool hover
);
102 bool expansionAreaHovered() const;
104 void setHoverPosition(const QPointF
&pos
);
106 void setAlternateBackground(bool enable
);
107 bool alternateBackground() const;
109 void setEnabledSelectionToggle(bool enabled
);
110 bool enabledSelectionToggle() const;
113 * Sets the sibling information for the item and all of its parents.
114 * The sibling information of the upper most parent is represented by
115 * the first bit, the sibling information of the item by the last bit.
116 * The sibling information is useful for drawing the branches in
119 void setSiblingsInformation(const QBitArray
&siblings
);
120 QBitArray
siblingsInformation() const;
123 * Allows the user to edit the role \a role. The signals
124 * roleEditingCanceled() or roleEditingFinished() will be
125 * emitted after editing. An ongoing editing gets canceled if
126 * the role is empty. Derived classes must implement
127 * editedRoleChanged().
129 void setEditedRole(const QByteArray
&role
);
130 QByteArray
editedRole() const;
133 * Contains the actual icon size used to draw the icon.
134 * Also used during icon resizing animation.
136 void setIconSize(int iconSize
);
137 int iconSize() const;
140 * @return True if \a point is inside KItemListWidget::hoverRect(),
141 * KItemListWidget::textRect(), KItemListWidget::selectionToggleRect()
142 * or KItemListWidget::expansionToggleRect().
145 bool contains(const QPointF
&point
) const override
;
148 * @return Rectangle for the area that shows the icon.
150 virtual QRectF
iconRect() const = 0;
153 * @return Rectangle for the area that contains the text-properties.
155 virtual QRectF
textRect() const = 0;
158 * @return Focus rectangle for indicating the current item. Per default
159 * textRect() will be returned. Overwrite this method if textRect()
160 * provides a larger rectangle than the actual text (e.g. to
161 * be aligned with the iconRect()). The textFocusRect() may not be
162 * outside the boundaries of textRect().
164 virtual QRectF
textFocusRect() const;
167 * @return Rectangle around which a selection box should be drawn if the item is selected.
169 virtual QRectF
selectionRect() const = 0;
172 * @return Rectangle for the selection-toggle that is used to select or deselect an item.
173 * Per default an empty rectangle is returned which means that no selection-toggle
176 virtual QRectF
selectionToggleRect() const;
179 * @return Rectangle for the expansion-toggle that is used to open a sub-tree of the model.
180 * Per default an empty rectangle is returned which means that no opening of sub-trees
183 virtual QRectF
expansionToggleRect() const;
186 * @return Pixmap that is used when dragging an item. Per default the current state of the
187 * widget is returned as pixmap.
189 virtual QPixmap
createDragPixmap(const QStyleOptionGraphicsItem
*option
, QWidget
*widget
= nullptr);
192 * Starts an animation that makes clear that the item will be activated soon.
193 * @param timeUntilActivation time in milliseconds until the item will activate
195 virtual void startActivateSoonAnimation(int timeUntilActivation
);
198 void roleEditingCanceled(int index
, const QByteArray
&role
, const QVariant
&value
);
199 void roleEditingFinished(int index
, const QByteArray
&role
, const QVariant
&value
);
202 virtual void dataChanged(const QHash
<QByteArray
, QVariant
> ¤t
, const QSet
<QByteArray
> &roles
= QSet
<QByteArray
>());
203 virtual void visibleRolesChanged(const QList
<QByteArray
> ¤t
, const QList
<QByteArray
> &previous
);
204 virtual void columnWidthChanged(const QByteArray
&role
, qreal current
, qreal previous
);
205 virtual void sidePaddingChanged(qreal leftPaddingWidth
, qreal rightPaddingWidth
);
206 virtual void styleOptionChanged(const KItemListStyleOption
¤t
, const KItemListStyleOption
&previous
);
207 virtual void currentChanged(bool current
);
208 virtual void selectedChanged(bool selected
);
209 virtual void hoveredChanged(bool hovered
);
210 virtual void alternateBackgroundChanged(bool enabled
);
211 virtual void siblingsInformationChanged(const QBitArray
¤t
, const QBitArray
&previous
);
212 virtual void editedRoleChanged(const QByteArray
¤t
, const QByteArray
&previous
);
213 virtual void iconSizeChanged(int current
, int previous
);
214 void resizeEvent(QGraphicsSceneResizeEvent
*event
) override
;
215 void clearHoverCache();
218 * Called when the user starts hovering this item.
220 virtual void hoverSequenceStarted();
223 * Called in regular intervals while the user is hovering this item.
225 * @param sequenceIndex An index that increases over time while the user hovers.
227 virtual void hoverSequenceIndexChanged(int sequenceIndex
);
230 * Called when the user stops hovering this item.
232 virtual void hoverSequenceEnded();
235 * @return The current opacity of the hover-animation. When implementing a custom painting-code for a hover-state
236 * this opacity value should be respected.
238 qreal
hoverOpacity() const;
240 int hoverSequenceIndex() const;
242 const KItemListWidgetInformant
*informant() const;
245 void slotHoverSequenceTimerTimeout();
248 void initializeSelectionToggle();
249 void setHoverOpacity(qreal opacity
);
250 void drawItemStyleOption(QPainter
*painter
, QWidget
*widget
, QStyle::State styleState
);
253 KItemListWidgetInformant
*m_informant
;
258 bool m_expansionAreaHovered
;
259 bool m_alternateBackground
;
260 bool m_enabledSelectionToggle
;
261 QHash
<QByteArray
, QVariant
> m_data
;
262 QList
<QByteArray
> m_visibleRoles
;
263 QHash
<QByteArray
, qreal
> m_columnWidths
;
265 qreal m_rightPadding
;
266 KItemListStyleOption m_styleOption
;
267 QBitArray m_siblingsInfo
;
269 qreal m_hoverOpacity
;
270 mutable QPixmap
*m_hoverCache
;
272 int m_hoverSequenceIndex
;
273 QTimer m_hoverSequenceTimer
;
275 KItemListSelectionToggle
*m_selectionToggle
;
277 QByteArray m_editedRole
;
281 inline const KItemListWidgetInformant
*KItemListWidget::informant() const