1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
4 * Based on the Itemviews NG project from Trolltech Labs *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
22 #ifndef KITEMLISTWIDGET_H
23 #define KITEMLISTWIDGET_H
25 #include "dolphin_export.h"
26 #include "kitemviews/kitemliststyleoption.h"
29 #include <QGraphicsWidget>
32 class KItemListSelectionToggle
;
34 class QPropertyAnimation
;
37 * @brief Provides information for creating an instance of KItemListWidget.
39 * KItemListView only creates KItemListWidget instances for the visible
40 * area. For calculating the required size of all items the expected
41 * size for the invisible items must be accessible. KItemListWidgetInformant
42 * provides this information.
44 class DOLPHIN_EXPORT KItemListWidgetInformant
47 KItemListWidgetInformant();
48 virtual ~KItemListWidgetInformant();
50 virtual void calculateItemSizeHints(QVector
<qreal
>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const = 0;
52 virtual qreal
preferredRoleColumnWidth(const QByteArray
& role
,
54 const KItemListView
* view
) const = 0;
58 * @brief Widget that shows a visible item from the model.
60 * For showing an item from a custom model it is required to at least overwrite KItemListWidget::paint().
61 * All properties are set by KItemListView, for each property there is a corresponding
62 * virtual protected method that allows to react on property changes.
64 class DOLPHIN_EXPORT KItemListWidget
: public QGraphicsWidget
69 KItemListWidget(KItemListWidgetInformant
* informant
, QGraphicsItem
* parent
);
70 ~KItemListWidget() override
;
72 void setIndex(int index
);
75 void setData(const QHash
<QByteArray
, QVariant
>& data
, const QSet
<QByteArray
>& roles
= QSet
<QByteArray
>());
76 QHash
<QByteArray
, QVariant
> data() const;
79 * Draws the hover-rectangle if the item is hovered. Overwrite this method
80 * to show the data of the custom model provided by KItemListWidget::data().
83 void paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
= nullptr) override
;
85 void setVisibleRoles(const QList
<QByteArray
>& roles
);
86 QList
<QByteArray
> visibleRoles() const;
89 * Sets the width of a role that should be used if the alignment of the content
90 * should be done in columns.
92 void setColumnWidth(const QByteArray
& role
, qreal width
);
93 qreal
columnWidth(const QByteArray
& role
) const;
95 void setStyleOption(const KItemListStyleOption
& option
);
96 const KItemListStyleOption
& styleOption() const;
98 // TODO: Hides QGraphicsItem::setSelected()/isSelected(). Replace
99 // this by using the default mechanism.
100 void setSelected(bool selected
);
101 bool isSelected() const;
103 void setCurrent(bool current
);
104 bool isCurrent() const;
106 void setHovered(bool hovered
);
107 bool isHovered() const;
109 void setHoverPosition(const QPointF
& pos
);
111 void setAlternateBackground(bool enable
);
112 bool alternateBackground() const;
114 void setEnabledSelectionToggle(bool enabled
);
115 bool enabledSelectionToggle() const;
118 * Sets the sibling information for the item and all of its parents.
119 * The sibling information of the upper most parent is represented by
120 * the first bit, the sibling information of the item by the last bit.
121 * The sibling information is useful for drawing the branches in
124 void setSiblingsInformation(const QBitArray
& siblings
);
125 QBitArray
siblingsInformation() const;
128 * Allows the user to edit the role \a role. The signals
129 * roleEditingCanceled() or roleEditingFinished() will be
130 * emitted after editing. An ongoing editing gets canceled if
131 * the role is empty. Derived classes must implement
132 * editedRoleChanged().
134 void setEditedRole(const QByteArray
& role
);
135 QByteArray
editedRole() const;
138 * @return True if \a point is inside KItemListWidget::hoverRect(),
139 * KItemListWidget::textRect(), KItemListWidget::selectionToggleRect()
140 * or KItemListWidget::expansionToggleRect().
143 bool contains(const QPointF
& point
) const override
;
146 * @return Rectangle for the area that shows the icon.
148 virtual QRectF
iconRect() const = 0;
151 * @return Rectangle for the area that contains the text-properties.
153 virtual QRectF
textRect() const = 0;
156 * @return Focus rectangle for indicating the current item. Per default
157 * textRect() will be returned. Overwrite this method if textRect()
158 * provides a larger rectangle than the actual text (e.g. to
159 * be aligned with the iconRect()). The textFocusRect() may not be
160 * outside the boundaries of textRect().
162 virtual QRectF
textFocusRect() const;
165 * @return Rectangle around which a selection box should be drawn if the item is selected.
167 virtual QRectF
selectionRect() const = 0;
170 * @return Rectangle for the selection-toggle that is used to select or deselect an item.
171 * Per default an empty rectangle is returned which means that no selection-toggle
174 virtual QRectF
selectionToggleRect() const;
177 * @return Rectangle for the expansion-toggle that is used to open a sub-tree of the model.
178 * Per default an empty rectangle is returned which means that no opening of sub-trees
181 virtual QRectF
expansionToggleRect() const;
184 * @return Pixmap that is used when dragging an item. Per default the current state of the
185 * widget is returned as pixmap.
187 virtual QPixmap
createDragPixmap(const QStyleOptionGraphicsItem
* option
, QWidget
* widget
= nullptr);
190 void roleEditingCanceled(int index
, const QByteArray
& role
, const QVariant
& value
);
191 void roleEditingFinished(int index
, const QByteArray
& role
, const QVariant
& value
);
194 virtual void dataChanged(const QHash
<QByteArray
, QVariant
>& current
, const QSet
<QByteArray
>& roles
= QSet
<QByteArray
>());
195 virtual void visibleRolesChanged(const QList
<QByteArray
>& current
, const QList
<QByteArray
>& previous
);
196 virtual void columnWidthChanged(const QByteArray
& role
, qreal current
, qreal previous
);
197 virtual void styleOptionChanged(const KItemListStyleOption
& current
, const KItemListStyleOption
& previous
);
198 virtual void currentChanged(bool current
);
199 virtual void selectedChanged(bool selected
);
200 virtual void hoveredChanged(bool hovered
);
201 virtual void alternateBackgroundChanged(bool enabled
);
202 virtual void siblingsInformationChanged(const QBitArray
& current
, const QBitArray
& previous
);
203 virtual void editedRoleChanged(const QByteArray
& current
, const QByteArray
& previous
);
204 void resizeEvent(QGraphicsSceneResizeEvent
* event
) override
;
207 * @return The current opacity of the hover-animation. When implementing a custom painting-code for a hover-state
208 * this opacity value should be respected.
210 qreal
hoverOpacity() const;
212 const KItemListWidgetInformant
* informant() const;
215 void slotHoverAnimationFinished();
218 void initializeSelectionToggle();
219 void setHoverOpacity(qreal opacity
);
220 void clearHoverCache();
221 void drawItemStyleOption(QPainter
* painter
, QWidget
* widget
, QStyle::State styleState
);
224 Q_PROPERTY(qreal hoverOpacity READ hoverOpacity WRITE setHoverOpacity
)
226 KItemListWidgetInformant
* m_informant
;
231 bool m_alternateBackground
;
232 bool m_enabledSelectionToggle
;
233 QHash
<QByteArray
, QVariant
> m_data
;
234 QList
<QByteArray
> m_visibleRoles
;
235 QHash
<QByteArray
, qreal
> m_columnWidths
;
236 KItemListStyleOption m_styleOption
;
237 QBitArray m_siblingsInfo
;
239 qreal m_hoverOpacity
;
240 mutable QPixmap
* m_hoverCache
;
241 QPropertyAnimation
* m_hoverAnimation
;
243 KItemListSelectionToggle
* m_selectionToggle
;
245 QByteArray m_editedRole
;
248 inline const KItemListWidgetInformant
* KItemListWidget::informant() const