1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
4 * Based on the Itemviews NG project from Trolltech Labs: *
5 * http://qt.gitorious.org/qt-labs/itemviews-ng *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
21 ***************************************************************************/
23 #ifndef KITEMLISTWIDGET_H
24 #define KITEMLISTWIDGET_H
26 #include "dolphin_export.h"
27 #include "kitemviews/kitemliststyleoption.h"
30 #include <QGraphicsWidget>
33 class KItemListSelectionToggle
;
35 class QPropertyAnimation
;
38 * @brief Provides information for creating an instance of KItemListWidget.
40 * KItemListView only creates KItemListWidget instances for the visible
41 * area. For calculating the required size of all items the expected
42 * size for the invisible items must be accessible. KItemListWidgetInformant
43 * provides this information.
45 class DOLPHIN_EXPORT KItemListWidgetInformant
48 KItemListWidgetInformant();
49 virtual ~KItemListWidgetInformant();
51 virtual void calculateItemSizeHints(QVector
<qreal
>& logicalHeightHints
, qreal
& logicalWidthHint
, const KItemListView
* view
) const = 0;
53 virtual qreal
preferredRoleColumnWidth(const QByteArray
& role
,
55 const KItemListView
* view
) const = 0;
59 * @brief Widget that shows a visible item from the model.
61 * For showing an item from a custom model it is required to at least overwrite KItemListWidget::paint().
62 * All properties are set by KItemListView, for each property there is a corresponding
63 * virtual protected method that allows to react on property changes.
65 class DOLPHIN_EXPORT KItemListWidget
: public QGraphicsWidget
70 KItemListWidget(KItemListWidgetInformant
* informant
, QGraphicsItem
* parent
);
71 ~KItemListWidget() override
;
73 void setIndex(int index
);
76 void setData(const QHash
<QByteArray
, QVariant
>& data
, const QSet
<QByteArray
>& roles
= QSet
<QByteArray
>());
77 QHash
<QByteArray
, QVariant
> data() const;
80 * Draws the hover-rectangle if the item is hovered. Overwrite this method
81 * to show the data of the custom model provided by KItemListWidget::data().
84 void paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
= nullptr) override
;
86 void setVisibleRoles(const QList
<QByteArray
>& roles
);
87 QList
<QByteArray
> visibleRoles() const;
90 * Sets the width of a role that should be used if the alignment of the content
91 * should be done in columns.
93 void setColumnWidth(const QByteArray
& role
, qreal width
);
94 qreal
columnWidth(const QByteArray
& role
) const;
96 void setStyleOption(const KItemListStyleOption
& option
);
97 const KItemListStyleOption
& styleOption() const;
99 // TODO: Hides QGraphicsItem::setSelected()/isSelected(). Replace
100 // this by using the default mechanism.
101 void setSelected(bool selected
);
102 bool isSelected() const;
104 void setCurrent(bool current
);
105 bool isCurrent() const;
107 void setHovered(bool hovered
);
108 bool isHovered() const;
110 void setHoverPosition(const QPointF
& pos
);
112 void setAlternateBackground(bool enable
);
113 bool alternateBackground() const;
115 void setEnabledSelectionToggle(bool enabled
);
116 bool enabledSelectionToggle() const;
119 * Sets the sibling information for the item and all of its parents.
120 * The sibling information of the upper most parent is represented by
121 * the first bit, the sibling information of the item by the last bit.
122 * The sibling information is useful for drawing the branches in
125 void setSiblingsInformation(const QBitArray
& siblings
);
126 QBitArray
siblingsInformation() const;
129 * Allows the user to edit the role \a role. The signals
130 * roleEditingCanceled() or roleEditingFinished() will be
131 * emitted after editing. An ongoing editing gets canceled if
132 * the role is empty. Derived classes must implement
133 * editedRoleChanged().
135 void setEditedRole(const QByteArray
& role
);
136 QByteArray
editedRole() const;
139 * @return True if \a point is inside KItemListWidget::hoverRect(),
140 * KItemListWidget::textRect(), KItemListWidget::selectionToggleRect()
141 * or KItemListWidget::expansionToggleRect().
144 bool contains(const QPointF
& point
) const override
;
147 * @return Rectangle for the area that shows the icon.
149 virtual QRectF
iconRect() const = 0;
152 * @return Rectangle for the area that contains the text-properties.
154 virtual QRectF
textRect() const = 0;
157 * @return Focus rectangle for indicating the current item. Per default
158 * textRect() will be returned. Overwrite this method if textRect()
159 * provides a larger rectangle than the actual text (e.g. to
160 * be aligned with the iconRect()). The textFocusRect() may not be
161 * outside the boundaries of textRect().
163 virtual QRectF
textFocusRect() const;
166 * @return Rectangle around which a selection box should be drawn if the item is selected.
168 virtual QRectF
selectionRect() const = 0;
171 * @return Rectangle for the selection-toggle that is used to select or deselect an item.
172 * Per default an empty rectangle is returned which means that no selection-toggle
175 virtual QRectF
selectionToggleRect() const;
178 * @return Rectangle for the expansion-toggle that is used to open a sub-tree of the model.
179 * Per default an empty rectangle is returned which means that no opening of sub-trees
182 virtual QRectF
expansionToggleRect() const;
185 * @return Pixmap that is used when dragging an item. Per default the current state of the
186 * widget is returned as pixmap.
188 virtual QPixmap
createDragPixmap(const QStyleOptionGraphicsItem
* option
, QWidget
* widget
= nullptr);
191 void roleEditingCanceled(int index
, const QByteArray
& role
, const QVariant
& value
);
192 void roleEditingFinished(int index
, const QByteArray
& role
, const QVariant
& value
);
195 virtual void dataChanged(const QHash
<QByteArray
, QVariant
>& current
, const QSet
<QByteArray
>& roles
= QSet
<QByteArray
>());
196 virtual void visibleRolesChanged(const QList
<QByteArray
>& current
, const QList
<QByteArray
>& previous
);
197 virtual void columnWidthChanged(const QByteArray
& role
, qreal current
, qreal previous
);
198 virtual void styleOptionChanged(const KItemListStyleOption
& current
, const KItemListStyleOption
& previous
);
199 virtual void currentChanged(bool current
);
200 virtual void selectedChanged(bool selected
);
201 virtual void hoveredChanged(bool hovered
);
202 virtual void alternateBackgroundChanged(bool enabled
);
203 virtual void siblingsInformationChanged(const QBitArray
& current
, const QBitArray
& previous
);
204 virtual void editedRoleChanged(const QByteArray
& current
, const QByteArray
& previous
);
205 void resizeEvent(QGraphicsSceneResizeEvent
* event
) override
;
208 * @return The current opacity of the hover-animation. When implementing a custom painting-code for a hover-state
209 * this opacity value should be respected.
211 qreal
hoverOpacity() const;
213 const KItemListWidgetInformant
* informant() const;
216 void slotHoverAnimationFinished();
219 void initializeSelectionToggle();
220 void setHoverOpacity(qreal opacity
);
221 void clearHoverCache();
222 void drawItemStyleOption(QPainter
* painter
, QWidget
* widget
, QStyle::State styleState
);
225 Q_PROPERTY(qreal hoverOpacity READ hoverOpacity WRITE setHoverOpacity
)
227 KItemListWidgetInformant
* m_informant
;
232 bool m_alternateBackground
;
233 bool m_enabledSelectionToggle
;
234 QHash
<QByteArray
, QVariant
> m_data
;
235 QList
<QByteArray
> m_visibleRoles
;
236 QHash
<QByteArray
, qreal
> m_columnWidths
;
237 KItemListStyleOption m_styleOption
;
238 QBitArray m_siblingsInfo
;
240 qreal m_hoverOpacity
;
241 mutable QPixmap
* m_hoverCache
;
242 QPropertyAnimation
* m_hoverAnimation
;
244 KItemListSelectionToggle
* m_selectionToggle
;
246 QByteArray m_editedRole
;
249 inline const KItemListWidgetInformant
* KItemListWidget::informant() const