]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistwidget.cpp
Icon-rectangle and selection-toggle optimizations
[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_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 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
126 const QRect iconBounds = iconRect().toRect();
127 const QRect textBounds = textRect().toRect();
128 if (iconBounds.bottom() > textBounds.top()) {
129 focusRectOption.rect = textBounds;
130 } else {
131 // See KItemListWidget::drawItemStyleOption(): The selection rectangle
132 // gets decreased.
133 focusRectOption.rect = textBounds.adjusted(1, 1, -1, -1);
134 }
135
136 focusRectOption.state = QStyle::State_Enabled | QStyle::State_Item | QStyle::State_KeyboardFocusChange;
137 if (m_selected) {
138 focusRectOption.state |= QStyle::State_Selected;
139 }
140
141 style()->drawPrimitive(QStyle::PE_FrameFocusRect, &focusRectOption, 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 const int duration = (KGlobalSettings::graphicEffectsLevel() == KGlobalSettings::NoEffects) ? 1 : 200;
248 m_hoverAnimation->setDuration(duration);
249 connect(m_hoverAnimation, SIGNAL(finished()), this, SLOT(slotHoverAnimationFinished()));
250 }
251 m_hoverAnimation->stop();
252
253 if (hovered) {
254 const qreal startValue = qMax(hoverOpacity(), qreal(0.1));
255 m_hoverAnimation->setStartValue(startValue);
256 m_hoverAnimation->setEndValue(1.0);
257 if (m_enabledSelectionToggle && !(QApplication::mouseButtons() & Qt::LeftButton)) {
258 initializeSelectionToggle();
259 }
260 } else {
261 m_hoverAnimation->setStartValue(hoverOpacity());
262 m_hoverAnimation->setEndValue(0.0);
263 }
264
265 m_hoverAnimation->start();
266
267 hoveredChanged(hovered);
268
269 update();
270 }
271
272 bool KItemListWidget::isHovered() const
273 {
274 return m_hovered;
275 }
276
277 void KItemListWidget::setAlternatingBackgroundColors(bool enable)
278 {
279 if (m_alternatingBackgroundColors != enable) {
280 m_alternatingBackgroundColors = enable;
281 alternatingBackgroundColorsChanged(enable);
282 update();
283 }
284 }
285
286 bool KItemListWidget::alternatingBackgroundColors() const
287 {
288 return m_alternatingBackgroundColors;
289 }
290
291 void KItemListWidget::setEnabledSelectionToggle(bool enable)
292 {
293 if (m_enabledSelectionToggle != enable) {
294 m_enabledSelectionToggle = enable;
295 update();
296 }
297 }
298
299 bool KItemListWidget::enabledSelectionToggle() const
300 {
301 return m_enabledSelectionToggle;
302 }
303
304 bool KItemListWidget::contains(const QPointF& point) const
305 {
306 if (!QGraphicsWidget::contains(point)) {
307 return false;
308 }
309
310 return iconRect().contains(point) ||
311 textRect().contains(point) ||
312 expansionToggleRect().contains(point) ||
313 selectionToggleRect().contains(point);
314 }
315
316 QRectF KItemListWidget::selectionToggleRect() const
317 {
318 return QRectF();
319 }
320
321 QRectF KItemListWidget::expansionToggleRect() const
322 {
323 return QRectF();
324 }
325
326 void KItemListWidget::dataChanged(const QHash<QByteArray, QVariant>& current,
327 const QSet<QByteArray>& roles)
328 {
329 Q_UNUSED(current);
330 Q_UNUSED(roles);
331 update();
332 }
333
334 void KItemListWidget::visibleRolesChanged(const QList<QByteArray>& current,
335 const QList<QByteArray>& previous)
336 {
337 Q_UNUSED(current);
338 Q_UNUSED(previous);
339 update();
340 }
341
342 void KItemListWidget::visibleRolesSizesChanged(const QHash<QByteArray, QSizeF>& current,
343 const QHash<QByteArray, QSizeF>& previous)
344 {
345 Q_UNUSED(current);
346 Q_UNUSED(previous);
347 update();
348 }
349
350 void KItemListWidget::styleOptionChanged(const KItemListStyleOption& current,
351 const KItemListStyleOption& previous)
352 {
353 Q_UNUSED(current);
354 Q_UNUSED(previous);
355 update();
356 }
357
358 void KItemListWidget::currentChanged(bool current)
359 {
360 Q_UNUSED(current);
361 }
362
363 void KItemListWidget::selectedChanged(bool selected)
364 {
365 Q_UNUSED(selected);
366 }
367
368 void KItemListWidget::hoveredChanged(bool hovered)
369 {
370 Q_UNUSED(hovered);
371 }
372
373 void KItemListWidget::alternatingBackgroundColorsChanged(bool enabled)
374 {
375 Q_UNUSED(enabled);
376 }
377
378 void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent* event)
379 {
380 QGraphicsWidget::resizeEvent(event);
381 clearHoverCache();
382 }
383
384 qreal KItemListWidget::hoverOpacity() const
385 {
386 return m_hoverOpacity;
387 }
388
389 void KItemListWidget::slotHoverAnimationFinished()
390 {
391 if (!m_hovered) {
392 delete m_selectionToggle;
393 m_selectionToggle = 0;
394 }
395 }
396
397 void KItemListWidget::initializeSelectionToggle()
398 {
399 Q_ASSERT(m_enabledSelectionToggle);
400
401 if (!m_selectionToggle) {
402 m_selectionToggle = new KItemListSelectionToggle(this);
403 }
404
405 const QRectF toggleRect = selectionToggleRect();
406 m_selectionToggle->setPos(toggleRect.topLeft());
407 m_selectionToggle->resize(toggleRect.size());
408
409 m_selectionToggle->setChecked(isSelected());
410 }
411
412 void KItemListWidget::setHoverOpacity(qreal opacity)
413 {
414 m_hoverOpacity = opacity;
415 if (m_selectionToggle) {
416 m_selectionToggle->setOpacity(opacity);
417 }
418
419 if (m_hoverOpacity <= 0.0) {
420 delete m_hoverCache;
421 m_hoverCache = 0;
422 }
423
424 update();
425 }
426
427 void KItemListWidget::clearHoverCache()
428 {
429 delete m_hoverCache;
430 m_hoverCache = 0;
431 }
432
433 void KItemListWidget::drawItemStyleOption(QPainter* painter, QWidget* widget, QStyle::State styleState)
434 {
435 const QRect textBounds = textRect().toRect();
436
437 QStyleOptionViewItemV4 viewItemOption;
438 viewItemOption.initFrom(widget);
439 viewItemOption.state = styleState;
440 viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
441 viewItemOption.showDecorationSelected = true;
442 viewItemOption.rect = textBounds;
443 widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget);
444 }
445
446 #include "kitemlistwidget.moc"