]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistwidget.h
Improve group-header layout
[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 <QGraphicsWidget>
31
32 class QPropertyAnimation;
33
34 /**
35 * @brief Widget that shows a visible item from the model.
36 *
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.
40 */
41 class LIBDOLPHINPRIVATE_EXPORT KItemListWidget : public QGraphicsWidget
42 {
43 Q_OBJECT
44
45 public:
46 KItemListWidget(QGraphicsItem* parent);
47 virtual ~KItemListWidget();
48
49 void setIndex(int index);
50 int index() const;
51
52 void setData(const QHash<QByteArray, QVariant>& data, const QSet<QByteArray>& roles = QSet<QByteArray>());
53 QHash<QByteArray, QVariant> data() const;
54
55 /**
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().
58 * @reimp
59 */
60 virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0);
61
62 void setVisibleRoles(const QList<QByteArray>& roles);
63 QList<QByteArray> visibleRoles() const;
64
65 void setVisibleRolesSizes(const QHash<QByteArray, QSizeF> rolesSizes);
66 QHash<QByteArray, QSizeF> visibleRolesSizes() const;
67
68 void setStyleOption(const KItemListStyleOption& option);
69 const KItemListStyleOption& styleOption() const;
70
71 void setSelected(bool selected);
72 bool isSelected() const;
73
74 void setCurrent(bool current);
75 bool isCurrent() const;
76
77 void setHovered(bool hovered);
78 bool isHovered() const;
79
80 void setAlternatingBackgroundColors(bool enable);
81 bool alternatingBackgroundColors() const;
82
83 /**
84 * @return True if \a point is inside KItemListWidget::hoverRect(),
85 * KItemListWidget::textRect(), KItemListWidget::selectionToggleRect()
86 * or KItemListWidget::expansionToggleRect().
87 * @reimp
88 */
89 virtual bool contains(const QPointF& point) const;
90
91 /**
92 * @return Rectangle for the area that shows the icon.
93 */
94 virtual QRectF iconRect() const = 0;
95
96 /**
97 * @return Rectangle for the area that contains the text-properties.
98 */
99 virtual QRectF textRect() const = 0;
100
101 /**
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
104 * is available.
105 */
106 virtual QRectF selectionToggleRect() const;
107
108 /**
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
111 * is supported.
112 */
113 virtual QRectF expansionToggleRect() const;
114
115 protected:
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);
125
126 /**
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.
129 */
130 qreal hoverOpacity() const;
131
132 private:
133 void setHoverOpacity(qreal opacity);
134 void clearHoverCache();
135 void drawTextBackground(QPainter* painter);
136
137 private:
138 Q_PROPERTY(qreal hoverOpacity READ hoverOpacity WRITE setHoverOpacity)
139
140 int m_index;
141 bool m_selected;
142 bool m_current;
143 bool m_hovered;
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;
149
150 qreal m_hoverOpacity;
151 mutable QPixmap* m_hoverCache;
152 QPropertyAnimation* m_hoverAnimation;
153 };
154 #endif
155
156