]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistwidget.cpp
Details view: Optionally remember user changed column-widths
[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_alternateBackground(false),
44 m_enabledSelectionToggle(false),
45 m_data(),
46 m_visibleRoles(),
47 m_columnWidths(),
48 m_styleOption(),
49 m_siblingsInfo(),
50 m_hoverOpacity(0),
51 m_hoverCache(0),
52 m_hoverAnimation(0),
53 m_selectionToggle(0)
54 {
55 }
56
57 KItemListWidget::~KItemListWidget()
58 {
59 clearHoverCache();
60 }
61
62 void KItemListWidget::setIndex(int index)
63 {
64 if (m_index != index) {
65 delete m_selectionToggle;
66 m_selectionToggle = 0;
67
68 if (m_hoverAnimation) {
69 m_hoverAnimation->stop();
70 m_hoverOpacity = 0;
71 }
72 clearHoverCache();
73
74 m_index = index;
75 }
76 }
77
78 int KItemListWidget::index() const
79 {
80 return m_index;
81 }
82
83 void KItemListWidget::setData(const QHash<QByteArray, QVariant>& data,
84 const QSet<QByteArray>& roles)
85 {
86 clearHoverCache();
87 if (roles.isEmpty()) {
88 m_data = data;
89 dataChanged(m_data);
90 } else {
91 foreach (const QByteArray& role, roles) {
92 m_data[role] = data[role];
93 }
94 dataChanged(m_data, roles);
95 }
96 update();
97 }
98
99 QHash<QByteArray, QVariant> KItemListWidget::data() const
100 {
101 return m_data;
102 }
103
104 void KItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
105 {
106 Q_UNUSED(option);
107
108 if (m_alternateBackground) {
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
171 visibleRolesChanged(roles, previousRoles);
172 update();
173 }
174
175 QList<QByteArray> KItemListWidget::visibleRoles() const
176 {
177 return m_visibleRoles;
178 }
179
180
181 void KItemListWidget::setColumnWidth(const QByteArray& role, qreal width)
182 {
183 if (m_columnWidths.value(role) != width) {
184 const qreal previousWidth = width;
185 m_columnWidths.insert(role, width);
186 columnWidthChanged(role, width, previousWidth);
187 update();
188 }
189 }
190
191 qreal KItemListWidget::columnWidth(const QByteArray& role) const
192 {
193 return m_columnWidths.value(role);
194 }
195
196 void KItemListWidget::setStyleOption(const KItemListStyleOption& option)
197 {
198 const KItemListStyleOption previous = m_styleOption;
199 clearHoverCache();
200 m_styleOption = option;
201
202 styleOptionChanged(option, previous);
203 update();
204 }
205
206 const KItemListStyleOption& KItemListWidget::styleOption() const
207 {
208 return m_styleOption;
209 }
210
211 void KItemListWidget::setSelected(bool selected)
212 {
213 if (m_selected != selected) {
214 m_selected = selected;
215 if (m_selectionToggle) {
216 m_selectionToggle->setChecked(selected);
217 }
218 selectedChanged(selected);
219 update();
220 }
221 }
222
223 bool KItemListWidget::isSelected() const
224 {
225 return m_selected;
226 }
227
228 void KItemListWidget::setCurrent(bool current)
229 {
230 if (m_current != current) {
231 m_current = current;
232 currentChanged(current);
233 update();
234 }
235 }
236
237 bool KItemListWidget::isCurrent() const
238 {
239 return m_current;
240 }
241
242 void KItemListWidget::setHovered(bool hovered)
243 {
244 if (hovered == m_hovered) {
245 return;
246 }
247
248 m_hovered = hovered;
249
250 if (!m_hoverAnimation) {
251 m_hoverAnimation = new QPropertyAnimation(this, "hoverOpacity", this);
252 const int duration = (KGlobalSettings::graphicEffectsLevel() == KGlobalSettings::NoEffects) ? 1 : 200;
253 m_hoverAnimation->setDuration(duration);
254 connect(m_hoverAnimation, SIGNAL(finished()), this, SLOT(slotHoverAnimationFinished()));
255 }
256 m_hoverAnimation->stop();
257
258 if (hovered) {
259 const qreal startValue = qMax(hoverOpacity(), qreal(0.1));
260 m_hoverAnimation->setStartValue(startValue);
261 m_hoverAnimation->setEndValue(1.0);
262 if (m_enabledSelectionToggle && !(QApplication::mouseButtons() & Qt::LeftButton)) {
263 initializeSelectionToggle();
264 }
265 } else {
266 m_hoverAnimation->setStartValue(hoverOpacity());
267 m_hoverAnimation->setEndValue(0.0);
268 }
269
270 m_hoverAnimation->start();
271
272 hoveredChanged(hovered);
273 update();
274 }
275
276 bool KItemListWidget::isHovered() const
277 {
278 return m_hovered;
279 }
280
281 void KItemListWidget::setAlternateBackground(bool enable)
282 {
283 if (m_alternateBackground != enable) {
284 m_alternateBackground = enable;
285 alternateBackgroundChanged(enable);
286 update();
287 }
288 }
289
290 bool KItemListWidget::alternateBackground() const
291 {
292 return m_alternateBackground;
293 }
294
295 void KItemListWidget::setEnabledSelectionToggle(bool enable)
296 {
297 if (m_enabledSelectionToggle != enable) {
298 m_enabledSelectionToggle = enable;
299 update();
300 }
301 }
302
303 bool KItemListWidget::enabledSelectionToggle() const
304 {
305 return m_enabledSelectionToggle;
306 }
307
308 void KItemListWidget::setSiblingsInformation(const QBitArray& siblings)
309 {
310 const QBitArray previous = m_siblingsInfo;
311 m_siblingsInfo = siblings;
312 siblingsInformationChanged(m_siblingsInfo, previous);
313 update();
314 }
315
316 QBitArray KItemListWidget::siblingsInformation() const
317 {
318 return m_siblingsInfo;
319 }
320
321 bool KItemListWidget::contains(const QPointF& point) const
322 {
323 if (!QGraphicsWidget::contains(point)) {
324 return false;
325 }
326
327 return iconRect().contains(point) ||
328 textRect().contains(point) ||
329 expansionToggleRect().contains(point) ||
330 selectionToggleRect().contains(point);
331 }
332
333 QRectF KItemListWidget::selectionToggleRect() const
334 {
335 return QRectF();
336 }
337
338 QRectF KItemListWidget::expansionToggleRect() const
339 {
340 return QRectF();
341 }
342
343 void KItemListWidget::dataChanged(const QHash<QByteArray, QVariant>& current,
344 const QSet<QByteArray>& roles)
345 {
346 Q_UNUSED(current);
347 Q_UNUSED(roles);
348 }
349
350 void KItemListWidget::visibleRolesChanged(const QList<QByteArray>& current,
351 const QList<QByteArray>& previous)
352 {
353 Q_UNUSED(current);
354 Q_UNUSED(previous);
355 }
356
357 void KItemListWidget::columnWidthChanged(const QByteArray& role,
358 qreal current,
359 qreal previous)
360 {
361 Q_UNUSED(role);
362 Q_UNUSED(current);
363 Q_UNUSED(previous);
364 }
365
366 void KItemListWidget::styleOptionChanged(const KItemListStyleOption& current,
367 const KItemListStyleOption& previous)
368 {
369 Q_UNUSED(current);
370 Q_UNUSED(previous);
371 }
372
373 void KItemListWidget::currentChanged(bool current)
374 {
375 Q_UNUSED(current);
376 }
377
378 void KItemListWidget::selectedChanged(bool selected)
379 {
380 Q_UNUSED(selected);
381 }
382
383 void KItemListWidget::hoveredChanged(bool hovered)
384 {
385 Q_UNUSED(hovered);
386 }
387
388 void KItemListWidget::alternateBackgroundChanged(bool enabled)
389 {
390 Q_UNUSED(enabled);
391 }
392
393 void KItemListWidget::siblingsInformationChanged(const QBitArray& current, const QBitArray& previous)
394 {
395 Q_UNUSED(current);
396 Q_UNUSED(previous);
397 }
398
399 void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent* event)
400 {
401 QGraphicsWidget::resizeEvent(event);
402 clearHoverCache();
403 }
404
405 qreal KItemListWidget::hoverOpacity() const
406 {
407 return m_hoverOpacity;
408 }
409
410 void KItemListWidget::slotHoverAnimationFinished()
411 {
412 if (!m_hovered) {
413 delete m_selectionToggle;
414 m_selectionToggle = 0;
415 }
416 }
417
418 void KItemListWidget::initializeSelectionToggle()
419 {
420 Q_ASSERT(m_enabledSelectionToggle);
421
422 if (!m_selectionToggle) {
423 m_selectionToggle = new KItemListSelectionToggle(this);
424 }
425
426 const QRectF toggleRect = selectionToggleRect();
427 m_selectionToggle->setPos(toggleRect.topLeft());
428 m_selectionToggle->resize(toggleRect.size());
429
430 m_selectionToggle->setChecked(isSelected());
431 }
432
433 void KItemListWidget::setHoverOpacity(qreal opacity)
434 {
435 m_hoverOpacity = opacity;
436 if (m_selectionToggle) {
437 m_selectionToggle->setOpacity(opacity);
438 }
439
440 if (m_hoverOpacity <= 0.0) {
441 delete m_hoverCache;
442 m_hoverCache = 0;
443 }
444
445 update();
446 }
447
448 void KItemListWidget::clearHoverCache()
449 {
450 delete m_hoverCache;
451 m_hoverCache = 0;
452 }
453
454 void KItemListWidget::drawItemStyleOption(QPainter* painter, QWidget* widget, QStyle::State styleState)
455 {
456 const QRect textBounds = textRect().toRect();
457
458 QStyleOptionViewItemV4 viewItemOption;
459 viewItemOption.initFrom(widget);
460 viewItemOption.state = styleState;
461 viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
462 viewItemOption.showDecorationSelected = true;
463 viewItemOption.rect = textBounds.adjusted(1, 0, -1, 0);
464 widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget);
465 }
466
467 #include "kitemlistwidget.moc"