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