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