]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistwidget.cpp
Fix current-item indicator in combination with the cleanlooks style
[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 QStyleOptionFocusRect focusRectOption;
123 focusRectOption.initFrom(widget);
124
125 const QRect iconBounds = iconRect().toRect();
126 const QRect textBounds = textRect().toRect();
127 if (iconBounds.bottom() > textBounds.top()) {
128 focusRectOption.rect = textBounds;
129 } else {
130 // See KItemListWidget::drawItemStyleOption(): The selection rectangle
131 // gets decreased.
132 focusRectOption.rect = textBounds.adjusted(1, 1, -1, -1);
133 }
134
135 focusRectOption.state = QStyle::State_Enabled | QStyle::State_Item | QStyle::State_KeyboardFocusChange;
136 if (m_selected) {
137 focusRectOption.state |= QStyle::State_Selected;
138 }
139
140 style()->drawPrimitive(QStyle::PE_FrameFocusRect, &focusRectOption, painter, widget);
141 }
142
143 if (m_hoverOpacity > 0.0) {
144 if (!m_hoverCache) {
145 // Initialize the m_hoverCache pixmap to improve the drawing performance
146 // when fading the hover background
147 m_hoverCache = new QPixmap(size().toSize());
148 m_hoverCache->fill(Qt::transparent);
149
150 QPainter pixmapPainter(m_hoverCache);
151 const QStyle::State activeState(isActiveWindow() ? QStyle::State_Active : 0);
152 drawItemStyleOption(&pixmapPainter, widget, activeState |
153 QStyle::State_Enabled |
154 QStyle::State_MouseOver |
155 QStyle::State_Item);
156 }
157
158 const qreal opacity = painter->opacity();
159 painter->setOpacity(m_hoverOpacity * opacity);
160 painter->drawPixmap(0, 0, *m_hoverCache);
161 painter->setOpacity(opacity);
162 }
163 }
164
165 void KItemListWidget::setVisibleRoles(const QList<QByteArray>& roles)
166 {
167 const QList<QByteArray> previousRoles = m_visibleRoles;
168 m_visibleRoles = roles;
169 visibleRolesChanged(roles, previousRoles);
170 }
171
172 QList<QByteArray> KItemListWidget::visibleRoles() const
173 {
174 return m_visibleRoles;
175 }
176
177 void KItemListWidget::setVisibleRolesSizes(const QHash<QByteArray, QSizeF> rolesSizes)
178 {
179 const QHash<QByteArray, QSizeF> previousRolesSizes = m_visibleRolesSizes;
180 m_visibleRolesSizes = rolesSizes;
181 visibleRolesSizesChanged(rolesSizes, previousRolesSizes);
182 }
183
184 QHash<QByteArray, QSizeF> KItemListWidget::visibleRolesSizes() const
185 {
186 return m_visibleRolesSizes;
187 }
188
189 void KItemListWidget::setStyleOption(const KItemListStyleOption& option)
190 {
191 const KItemListStyleOption previous = m_styleOption;
192 clearHoverCache();
193 m_styleOption = option;
194
195 styleOptionChanged(option, previous);
196 }
197
198 const KItemListStyleOption& KItemListWidget::styleOption() const
199 {
200 return m_styleOption;
201 }
202
203 void KItemListWidget::setSelected(bool selected)
204 {
205 if (m_selected != selected) {
206 m_selected = selected;
207 if (m_selectionToggle) {
208 m_selectionToggle->setChecked(selected);
209 }
210
211 selectedChanged(selected);
212 update();
213 }
214 }
215
216 bool KItemListWidget::isSelected() const
217 {
218 return m_selected;
219 }
220
221 void KItemListWidget::setCurrent(bool current)
222 {
223 if (m_current != current) {
224 m_current = current;
225
226 currentChanged(current);
227 update();
228 }
229 }
230
231 bool KItemListWidget::isCurrent() const
232 {
233 return m_current;
234 }
235
236 void KItemListWidget::setHovered(bool hovered)
237 {
238 if (hovered == m_hovered) {
239 return;
240 }
241
242 m_hovered = hovered;
243
244 if (!m_hoverAnimation) {
245 m_hoverAnimation = new QPropertyAnimation(this, "hoverOpacity", this);
246 m_hoverAnimation->setDuration(200);
247 connect(m_hoverAnimation, SIGNAL(finished()), this, SLOT(slotHoverAnimationFinished()));
248 }
249 m_hoverAnimation->stop();
250
251 if (hovered) {
252 const qreal startValue = qMax(hoverOpacity(), qreal(0.1));
253 m_hoverAnimation->setStartValue(startValue);
254 m_hoverAnimation->setEndValue(1.0);
255 if (m_enabledSelectionToggle && !(QApplication::mouseButtons() & Qt::LeftButton)) {
256 initializeSelectionToggle();
257 }
258 } else {
259 m_hoverAnimation->setStartValue(hoverOpacity());
260 m_hoverAnimation->setEndValue(0.0);
261 }
262
263 m_hoverAnimation->start();
264
265 hoveredChanged(hovered);
266
267 update();
268 }
269
270 bool KItemListWidget::isHovered() const
271 {
272 return m_hovered;
273 }
274
275 void KItemListWidget::setAlternatingBackgroundColors(bool enable)
276 {
277 if (m_alternatingBackgroundColors != enable) {
278 m_alternatingBackgroundColors = enable;
279 alternatingBackgroundColorsChanged(enable);
280 update();
281 }
282 }
283
284 bool KItemListWidget::alternatingBackgroundColors() const
285 {
286 return m_alternatingBackgroundColors;
287 }
288
289 void KItemListWidget::setEnabledSelectionToggle(bool enable)
290 {
291 if (m_enabledSelectionToggle != enable) {
292 m_enabledSelectionToggle = enable;
293 update();
294 }
295 }
296
297 bool KItemListWidget::enabledSelectionToggle() const
298 {
299 return m_enabledSelectionToggle;
300 }
301
302 bool KItemListWidget::contains(const QPointF& point) const
303 {
304 if (!QGraphicsWidget::contains(point)) {
305 return false;
306 }
307
308 return iconRect().contains(point) ||
309 textRect().contains(point) ||
310 expansionToggleRect().contains(point) ||
311 selectionToggleRect().contains(point);
312 }
313
314 QRectF KItemListWidget::selectionToggleRect() const
315 {
316 return QRectF();
317 }
318
319 QRectF KItemListWidget::expansionToggleRect() const
320 {
321 return QRectF();
322 }
323
324 void KItemListWidget::dataChanged(const QHash<QByteArray, QVariant>& current,
325 const QSet<QByteArray>& roles)
326 {
327 Q_UNUSED(current);
328 Q_UNUSED(roles);
329 update();
330 }
331
332 void KItemListWidget::visibleRolesChanged(const QList<QByteArray>& current,
333 const QList<QByteArray>& previous)
334 {
335 Q_UNUSED(current);
336 Q_UNUSED(previous);
337 update();
338 }
339
340 void KItemListWidget::visibleRolesSizesChanged(const QHash<QByteArray, QSizeF>& current,
341 const QHash<QByteArray, QSizeF>& previous)
342 {
343 Q_UNUSED(current);
344 Q_UNUSED(previous);
345 update();
346 }
347
348 void KItemListWidget::styleOptionChanged(const KItemListStyleOption& current,
349 const KItemListStyleOption& previous)
350 {
351 Q_UNUSED(current);
352 Q_UNUSED(previous);
353 update();
354 }
355
356 void KItemListWidget::currentChanged(bool current)
357 {
358 Q_UNUSED(current);
359 }
360
361 void KItemListWidget::selectedChanged(bool selected)
362 {
363 Q_UNUSED(selected);
364 }
365
366 void KItemListWidget::hoveredChanged(bool hovered)
367 {
368 Q_UNUSED(hovered);
369 }
370
371 void KItemListWidget::alternatingBackgroundColorsChanged(bool enabled)
372 {
373 Q_UNUSED(enabled);
374 }
375
376 void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent* event)
377 {
378 QGraphicsWidget::resizeEvent(event);
379 clearHoverCache();
380 }
381
382 qreal KItemListWidget::hoverOpacity() const
383 {
384 return m_hoverOpacity;
385 }
386
387 void KItemListWidget::slotHoverAnimationFinished()
388 {
389 if (!m_hovered) {
390 delete m_selectionToggle;
391 m_selectionToggle = 0;
392 }
393 }
394
395 void KItemListWidget::initializeSelectionToggle()
396 {
397 Q_ASSERT(m_enabledSelectionToggle);
398
399 if (!m_selectionToggle) {
400 m_selectionToggle = new KItemListSelectionToggle(this);
401 }
402
403 const QRectF toggleRect = selectionToggleRect();
404 m_selectionToggle->setPos(toggleRect.topLeft());
405 m_selectionToggle->resize(toggleRect.size());
406
407 m_selectionToggle->setChecked(isSelected());
408 }
409
410 void KItemListWidget::setHoverOpacity(qreal opacity)
411 {
412 m_hoverOpacity = opacity;
413 if (m_selectionToggle) {
414 m_selectionToggle->setOpacity(opacity);
415 }
416
417 if (m_hoverOpacity <= 0.0) {
418 delete m_hoverCache;
419 m_hoverCache = 0;
420 }
421
422 update();
423 }
424
425 void KItemListWidget::clearHoverCache()
426 {
427 delete m_hoverCache;
428 m_hoverCache = 0;
429 }
430
431 void KItemListWidget::drawItemStyleOption(QPainter* painter, QWidget* widget, QStyle::State styleState)
432 {
433 const QRect iconBounds = iconRect().toRect();
434 const QRect textBounds = textRect().toRect();
435
436 QStyleOptionViewItemV4 viewItemOption;
437 viewItemOption.initFrom(widget);
438 viewItemOption.state = styleState;
439 viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
440 viewItemOption.showDecorationSelected = true;
441
442 if (iconBounds.bottom() > textBounds.top()) {
443 viewItemOption.rect = iconBounds | textBounds;
444 widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget);
445 } else {
446 viewItemOption.rect = iconBounds;
447 widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget);
448
449 viewItemOption.rect = textBounds.adjusted(1, 1, -1, -1);
450 widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget);
451 }
452 }
453
454 #include "kitemlistwidget.moc"