]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistwidget.cpp
Fix colors for the selection/hover-area
[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 "kitemlistselectiontoggle_p.h"
26 #include "kitemlistview.h"
27 #include "kitemmodelbase.h"
28
29 #include <KDebug>
30
31 #include <QApplication>
32 #include <QPainter>
33 #include <QPropertyAnimation>
34 #include <QStyleOption>
35
36 KItemListWidget::KItemListWidget(QGraphicsItem* parent) :
37 QGraphicsWidget(parent, 0),
38 m_index(-1),
39 m_selected(false),
40 m_current(false),
41 m_hovered(false),
42 m_alternatingBackgroundColors(false),
43 m_enabledSelectionToggle(false),
44 m_data(),
45 m_visibleRoles(),
46 m_visibleRolesSizes(),
47 m_styleOption(),
48 m_hoverOpacity(0),
49 m_hoverCache(0),
50 m_hoverAnimation(0),
51 m_selectionToggle(0)
52 {
53 }
54
55 KItemListWidget::~KItemListWidget()
56 {
57 clearHoverCache();
58 }
59
60 void KItemListWidget::setIndex(int index)
61 {
62 if (m_index != index) {
63 delete m_selectionToggle;
64 m_selectionToggle = 0;
65
66 if (m_hoverAnimation) {
67 m_hoverAnimation->stop();
68 m_hoverOpacity = 0;
69 }
70 clearHoverCache();
71
72 m_index = index;
73 }
74 }
75
76 int KItemListWidget::index() const
77 {
78 return m_index;
79 }
80
81 void KItemListWidget::setData(const QHash<QByteArray, QVariant>& data,
82 const QSet<QByteArray>& roles)
83 {
84 clearHoverCache();
85 if (roles.isEmpty()) {
86 m_data = data;
87 dataChanged(m_data);
88 } else {
89 foreach (const QByteArray& role, roles) {
90 m_data[role] = data[role];
91 }
92 dataChanged(m_data, roles);
93 }
94 }
95
96 QHash<QByteArray, QVariant> KItemListWidget::data() const
97 {
98 return m_data;
99 }
100
101 void KItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
102 {
103 Q_UNUSED(option);
104
105 painter->setRenderHint(QPainter::Antialiasing);
106
107 if (m_alternatingBackgroundColors && (m_index & 0x1)) {
108 const QColor backgroundColor = m_styleOption.palette.color(QPalette::AlternateBase);
109 const QRectF backgroundRect(0, 0, size().width(), size().height());
110 painter->fillRect(backgroundRect, backgroundColor);
111 }
112
113 if (m_selected) {
114 const QStyle::State activeState(isActiveWindow() ? QStyle::State_Active : 0);
115 drawItemStyleOption(painter, widget, activeState |
116 QStyle::State_Enabled |
117 QStyle::State_Selected |
118 QStyle::State_Item);
119 }
120
121 if (isCurrent()) {
122 QStyleOptionViewItemV4 viewItemOption;
123 viewItemOption.initFrom(widget);
124 viewItemOption.rect = textRect().toRect();
125 viewItemOption.state = QStyle::State_Enabled | QStyle::State_Item;
126 viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
127 style()->drawPrimitive(QStyle::PE_FrameFocusRect, &viewItemOption, painter, widget);
128 }
129
130 if (m_hoverOpacity > 0.0) {
131 if (!m_hoverCache) {
132 // Initialize the m_hoverCache pixmap to improve the drawing performance
133 // when fading the hover background
134 m_hoverCache = new QPixmap(size().toSize());
135 m_hoverCache->fill(Qt::transparent);
136
137 QPainter pixmapPainter(m_hoverCache);
138 const QStyle::State activeState(isActiveWindow() ? QStyle::State_Active : 0);
139 drawItemStyleOption(&pixmapPainter, widget, activeState |
140 QStyle::State_Enabled |
141 QStyle::State_MouseOver |
142 QStyle::State_Item);
143 }
144
145 const qreal opacity = painter->opacity();
146 painter->setOpacity(m_hoverOpacity * opacity);
147 painter->drawPixmap(0, 0, *m_hoverCache);
148 painter->setOpacity(opacity);
149 }
150 }
151
152 void KItemListWidget::setVisibleRoles(const QList<QByteArray>& roles)
153 {
154 const QList<QByteArray> previousRoles = m_visibleRoles;
155 m_visibleRoles = roles;
156 visibleRolesChanged(roles, previousRoles);
157 }
158
159 QList<QByteArray> KItemListWidget::visibleRoles() const
160 {
161 return m_visibleRoles;
162 }
163
164 void KItemListWidget::setVisibleRolesSizes(const QHash<QByteArray, QSizeF> rolesSizes)
165 {
166 const QHash<QByteArray, QSizeF> previousRolesSizes = m_visibleRolesSizes;
167 m_visibleRolesSizes = rolesSizes;
168 visibleRolesSizesChanged(rolesSizes, previousRolesSizes);
169 }
170
171 QHash<QByteArray, QSizeF> KItemListWidget::visibleRolesSizes() const
172 {
173 return m_visibleRolesSizes;
174 }
175
176 void KItemListWidget::setStyleOption(const KItemListStyleOption& option)
177 {
178 const KItemListStyleOption previous = m_styleOption;
179 clearHoverCache();
180 m_styleOption = option;
181
182 styleOptionChanged(option, previous);
183 }
184
185 const KItemListStyleOption& KItemListWidget::styleOption() const
186 {
187 return m_styleOption;
188 }
189
190 void KItemListWidget::setSelected(bool selected)
191 {
192 if (m_selected != selected) {
193 m_selected = selected;
194 if (m_selectionToggle) {
195 m_selectionToggle->setChecked(selected);
196 }
197
198 selectedChanged(selected);
199 update();
200 }
201 }
202
203 bool KItemListWidget::isSelected() const
204 {
205 return m_selected;
206 }
207
208 void KItemListWidget::setCurrent(bool current)
209 {
210 if (m_current != current) {
211 m_current = current;
212
213 currentChanged(current);
214 update();
215 }
216 }
217
218 bool KItemListWidget::isCurrent() const
219 {
220 return m_current;
221 }
222
223 void KItemListWidget::setHovered(bool hovered)
224 {
225 if (hovered == m_hovered) {
226 return;
227 }
228
229 m_hovered = hovered;
230
231 if (!m_hoverAnimation) {
232 m_hoverAnimation = new QPropertyAnimation(this, "hoverOpacity", this);
233 m_hoverAnimation->setDuration(200);
234 connect(m_hoverAnimation, SIGNAL(finished()), this, SLOT(slotHoverAnimationFinished()));
235 }
236 m_hoverAnimation->stop();
237
238 if (hovered) {
239 const qreal startValue = qMax(hoverOpacity(), qreal(0.1));
240 m_hoverAnimation->setStartValue(startValue);
241 m_hoverAnimation->setEndValue(1.0);
242 if (m_enabledSelectionToggle && !(QApplication::mouseButtons() & Qt::LeftButton)) {
243 initializeSelectionToggle();
244 }
245 } else {
246 m_hoverAnimation->setStartValue(hoverOpacity());
247 m_hoverAnimation->setEndValue(0.0);
248 }
249
250 m_hoverAnimation->start();
251
252 hoveredChanged(hovered);
253
254 update();
255 }
256
257 bool KItemListWidget::isHovered() const
258 {
259 return m_hovered;
260 }
261
262 void KItemListWidget::setAlternatingBackgroundColors(bool enable)
263 {
264 if (m_alternatingBackgroundColors != enable) {
265 m_alternatingBackgroundColors = enable;
266 alternatingBackgroundColorsChanged(enable);
267 update();
268 }
269 }
270
271 bool KItemListWidget::alternatingBackgroundColors() const
272 {
273 return m_alternatingBackgroundColors;
274 }
275
276 void KItemListWidget::setEnabledSelectionToggle(bool enable)
277 {
278 if (m_enabledSelectionToggle != enable) {
279 m_enabledSelectionToggle = enable;
280 update();
281 }
282 }
283
284 bool KItemListWidget::enabledSelectionToggle() const
285 {
286 return m_enabledSelectionToggle;
287 }
288
289 bool KItemListWidget::contains(const QPointF& point) const
290 {
291 if (!QGraphicsWidget::contains(point)) {
292 return false;
293 }
294
295 return iconRect().contains(point) ||
296 textRect().contains(point) ||
297 expansionToggleRect().contains(point) ||
298 selectionToggleRect().contains(point);
299 }
300
301 QRectF KItemListWidget::selectionToggleRect() const
302 {
303 return QRectF();
304 }
305
306 QRectF KItemListWidget::expansionToggleRect() const
307 {
308 return QRectF();
309 }
310
311 void KItemListWidget::dataChanged(const QHash<QByteArray, QVariant>& current,
312 const QSet<QByteArray>& roles)
313 {
314 Q_UNUSED(current);
315 Q_UNUSED(roles);
316 update();
317 }
318
319 void KItemListWidget::visibleRolesChanged(const QList<QByteArray>& current,
320 const QList<QByteArray>& previous)
321 {
322 Q_UNUSED(current);
323 Q_UNUSED(previous);
324 update();
325 }
326
327 void KItemListWidget::visibleRolesSizesChanged(const QHash<QByteArray, QSizeF>& current,
328 const QHash<QByteArray, QSizeF>& previous)
329 {
330 Q_UNUSED(current);
331 Q_UNUSED(previous);
332 update();
333 }
334
335 void KItemListWidget::styleOptionChanged(const KItemListStyleOption& current,
336 const KItemListStyleOption& previous)
337 {
338 Q_UNUSED(current);
339 Q_UNUSED(previous);
340 update();
341 }
342
343 void KItemListWidget::currentChanged(bool current)
344 {
345 Q_UNUSED(current);
346 }
347
348 void KItemListWidget::selectedChanged(bool selected)
349 {
350 Q_UNUSED(selected);
351 }
352
353 void KItemListWidget::hoveredChanged(bool hovered)
354 {
355 Q_UNUSED(hovered);
356 }
357
358 void KItemListWidget::alternatingBackgroundColorsChanged(bool enabled)
359 {
360 Q_UNUSED(enabled);
361 }
362
363 void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent* event)
364 {
365 QGraphicsWidget::resizeEvent(event);
366 clearHoverCache();
367 }
368
369 qreal KItemListWidget::hoverOpacity() const
370 {
371 return m_hoverOpacity;
372 }
373
374 void KItemListWidget::slotHoverAnimationFinished()
375 {
376 if (!m_hovered) {
377 delete m_selectionToggle;
378 m_selectionToggle = 0;
379 }
380 }
381
382 void KItemListWidget::initializeSelectionToggle()
383 {
384 Q_ASSERT(m_enabledSelectionToggle);
385
386 if (!m_selectionToggle) {
387 m_selectionToggle = new KItemListSelectionToggle(this);
388 }
389
390 const QRectF toggleRect = selectionToggleRect();
391 m_selectionToggle->setPos(toggleRect.topLeft());
392 m_selectionToggle->resize(toggleRect.size());
393
394 m_selectionToggle->setChecked(isSelected());
395 }
396
397 void KItemListWidget::setHoverOpacity(qreal opacity)
398 {
399 m_hoverOpacity = opacity;
400 if (m_selectionToggle) {
401 m_selectionToggle->setOpacity(opacity);
402 }
403
404 if (m_hoverOpacity <= 0.0) {
405 delete m_hoverCache;
406 m_hoverCache = 0;
407 }
408
409 update();
410 }
411
412 void KItemListWidget::clearHoverCache()
413 {
414 delete m_hoverCache;
415 m_hoverCache = 0;
416 }
417
418 void KItemListWidget::drawItemStyleOption(QPainter* painter, QWidget* widget, QStyle::State styleState)
419 {
420 const QRect iconBounds = iconRect().toRect();
421 const QRect textBounds = textRect().toRect();
422
423 QStyleOptionViewItemV4 viewItemOption;
424 viewItemOption.initFrom(widget);
425 viewItemOption.state = styleState;
426 viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
427
428 const bool drawMerged = (iconBounds.top() == textBounds.top() &&
429 iconBounds.bottom() == textBounds.bottom());
430
431 if (drawMerged) {
432 viewItemOption.rect = iconBounds | textBounds;
433 widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget);
434 } else {
435 viewItemOption.rect = iconBounds;
436 widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget);
437
438 viewItemOption.rect = textBounds.adjusted(2, 2, -2, -2);
439 widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget);
440 }
441 }
442
443 #include "kitemlistwidget.moc"