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