]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistwidget.cpp
Provide alternating background colors for the details-view
[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_alternatingBackgroundColors(false),
42 m_data(),
43 m_visibleRoles(),
44 m_visibleRolesSizes(),
45 m_styleOption(),
46 m_hoverOpacity(0),
47 m_hoverCache(0),
48 m_hoverAnimation(0)
49 {
50 }
51
52 KItemListWidget::~KItemListWidget()
53 {
54 clearHoverCache();
55 }
56
57 void KItemListWidget::setIndex(int index)
58 {
59 if (m_index != index) {
60 if (m_hoverAnimation) {
61 m_hoverAnimation->stop();
62 m_hoverOpacity = 0;
63 }
64 clearHoverCache();
65
66 m_index = index;
67 }
68 }
69
70 int KItemListWidget::index() const
71 {
72 return m_index;
73 }
74
75 void KItemListWidget::setData(const QHash<QByteArray, QVariant>& data,
76 const QSet<QByteArray>& roles)
77 {
78 clearHoverCache();
79 if (roles.isEmpty()) {
80 m_data = data;
81 dataChanged(m_data);
82 } else {
83 foreach (const QByteArray& role, roles) {
84 m_data[role] = data[role];
85 }
86 dataChanged(m_data, roles);
87 }
88 }
89
90 QHash<QByteArray, QVariant> KItemListWidget::data() const
91 {
92 return m_data;
93 }
94
95 void KItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
96 {
97 Q_UNUSED(option);
98
99 painter->setRenderHint(QPainter::Antialiasing);
100
101 if (m_alternatingBackgroundColors && (m_index & 0x1)) {
102 const QColor backgroundColor = m_styleOption.palette.color(QPalette::AlternateBase);
103 const QRectF backgroundRect(0, 0, size().width(), size().height());
104 painter->fillRect(backgroundRect, backgroundColor);
105 }
106
107 const QRect iconBounds = iconBoundingRect().toRect();
108 if (m_selected) {
109 QStyleOptionViewItemV4 viewItemOption;
110 viewItemOption.initFrom(widget);
111 viewItemOption.rect = iconBounds;
112 viewItemOption.state = QStyle::State_Enabled | QStyle::State_Selected | QStyle::State_Item;
113 viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
114 widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget);
115
116 drawTextBackground(painter);
117 }
118
119 if (isCurrent()) {
120 drawFocusIndicator(painter);
121 }
122
123 if (m_hoverOpacity <= 0.0) {
124 return;
125 }
126
127 if (!m_hoverCache) {
128 // Initialize the m_hoverCache pixmap to improve the drawing performance
129 // when fading the hover background
130 m_hoverCache = new QPixmap(iconBounds.size());
131 m_hoverCache->fill(Qt::transparent);
132
133 QPainter pixmapPainter(m_hoverCache);
134
135 QStyleOptionViewItemV4 viewItemOption;
136 viewItemOption.initFrom(widget);
137 viewItemOption.rect = QRect(0, 0, iconBounds.width(), iconBounds.height());
138 viewItemOption.state = QStyle::State_Enabled | QStyle::State_MouseOver | QStyle::State_Item;
139 viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
140
141 widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, &pixmapPainter, widget);
142 }
143
144 const qreal opacity = painter->opacity();
145 painter->setOpacity(m_hoverOpacity * opacity);
146 painter->drawPixmap(iconBounds.topLeft(), *m_hoverCache);
147 drawTextBackground(painter);
148 painter->setOpacity(opacity);
149 }
150
151 void KItemListWidget::setVisibleRoles(const QList<QByteArray>& roles)
152 {
153 const QList<QByteArray> previousRoles = m_visibleRoles;
154 m_visibleRoles = roles;
155 visibleRolesChanged(roles, previousRoles);
156 }
157
158 QList<QByteArray> KItemListWidget::visibleRoles() const
159 {
160 return m_visibleRoles;
161 }
162
163 void KItemListWidget::setVisibleRolesSizes(const QHash<QByteArray, QSizeF> rolesSizes)
164 {
165 const QHash<QByteArray, QSizeF> previousRolesSizes = m_visibleRolesSizes;
166 m_visibleRolesSizes = rolesSizes;
167 visibleRolesSizesChanged(rolesSizes, previousRolesSizes);
168 }
169
170 QHash<QByteArray, QSizeF> KItemListWidget::visibleRolesSizes() const
171 {
172 return m_visibleRolesSizes;
173 }
174
175 void KItemListWidget::setStyleOption(const KItemListStyleOption& option)
176 {
177 const KItemListStyleOption previous = m_styleOption;
178 clearHoverCache();
179 m_styleOption = option;
180
181 styleOptionChanged(option, previous);
182 }
183
184 const KItemListStyleOption& KItemListWidget::styleOption() const
185 {
186 return m_styleOption;
187 }
188
189 void KItemListWidget::setSelected(bool selected)
190 {
191 if (m_selected != selected) {
192 m_selected = selected;
193 selectedChanged(selected);
194 update();
195 }
196 }
197
198 bool KItemListWidget::isSelected() const
199 {
200 return m_selected;
201 }
202
203 void KItemListWidget::setCurrent(bool current)
204 {
205 if (m_current != current) {
206 m_current = current;
207 currentChanged(current);
208 update();
209 }
210 }
211
212 bool KItemListWidget::isCurrent() const
213 {
214 return m_current;
215 }
216
217 void KItemListWidget::setHovered(bool hovered)
218 {
219 if (hovered == m_hovered) {
220 return;
221 }
222
223 m_hovered = hovered;
224
225 if (!m_hoverAnimation) {
226 m_hoverAnimation = new QPropertyAnimation(this, "hoverOpacity", this);
227 m_hoverAnimation->setDuration(200);
228 }
229 m_hoverAnimation->stop();
230
231 if (hovered) {
232 m_hoverAnimation->setEndValue(1.0);
233 } else {
234 m_hoverAnimation->setEndValue(0.0);
235 }
236
237 m_hoverAnimation->start();
238
239 hoveredChanged(hovered);
240
241 update();
242 }
243
244 bool KItemListWidget::isHovered() const
245 {
246 return m_hovered;
247 }
248
249 void KItemListWidget::setAlternatingBackgroundColors(bool enable)
250 {
251 if (m_alternatingBackgroundColors != enable) {
252 m_alternatingBackgroundColors = enable;
253 alternatingBackgroundColorsChanged(enable);
254 update();
255 }
256 }
257
258 bool KItemListWidget::alternatingBackgroundColors() const
259 {
260 return m_alternatingBackgroundColors;
261 }
262
263 bool KItemListWidget::contains(const QPointF& point) const
264 {
265 if (!QGraphicsWidget::contains(point)) {
266 return false;
267 }
268
269 return iconBoundingRect().contains(point) ||
270 textBoundingRect().contains(point) ||
271 expansionToggleRect().contains(point) ||
272 selectionToggleRect().contains(point);
273 }
274
275 QRectF KItemListWidget::selectionToggleRect() const
276 {
277 return QRectF();
278 }
279
280 QRectF KItemListWidget::expansionToggleRect() const
281 {
282 return QRectF();
283 }
284
285 void KItemListWidget::dataChanged(const QHash<QByteArray, QVariant>& current,
286 const QSet<QByteArray>& roles)
287 {
288 Q_UNUSED(current);
289 Q_UNUSED(roles);
290 update();
291 }
292
293 void KItemListWidget::visibleRolesChanged(const QList<QByteArray>& current,
294 const QList<QByteArray>& previous)
295 {
296 Q_UNUSED(current);
297 Q_UNUSED(previous);
298 update();
299 }
300
301 void KItemListWidget::visibleRolesSizesChanged(const QHash<QByteArray, QSizeF>& current,
302 const QHash<QByteArray, QSizeF>& previous)
303 {
304 Q_UNUSED(current);
305 Q_UNUSED(previous);
306 update();
307 }
308
309 void KItemListWidget::styleOptionChanged(const KItemListStyleOption& current,
310 const KItemListStyleOption& previous)
311 {
312 Q_UNUSED(current);
313 Q_UNUSED(previous);
314 update();
315 }
316
317 void KItemListWidget::currentChanged(bool current)
318 {
319 Q_UNUSED(current);
320 }
321
322 void KItemListWidget::selectedChanged(bool selected)
323 {
324 Q_UNUSED(selected);
325 }
326
327 void KItemListWidget::hoveredChanged(bool hovered)
328 {
329 Q_UNUSED(hovered);
330 }
331
332 void KItemListWidget::alternatingBackgroundColorsChanged(bool enabled)
333 {
334 Q_UNUSED(enabled);
335 }
336
337 void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent* event)
338 {
339 QGraphicsWidget::resizeEvent(event);
340 clearHoverCache();
341 }
342
343 qreal KItemListWidget::hoverOpacity() const
344 {
345 return m_hoverOpacity;
346 }
347
348 void KItemListWidget::setHoverOpacity(qreal opacity)
349 {
350 m_hoverOpacity = opacity;
351 update();
352 }
353
354 void KItemListWidget::clearHoverCache()
355 {
356 delete m_hoverCache;
357 m_hoverCache = 0;
358 }
359
360 void KItemListWidget::drawFocusIndicator(QPainter* painter)
361 {
362 // Ideally style()->drawPrimitive(QStyle::PE_FrameFocusRect...)
363 // should be used, but Oxygen only draws indicators within classes
364 // derived from QAbstractItemView or Q3ListView. As a workaround
365 // the indicator is drawn manually. Code copied from oxygenstyle.cpp
366 // Copyright ( C ) 2009-2010 Hugo Pereira Da Costa <hugo@oxygen-icons.org>
367 // TODO: Clarify with Oxygen maintainers how to proceed with this.
368
369 const KItemListStyleOption& option = styleOption();
370 const QPalette palette = option.palette;
371 const QRect rect = textBoundingRect().toRect().adjusted(0, 0, 0, -1);
372
373 QLinearGradient gradient(rect.bottomLeft(), rect.bottomRight());
374 gradient.setColorAt(0.0, Qt::transparent);
375 gradient.setColorAt(1.0, Qt::transparent);
376 gradient.setColorAt(0.2, palette.color(QPalette::Text));
377 gradient.setColorAt(0.8, palette.color(QPalette::Text));
378
379 painter->setRenderHint(QPainter::Antialiasing, false);
380 painter->setPen(QPen(gradient, 1));
381 painter->drawLine(rect.bottomLeft(), rect.bottomRight());
382 painter->setRenderHint(QPainter::Antialiasing, true);
383 }
384
385 void KItemListWidget::drawTextBackground(QPainter* painter)
386 {
387 const qreal opacity = painter->opacity();
388
389 QRectF textBounds = textBoundingRect();
390 const qreal marginDiff = m_styleOption.margin / 2;
391 textBounds.adjust(marginDiff, marginDiff, -marginDiff, -marginDiff);
392 painter->setOpacity(opacity * 0.1);
393 painter->setPen(Qt::NoPen);
394 painter->setBrush(m_styleOption.palette.text());
395 painter->drawRoundedRect(textBounds, 4, 4);
396
397 painter->setOpacity(opacity);
398 }
399
400 #include "kitemlistwidget.moc"