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>
32 class QPropertyAnimation
;
35 * @brief Widget that shows a visible item from the model.
37 * For showing an item from a custom model it is required to at least overwrite KItemListWidget::paint().
38 * All properties are set by KItemListView, for each property there is a corresponding
39 * virtual protected method that allows to react on property changes.
41 class LIBDOLPHINPRIVATE_EXPORT KItemListWidget
: public QGraphicsWidget
46 KItemListWidget(QGraphicsItem
* parent
);
47 virtual ~KItemListWidget();
49 void setIndex(int index
);
52 void setData(const QHash
<QByteArray
, QVariant
>& data
, const QSet
<QByteArray
>& roles
= QSet
<QByteArray
>());
53 QHash
<QByteArray
, QVariant
> data() const;
56 * Draws the hover-rectangle if the item is hovered. Overwrite this method
57 * to show the data of the custom model provided by KItemListWidget::data().
60 virtual void paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
= 0);
62 void setVisibleRoles(const QList
<QByteArray
>& roles
);
63 QList
<QByteArray
> visibleRoles() const;
65 void setVisibleRolesSizes(const QHash
<QByteArray
, QSizeF
> rolesSizes
);
66 QHash
<QByteArray
, QSizeF
> visibleRolesSizes() const;
68 void setStyleOption(const KItemListStyleOption
& option
);
69 const KItemListStyleOption
& styleOption() const;
71 void setSelected(bool selected
);
72 bool isSelected() const;
74 void setCurrent(bool current
);
75 bool isCurrent() const;
77 void setHovered(bool hovered
);
78 bool isHovered() const;
80 void setAlternatingBackgroundColors(bool enable
);
81 bool alternatingBackgroundColors() const;
84 * @return True if \a point is inside KItemListWidget::hoverRect(),
85 * KItemListWidget::textRect(), KItemListWidget::selectionToggleRect()
86 * or KItemListWidget::expansionToggleRect().
89 virtual bool contains(const QPointF
& point
) const;
92 * @return Rectangle for the area that shows the icon.
94 virtual QRectF
iconRect() const = 0;
97 * @return Rectangle for the area that contains the text-properties.
99 virtual QRectF
textRect() const = 0;
102 * @return Rectangle for the selection-toggle that is used to select or deselect an item.
103 * Per default an empty rectangle is returned which means that no selection-toggle
106 virtual QRectF
selectionToggleRect() const;
109 * @return Rectangle for the expansion-toggle that is used to open a sub-tree of the model.
110 * Per default an empty rectangle is returned which means that no opening of sub-trees
113 virtual QRectF
expansionToggleRect() const;
116 virtual void dataChanged(const QHash
<QByteArray
, QVariant
>& current
, const QSet
<QByteArray
>& roles
= QSet
<QByteArray
>());
117 virtual void visibleRolesChanged(const QList
<QByteArray
>& current
, const QList
<QByteArray
>& previous
);
118 virtual void visibleRolesSizesChanged(const QHash
<QByteArray
, QSizeF
>& current
, const QHash
<QByteArray
, QSizeF
>& previous
);
119 virtual void styleOptionChanged(const KItemListStyleOption
& current
, const KItemListStyleOption
& previous
);
120 virtual void currentChanged(bool current
);
121 virtual void selectedChanged(bool selected
);
122 virtual void hoveredChanged(bool hovered
);
123 virtual void alternatingBackgroundColorsChanged(bool enabled
);
124 virtual void resizeEvent(QGraphicsSceneResizeEvent
* event
);
127 * @return The current opacity of the hover-animation. When implementing a custom painting-code for a hover-state
128 * this opacity value should be respected.
130 qreal
hoverOpacity() const;
133 void setHoverOpacity(qreal opacity
);
134 void clearHoverCache();
135 void drawTextBackground(QPainter
* painter
);
138 Q_PROPERTY(qreal hoverOpacity READ hoverOpacity WRITE setHoverOpacity
)
144 bool m_alternatingBackgroundColors
;
145 QHash
<QByteArray
, QVariant
> m_data
;
146 QList
<QByteArray
> m_visibleRoles
;
147 QHash
<QByteArray
, QSizeF
> m_visibleRolesSizes
;
148 KItemListStyleOption m_styleOption
;
150 qreal m_hoverOpacity
;
151 mutable QPixmap
* m_hoverCache
;
152 QPropertyAnimation
* m_hoverAnimation
;