]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placesitemlistwidget.cpp
Fix crash when device with capacitybar is dragged
[dolphin.git] / src / panels / places / placesitemlistwidget.cpp
1 /*
2 * SPDX-FileCopyrightText: 2012 Peter Penz <peter.penz19@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "placesitemlistwidget.h"
8
9 #include <QStyleOption>
10 #include <QPainter>
11
12 #include <KColorScheme>
13
14 #include <KIO/FileSystemFreeSpaceJob>
15 #include <Solid/Device>
16 #include <Solid/NetworkShare>
17
18 #define CAPACITYBAR_HEIGHT 2
19 #define CAPACITYBAR_MARGIN 2
20 #define CAPACITYBAR_CACHE_TTL 60000
21
22
23 PlacesItemListWidget::PlacesItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent) :
24 KStandardItemListWidget(informant, parent)
25 , m_drawCapacityBar(false)
26 {
27 }
28
29 PlacesItemListWidget::~PlacesItemListWidget()
30 {
31 }
32
33 bool PlacesItemListWidget::isHidden() const
34 {
35 return data().value("isHidden").toBool() ||
36 data().value("isGroupHidden").toBool();
37 }
38
39 QPalette::ColorRole PlacesItemListWidget::normalTextColorRole() const
40 {
41 return QPalette::WindowText;
42 }
43
44 void PlacesItemListWidget::updateCapacityBar()
45 {
46 const QString udi = data().value("udi").toString();
47 if (udi.isEmpty()) {
48 resetCapacityBar();
49 return;
50 }
51 const Solid::Device device = Solid::Device(udi);
52 if (device.isDeviceInterface(Solid::DeviceInterface::NetworkShare)
53 || device.isDeviceInterface(Solid::DeviceInterface::OpticalDrive)
54 || device.isDeviceInterface(Solid::DeviceInterface::OpticalDisc)) {
55 resetCapacityBar();
56 return;
57 }
58 const QUrl url = data().value("url").toUrl();
59
60 if (url.isEmpty() || m_freeSpaceInfo.job || !m_freeSpaceInfo.lastUpdated.hasExpired()) {
61 // No url, job running or cache is still valid.
62 return;
63 }
64
65 m_freeSpaceInfo.job = KIO::fileSystemFreeSpace(url);
66 connect(
67 m_freeSpaceInfo.job,
68 &KIO::FileSystemFreeSpaceJob::result,
69 this,
70 [this](KIO::Job *job, KIO::filesize_t size, KIO::filesize_t available) {
71 // even if we receive an error we want to refresh lastUpdated to avoid repeatedly querying in this case
72 m_freeSpaceInfo.lastUpdated.setRemainingTime(CAPACITYBAR_CACHE_TTL);
73
74 if (job->error()) {
75 return;
76 }
77
78 m_freeSpaceInfo.size = size;
79 m_freeSpaceInfo.used = size - available;
80 m_freeSpaceInfo.usedRatio = (qreal)m_freeSpaceInfo.used / (qreal)m_freeSpaceInfo.size;
81 m_drawCapacityBar = size > 0;
82
83 update();
84 }
85 );
86 }
87
88 void PlacesItemListWidget::resetCapacityBar()
89 {
90 m_drawCapacityBar = false;
91 delete m_freeSpaceInfo.job;
92 m_freeSpaceInfo.lastUpdated.setRemainingTime(0);
93 m_freeSpaceInfo.size = 0;
94 m_freeSpaceInfo.used = 0;
95 m_freeSpaceInfo.usedRatio = 0;
96 }
97
98 void PlacesItemListWidget::polishEvent()
99 {
100 updateCapacityBar();
101
102 QGraphicsWidget::polishEvent();
103 }
104
105 void PlacesItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
106 {
107 KStandardItemListWidget::paint(painter, option, widget);
108
109 // We check if option=nullptr since it is null when the place is dragged (Bug #430441)
110 if (m_drawCapacityBar && option) {
111 const TextInfo* textInfo = m_textInfo.value("text");
112 if (textInfo) { // See KStandarItemListWidget::paint() for info on why we check textInfo.
113 painter->save();
114
115 const QRect capacityRect(
116 textInfo->pos.x(),
117 option->rect.top() + option->rect.height() - CAPACITYBAR_HEIGHT - CAPACITYBAR_MARGIN,
118 qMin((qreal)option->rect.width(), selectionRect().width()) - (textInfo->pos.x() - option->rect.left()),
119 CAPACITYBAR_HEIGHT
120 );
121
122 const QPalette pal = palette();
123 const QPalette::ColorGroup group = isActiveWindow() ? QPalette::Active : QPalette::Inactive;
124
125 // Background
126 const QColor bgColor = isSelected()
127 ? pal.color(group, QPalette::Highlight).darker(180)
128 : pal.color(group, QPalette::Window).darker(120);
129
130 painter->fillRect(capacityRect, bgColor);
131
132 // Fill
133 const QRect fillRect(capacityRect.x(), capacityRect.y(), capacityRect.width() * m_freeSpaceInfo.usedRatio, capacityRect.height());
134 if (m_freeSpaceInfo.usedRatio >= 0.95) { // More than 95% full!
135 const QColor dangerUsedColor = KColorScheme(group, KColorScheme::View).foreground(KColorScheme::NegativeText).color();
136 painter->fillRect(fillRect, dangerUsedColor);
137 } else {
138 const QPalette::ColorRole role = isSelected() ? QPalette::HighlightedText : QPalette::Highlight;
139 const QColor normalUsedColor = styleOption().palette.color(group, role);
140 painter->fillRect(fillRect, normalUsedColor);
141 }
142
143 painter->restore();
144 }
145 }
146
147 updateCapacityBar();
148 }