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>
19 class KItemListSelectionToggle
;
21 class QPropertyAnimation
;
24 * @brief Provides information for creating an instance of KItemListWidget.
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.
31 class DOLPHIN_EXPORT KItemListWidgetInformant
34 KItemListWidgetInformant();
35 virtual ~KItemListWidgetInformant();
37 virtual void calculateItemSizeHints(QVector
<qreal
>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const = 0;
39 virtual qreal
preferredRoleColumnWidth(const QByteArray
& role
,
41 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;
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().
70 void paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
= nullptr) override
;
72 void setVisibleRoles(const QList
<QByteArray
>& roles
);
73 QList
<QByteArray
> visibleRoles() const;
76 * Sets the width of a role that should be used if the alignment of the content
77 * should be done in columns.
79 void setColumnWidth(const QByteArray
& role
, qreal width
);
80 qreal
columnWidth(const QByteArray
& role
) const;
82 void setStyleOption(const KItemListStyleOption
& option
);
83 const KItemListStyleOption
& styleOption() const;
85 // TODO: Hides QGraphicsItem::setSelected()/isSelected(). Replace
86 // this by using the default mechanism.
87 void setSelected(bool selected
);
88 bool isSelected() const;
90 void setCurrent(bool current
);
91 bool isCurrent() const;
93 void setHovered(bool hovered
);
94 bool isHovered() const;
96 void setHoverPosition(const QPointF
& pos
);
98 void setAlternateBackground(bool enable
);
99 bool alternateBackground() const;
101 void setEnabledSelectionToggle(bool enabled
);
102 bool enabledSelectionToggle() const;
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
111 void setSiblingsInformation(const QBitArray
& siblings
);
112 QBitArray
siblingsInformation() const;
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().
121 void setEditedRole(const QByteArray
& role
);
122 QByteArray
editedRole() const;
125 * @return True if \a point is inside KItemListWidget::hoverRect(),
126 * KItemListWidget::textRect(), KItemListWidget::selectionToggleRect()
127 * or KItemListWidget::expansionToggleRect().
130 bool contains(const QPointF
& point
) const override
;
133 * @return Rectangle for the area that shows the icon.
135 virtual QRectF
iconRect() const = 0;
138 * @return Rectangle for the area that contains the text-properties.
140 virtual QRectF
textRect() const = 0;
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().
149 virtual QRectF
textFocusRect() const;
152 * @return Rectangle around which a selection box should be drawn if the item is selected.
154 virtual QRectF
selectionRect() const = 0;
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
161 virtual QRectF
selectionToggleRect() const;
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
168 virtual QRectF
expansionToggleRect() const;
171 * @return Pixmap that is used when dragging an item. Per default the current state of the
172 * widget is returned as pixmap.
174 virtual QPixmap
createDragPixmap(const QStyleOptionGraphicsItem
* option
, QWidget
* widget
= nullptr);
177 void roleEditingCanceled(int index
, const QByteArray
& role
, const QVariant
& value
);
178 void roleEditingFinished(int index
, const QByteArray
& role
, const QVariant
& value
);
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
;
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.
197 qreal
hoverOpacity() const;
199 const KItemListWidgetInformant
* informant() const;
202 void slotHoverAnimationFinished();
205 void initializeSelectionToggle();
206 void setHoverOpacity(qreal opacity
);
207 void clearHoverCache();
208 void drawItemStyleOption(QPainter
* painter
, QWidget
* widget
, QStyle::State styleState
);
211 Q_PROPERTY(qreal hoverOpacity READ hoverOpacity WRITE setHoverOpacity
)
213 KItemListWidgetInformant
* m_informant
;
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
;
226 qreal m_hoverOpacity
;
227 mutable QPixmap
* m_hoverCache
;
228 QPropertyAnimation
* m_hoverAnimation
;
230 KItemListSelectionToggle
* m_selectionToggle
;
232 QByteArray m_editedRole
;
235 inline const KItemListWidgetInformant
* KItemListWidget::informant() const