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