]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistwidget.cpp
Fix selection rect after porting from QFontMetrics::width()
[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 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21
22 #include "kitemlistwidget.h"
23
24 #include "kitemlistview.h"
25 #include "private/kitemlistselectiontoggle.h"
26
27 #include <QApplication>
28 #include <QPainter>
29 #include <QPropertyAnimation>
30 #include <QStyleOption>
31
32 KItemListWidgetInformant::KItemListWidgetInformant()
33 {
34 }
35
36 KItemListWidgetInformant::~KItemListWidgetInformant()
37 {
38 }
39
40 KItemListWidget::KItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent) :
41 QGraphicsWidget(parent),
42 m_informant(informant),
43 m_index(-1),
44 m_selected(false),
45 m_current(false),
46 m_hovered(false),
47 m_alternateBackground(false),
48 m_enabledSelectionToggle(false),
49 m_data(),
50 m_visibleRoles(),
51 m_columnWidths(),
52 m_styleOption(),
53 m_siblingsInfo(),
54 m_hoverOpacity(0),
55 m_hoverCache(nullptr),
56 m_hoverAnimation(nullptr),
57 m_selectionToggle(nullptr),
58 m_editedRole()
59 {
60 }
61
62 KItemListWidget::~KItemListWidget()
63 {
64 clearHoverCache();
65 }
66
67 void KItemListWidget::setIndex(int index)
68 {
69 if (m_index != index) {
70 delete m_selectionToggle;
71 m_selectionToggle = nullptr;
72
73 if (m_hoverAnimation) {
74 m_hoverAnimation->stop();
75 m_hoverOpacity = 0;
76 }
77 clearHoverCache();
78
79 m_index = index;
80 }
81 }
82
83 int KItemListWidget::index() const
84 {
85 return m_index;
86 }
87
88 void KItemListWidget::setData(const QHash<QByteArray, QVariant>& data,
89 const QSet<QByteArray>& roles)
90 {
91 clearHoverCache();
92 if (roles.isEmpty()) {
93 m_data = data;
94 dataChanged(m_data);
95 } else {
96 foreach (const QByteArray& role, roles) {
97 m_data[role] = data[role];
98 }
99 dataChanged(m_data, roles);
100 }
101 update();
102 }
103
104 QHash<QByteArray, QVariant> KItemListWidget::data() const
105 {
106 return m_data;
107 }
108
109 void KItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
110 {
111 Q_UNUSED(option)
112
113 if (m_alternateBackground) {
114 const QColor backgroundColor = m_styleOption.palette.color(QPalette::AlternateBase);
115 const QRectF backgroundRect(0, 0, size().width(), size().height());
116 painter->fillRect(backgroundRect, backgroundColor);
117 }
118
119 if (m_selected && m_editedRole.isEmpty()) {
120 const QStyle::State activeState(isActiveWindow() ? QStyle::State_Active : 0);
121 drawItemStyleOption(painter, widget, activeState |
122 QStyle::State_Enabled |
123 QStyle::State_Selected |
124 QStyle::State_Item);
125 }
126
127 if (m_current && m_editedRole.isEmpty()) {
128 QStyleOptionFocusRect focusRectOption;
129 initStyleOption(&focusRectOption);
130 focusRectOption.rect = textFocusRect().toRect();
131 focusRectOption.state = QStyle::State_Enabled | QStyle::State_Item | QStyle::State_KeyboardFocusChange;
132 if (m_selected) {
133 focusRectOption.state |= QStyle::State_Selected;
134 }
135
136 style()->drawPrimitive(QStyle::PE_FrameFocusRect, &focusRectOption, painter, widget);
137 }
138
139 if (m_hoverOpacity > 0.0) {
140 if (!m_hoverCache) {
141 // Initialize the m_hoverCache pixmap to improve the drawing performance
142 // when fading the hover background
143 m_hoverCache = new QPixmap(size().toSize());
144 m_hoverCache->fill(Qt::transparent);
145
146 QPainter pixmapPainter(m_hoverCache);
147 const QStyle::State activeState(isActiveWindow() ? QStyle::State_Active : 0);
148 drawItemStyleOption(&pixmapPainter, widget, activeState |
149 QStyle::State_Enabled |
150 QStyle::State_MouseOver |
151 QStyle::State_Item);
152 }
153
154 const qreal opacity = painter->opacity();
155 painter->setOpacity(m_hoverOpacity * opacity);
156 painter->drawPixmap(0, 0, *m_hoverCache);
157 painter->setOpacity(opacity);
158 }
159 }
160
161 void KItemListWidget::setVisibleRoles(const QList<QByteArray>& roles)
162 {
163 const QList<QByteArray> previousRoles = m_visibleRoles;
164 m_visibleRoles = roles;
165
166 visibleRolesChanged(roles, previousRoles);
167 update();
168 }
169
170 QList<QByteArray> KItemListWidget::visibleRoles() const
171 {
172 return m_visibleRoles;
173 }
174
175
176 void KItemListWidget::setColumnWidth(const QByteArray& role, qreal width)
177 {
178 const qreal previousWidth = m_columnWidths.value(role);
179 if (previousWidth != width) {
180 m_columnWidths.insert(role, width);
181 columnWidthChanged(role, width, previousWidth);
182 update();
183 }
184 }
185
186 qreal KItemListWidget::columnWidth(const QByteArray& role) const
187 {
188 return m_columnWidths.value(role);
189 }
190
191 void KItemListWidget::setStyleOption(const KItemListStyleOption& option)
192 {
193 if (m_styleOption == option) {
194 return;
195 }
196
197 const KItemListStyleOption previous = m_styleOption;
198 clearHoverCache();
199 m_styleOption = option;
200 styleOptionChanged(option, previous);
201 update();
202 }
203
204 const KItemListStyleOption& KItemListWidget::styleOption() const
205 {
206 return m_styleOption;
207 }
208
209 void KItemListWidget::setSelected(bool selected)
210 {
211 if (m_selected != selected) {
212 m_selected = selected;
213 if (m_selectionToggle) {
214 m_selectionToggle->setChecked(selected);
215 }
216 selectedChanged(selected);
217 update();
218 }
219 }
220
221 bool KItemListWidget::isSelected() const
222 {
223 return m_selected;
224 }
225
226 void KItemListWidget::setCurrent(bool current)
227 {
228 if (m_current != current) {
229 m_current = current;
230 currentChanged(current);
231 update();
232 }
233 }
234
235 bool KItemListWidget::isCurrent() const
236 {
237 return m_current;
238 }
239
240 void KItemListWidget::setHovered(bool hovered)
241 {
242 if (hovered == m_hovered) {
243 return;
244 }
245
246 m_hovered = hovered;
247
248 if (!m_hoverAnimation) {
249 m_hoverAnimation = new QPropertyAnimation(this, "hoverOpacity", this);
250 const int duration = style()->styleHint(QStyle::SH_Widget_Animate) ? 200 : 1;
251 m_hoverAnimation->setDuration(duration);
252 connect(m_hoverAnimation, &QPropertyAnimation::finished, this, &KItemListWidget::slotHoverAnimationFinished);
253 }
254 m_hoverAnimation->stop();
255
256 if (hovered) {
257 const qreal startValue = qMax(hoverOpacity(), qreal(0.1));
258 m_hoverAnimation->setStartValue(startValue);
259 m_hoverAnimation->setEndValue(1.0);
260 if (m_enabledSelectionToggle && !(QApplication::mouseButtons() & Qt::LeftButton)) {
261 initializeSelectionToggle();
262 }
263 } else {
264 m_hoverAnimation->setStartValue(hoverOpacity());
265 m_hoverAnimation->setEndValue(0.0);
266 }
267
268 m_hoverAnimation->start();
269
270 hoveredChanged(hovered);
271 update();
272 }
273
274 bool KItemListWidget::isHovered() const
275 {
276 return m_hovered;
277 }
278
279 void KItemListWidget::setHoverPosition(const QPointF& pos)
280 {
281 if (m_selectionToggle) {
282 m_selectionToggle->setHovered(selectionToggleRect().contains(pos));
283 }
284 }
285
286 void KItemListWidget::setAlternateBackground(bool enable)
287 {
288 if (m_alternateBackground != enable) {
289 m_alternateBackground = enable;
290 alternateBackgroundChanged(enable);
291 update();
292 }
293 }
294
295 bool KItemListWidget::alternateBackground() const
296 {
297 return m_alternateBackground;
298 }
299
300 void KItemListWidget::setEnabledSelectionToggle(bool enable)
301 {
302 if (m_enabledSelectionToggle != enable) {
303 m_enabledSelectionToggle = enable;
304 update();
305 }
306 }
307
308 bool KItemListWidget::enabledSelectionToggle() const
309 {
310 return m_enabledSelectionToggle;
311 }
312
313 void KItemListWidget::setSiblingsInformation(const QBitArray& siblings)
314 {
315 const QBitArray previous = m_siblingsInfo;
316 m_siblingsInfo = siblings;
317 siblingsInformationChanged(m_siblingsInfo, previous);
318 update();
319 }
320
321 QBitArray KItemListWidget::siblingsInformation() const
322 {
323 return m_siblingsInfo;
324 }
325
326 void KItemListWidget::setEditedRole(const QByteArray& role)
327 {
328 if (m_editedRole != role) {
329 const QByteArray previous = m_editedRole;
330 m_editedRole = role;
331 editedRoleChanged(role, previous);
332 }
333 }
334
335 QByteArray KItemListWidget::editedRole() const
336 {
337 return m_editedRole;
338 }
339
340 bool KItemListWidget::contains(const QPointF& point) const
341 {
342 if (!QGraphicsWidget::contains(point)) {
343 return false;
344 }
345
346 return iconRect().contains(point) ||
347 textRect().contains(point) ||
348 expansionToggleRect().contains(point) ||
349 selectionToggleRect().contains(point);
350 }
351
352 QRectF KItemListWidget::textFocusRect() const
353 {
354 return textRect();
355 }
356
357 QRectF KItemListWidget::selectionToggleRect() const
358 {
359 return QRectF();
360 }
361
362 QRectF KItemListWidget::expansionToggleRect() const
363 {
364 return QRectF();
365 }
366
367 QPixmap KItemListWidget::createDragPixmap(const QStyleOptionGraphicsItem* option,
368 QWidget* widget)
369 {
370 QPixmap pixmap(size().toSize() * widget->devicePixelRatio());
371 pixmap.setDevicePixelRatio(widget->devicePixelRatio());
372 pixmap.fill(Qt::transparent);
373
374 QPainter painter(&pixmap);
375
376 const bool oldAlternateBackground = m_alternateBackground;
377 const bool wasSelected = m_selected;
378 const bool wasHovered = m_hovered;
379
380 setAlternateBackground(false);
381 setHovered(false);
382
383 paint(&painter, option, widget);
384
385 setAlternateBackground(oldAlternateBackground);
386 setSelected(wasSelected);
387 setHovered(wasHovered);
388
389 return pixmap;
390 }
391
392 void KItemListWidget::dataChanged(const QHash<QByteArray, QVariant>& current,
393 const QSet<QByteArray>& roles)
394 {
395 Q_UNUSED(current)
396 Q_UNUSED(roles)
397 }
398
399 void KItemListWidget::visibleRolesChanged(const QList<QByteArray>& current,
400 const QList<QByteArray>& previous)
401 {
402 Q_UNUSED(current)
403 Q_UNUSED(previous)
404 }
405
406 void KItemListWidget::columnWidthChanged(const QByteArray& role,
407 qreal current,
408 qreal previous)
409 {
410 Q_UNUSED(role)
411 Q_UNUSED(current)
412 Q_UNUSED(previous)
413 }
414
415 void KItemListWidget::styleOptionChanged(const KItemListStyleOption& current,
416 const KItemListStyleOption& previous)
417 {
418 Q_UNUSED(current)
419 Q_UNUSED(previous)
420 }
421
422 void KItemListWidget::currentChanged(bool current)
423 {
424 Q_UNUSED(current)
425 }
426
427 void KItemListWidget::selectedChanged(bool selected)
428 {
429 Q_UNUSED(selected)
430 }
431
432 void KItemListWidget::hoveredChanged(bool hovered)
433 {
434 Q_UNUSED(hovered)
435 }
436
437 void KItemListWidget::alternateBackgroundChanged(bool enabled)
438 {
439 Q_UNUSED(enabled)
440 }
441
442 void KItemListWidget::siblingsInformationChanged(const QBitArray& current, const QBitArray& previous)
443 {
444 Q_UNUSED(current)
445 Q_UNUSED(previous)
446 }
447
448 void KItemListWidget::editedRoleChanged(const QByteArray& current, const QByteArray& previous)
449 {
450 Q_UNUSED(current)
451 Q_UNUSED(previous)
452 }
453
454 void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent* event)
455 {
456 QGraphicsWidget::resizeEvent(event);
457 clearHoverCache();
458
459 if (m_selectionToggle) {
460 const QRectF& toggleRect = selectionToggleRect();
461 m_selectionToggle->setPos(toggleRect.topLeft());
462 m_selectionToggle->resize(toggleRect.size());
463 }
464 }
465
466 qreal KItemListWidget::hoverOpacity() const
467 {
468 return m_hoverOpacity;
469 }
470
471 void KItemListWidget::slotHoverAnimationFinished()
472 {
473 if (!m_hovered && m_selectionToggle) {
474 m_selectionToggle->deleteLater();
475 m_selectionToggle = nullptr;
476 }
477 }
478
479 void KItemListWidget::initializeSelectionToggle()
480 {
481 Q_ASSERT(m_enabledSelectionToggle);
482
483 if (!m_selectionToggle) {
484 m_selectionToggle = new KItemListSelectionToggle(this);
485 }
486
487 const QRectF toggleRect = selectionToggleRect();
488 m_selectionToggle->setPos(toggleRect.topLeft());
489 m_selectionToggle->resize(toggleRect.size());
490
491 m_selectionToggle->setChecked(isSelected());
492 }
493
494 void KItemListWidget::setHoverOpacity(qreal opacity)
495 {
496 m_hoverOpacity = opacity;
497 if (m_selectionToggle) {
498 m_selectionToggle->setOpacity(opacity);
499 }
500
501 if (m_hoverOpacity <= 0.0) {
502 delete m_hoverCache;
503 m_hoverCache = nullptr;
504 }
505
506 update();
507 }
508
509 void KItemListWidget::clearHoverCache()
510 {
511 delete m_hoverCache;
512 m_hoverCache = nullptr;
513 }
514
515 void KItemListWidget::drawItemStyleOption(QPainter* painter, QWidget* widget, QStyle::State styleState)
516 {
517 QStyleOptionViewItem viewItemOption;
518 initStyleOption(&viewItemOption);
519 viewItemOption.state = styleState;
520 viewItemOption.viewItemPosition = QStyleOptionViewItem::OnlyOne;
521 viewItemOption.showDecorationSelected = true;
522 viewItemOption.rect = selectionRect().toRect();
523 style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget);
524 }
525