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 information for creating an instance of KItemListWidget.
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.
32 class DOLPHIN_EXPORT KItemListWidgetInformant
35 KItemListWidgetInformant();
36 virtual ~KItemListWidgetInformant();
38 virtual void calculateItemSizeHints(QVector
<std::pair
<qreal
, bool>>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const = 0;
40 virtual qreal
preferredRoleColumnWidth(const QByteArray
& role
,
42 const KItemListView
* view
) const = 0;
46 * @brief Widget that shows a visible item from the model.
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.
52 class DOLPHIN_EXPORT KItemListWidget
: public QGraphicsWidget
56 Q_PROPERTY(int iconSize READ iconSize WRITE setIconSize
)
59 KItemListWidget(KItemListWidgetInformant
* informant
, QGraphicsItem
* parent
);
60 ~KItemListWidget() override
;
62 void setIndex(int index
);
65 void setData(const QHash
<QByteArray
, QVariant
>& data
, const QSet
<QByteArray
>& roles
= QSet
<QByteArray
>());
66 QHash
<QByteArray
, QVariant
> data() const;
69 * Draws the hover-rectangle if the item is hovered. Overwrite this method
70 * to show the data of the custom model provided by KItemListWidget::data().
73 void paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
= nullptr) override
;
75 void setVisibleRoles(const QList
<QByteArray
>& roles
);
76 QList
<QByteArray
> visibleRoles() const;
79 * Sets the width of a role that should be used if the alignment of the content
80 * should be done in columns.
82 void setColumnWidth(const QByteArray
& role
, qreal width
);
83 qreal
columnWidth(const QByteArray
& role
) const;
85 void setSidePadding(qreal width
);
86 qreal
sidePadding() const;
88 void setStyleOption(const KItemListStyleOption
& option
);
89 const KItemListStyleOption
& styleOption() const;
91 // TODO: Hides QGraphicsItem::setSelected()/isSelected(). Replace
92 // this by using the default mechanism.
93 void setSelected(bool selected
);
94 bool isSelected() const;
96 void setCurrent(bool current
);
97 bool isCurrent() const;
99 void setHovered(bool hovered
);
100 bool isHovered() const;
102 void setExpansionAreaHovered(bool hover
);
103 bool expansionAreaHovered() const;
105 void setHoverPosition(const QPointF
& pos
);
107 void setAlternateBackground(bool enable
);
108 bool alternateBackground() const;
110 void setEnabledSelectionToggle(bool enabled
);
111 bool enabledSelectionToggle() const;
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
120 void setSiblingsInformation(const QBitArray
& siblings
);
121 QBitArray
siblingsInformation() const;
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().
130 void setEditedRole(const QByteArray
& role
);
131 QByteArray
editedRole() const;
134 * Contains the actual icon size used to draw the icon.
135 * Also used during icon resizing animation.
137 void setIconSize(int iconSize
);
138 int iconSize() const;
141 * @return True if \a point is inside KItemListWidget::hoverRect(),
142 * KItemListWidget::textRect(), KItemListWidget::selectionToggleRect()
143 * or KItemListWidget::expansionToggleRect().
146 bool contains(const QPointF
& point
) const override
;
149 * @return Rectangle for the area that shows the icon.
151 virtual QRectF
iconRect() const = 0;
154 * @return Rectangle for the area that contains the text-properties.
156 virtual QRectF
textRect() const = 0;
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().
165 virtual QRectF
textFocusRect() const;
168 * @return Rectangle around which a selection box should be drawn if the item is selected.
170 virtual QRectF
selectionRect() const = 0;
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
177 virtual QRectF
selectionToggleRect() const;
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
184 virtual QRectF
expansionToggleRect() const;
187 * @return Pixmap that is used when dragging an item. Per default the current state of the
188 * widget is returned as pixmap.
190 virtual QPixmap
createDragPixmap(const QStyleOptionGraphicsItem
* option
, QWidget
* widget
= nullptr);
193 void roleEditingCanceled(int index
, const QByteArray
& role
, const QVariant
& value
);
194 void roleEditingFinished(int index
, const QByteArray
& role
, const QVariant
& value
);
197 virtual void dataChanged(const QHash
<QByteArray
, QVariant
>& current
, const QSet
<QByteArray
>& roles
= QSet
<QByteArray
>());
198 virtual void visibleRolesChanged(const QList
<QByteArray
>& current
, const QList
<QByteArray
>& previous
);
199 virtual void columnWidthChanged(const QByteArray
& role
, qreal current
, qreal previous
);
200 virtual void sidePaddingChanged(qreal width
);
201 virtual void styleOptionChanged(const KItemListStyleOption
& current
, const KItemListStyleOption
& previous
);
202 virtual void currentChanged(bool current
);
203 virtual void selectedChanged(bool selected
);
204 virtual void hoveredChanged(bool hovered
);
205 virtual void alternateBackgroundChanged(bool enabled
);
206 virtual void siblingsInformationChanged(const QBitArray
& current
, const QBitArray
& previous
);
207 virtual void editedRoleChanged(const QByteArray
& current
, const QByteArray
& previous
);
208 virtual void iconSizeChanged(int current
, int previous
);
209 void resizeEvent(QGraphicsSceneResizeEvent
* event
) override
;
210 void clearHoverCache();
213 * Called when the user starts hovering this item.
215 virtual void hoverSequenceStarted();
218 * Called in regular intervals while the user is hovering this item.
220 * @param sequenceIndex An index that increases over time while the user hovers.
222 virtual void hoverSequenceIndexChanged(int sequenceIndex
);
225 * Called when the user stops hovering this item.
227 virtual void hoverSequenceEnded();
230 * @return The current opacity of the hover-animation. When implementing a custom painting-code for a hover-state
231 * this opacity value should be respected.
233 qreal
hoverOpacity() const;
235 int hoverSequenceIndex() const;
237 const KItemListWidgetInformant
* informant() const;
240 void slotHoverAnimationFinished();
241 void slotHoverSequenceTimerTimeout();
244 void initializeSelectionToggle();
245 void setHoverOpacity(qreal opacity
);
246 void drawItemStyleOption(QPainter
* painter
, QWidget
* widget
, QStyle::State styleState
);
249 Q_PROPERTY(qreal hoverOpacity READ hoverOpacity WRITE setHoverOpacity
)
251 KItemListWidgetInformant
* m_informant
;
256 bool m_expansionAreaHovered
;
257 bool m_alternateBackground
;
258 bool m_enabledSelectionToggle
;
259 QHash
<QByteArray
, QVariant
> m_data
;
260 QList
<QByteArray
> m_visibleRoles
;
261 QHash
<QByteArray
, qreal
> m_columnWidths
;
263 KItemListStyleOption m_styleOption
;
264 QBitArray m_siblingsInfo
;
266 qreal m_hoverOpacity
;
267 mutable QPixmap
* m_hoverCache
;
268 QPropertyAnimation
* m_hoverAnimation
;
270 int m_hoverSequenceIndex
;
271 QTimer m_hoverSequenceTimer
;
273 KItemListSelectionToggle
* m_selectionToggle
;
275 QByteArray m_editedRole
;
279 inline const KItemListWidgetInformant
* KItemListWidget::informant() const