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