]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistwidget.cpp
74b96ca1f572dd3500e6bf3bc948148729a63c22
[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 "private/kitemlistselectiontoggle.h"
29
30 #include <KDebug>
31
32 #include <KGlobalSettings>
33 #include <QApplication>
34 #include <QPainter>
35 #include <QPropertyAnimation>
36 #include <QStyleOption>
37
38 KItemListWidget::KItemListWidget(QGraphicsItem* parent) :
39 QGraphicsWidget(parent, 0),
40 m_index(-1),
41 m_selected(false),
42 m_current(false),
43 m_hovered(false),
44 m_alternateBackground(false),
45 m_enabledSelectionToggle(false),
46 m_data(),
47 m_visibleRoles(),
48 m_columnWidths(),
49 m_styleOption(),
50 m_siblingsInfo(),
51 m_hoverOpacity(0),
52 m_hoverCache(0),
53 m_hoverAnimation(0),
54 m_selectionToggle(0)
55 {
56 }
57
58 KItemListWidget::~KItemListWidget()
59 {
60 clearHoverCache();
61 }
62
63 void KItemListWidget::setIndex(int index)
64 {
65 if (m_index != index) {
66 delete m_selectionToggle;
67 m_selectionToggle = 0;
68
69 if (m_hoverAnimation) {
70 m_hoverAnimation->stop();
71 m_hoverOpacity = 0;
72 }
73 clearHoverCache();
74
75 m_index = index;
76 }
77 }
78
79 int KItemListWidget::index() const
80 {
81 return m_index;
82 }
83
84 void KItemListWidget::setData(const QHash<QByteArray, QVariant>& data,
85 const QSet<QByteArray>& roles)
86 {
87 clearHoverCache();
88 if (roles.isEmpty()) {
89 m_data = data;
90 dataChanged(m_data);
91 } else {
92 foreach (const QByteArray& role, roles) {
93 m_data[role] = data[role];
94 }
95 dataChanged(m_data, roles);
96 }
97 update();
98 }
99
100 QHash<QByteArray, QVariant> KItemListWidget::data() const
101 {
102 return m_data;
103 }
104
105 void KItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
106 {
107 Q_UNUSED(option);
108
109 if (m_alternateBackground) {
110 const QColor backgroundColor = m_styleOption.palette.color(QPalette::AlternateBase);
111 const QRectF backgroundRect(0, 0, size().width(), size().height());
112 painter->fillRect(backgroundRect, backgroundColor);
113 }
114
115 if (m_selected) {
116 const QStyle::State activeState(isActiveWindow() ? QStyle::State_Active : 0);
117 drawItemStyleOption(painter, widget, activeState |
118 QStyle::State_Enabled |
119 QStyle::State_Selected |
120 QStyle::State_Item);
121 }
122
123 if (isCurrent()) {
124 QStyleOptionFocusRect focusRectOption;
125 focusRectOption.initFrom(widget);
126 focusRectOption.rect = textFocusRect().toRect();
127 focusRectOption.state = QStyle::State_Enabled | QStyle::State_Item | QStyle::State_KeyboardFocusChange;
128 if (m_selected) {
129 focusRectOption.state |= QStyle::State_Selected;
130 }
131
132 style()->drawPrimitive(QStyle::PE_FrameFocusRect, &focusRectOption, painter, widget);
133 }
134
135 if (m_hoverOpacity > 0.0) {
136 if (!m_hoverCache) {
137 // Initialize the m_hoverCache pixmap to improve the drawing performance
138 // when fading the hover background
139 m_hoverCache = new QPixmap(size().toSize());
140 m_hoverCache->fill(Qt::transparent);
141
142 QPainter pixmapPainter(m_hoverCache);
143 const QStyle::State activeState(isActiveWindow() ? QStyle::State_Active : 0);
144 drawItemStyleOption(&pixmapPainter, widget, activeState |
145 QStyle::State_Enabled |
146 QStyle::State_MouseOver |
147 QStyle::State_Item);
148 }
149
150 const qreal opacity = painter->opacity();
151 painter->setOpacity(m_hoverOpacity * opacity);
152 painter->drawPixmap(0, 0, *m_hoverCache);
153 painter->setOpacity(opacity);
154 }
155 }
156
157 void KItemListWidget::setVisibleRoles(const QList<QByteArray>& roles)
158 {
159 const QList<QByteArray> previousRoles = m_visibleRoles;
160 m_visibleRoles = roles;
161
162 visibleRolesChanged(roles, previousRoles);
163 update();
164 }
165
166 QList<QByteArray> KItemListWidget::visibleRoles() const
167 {
168 return m_visibleRoles;
169 }
170
171
172 void KItemListWidget::setColumnWidth(const QByteArray& role, qreal width)
173 {
174 if (m_columnWidths.value(role) != width) {
175 const qreal previousWidth = width;
176 m_columnWidths.insert(role, width);
177 columnWidthChanged(role, width, previousWidth);
178 update();
179 }
180 }
181
182 qreal KItemListWidget::columnWidth(const QByteArray& role) const
183 {
184 return m_columnWidths.value(role);
185 }
186
187 void KItemListWidget::setStyleOption(const KItemListStyleOption& option)
188 {
189 const KItemListStyleOption previous = m_styleOption;
190 clearHoverCache();
191 m_styleOption = option;
192
193 styleOptionChanged(option, previous);
194 update();
195 }
196
197 const KItemListStyleOption& KItemListWidget::styleOption() const
198 {
199 return m_styleOption;
200 }
201
202 void KItemListWidget::setSelected(bool selected)
203 {
204 if (m_selected != selected) {
205 m_selected = selected;
206 if (m_selectionToggle) {
207 m_selectionToggle->setChecked(selected);
208 }
209 selectedChanged(selected);
210 update();
211 }
212 }
213
214 bool KItemListWidget::isSelected() const
215 {
216 return m_selected;
217 }
218
219 void KItemListWidget::setCurrent(bool current)
220 {
221 if (m_current != current) {
222 m_current = current;
223 currentChanged(current);
224 update();
225 }
226 }
227
228 bool KItemListWidget::isCurrent() const
229 {
230 return m_current;
231 }
232
233 void KItemListWidget::setHovered(bool hovered)
234 {
235 if (hovered == m_hovered) {
236 return;
237 }
238
239 m_hovered = hovered;
240
241 if (!m_hoverAnimation) {
242 m_hoverAnimation = new QPropertyAnimation(this, "hoverOpacity", this);
243 const int duration = (KGlobalSettings::graphicEffectsLevel() == KGlobalSettings::NoEffects) ? 1 : 200;
244 m_hoverAnimation->setDuration(duration);
245 connect(m_hoverAnimation, SIGNAL(finished()), this, SLOT(slotHoverAnimationFinished()));
246 }
247 m_hoverAnimation->stop();
248
249 if (hovered) {
250 const qreal startValue = qMax(hoverOpacity(), qreal(0.1));
251 m_hoverAnimation->setStartValue(startValue);
252 m_hoverAnimation->setEndValue(1.0);
253 if (m_enabledSelectionToggle && !(QApplication::mouseButtons() & Qt::LeftButton)) {
254 initializeSelectionToggle();
255 }
256 } else {
257 m_hoverAnimation->setStartValue(hoverOpacity());
258 m_hoverAnimation->setEndValue(0.0);
259 }
260
261 m_hoverAnimation->start();
262
263 hoveredChanged(hovered);
264 update();
265 }
266
267 bool KItemListWidget::isHovered() const
268 {
269 return m_hovered;
270 }
271
272 void KItemListWidget::setAlternateBackground(bool enable)
273 {
274 if (m_alternateBackground != enable) {
275 m_alternateBackground = enable;
276 alternateBackgroundChanged(enable);
277 update();
278 }
279 }
280
281 bool KItemListWidget::alternateBackground() const
282 {
283 return m_alternateBackground;
284 }
285
286 void KItemListWidget::setEnabledSelectionToggle(bool enable)
287 {
288 if (m_enabledSelectionToggle != enable) {
289 m_enabledSelectionToggle = enable;
290 update();
291 }
292 }
293
294 bool KItemListWidget::enabledSelectionToggle() const
295 {
296 return m_enabledSelectionToggle;
297 }
298
299 void KItemListWidget::setSiblingsInformation(const QBitArray& siblings)
300 {
301 const QBitArray previous = m_siblingsInfo;
302 m_siblingsInfo = siblings;
303 siblingsInformationChanged(m_siblingsInfo, previous);
304 update();
305 }
306
307 QBitArray KItemListWidget::siblingsInformation() const
308 {
309 return m_siblingsInfo;
310 }
311
312 bool KItemListWidget::contains(const QPointF& point) const
313 {
314 if (!QGraphicsWidget::contains(point)) {
315 return false;
316 }
317
318 return iconRect().contains(point) ||
319 textRect().contains(point) ||
320 expansionToggleRect().contains(point) ||
321 selectionToggleRect().contains(point);
322 }
323
324 QRectF KItemListWidget::textFocusRect() const
325 {
326 return textRect();
327 }
328
329 QRectF KItemListWidget::selectionToggleRect() const
330 {
331 return QRectF();
332 }
333
334 QRectF KItemListWidget::expansionToggleRect() const
335 {
336 return QRectF();
337 }
338
339 void KItemListWidget::dataChanged(const QHash<QByteArray, QVariant>& current,
340 const QSet<QByteArray>& roles)
341 {
342 Q_UNUSED(current);
343 Q_UNUSED(roles);
344 }
345
346 void KItemListWidget::visibleRolesChanged(const QList<QByteArray>& current,
347 const QList<QByteArray>& previous)
348 {
349 Q_UNUSED(current);
350 Q_UNUSED(previous);
351 }
352
353 void KItemListWidget::columnWidthChanged(const QByteArray& role,
354 qreal current,
355 qreal previous)
356 {
357 Q_UNUSED(role);
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::alternateBackgroundChanged(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 QStyleOptionViewItemV4 viewItemOption;
453 viewItemOption.initFrom(widget);
454 viewItemOption.state = styleState;
455 viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
456 viewItemOption.showDecorationSelected = true;
457 viewItemOption.rect = textRect().toRect();
458 widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget);
459 }
460
461 #include "kitemlistwidget.moc"