]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistwidget.h
8a28913b663e6a0345d63a78cafa1add07ee2b78
[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 /**
69 * Sets the width of a role that should be used if the alignment of the content
70 * should be done in columns.
71 */
72 void setColumnWidth(const QByteArray& role, qreal width);
73 qreal columnWidth(const QByteArray& role) const;
74
75 void setStyleOption(const KItemListStyleOption& option);
76 const KItemListStyleOption& styleOption() const;
77
78 // TODO: Hides QGraphicsItem::setSelected()/isSelected(). Replace
79 // this by using the default mechanism.
80 void setSelected(bool selected);
81 bool isSelected() const;
82
83 void setCurrent(bool current);
84 bool isCurrent() const;
85
86 void setHovered(bool hovered);
87 bool isHovered() const;
88
89 void setAlternateBackground(bool enable);
90 bool alternateBackground() const;
91
92 void setEnabledSelectionToggle(bool enabled);
93 bool enabledSelectionToggle() const;
94
95 /**
96 * Sets the sibling information for the item and all of its parents.
97 * The sibling information of the upper most parent is represented by
98 * the first bit, the sibling information of the item by the last bit.
99 * The sibling information is useful for drawing the branches in
100 * tree views.
101 */
102 void setSiblingsInformation(const QBitArray& siblings);
103 QBitArray siblingsInformation() const;
104
105 /**
106 * Allows the user to edit the role \a role. The signals
107 * roleEditingCanceled() or roleEditingFinished() will be
108 * emitted after editing. An ongoing editing gets canceled if
109 * the role is empty. Derived classes must implement
110 * editedRoleChanged().
111 */
112 void setEditedRole(const QByteArray& role);
113 QByteArray editedRole() const;
114
115 /**
116 * @return True if \a point is inside KItemListWidget::hoverRect(),
117 * KItemListWidget::textRect(), KItemListWidget::selectionToggleRect()
118 * or KItemListWidget::expansionToggleRect().
119 * @reimp
120 */
121 virtual bool contains(const QPointF& point) const;
122
123 /**
124 * @return Rectangle for the area that shows the icon.
125 */
126 virtual QRectF iconRect() const = 0;
127
128 /**
129 * @return Rectangle for the area that contains the text-properties.
130 */
131 virtual QRectF textRect() const = 0;
132
133 /**
134 * @return Focus rectangle for indicating the current item. Per default
135 * textRect() will be returned. Overwrite this method if textRect()
136 * provides a larger rectangle than the actual text (e.g. to
137 * be aligned with the iconRect()). The textFocusRect() may not be
138 * outside the boundaries of textRect().
139 */
140 virtual QRectF textFocusRect() const;
141
142 /**
143 * @return Rectangle for the selection-toggle that is used to select or deselect an item.
144 * Per default an empty rectangle is returned which means that no selection-toggle
145 * is available.
146 */
147 virtual QRectF selectionToggleRect() const;
148
149 /**
150 * @return Rectangle for the expansion-toggle that is used to open a sub-tree of the model.
151 * Per default an empty rectangle is returned which means that no opening of sub-trees
152 * is supported.
153 */
154 virtual QRectF expansionToggleRect() const;
155
156 signals:
157 void roleEditingCanceled(int index, const QByteArray& role, const QVariant& value);
158 void roleEditingFinished(int index, const QByteArray& role, const QVariant& value);
159
160 protected:
161 virtual void dataChanged(const QHash<QByteArray, QVariant>& current, const QSet<QByteArray>& roles = QSet<QByteArray>());
162 virtual void visibleRolesChanged(const QList<QByteArray>& current, const QList<QByteArray>& previous);
163 virtual void columnWidthChanged(const QByteArray& role, qreal current, qreal previous);
164 virtual void styleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous);
165 virtual void currentChanged(bool current);
166 virtual void selectedChanged(bool selected);
167 virtual void hoveredChanged(bool hovered);
168 virtual void alternateBackgroundChanged(bool enabled);
169 virtual void siblingsInformationChanged(const QBitArray& current, const QBitArray& previous);
170 virtual void editedRoleChanged(const QByteArray& current, const QByteArray& previous);
171 virtual void resizeEvent(QGraphicsSceneResizeEvent* event);
172
173 /**
174 * @return The current opacity of the hover-animation. When implementing a custom painting-code for a hover-state
175 * this opacity value should be respected.
176 */
177 qreal hoverOpacity() const;
178
179 private slots:
180 void slotHoverAnimationFinished();
181
182 private:
183 void initializeSelectionToggle();
184 void setHoverOpacity(qreal opacity);
185 void clearHoverCache();
186 void drawItemStyleOption(QPainter* painter, QWidget* widget, QStyle::State styleState);
187
188 private:
189 Q_PROPERTY(qreal hoverOpacity READ hoverOpacity WRITE setHoverOpacity)
190
191 int m_index;
192 bool m_selected;
193 bool m_current;
194 bool m_hovered;
195 bool m_alternateBackground;
196 bool m_enabledSelectionToggle;
197 QHash<QByteArray, QVariant> m_data;
198 QList<QByteArray> m_visibleRoles;
199 QHash<QByteArray, qreal> m_columnWidths;
200 KItemListStyleOption m_styleOption;
201 QBitArray m_siblingsInfo;
202
203 qreal m_hoverOpacity;
204 mutable QPixmap* m_hoverCache;
205 QPropertyAnimation* m_hoverAnimation;
206
207 KItemListSelectionToggle* m_selectionToggle;
208
209 QByteArray m_editedRole;
210 };
211 #endif
212
213