]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistwidget.cpp
Merge branch 'release/21.12'
[dolphin.git] / src / kitemviews / kitemlistwidget.cpp
1 /*
2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
3 *
4 * Based on the Itemviews NG project from Trolltech Labs
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9 #include "kitemlistwidget.h"
10
11 #include "kitemlistview.h"
12 #include "private/kitemlistselectiontoggle.h"
13
14 #include <KConfigGroup>
15 #include <KSharedConfig>
16
17 #include <QApplication>
18 #include <QPainter>
19 #include <QPropertyAnimation>
20 #include <QStyleOption>
21
22 KItemListWidgetInformant::KItemListWidgetInformant()
23 {
24 }
25
26 KItemListWidgetInformant::~KItemListWidgetInformant()
27 {
28 }
29
30 KItemListWidget::KItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent) :
31 QGraphicsWidget(parent),
32 m_informant(informant),
33 m_index(-1),
34 m_selected(false),
35 m_current(false),
36 m_hovered(false),
37 m_expansionAreaHovered(false),
38 m_alternateBackground(false),
39 m_enabledSelectionToggle(false),
40 m_data(),
41 m_visibleRoles(),
42 m_columnWidths(),
43 m_styleOption(),
44 m_siblingsInfo(),
45 m_hoverOpacity(0),
46 m_hoverCache(nullptr),
47 m_hoverAnimation(nullptr),
48 m_hoverSequenceIndex(0),
49 m_selectionToggle(nullptr),
50 m_editedRole(),
51 m_iconSize(-1)
52 {
53 connect(&m_hoverSequenceTimer, &QTimer::timeout, this, &KItemListWidget::slotHoverSequenceTimerTimeout);
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 = nullptr;
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 for (const QByteArray& role : roles) {
91 m_data[role] = data[role];
92 }
93 dataChanged(m_data, roles);
94 }
95 update();
96 }
97
98 QHash<QByteArray, QVariant> KItemListWidget::data() const
99 {
100 return m_data;
101 }
102
103 void KItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
104 {
105 Q_UNUSED(option)
106
107 if (m_alternateBackground) {
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 && m_editedRole.isEmpty()) {
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 (m_current && m_editedRole.isEmpty()) {
122 QStyleOptionFocusRect focusRectOption;
123 initStyleOption(&focusRectOption);
124 focusRectOption.rect = textFocusRect().toRect();
125 focusRectOption.state = QStyle::State_Enabled | QStyle::State_Item | QStyle::State_KeyboardFocusChange;
126 if (m_selected) {
127 focusRectOption.state |= QStyle::State_Selected;
128 }
129
130 style()->drawPrimitive(QStyle::PE_FrameFocusRect, &focusRectOption, 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
160 visibleRolesChanged(roles, previousRoles);
161 update();
162 }
163
164 QList<QByteArray> KItemListWidget::visibleRoles() const
165 {
166 return m_visibleRoles;
167 }
168
169
170 void KItemListWidget::setColumnWidth(const QByteArray& role, qreal width)
171 {
172 const qreal previousWidth = m_columnWidths.value(role);
173 if (previousWidth != width) {
174 m_columnWidths.insert(role, width);
175 columnWidthChanged(role, width, previousWidth);
176 update();
177 }
178 }
179
180 qreal KItemListWidget::columnWidth(const QByteArray& role) const
181 {
182 return m_columnWidths.value(role);
183 }
184
185 qreal KItemListWidget::leadingPadding() const {
186 return m_leadingPadding;
187 }
188
189 void KItemListWidget::setLeadingPadding(qreal width) {
190 if (m_leadingPadding != width){
191 m_leadingPadding = width;
192 leadingPaddingChanged(width);
193 update();
194 }
195 }
196
197 void KItemListWidget::setStyleOption(const KItemListStyleOption& option)
198 {
199 if (m_styleOption == option) {
200 return;
201 }
202
203 const KItemListStyleOption previous = m_styleOption;
204 clearHoverCache();
205 m_styleOption = option;
206 styleOptionChanged(option, previous);
207 update();
208 }
209
210 const KItemListStyleOption& KItemListWidget::styleOption() const
211 {
212 return m_styleOption;
213 }
214
215 void KItemListWidget::setSelected(bool selected)
216 {
217 if (m_selected != selected) {
218 m_selected = selected;
219 if (m_selectionToggle) {
220 m_selectionToggle->setChecked(selected);
221 }
222 selectedChanged(selected);
223 update();
224 }
225 }
226
227 bool KItemListWidget::isSelected() const
228 {
229 return m_selected;
230 }
231
232 void KItemListWidget::setCurrent(bool current)
233 {
234 if (m_current != current) {
235 m_current = current;
236 currentChanged(current);
237 update();
238 }
239 }
240
241 bool KItemListWidget::isCurrent() const
242 {
243 return m_current;
244 }
245
246 void KItemListWidget::setHovered(bool hovered)
247 {
248 if (hovered == m_hovered) {
249 return;
250 }
251
252 m_hovered = hovered;
253
254 if (!m_hoverAnimation) {
255 m_hoverAnimation = new QPropertyAnimation(this, "hoverOpacity", this);
256 const int duration = style()->styleHint(QStyle::SH_Widget_Animate) ? 200 : 1;
257 m_hoverAnimation->setDuration(duration);
258 connect(m_hoverAnimation, &QPropertyAnimation::finished, this, &KItemListWidget::slotHoverAnimationFinished);
259 }
260 m_hoverAnimation->stop();
261
262 m_hoverSequenceIndex = 0;
263
264 if (hovered) {
265 const qreal startValue = qMax(hoverOpacity(), qreal(0.1));
266 m_hoverAnimation->setStartValue(startValue);
267 m_hoverAnimation->setEndValue(1.0);
268 if (m_enabledSelectionToggle && !(QApplication::mouseButtons() & Qt::LeftButton)) {
269 initializeSelectionToggle();
270 }
271
272 hoverSequenceStarted();
273
274 const KConfigGroup globalConfig(KSharedConfig::openConfig(), "PreviewSettings");
275 const int interval = globalConfig.readEntry("HoverSequenceInterval", 700);
276
277 m_hoverSequenceTimer.start(interval);
278 } else {
279 m_hoverAnimation->setStartValue(hoverOpacity());
280 m_hoverAnimation->setEndValue(0.0);
281
282 hoverSequenceEnded();
283 m_hoverSequenceTimer.stop();
284 }
285
286 m_hoverAnimation->start();
287
288 hoveredChanged(hovered);
289 update();
290 }
291
292 bool KItemListWidget::isHovered() const
293 {
294 return m_hovered;
295 }
296
297 void KItemListWidget::setExpansionAreaHovered(bool hovered)
298 {
299 if (hovered == m_expansionAreaHovered) {
300 return;
301 }
302 m_expansionAreaHovered = hovered;
303 update();
304 }
305
306 bool KItemListWidget::expansionAreaHovered() const
307 {
308 return m_expansionAreaHovered;
309 }
310
311 void KItemListWidget::setHoverPosition(const QPointF& pos)
312 {
313 if (m_selectionToggle) {
314 m_selectionToggle->setHovered(selectionToggleRect().contains(pos));
315 }
316 }
317
318 void KItemListWidget::setAlternateBackground(bool enable)
319 {
320 if (m_alternateBackground != enable) {
321 m_alternateBackground = enable;
322 alternateBackgroundChanged(enable);
323 update();
324 }
325 }
326
327 bool KItemListWidget::alternateBackground() const
328 {
329 return m_alternateBackground;
330 }
331
332 void KItemListWidget::setEnabledSelectionToggle(bool enable)
333 {
334 if (m_enabledSelectionToggle != enable) {
335 m_enabledSelectionToggle = enable;
336 update();
337 }
338 }
339
340 bool KItemListWidget::enabledSelectionToggle() const
341 {
342 return m_enabledSelectionToggle;
343 }
344
345 void KItemListWidget::setSiblingsInformation(const QBitArray& siblings)
346 {
347 const QBitArray previous = m_siblingsInfo;
348 m_siblingsInfo = siblings;
349 siblingsInformationChanged(m_siblingsInfo, previous);
350 update();
351 }
352
353 QBitArray KItemListWidget::siblingsInformation() const
354 {
355 return m_siblingsInfo;
356 }
357
358 void KItemListWidget::setEditedRole(const QByteArray& role)
359 {
360 if (m_editedRole != role) {
361 const QByteArray previous = m_editedRole;
362 m_editedRole = role;
363 editedRoleChanged(role, previous);
364 }
365 }
366
367 QByteArray KItemListWidget::editedRole() const
368 {
369 return m_editedRole;
370 }
371
372 void KItemListWidget::setIconSize(int iconSize)
373 {
374 if (m_iconSize != iconSize) {
375 const int previousIconSize = m_iconSize;
376 m_iconSize = iconSize;
377 iconSizeChanged(iconSize, previousIconSize);
378 }
379 }
380
381 int KItemListWidget::iconSize() const
382 {
383 return m_iconSize;
384 }
385
386 bool KItemListWidget::contains(const QPointF& point) const
387 {
388 if (!QGraphicsWidget::contains(point)) {
389 return false;
390 }
391
392 return iconRect().contains(point) ||
393 textRect().contains(point) ||
394 expansionToggleRect().contains(point) ||
395 selectionToggleRect().contains(point);
396 }
397
398 QRectF KItemListWidget::textFocusRect() const
399 {
400 return textRect();
401 }
402
403 QRectF KItemListWidget::selectionToggleRect() const
404 {
405 return QRectF();
406 }
407
408 QRectF KItemListWidget::expansionToggleRect() const
409 {
410 return QRectF();
411 }
412
413 QPixmap KItemListWidget::createDragPixmap(const QStyleOptionGraphicsItem* option,
414 QWidget* widget)
415 {
416 QPixmap pixmap(size().toSize() * widget->devicePixelRatio());
417 pixmap.setDevicePixelRatio(widget->devicePixelRatio());
418 pixmap.fill(Qt::transparent);
419
420 QPainter painter(&pixmap);
421
422 const bool oldAlternateBackground = m_alternateBackground;
423 const bool wasSelected = m_selected;
424 const bool wasHovered = m_hovered;
425
426 setAlternateBackground(false);
427 setHovered(false);
428
429 paint(&painter, option, widget);
430
431 setAlternateBackground(oldAlternateBackground);
432 setSelected(wasSelected);
433 setHovered(wasHovered);
434
435 return pixmap;
436 }
437
438 void KItemListWidget::dataChanged(const QHash<QByteArray, QVariant>& current,
439 const QSet<QByteArray>& roles)
440 {
441 Q_UNUSED(current)
442 Q_UNUSED(roles)
443 }
444
445 void KItemListWidget::visibleRolesChanged(const QList<QByteArray>& current,
446 const QList<QByteArray>& previous)
447 {
448 Q_UNUSED(current)
449 Q_UNUSED(previous)
450 }
451
452 void KItemListWidget::columnWidthChanged(const QByteArray& role,
453 qreal current,
454 qreal previous)
455 {
456 Q_UNUSED(role)
457 Q_UNUSED(current)
458 Q_UNUSED(previous)
459 }
460
461 void KItemListWidget::leadingPaddingChanged(qreal width)
462 {
463 Q_UNUSED(width)
464 }
465
466 void KItemListWidget::styleOptionChanged(const KItemListStyleOption& current,
467 const KItemListStyleOption& previous)
468 {
469 Q_UNUSED(previous)
470
471 // set the initial value of m_iconSize if not set
472 if (m_iconSize == -1) {
473 m_iconSize = current.iconSize;
474 }
475 }
476
477 void KItemListWidget::currentChanged(bool current)
478 {
479 Q_UNUSED(current)
480 }
481
482 void KItemListWidget::selectedChanged(bool selected)
483 {
484 Q_UNUSED(selected)
485 }
486
487 void KItemListWidget::hoveredChanged(bool hovered)
488 {
489 Q_UNUSED(hovered)
490 }
491
492 void KItemListWidget::alternateBackgroundChanged(bool enabled)
493 {
494 Q_UNUSED(enabled)
495 }
496
497 void KItemListWidget::siblingsInformationChanged(const QBitArray& current, const QBitArray& previous)
498 {
499 Q_UNUSED(current)
500 Q_UNUSED(previous)
501 }
502
503 void KItemListWidget::editedRoleChanged(const QByteArray& current, const QByteArray& previous)
504 {
505 Q_UNUSED(current)
506 Q_UNUSED(previous)
507 }
508
509 void KItemListWidget::iconSizeChanged(int current, int previous)
510 {
511 Q_UNUSED(current)
512 Q_UNUSED(previous)
513 }
514
515 void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent* event)
516 {
517 QGraphicsWidget::resizeEvent(event);
518 clearHoverCache();
519
520 if (m_selectionToggle) {
521 const QRectF& toggleRect = selectionToggleRect();
522 m_selectionToggle->setPos(toggleRect.topLeft());
523 m_selectionToggle->resize(toggleRect.size());
524 }
525 }
526
527 void KItemListWidget::hoverSequenceStarted()
528 {
529 }
530
531 void KItemListWidget::hoverSequenceIndexChanged(int sequenceIndex)
532 {
533 Q_UNUSED(sequenceIndex);
534 }
535
536 void KItemListWidget::hoverSequenceEnded()
537 {
538 }
539
540 qreal KItemListWidget::hoverOpacity() const
541 {
542 return m_hoverOpacity;
543 }
544
545 int KItemListWidget::hoverSequenceIndex() const
546 {
547 return m_hoverSequenceIndex;
548 }
549
550 void KItemListWidget::slotHoverAnimationFinished()
551 {
552 if (!m_hovered && m_selectionToggle) {
553 m_selectionToggle->deleteLater();
554 m_selectionToggle = nullptr;
555 }
556 }
557
558 void KItemListWidget::slotHoverSequenceTimerTimeout()
559 {
560 m_hoverSequenceIndex++;
561 hoverSequenceIndexChanged(m_hoverSequenceIndex);
562 }
563
564 void KItemListWidget::initializeSelectionToggle()
565 {
566 Q_ASSERT(m_enabledSelectionToggle);
567
568 if (!m_selectionToggle) {
569 m_selectionToggle = new KItemListSelectionToggle(this);
570 }
571
572 const QRectF toggleRect = selectionToggleRect();
573 m_selectionToggle->setPos(toggleRect.topLeft());
574 m_selectionToggle->resize(toggleRect.size());
575
576 m_selectionToggle->setChecked(isSelected());
577 }
578
579 void KItemListWidget::setHoverOpacity(qreal opacity)
580 {
581 m_hoverOpacity = opacity;
582 if (m_selectionToggle) {
583 m_selectionToggle->setOpacity(opacity);
584 }
585
586 if (m_hoverOpacity <= 0.0) {
587 delete m_hoverCache;
588 m_hoverCache = nullptr;
589 }
590
591 update();
592 }
593
594 void KItemListWidget::clearHoverCache()
595 {
596 delete m_hoverCache;
597 m_hoverCache = nullptr;
598 }
599
600 void KItemListWidget::drawItemStyleOption(QPainter* painter, QWidget* widget, QStyle::State styleState)
601 {
602 QStyleOptionViewItem viewItemOption;
603 initStyleOption(&viewItemOption);
604 viewItemOption.state = styleState;
605 viewItemOption.viewItemPosition = QStyleOptionViewItem::OnlyOne;
606 viewItemOption.showDecorationSelected = true;
607 viewItemOption.rect = selectionRect().toRect();
608 style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget);
609 }
610