]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistwidget.cpp
Inform the selection manager about model changes.
[dolphin.git] / src / kitemviews / kitemlistwidget.cpp
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 #include "kitemlistwidget.h"
24
25 #include "kitemlistview.h"
26 #include "kitemmodelbase.h"
27
28 #include <KDebug>
29
30 #include <QPainter>
31 #include <QPropertyAnimation>
32 #include <QStyle>
33
34 KItemListWidget::KItemListWidget(QGraphicsItem* parent) :
35 QGraphicsWidget(parent, 0),
36 m_index(-1),
37 m_data(),
38 m_visibleRoles(),
39 m_visibleRolesSizes(),
40 m_styleOption(),
41 m_hoverOpacity(0),
42 m_hoverCache(0),
43 m_hoverAnimation(0)
44 {
45 }
46
47 KItemListWidget::~KItemListWidget()
48 {
49 clearCache();
50 }
51
52 void KItemListWidget::setIndex(int index)
53 {
54 if (m_index != index) {
55 if (m_hoverAnimation) {
56 m_hoverAnimation->stop();
57 m_hoverOpacity = 0;
58 }
59 clearCache();
60
61 m_index = index;
62 }
63 }
64
65 int KItemListWidget::index() const
66 {
67 return m_index;
68 }
69
70 void KItemListWidget::setData(const QHash<QByteArray, QVariant>& data,
71 const QSet<QByteArray>& roles)
72 {
73 clearCache();
74 if (roles.isEmpty()) {
75 m_data = data;
76 dataChanged(m_data);
77 } else {
78 foreach (const QByteArray& role, roles) {
79 m_data[role] = data[role];
80 }
81 dataChanged(m_data, roles);
82 }
83 }
84
85 QHash<QByteArray, QVariant> KItemListWidget::data() const
86 {
87 return m_data;
88 }
89
90 void KItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
91 {
92 Q_UNUSED(option);
93 if (m_hoverOpacity <= 0.0) {
94 return;
95 }
96
97 const QRect hoverBounds = hoverBoundingRect().toRect();
98 if (!m_hoverCache) {
99 m_hoverCache = new QPixmap(hoverBounds.size());
100 m_hoverCache->fill(Qt::transparent);
101
102 QPainter pixmapPainter(m_hoverCache);
103
104 QStyleOptionViewItemV4 viewItemOption;
105 viewItemOption.initFrom(widget);
106 viewItemOption.rect = QRect(0, 0, hoverBounds.width(), hoverBounds.height());
107 viewItemOption.state = QStyle::State_Enabled | QStyle::State_MouseOver;
108 viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
109
110 widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, &pixmapPainter, widget);
111 }
112
113 const qreal opacity = painter->opacity();
114 painter->setOpacity(m_hoverOpacity * opacity);
115 painter->drawPixmap(hoverBounds.topLeft(), *m_hoverCache);
116 painter->setOpacity(opacity);
117 }
118
119 void KItemListWidget::setVisibleRoles(const QHash<QByteArray, int>& roles)
120 {
121 const QHash<QByteArray, int> previousRoles = m_visibleRoles;
122 m_visibleRoles = roles;
123 visibleRolesChanged(roles, previousRoles);
124 }
125
126 QHash<QByteArray, int> KItemListWidget::visibleRoles() const
127 {
128 return m_visibleRoles;
129 }
130
131 void KItemListWidget::setVisibleRolesSizes(const QHash<QByteArray, QSizeF> rolesSizes)
132 {
133 const QHash<QByteArray, QSizeF> previousRolesSizes = m_visibleRolesSizes;
134 m_visibleRolesSizes = rolesSizes;
135 visibleRolesSizesChanged(rolesSizes, previousRolesSizes);
136 }
137
138 QHash<QByteArray, QSizeF> KItemListWidget::visibleRolesSizes() const
139 {
140 return m_visibleRolesSizes;
141 }
142
143 void KItemListWidget::setStyleOption(const KItemListStyleOption& option)
144 {
145 const KItemListStyleOption previous = m_styleOption;
146 if (m_index >= 0) {
147 clearCache();
148
149 const bool wasHovered = (previous.state & QStyle::State_MouseOver);
150 m_styleOption = option;
151 const bool isHovered = (m_styleOption.state & QStyle::State_MouseOver);
152
153 if (wasHovered != isHovered) {
154 // The hovering state has been changed. Assure that a fade-animation
155 // is done to the new state.
156 if (!m_hoverAnimation) {
157 m_hoverAnimation = new QPropertyAnimation(this, "hoverOpacity", this);
158 m_hoverAnimation->setDuration(200);
159 }
160 m_hoverAnimation->stop();
161
162 if (!wasHovered && isHovered) {
163 m_hoverAnimation->setEndValue(1.0);
164 } else {
165 Q_ASSERT(wasHovered && !isHovered);
166 m_hoverAnimation->setEndValue(0.0);
167 }
168
169 m_hoverAnimation->start();
170 }
171 } else {
172 m_styleOption = option;
173 }
174
175 styleOptionChanged(option, previous);
176 }
177
178 const KItemListStyleOption& KItemListWidget::styleOption() const
179 {
180 return m_styleOption;
181 }
182
183 bool KItemListWidget::contains(const QPointF& point) const
184 {
185 return hoverBoundingRect().contains(point) ||
186 expansionToggleRect().contains(point) ||
187 selectionToggleRect().contains(point);
188 }
189
190 QRectF KItemListWidget::hoverBoundingRect() const
191 {
192 return QRectF(QPointF(0, 0), size());
193 }
194
195 QRectF KItemListWidget::selectionToggleRect() const
196 {
197 return QRectF();
198 }
199
200 QRectF KItemListWidget::expansionToggleRect() const
201 {
202 return QRectF();
203 }
204
205 void KItemListWidget::dataChanged(const QHash<QByteArray, QVariant>& current,
206 const QSet<QByteArray>& roles)
207 {
208 Q_UNUSED(current);
209 Q_UNUSED(roles);
210 update();
211 }
212
213 void KItemListWidget::visibleRolesChanged(const QHash<QByteArray, int>& current,
214 const QHash<QByteArray, int>& previous)
215 {
216 Q_UNUSED(current);
217 Q_UNUSED(previous);
218 update();
219 }
220
221 void KItemListWidget::visibleRolesSizesChanged(const QHash<QByteArray, QSizeF>& current,
222 const QHash<QByteArray, QSizeF>& previous)
223 {
224 Q_UNUSED(current);
225 Q_UNUSED(previous);
226 update();
227 }
228
229 void KItemListWidget::styleOptionChanged(const KItemListStyleOption& current,
230 const KItemListStyleOption& previous)
231 {
232 Q_UNUSED(current);
233 Q_UNUSED(previous);
234 update();
235 }
236
237 void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent* event)
238 {
239 QGraphicsWidget::resizeEvent(event);
240 clearCache();
241 }
242
243 qreal KItemListWidget::hoverOpacity() const
244 {
245 return m_hoverOpacity;
246 }
247
248 void KItemListWidget::setHoverOpacity(qreal opacity)
249 {
250 m_hoverOpacity = opacity;
251 update();
252 }
253
254 void KItemListWidget::clearCache()
255 {
256 delete m_hoverCache;
257 m_hoverCache = 0;
258 }
259
260 #include "kitemlistwidget.moc"