]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistwidget.h
Details view: Fix indicator-branches
[dolphin.git] / src / kitemviews / kitemlistwidget.h
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * Based on the Itemviews NG project from Trolltech Labs: *
5 * http://qt.gitorious.org/qt-labs/itemviews-ng *
6 * *
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. *
11 * *
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. *
16 * *
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 ***************************************************************************/
22
23 #ifndef KITEMLISTWIDGET_H
24 #define KITEMLISTWIDGET_H
25
26 #include <libdolphin_export.h>
27
28 #include <kitemviews/kitemliststyleoption.h>
29
30 #include <QBitArray>
31 #include <QGraphicsWidget>
32 #include <QStyle>
33
34 class KItemListSelectionToggle;
35 class QPropertyAnimation;
36
37 /**
38 * @brief Widget that shows a visible item from the model.
39 *
40 * For showing an item from a custom model it is required to at least overwrite KItemListWidget::paint().
41 * All properties are set by KItemListView, for each property there is a corresponding
42 * virtual protected method that allows to react on property changes.
43 */
44 class LIBDOLPHINPRIVATE_EXPORT KItemListWidget : public QGraphicsWidget
45 {
46 Q_OBJECT
47
48 public:
49 KItemListWidget(QGraphicsItem* parent);
50 virtual ~KItemListWidget();
51
52 void setIndex(int index);
53 int index() const;
54
55 void setData(const QHash<QByteArray, QVariant>& data, const QSet<QByteArray>& roles = QSet<QByteArray>());
56 QHash<QByteArray, QVariant> data() const;
57
58 /**
59 * Draws the hover-rectangle if the item is hovered. Overwrite this method
60 * to show the data of the custom model provided by KItemListWidget::data().
61 * @reimp
62 */
63 virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0);
64
65 void setVisibleRoles(const QList<QByteArray>& roles);
66 QList<QByteArray> visibleRoles() const;
67
68 void setVisibleRolesSizes(const QHash<QByteArray, QSizeF> rolesSizes);
69 QHash<QByteArray, QSizeF> visibleRolesSizes() const;
70
71 void setStyleOption(const KItemListStyleOption& option);
72 const KItemListStyleOption& styleOption() const;
73
74 // TODO: Hides QGraphicsItem::setSelected()/isSelected(). Replace
75 // this by using the default mechanism.
76 void setSelected(bool selected);
77 bool isSelected() const;
78
79 void setCurrent(bool current);
80 bool isCurrent() const;
81
82 void setHovered(bool hovered);
83 bool isHovered() const;
84
85 void setAlternatingBackgroundColors(bool enable);
86 bool alternatingBackgroundColors() const;
87
88 void setEnabledSelectionToggle(bool enabled);
89 bool enabledSelectionToggle() const;
90
91 /**
92 * Sets the sibling information for the item and all of its parents.
93 * The sibling information of the upper most parent is represented by
94 * the first bit, the sibling information of the item by the last bit.
95 * The sibling information is useful for drawing the branches in
96 * tree views.
97 */
98 void setSiblingsInformation(const QBitArray& siblings);
99 QBitArray siblingsInformation() const;
100
101 /**
102 * @return True if \a point is inside KItemListWidget::hoverRect(),
103 * KItemListWidget::textRect(), KItemListWidget::selectionToggleRect()
104 * or KItemListWidget::expansionToggleRect().
105 * @reimp
106 */
107 virtual bool contains(const QPointF& point) const;
108
109 /**
110 * @return Rectangle for the area that shows the icon.
111 */
112 virtual QRectF iconRect() const = 0;
113
114 /**
115 * @return Rectangle for the area that contains the text-properties.
116 */
117 virtual QRectF textRect() const = 0;
118
119 /**
120 * @return Rectangle for the selection-toggle that is used to select or deselect an item.
121 * Per default an empty rectangle is returned which means that no selection-toggle
122 * is available.
123 */
124 virtual QRectF selectionToggleRect() const;
125
126 /**
127 * @return Rectangle for the expansion-toggle that is used to open a sub-tree of the model.
128 * Per default an empty rectangle is returned which means that no opening of sub-trees
129 * is supported.
130 */
131 virtual QRectF expansionToggleRect() const;
132
133 protected:
134 virtual void dataChanged(const QHash<QByteArray, QVariant>& current, const QSet<QByteArray>& roles = QSet<QByteArray>());
135 virtual void visibleRolesChanged(const QList<QByteArray>& current, const QList<QByteArray>& previous);
136 virtual void visibleRolesSizesChanged(const QHash<QByteArray, QSizeF>& current, const QHash<QByteArray, QSizeF>& previous);
137 virtual void styleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous);
138 virtual void currentChanged(bool current);
139 virtual void selectedChanged(bool selected);
140 virtual void hoveredChanged(bool hovered);
141 virtual void alternatingBackgroundColorsChanged(bool enabled);
142 virtual void siblingsInformationChanged(const QBitArray& current, const QBitArray& previous);
143 virtual void resizeEvent(QGraphicsSceneResizeEvent* event);
144
145 /**
146 * @return The current opacity of the hover-animation. When implementing a custom painting-code for a hover-state
147 * this opacity value should be respected.
148 */
149 qreal hoverOpacity() const;
150
151 private slots:
152 void slotHoverAnimationFinished();
153
154 private:
155 void initializeSelectionToggle();
156 void setHoverOpacity(qreal opacity);
157 void clearHoverCache();
158 void drawItemStyleOption(QPainter* painter, QWidget* widget, QStyle::State styleState);
159
160 private:
161 Q_PROPERTY(qreal hoverOpacity READ hoverOpacity WRITE setHoverOpacity)
162
163 int m_index;
164 bool m_selected;
165 bool m_current;
166 bool m_hovered;
167 bool m_alternatingBackgroundColors;
168 bool m_enabledSelectionToggle;
169 QHash<QByteArray, QVariant> m_data;
170 QList<QByteArray> m_visibleRoles;
171 QHash<QByteArray, QSizeF> m_visibleRolesSizes;
172 KItemListStyleOption m_styleOption;
173 QBitArray m_siblingsInfo;
174
175 qreal m_hoverOpacity;
176 mutable QPixmap* m_hoverCache;
177 QPropertyAnimation* m_hoverAnimation;
178
179 KItemListSelectionToggle* m_selectionToggle;
180 };
181 #endif
182
183