]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistwidget.cpp
Bring back the selection-markers
[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 <QStyle>
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_hoverOpacity(0),
50 m_hoverCache(0),
51 m_hoverAnimation(0),
52 m_selectionToggle(0)
53 {
54 }
55
56 KItemListWidget::~KItemListWidget()
57 {
58 clearHoverCache();
59 }
60
61 void KItemListWidget::setIndex(int index)
62 {
63 if (m_index != index) {
64 delete m_selectionToggle;
65 m_selectionToggle = 0;
66
67 if (m_hoverAnimation) {
68 m_hoverAnimation->stop();
69 m_hoverOpacity = 0;
70 }
71 clearHoverCache();
72
73 m_index = index;
74 }
75 }
76
77 int KItemListWidget::index() const
78 {
79 return m_index;
80 }
81
82 void KItemListWidget::setData(const QHash<QByteArray, QVariant>& data,
83 const QSet<QByteArray>& roles)
84 {
85 clearHoverCache();
86 if (roles.isEmpty()) {
87 m_data = data;
88 dataChanged(m_data);
89 } else {
90 foreach (const QByteArray& role, roles) {
91 m_data[role] = data[role];
92 }
93 dataChanged(m_data, roles);
94 }
95 }
96
97 QHash<QByteArray, QVariant> KItemListWidget::data() const
98 {
99 return m_data;
100 }
101
102 void KItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
103 {
104 Q_UNUSED(option);
105
106 painter->setRenderHint(QPainter::Antialiasing);
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 const QRect iconBounds = iconRect().toRect();
115 if (m_selected) {
116 QStyleOptionViewItemV4 viewItemOption;
117 viewItemOption.initFrom(widget);
118 viewItemOption.rect = iconBounds;
119 viewItemOption.state = QStyle::State_Enabled | QStyle::State_Selected | QStyle::State_Item;
120 viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
121 widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget);
122
123 drawTextBackground(painter);
124 }
125
126 if (isCurrent()) {
127 QStyleOptionViewItemV4 viewItemOption;
128 viewItemOption.initFrom(widget);
129 viewItemOption.rect = textRect().toRect();
130 viewItemOption.state = QStyle::State_Enabled | QStyle::State_Item;
131 viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
132 style()->drawPrimitive(QStyle::PE_FrameFocusRect, &viewItemOption, painter, widget);
133 }
134
135 if (m_hoverOpacity <= 0.0) {
136 return;
137 }
138
139 if (!m_hoverCache) {
140 // Initialize the m_hoverCache pixmap to improve the drawing performance
141 // when fading the hover background
142 m_hoverCache = new QPixmap(iconBounds.size());
143 m_hoverCache->fill(Qt::transparent);
144
145 QPainter pixmapPainter(m_hoverCache);
146
147 QStyleOptionViewItemV4 viewItemOption;
148 viewItemOption.initFrom(widget);
149 viewItemOption.rect = QRect(0, 0, iconBounds.width(), iconBounds.height());
150 viewItemOption.state = QStyle::State_Enabled | QStyle::State_MouseOver | QStyle::State_Item;
151 viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
152
153 widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, &pixmapPainter, widget);
154 }
155
156 const qreal opacity = painter->opacity();
157 painter->setOpacity(m_hoverOpacity * opacity);
158 painter->drawPixmap(iconBounds.topLeft(), *m_hoverCache);
159 drawTextBackground(painter);
160 painter->setOpacity(opacity);
161 }
162
163 void KItemListWidget::setVisibleRoles(const QList<QByteArray>& roles)
164 {
165 const QList<QByteArray> previousRoles = m_visibleRoles;
166 m_visibleRoles = roles;
167 visibleRolesChanged(roles, previousRoles);
168 }
169
170 QList<QByteArray> KItemListWidget::visibleRoles() const
171 {
172 return m_visibleRoles;
173 }
174
175 void KItemListWidget::setVisibleRolesSizes(const QHash<QByteArray, QSizeF> rolesSizes)
176 {
177 const QHash<QByteArray, QSizeF> previousRolesSizes = m_visibleRolesSizes;
178 m_visibleRolesSizes = rolesSizes;
179 visibleRolesSizesChanged(rolesSizes, previousRolesSizes);
180 }
181
182 QHash<QByteArray, QSizeF> KItemListWidget::visibleRolesSizes() const
183 {
184 return m_visibleRolesSizes;
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 }
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
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
224 currentChanged(current);
225 update();
226 }
227 }
228
229 bool KItemListWidget::isCurrent() const
230 {
231 return m_current;
232 }
233
234 void KItemListWidget::setHovered(bool hovered)
235 {
236 if (hovered == m_hovered) {
237 return;
238 }
239
240 m_hovered = hovered;
241
242 if (!m_hoverAnimation) {
243 m_hoverAnimation = new QPropertyAnimation(this, "hoverOpacity", this);
244 m_hoverAnimation->setDuration(200);
245 connect(m_hoverAnimation, SIGNAL(finished()), this, SLOT(slotHoverAnimationFinished()));
246 }
247 m_hoverAnimation->stop();
248
249 if (hovered) {
250 m_hoverAnimation->setEndValue(1.0);
251 if (m_enabledSelectionToggle && !(QApplication::mouseButtons() & Qt::LeftButton)) {
252 initializeSelectionToggle();
253 }
254 } else {
255 m_hoverAnimation->setEndValue(0.0);
256 }
257
258 m_hoverAnimation->start();
259
260 hoveredChanged(hovered);
261
262 update();
263 }
264
265 bool KItemListWidget::isHovered() const
266 {
267 return m_hovered;
268 }
269
270 void KItemListWidget::setAlternatingBackgroundColors(bool enable)
271 {
272 if (m_alternatingBackgroundColors != enable) {
273 m_alternatingBackgroundColors = enable;
274 alternatingBackgroundColorsChanged(enable);
275 update();
276 }
277 }
278
279 bool KItemListWidget::alternatingBackgroundColors() const
280 {
281 return m_alternatingBackgroundColors;
282 }
283
284 void KItemListWidget::setEnabledSelectionToggle(bool enable)
285 {
286 if (m_enabledSelectionToggle != enable) {
287 m_enabledSelectionToggle = enable;
288 update();
289 }
290 }
291
292 bool KItemListWidget::enabledSelectionToggle() const
293 {
294 return m_enabledSelectionToggle;
295 }
296
297 bool KItemListWidget::contains(const QPointF& point) const
298 {
299 if (!QGraphicsWidget::contains(point)) {
300 return false;
301 }
302
303 return iconRect().contains(point) ||
304 textRect().contains(point) ||
305 expansionToggleRect().contains(point) ||
306 selectionToggleRect().contains(point);
307 }
308
309 QRectF KItemListWidget::selectionToggleRect() const
310 {
311 return QRectF();
312 }
313
314 QRectF KItemListWidget::expansionToggleRect() const
315 {
316 return QRectF();
317 }
318
319 void KItemListWidget::dataChanged(const QHash<QByteArray, QVariant>& current,
320 const QSet<QByteArray>& roles)
321 {
322 Q_UNUSED(current);
323 Q_UNUSED(roles);
324 update();
325 }
326
327 void KItemListWidget::visibleRolesChanged(const QList<QByteArray>& current,
328 const QList<QByteArray>& previous)
329 {
330 Q_UNUSED(current);
331 Q_UNUSED(previous);
332 update();
333 }
334
335 void KItemListWidget::visibleRolesSizesChanged(const QHash<QByteArray, QSizeF>& current,
336 const QHash<QByteArray, QSizeF>& previous)
337 {
338 Q_UNUSED(current);
339 Q_UNUSED(previous);
340 update();
341 }
342
343 void KItemListWidget::styleOptionChanged(const KItemListStyleOption& current,
344 const KItemListStyleOption& previous)
345 {
346 Q_UNUSED(current);
347 Q_UNUSED(previous);
348 update();
349 }
350
351 void KItemListWidget::currentChanged(bool current)
352 {
353 Q_UNUSED(current);
354 }
355
356 void KItemListWidget::selectedChanged(bool selected)
357 {
358 Q_UNUSED(selected);
359 }
360
361 void KItemListWidget::hoveredChanged(bool hovered)
362 {
363 Q_UNUSED(hovered);
364 }
365
366 void KItemListWidget::alternatingBackgroundColorsChanged(bool enabled)
367 {
368 Q_UNUSED(enabled);
369 }
370
371 void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent* event)
372 {
373 QGraphicsWidget::resizeEvent(event);
374 clearHoverCache();
375 }
376
377 qreal KItemListWidget::hoverOpacity() const
378 {
379 return m_hoverOpacity;
380 }
381
382 void KItemListWidget::slotHoverAnimationFinished()
383 {
384 if (!m_hovered) {
385 delete m_selectionToggle;
386 m_selectionToggle = 0;
387 }
388 }
389
390 void KItemListWidget::initializeSelectionToggle()
391 {
392 Q_ASSERT(m_enabledSelectionToggle);
393
394 if (!m_selectionToggle) {
395 m_selectionToggle = new KItemListSelectionToggle(this);
396 }
397
398 const QRectF toggleRect = selectionToggleRect();
399 m_selectionToggle->setPos(toggleRect.topLeft());
400 m_selectionToggle->resize(toggleRect.size());
401
402 m_selectionToggle->setChecked(isSelected());
403 }
404
405 void KItemListWidget::setHoverOpacity(qreal opacity)
406 {
407 m_hoverOpacity = opacity;
408 if (m_selectionToggle) {
409 m_selectionToggle->setOpacity(opacity);
410 }
411 update();
412 }
413
414 void KItemListWidget::clearHoverCache()
415 {
416 delete m_hoverCache;
417 m_hoverCache = 0;
418 }
419
420 void KItemListWidget::drawTextBackground(QPainter* painter)
421 {
422 const qreal opacity = painter->opacity();
423
424 QRectF textBounds = textRect();
425 const qreal marginDiff = m_styleOption.margin / 2;
426 textBounds.adjust(marginDiff, marginDiff, -marginDiff, -marginDiff);
427 painter->setOpacity(opacity * 0.1);
428 painter->setPen(Qt::NoPen);
429 painter->setBrush(m_styleOption.palette.text());
430 painter->drawRoundedRect(textBounds, 4, 4);
431
432 painter->setOpacity(opacity);
433 }
434
435 #include "kitemlistwidget.moc"