]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistwidget.cpp
Rename KItemListWidget::hoverBoundingRect()
[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 #include <QStyleOption>
34
35 KItemListWidget::KItemListWidget(QGraphicsItem* parent) :
36 QGraphicsWidget(parent, 0),
37 m_index(-1),
38 m_selected(false),
39 m_current(false),
40 m_hovered(false),
41 m_data(),
42 m_visibleRoles(),
43 m_visibleRolesSizes(),
44 m_styleOption(),
45 m_hoverOpacity(0),
46 m_hoverCache(0),
47 m_hoverAnimation(0)
48 {
49 }
50
51 KItemListWidget::~KItemListWidget()
52 {
53 clearCache();
54 }
55
56 void KItemListWidget::setIndex(int index)
57 {
58 if (m_index != index) {
59 if (m_hoverAnimation) {
60 m_hoverAnimation->stop();
61 m_hoverOpacity = 0;
62 }
63 clearCache();
64
65 m_index = index;
66 }
67 }
68
69 int KItemListWidget::index() const
70 {
71 return m_index;
72 }
73
74 void KItemListWidget::setData(const QHash<QByteArray, QVariant>& data,
75 const QSet<QByteArray>& roles)
76 {
77 clearCache();
78 if (roles.isEmpty()) {
79 m_data = data;
80 dataChanged(m_data);
81 } else {
82 foreach (const QByteArray& role, roles) {
83 m_data[role] = data[role];
84 }
85 dataChanged(m_data, roles);
86 }
87 }
88
89 QHash<QByteArray, QVariant> KItemListWidget::data() const
90 {
91 return m_data;
92 }
93
94 void KItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
95 {
96 Q_UNUSED(option);
97
98 const QRect iconBounds = iconBoundingRect().toRect();
99 if (m_selected) {
100 QStyleOptionViewItemV4 viewItemOption;
101 viewItemOption.initFrom(widget);
102 viewItemOption.rect = iconBounds;
103 viewItemOption.state = QStyle::State_Enabled | QStyle::State_Selected | QStyle::State_Item;
104 viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
105 widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget);
106 }
107
108 if (m_hoverOpacity <= 0.0) {
109 return;
110 }
111
112 if (!m_hoverCache) {
113 m_hoverCache = new QPixmap(iconBounds.size());
114 m_hoverCache->fill(Qt::transparent);
115
116 QPainter pixmapPainter(m_hoverCache);
117
118 QStyleOptionViewItemV4 viewItemOption;
119 viewItemOption.initFrom(widget);
120 viewItemOption.rect = QRect(0, 0, iconBounds.width(), iconBounds.height());
121 viewItemOption.state = QStyle::State_Enabled | QStyle::State_MouseOver | QStyle::State_Item;
122 viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
123
124 widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, &pixmapPainter, widget);
125 }
126
127 const qreal opacity = painter->opacity();
128 painter->setOpacity(m_hoverOpacity * opacity);
129 painter->drawPixmap(iconBounds.topLeft(), *m_hoverCache);
130 painter->setOpacity(opacity);
131 }
132
133 void KItemListWidget::setVisibleRoles(const QHash<QByteArray, int>& roles)
134 {
135 const QHash<QByteArray, int> previousRoles = m_visibleRoles;
136 m_visibleRoles = roles;
137 visibleRolesChanged(roles, previousRoles);
138 }
139
140 QHash<QByteArray, int> KItemListWidget::visibleRoles() const
141 {
142 return m_visibleRoles;
143 }
144
145 void KItemListWidget::setVisibleRolesSizes(const QHash<QByteArray, QSizeF> rolesSizes)
146 {
147 const QHash<QByteArray, QSizeF> previousRolesSizes = m_visibleRolesSizes;
148 m_visibleRolesSizes = rolesSizes;
149 visibleRolesSizesChanged(rolesSizes, previousRolesSizes);
150 }
151
152 QHash<QByteArray, QSizeF> KItemListWidget::visibleRolesSizes() const
153 {
154 return m_visibleRolesSizes;
155 }
156
157 void KItemListWidget::setStyleOption(const KItemListStyleOption& option)
158 {
159 const KItemListStyleOption previous = m_styleOption;
160 clearCache();
161 m_styleOption = option;
162
163 styleOptionChanged(option, previous);
164 }
165
166 const KItemListStyleOption& KItemListWidget::styleOption() const
167 {
168 return m_styleOption;
169 }
170
171 void KItemListWidget::setSelected(bool selected)
172 {
173 if (m_selected != selected) {
174 m_selected = selected;
175 selectedChanged(selected);
176 update();
177 }
178 }
179
180 bool KItemListWidget::isSelected() const
181 {
182 return m_selected;
183 }
184
185 void KItemListWidget::setCurrent(bool current)
186 {
187 if (m_current != current) {
188 m_current = current;
189 currentChanged(current);
190 update();
191 }
192 }
193
194 bool KItemListWidget::isCurrent() const
195 {
196 return m_current;
197 }
198
199 void KItemListWidget::setHovered(bool hovered)
200 {
201 if (hovered == m_hovered) {
202 return;
203 }
204
205 m_hovered = hovered;
206
207 if (!m_hoverAnimation) {
208 m_hoverAnimation = new QPropertyAnimation(this, "hoverOpacity", this);
209 m_hoverAnimation->setDuration(200);
210 }
211 m_hoverAnimation->stop();
212
213 if (hovered) {
214 m_hoverAnimation->setEndValue(1.0);
215 } else {
216 m_hoverAnimation->setEndValue(0.0);
217 }
218
219 m_hoverAnimation->start();
220
221 hoveredChanged(hovered);
222
223 update();
224 }
225
226 bool KItemListWidget::isHovered() const
227 {
228 return m_hovered;
229 }
230
231 bool KItemListWidget::contains(const QPointF& point) const
232 {
233 if (!QGraphicsWidget::contains(point)) {
234 return false;
235 }
236
237 return iconBoundingRect().contains(point) ||
238 textBoundingRect().contains(point) ||
239 expansionToggleRect().contains(point) ||
240 selectionToggleRect().contains(point);
241 }
242
243 QRectF KItemListWidget::selectionToggleRect() const
244 {
245 return QRectF();
246 }
247
248 QRectF KItemListWidget::expansionToggleRect() const
249 {
250 return QRectF();
251 }
252
253 void KItemListWidget::dataChanged(const QHash<QByteArray, QVariant>& current,
254 const QSet<QByteArray>& roles)
255 {
256 Q_UNUSED(current);
257 Q_UNUSED(roles);
258 update();
259 }
260
261 void KItemListWidget::visibleRolesChanged(const QHash<QByteArray, int>& current,
262 const QHash<QByteArray, int>& previous)
263 {
264 Q_UNUSED(current);
265 Q_UNUSED(previous);
266 update();
267 }
268
269 void KItemListWidget::visibleRolesSizesChanged(const QHash<QByteArray, QSizeF>& current,
270 const QHash<QByteArray, QSizeF>& previous)
271 {
272 Q_UNUSED(current);
273 Q_UNUSED(previous);
274 update();
275 }
276
277 void KItemListWidget::styleOptionChanged(const KItemListStyleOption& current,
278 const KItemListStyleOption& previous)
279 {
280 Q_UNUSED(current);
281 Q_UNUSED(previous);
282 update();
283 }
284
285 void KItemListWidget::currentChanged(bool current)
286 {
287 Q_UNUSED(current);
288 }
289
290 void KItemListWidget::selectedChanged(bool selected)
291 {
292 Q_UNUSED(selected);
293 }
294
295 void KItemListWidget::hoveredChanged(bool hovered)
296 {
297 Q_UNUSED(hovered);
298 }
299
300 void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent* event)
301 {
302 QGraphicsWidget::resizeEvent(event);
303 clearCache();
304 }
305
306 qreal KItemListWidget::hoverOpacity() const
307 {
308 return m_hoverOpacity;
309 }
310
311 void KItemListWidget::setHoverOpacity(qreal opacity)
312 {
313 m_hoverOpacity = opacity;
314 update();
315 }
316
317 void KItemListWidget::clearCache()
318 {
319 delete m_hoverCache;
320 m_hoverCache = 0;
321 }
322
323 #include "kitemlistwidget.moc"