]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistwidget.cpp
Inline renaming fixes
[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 "kitemlistview.h"
26 #include "kitemmodelbase.h"
27
28 #include "private/kitemlistselectiontoggle.h"
29
30 #include <KDebug>
31
32 #include <KGlobalSettings>
33 #include <QApplication>
34 #include <QPainter>
35 #include <QPropertyAnimation>
36 #include <QStyleOption>
37
38 KItemListWidget::KItemListWidget(QGraphicsItem* parent) :
39 QGraphicsWidget(parent, 0),
40 m_index(-1),
41 m_selected(false),
42 m_current(false),
43 m_hovered(false),
44 m_alternateBackground(false),
45 m_enabledSelectionToggle(false),
46 m_data(),
47 m_visibleRoles(),
48 m_columnWidths(),
49 m_styleOption(),
50 m_siblingsInfo(),
51 m_hoverOpacity(0),
52 m_hoverCache(0),
53 m_hoverAnimation(0),
54 m_selectionToggle(0),
55 m_editedRole()
56 {
57 }
58
59 KItemListWidget::~KItemListWidget()
60 {
61 clearHoverCache();
62 }
63
64 void KItemListWidget::setIndex(int index)
65 {
66 if (m_index != index) {
67 delete m_selectionToggle;
68 m_selectionToggle = 0;
69
70 if (m_hoverAnimation) {
71 m_hoverAnimation->stop();
72 m_hoverOpacity = 0;
73 }
74 clearHoverCache();
75
76 m_index = index;
77 }
78 }
79
80 int KItemListWidget::index() const
81 {
82 return m_index;
83 }
84
85 void KItemListWidget::setData(const QHash<QByteArray, QVariant>& data,
86 const QSet<QByteArray>& roles)
87 {
88 clearHoverCache();
89 if (roles.isEmpty()) {
90 m_data = data;
91 dataChanged(m_data);
92 } else {
93 foreach (const QByteArray& role, roles) {
94 m_data[role] = data[role];
95 }
96 dataChanged(m_data, roles);
97 }
98 update();
99 }
100
101 QHash<QByteArray, QVariant> KItemListWidget::data() const
102 {
103 return m_data;
104 }
105
106 void KItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
107 {
108 Q_UNUSED(option);
109
110 if (m_alternateBackground) {
111 const QColor backgroundColor = m_styleOption.palette.color(QPalette::AlternateBase);
112 const QRectF backgroundRect(0, 0, size().width(), size().height());
113 painter->fillRect(backgroundRect, backgroundColor);
114 }
115
116 if (m_selected && m_editedRole.isEmpty()) {
117 const QStyle::State activeState(isActiveWindow() ? QStyle::State_Active : 0);
118 drawItemStyleOption(painter, widget, activeState |
119 QStyle::State_Enabled |
120 QStyle::State_Selected |
121 QStyle::State_Item);
122 }
123
124 if (m_current && m_editedRole.isEmpty()) {
125 QStyleOptionFocusRect focusRectOption;
126 focusRectOption.initFrom(widget);
127 focusRectOption.rect = textFocusRect().toRect();
128 focusRectOption.state = QStyle::State_Enabled | QStyle::State_Item | QStyle::State_KeyboardFocusChange;
129 if (m_selected) {
130 focusRectOption.state |= QStyle::State_Selected;
131 }
132
133 style()->drawPrimitive(QStyle::PE_FrameFocusRect, &focusRectOption, painter, widget);
134 }
135
136 if (m_hoverOpacity > 0.0) {
137 if (!m_hoverCache) {
138 // Initialize the m_hoverCache pixmap to improve the drawing performance
139 // when fading the hover background
140 m_hoverCache = new QPixmap(size().toSize());
141 m_hoverCache->fill(Qt::transparent);
142
143 QPainter pixmapPainter(m_hoverCache);
144 const QStyle::State activeState(isActiveWindow() ? QStyle::State_Active : 0);
145 drawItemStyleOption(&pixmapPainter, widget, activeState |
146 QStyle::State_Enabled |
147 QStyle::State_MouseOver |
148 QStyle::State_Item);
149 }
150
151 const qreal opacity = painter->opacity();
152 painter->setOpacity(m_hoverOpacity * opacity);
153 painter->drawPixmap(0, 0, *m_hoverCache);
154 painter->setOpacity(opacity);
155 }
156 }
157
158 void KItemListWidget::setVisibleRoles(const QList<QByteArray>& roles)
159 {
160 const QList<QByteArray> previousRoles = m_visibleRoles;
161 m_visibleRoles = roles;
162
163 visibleRolesChanged(roles, previousRoles);
164 update();
165 }
166
167 QList<QByteArray> KItemListWidget::visibleRoles() const
168 {
169 return m_visibleRoles;
170 }
171
172
173 void KItemListWidget::setColumnWidth(const QByteArray& role, qreal width)
174 {
175 if (m_columnWidths.value(role) != width) {
176 const qreal previousWidth = width;
177 m_columnWidths.insert(role, width);
178 columnWidthChanged(role, width, previousWidth);
179 update();
180 }
181 }
182
183 qreal KItemListWidget::columnWidth(const QByteArray& role) const
184 {
185 return m_columnWidths.value(role);
186 }
187
188 void KItemListWidget::setStyleOption(const KItemListStyleOption& option)
189 {
190 const KItemListStyleOption previous = m_styleOption;
191 clearHoverCache();
192 m_styleOption = option;
193
194 styleOptionChanged(option, previous);
195 update();
196 }
197
198 const KItemListStyleOption& KItemListWidget::styleOption() const
199 {
200 return m_styleOption;
201 }
202
203 void KItemListWidget::setSelected(bool selected)
204 {
205 if (m_selected != selected) {
206 m_selected = selected;
207 if (m_selectionToggle) {
208 m_selectionToggle->setChecked(selected);
209 }
210 selectedChanged(selected);
211 update();
212 }
213 }
214
215 bool KItemListWidget::isSelected() const
216 {
217 return m_selected;
218 }
219
220 void KItemListWidget::setCurrent(bool current)
221 {
222 if (m_current != current) {
223 m_current = current;
224 currentChanged(current);
225 update();
226 }
227 }
228
229 bool KItemListWidget::isCurrent() const
230 {
231 return m_current;
232 }
233
234 void KItemListWidget::setHovered(bool hovered)
235 {
236 if (hovered == m_hovered) {
237 return;
238 }
239
240 m_hovered = hovered;
241
242 if (!m_hoverAnimation) {
243 m_hoverAnimation = new QPropertyAnimation(this, "hoverOpacity", this);
244 const int duration = (KGlobalSettings::graphicEffectsLevel() == KGlobalSettings::NoEffects) ? 1 : 200;
245 m_hoverAnimation->setDuration(duration);
246 connect(m_hoverAnimation, SIGNAL(finished()), this, SLOT(slotHoverAnimationFinished()));
247 }
248 m_hoverAnimation->stop();
249
250 if (hovered) {
251 const qreal startValue = qMax(hoverOpacity(), qreal(0.1));
252 m_hoverAnimation->setStartValue(startValue);
253 m_hoverAnimation->setEndValue(1.0);
254 if (m_enabledSelectionToggle && !(QApplication::mouseButtons() & Qt::LeftButton)) {
255 initializeSelectionToggle();
256 }
257 } else {
258 m_hoverAnimation->setStartValue(hoverOpacity());
259 m_hoverAnimation->setEndValue(0.0);
260 }
261
262 m_hoverAnimation->start();
263
264 hoveredChanged(hovered);
265 update();
266 }
267
268 bool KItemListWidget::isHovered() const
269 {
270 return m_hovered;
271 }
272
273 void KItemListWidget::setAlternateBackground(bool enable)
274 {
275 if (m_alternateBackground != enable) {
276 m_alternateBackground = enable;
277 alternateBackgroundChanged(enable);
278 update();
279 }
280 }
281
282 bool KItemListWidget::alternateBackground() const
283 {
284 return m_alternateBackground;
285 }
286
287 void KItemListWidget::setEnabledSelectionToggle(bool enable)
288 {
289 if (m_enabledSelectionToggle != enable) {
290 m_enabledSelectionToggle = enable;
291 update();
292 }
293 }
294
295 bool KItemListWidget::enabledSelectionToggle() const
296 {
297 return m_enabledSelectionToggle;
298 }
299
300 void KItemListWidget::setSiblingsInformation(const QBitArray& siblings)
301 {
302 const QBitArray previous = m_siblingsInfo;
303 m_siblingsInfo = siblings;
304 siblingsInformationChanged(m_siblingsInfo, previous);
305 update();
306 }
307
308 QBitArray KItemListWidget::siblingsInformation() const
309 {
310 return m_siblingsInfo;
311 }
312
313 void KItemListWidget::setEditedRole(const QByteArray& role)
314 {
315 if (m_editedRole != role) {
316 const QByteArray previous = m_editedRole;
317 m_editedRole = role;
318 editedRoleChanged(role, previous);
319 }
320 }
321
322 QByteArray KItemListWidget::editedRole() const
323 {
324 return m_editedRole;
325 }
326
327 bool KItemListWidget::contains(const QPointF& point) const
328 {
329 if (!QGraphicsWidget::contains(point)) {
330 return false;
331 }
332
333 return iconRect().contains(point) ||
334 textRect().contains(point) ||
335 expansionToggleRect().contains(point) ||
336 selectionToggleRect().contains(point);
337 }
338
339 QRectF KItemListWidget::textFocusRect() const
340 {
341 return textRect();
342 }
343
344 QRectF KItemListWidget::selectionToggleRect() const
345 {
346 return QRectF();
347 }
348
349 QRectF KItemListWidget::expansionToggleRect() const
350 {
351 return QRectF();
352 }
353
354 void KItemListWidget::dataChanged(const QHash<QByteArray, QVariant>& current,
355 const QSet<QByteArray>& roles)
356 {
357 Q_UNUSED(current);
358 Q_UNUSED(roles);
359 }
360
361 void KItemListWidget::visibleRolesChanged(const QList<QByteArray>& current,
362 const QList<QByteArray>& previous)
363 {
364 Q_UNUSED(current);
365 Q_UNUSED(previous);
366 }
367
368 void KItemListWidget::columnWidthChanged(const QByteArray& role,
369 qreal current,
370 qreal previous)
371 {
372 Q_UNUSED(role);
373 Q_UNUSED(current);
374 Q_UNUSED(previous);
375 }
376
377 void KItemListWidget::styleOptionChanged(const KItemListStyleOption& current,
378 const KItemListStyleOption& previous)
379 {
380 Q_UNUSED(current);
381 Q_UNUSED(previous);
382 }
383
384 void KItemListWidget::currentChanged(bool current)
385 {
386 Q_UNUSED(current);
387 }
388
389 void KItemListWidget::selectedChanged(bool selected)
390 {
391 Q_UNUSED(selected);
392 }
393
394 void KItemListWidget::hoveredChanged(bool hovered)
395 {
396 Q_UNUSED(hovered);
397 }
398
399 void KItemListWidget::alternateBackgroundChanged(bool enabled)
400 {
401 Q_UNUSED(enabled);
402 }
403
404 void KItemListWidget::siblingsInformationChanged(const QBitArray& current, const QBitArray& previous)
405 {
406 Q_UNUSED(current);
407 Q_UNUSED(previous);
408 }
409
410 void KItemListWidget::editedRoleChanged(const QByteArray& current, const QByteArray& previous)
411 {
412 Q_UNUSED(current);
413 Q_UNUSED(previous);
414 }
415
416 void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent* event)
417 {
418 QGraphicsWidget::resizeEvent(event);
419 clearHoverCache();
420 }
421
422 qreal KItemListWidget::hoverOpacity() const
423 {
424 return m_hoverOpacity;
425 }
426
427 void KItemListWidget::slotHoverAnimationFinished()
428 {
429 if (!m_hovered) {
430 delete m_selectionToggle;
431 m_selectionToggle = 0;
432 }
433 }
434
435 void KItemListWidget::initializeSelectionToggle()
436 {
437 Q_ASSERT(m_enabledSelectionToggle);
438
439 if (!m_selectionToggle) {
440 m_selectionToggle = new KItemListSelectionToggle(this);
441 }
442
443 const QRectF toggleRect = selectionToggleRect();
444 m_selectionToggle->setPos(toggleRect.topLeft());
445 m_selectionToggle->resize(toggleRect.size());
446
447 m_selectionToggle->setChecked(isSelected());
448 }
449
450 void KItemListWidget::setHoverOpacity(qreal opacity)
451 {
452 m_hoverOpacity = opacity;
453 if (m_selectionToggle) {
454 m_selectionToggle->setOpacity(opacity);
455 }
456
457 if (m_hoverOpacity <= 0.0) {
458 delete m_hoverCache;
459 m_hoverCache = 0;
460 }
461
462 update();
463 }
464
465 void KItemListWidget::clearHoverCache()
466 {
467 delete m_hoverCache;
468 m_hoverCache = 0;
469 }
470
471 void KItemListWidget::drawItemStyleOption(QPainter* painter, QWidget* widget, QStyle::State styleState)
472 {
473 QStyleOptionViewItemV4 viewItemOption;
474 viewItemOption.initFrom(widget);
475 viewItemOption.state = styleState;
476 viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
477 viewItemOption.showDecorationSelected = true;
478 viewItemOption.rect = textRect().toRect();
479 widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget);
480 }
481
482 #include "kitemlistwidget.moc"