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