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