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 <libdolphin_export.h>
28 #include <kitemviews/kitemliststyleoption.h>
30 #include <QGraphicsWidget>
33 class KItemListSelectionToggle
;
34 class QPropertyAnimation
;
37 * @brief Widget that shows a visible item from the model.
39 * For showing an item from a custom model it is required to at least overwrite KItemListWidget::paint().
40 * All properties are set by KItemListView, for each property there is a corresponding
41 * virtual protected method that allows to react on property changes.
43 class LIBDOLPHINPRIVATE_EXPORT KItemListWidget
: public QGraphicsWidget
48 KItemListWidget(QGraphicsItem
* parent
);
49 virtual ~KItemListWidget();
51 void setIndex(int index
);
54 void setData(const QHash
<QByteArray
, QVariant
>& data
, const QSet
<QByteArray
>& roles
= QSet
<QByteArray
>());
55 QHash
<QByteArray
, QVariant
> data() const;
58 * Draws the hover-rectangle if the item is hovered. Overwrite this method
59 * to show the data of the custom model provided by KItemListWidget::data().
62 virtual void paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
= 0);
64 void setVisibleRoles(const QList
<QByteArray
>& roles
);
65 QList
<QByteArray
> visibleRoles() const;
67 void setVisibleRolesSizes(const QHash
<QByteArray
, QSizeF
> rolesSizes
);
68 QHash
<QByteArray
, QSizeF
> visibleRolesSizes() const;
70 void setStyleOption(const KItemListStyleOption
& option
);
71 const KItemListStyleOption
& styleOption() const;
73 // TODO: Hides QGraphicsItem::setSelected()/isSelected(). Replace
74 // this by using the default mechanism.
75 void setSelected(bool selected
);
76 bool isSelected() const;
78 void setCurrent(bool current
);
79 bool isCurrent() const;
81 void setHovered(bool hovered
);
82 bool isHovered() const;
84 void setAlternatingBackgroundColors(bool enable
);
85 bool alternatingBackgroundColors() const;
87 void setEnabledSelectionToggle(bool enabled
);
88 bool enabledSelectionToggle() const;
91 * @return True if \a point is inside KItemListWidget::hoverRect(),
92 * KItemListWidget::textRect(), KItemListWidget::selectionToggleRect()
93 * or KItemListWidget::expansionToggleRect().
96 virtual bool contains(const QPointF
& point
) const;
99 * @return Rectangle for the area that shows the icon.
101 virtual QRectF
iconRect() const = 0;
104 * @return Rectangle for the area that contains the text-properties.
106 virtual QRectF
textRect() const = 0;
109 * @return Rectangle for the selection-toggle that is used to select or deselect an item.
110 * Per default an empty rectangle is returned which means that no selection-toggle
113 virtual QRectF
selectionToggleRect() const;
116 * @return Rectangle for the expansion-toggle that is used to open a sub-tree of the model.
117 * Per default an empty rectangle is returned which means that no opening of sub-trees
120 virtual QRectF
expansionToggleRect() const;
123 virtual void dataChanged(const QHash
<QByteArray
, QVariant
>& current
, const QSet
<QByteArray
>& roles
= QSet
<QByteArray
>());
124 virtual void visibleRolesChanged(const QList
<QByteArray
>& current
, const QList
<QByteArray
>& previous
);
125 virtual void visibleRolesSizesChanged(const QHash
<QByteArray
, QSizeF
>& current
, const QHash
<QByteArray
, QSizeF
>& previous
);
126 virtual void styleOptionChanged(const KItemListStyleOption
& current
, const KItemListStyleOption
& previous
);
127 virtual void currentChanged(bool current
);
128 virtual void selectedChanged(bool selected
);
129 virtual void hoveredChanged(bool hovered
);
130 virtual void alternatingBackgroundColorsChanged(bool enabled
);
131 virtual void resizeEvent(QGraphicsSceneResizeEvent
* event
);
134 * @return The current opacity of the hover-animation. When implementing a custom painting-code for a hover-state
135 * this opacity value should be respected.
137 qreal
hoverOpacity() const;
140 void slotHoverAnimationFinished();
143 void initializeSelectionToggle();
144 void setHoverOpacity(qreal opacity
);
145 void clearHoverCache();
146 void drawItemStyleOption(QPainter
* painter
, QWidget
* widget
, QStyle::State styleState
);
149 Q_PROPERTY(qreal hoverOpacity READ hoverOpacity WRITE setHoverOpacity
)
155 bool m_alternatingBackgroundColors
;
156 bool m_enabledSelectionToggle
;
157 QHash
<QByteArray
, QVariant
> m_data
;
158 QList
<QByteArray
> m_visibleRoles
;
159 QHash
<QByteArray
, QSizeF
> m_visibleRolesSizes
;
160 KItemListStyleOption m_styleOption
;
162 qreal m_hoverOpacity
;
163 mutable QPixmap
* m_hoverCache
;
164 QPropertyAnimation
* m_hoverAnimation
;
166 KItemListSelectionToggle
* m_selectionToggle
;