]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placesitemlistwidget.cpp
Use QDeadlineTimer instead of QDateTime.
[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
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 bool isDevice = !data().value("udi").toString().isEmpty();
45 const QUrl url = data().value("url").toUrl();
46 if (!(isDevice && url.isLocalFile())) {
47 resetCapacityBar();
48 return;
49 }
50
51 if (m_freeSpaceInfo.job || !m_freeSpaceInfo.lastUpdated.hasExpired()) {
52 // Job running or cache is still valid.
53 return;
54 }
55
56 m_freeSpaceInfo.job = KIO::fileSystemFreeSpace(url);
57 connect(
58 m_freeSpaceInfo.job,
59 &KIO::FileSystemFreeSpaceJob::result,
60 this,
61 [this](KIO::Job *job, KIO::filesize_t size, KIO::filesize_t available) {
62 // even if we receive an error we want to refresh lastUpdated to avoid repeatedly querying in this case
63 m_freeSpaceInfo.lastUpdated.setRemainingTime(CAPACITYBAR_CACHE_TTL);
64
65 if (job->error()) {
66 return;
67 }
68
69 m_freeSpaceInfo.size = size;
70 m_freeSpaceInfo.used = size - available;
71 m_freeSpaceInfo.usedRatio = (qreal)m_freeSpaceInfo.used / (qreal)m_freeSpaceInfo.size;
72 m_drawCapacityBar = size > 0;
73
74 update();
75 }
76 );
77 }
78
79 void PlacesItemListWidget::resetCapacityBar()
80 {
81 m_drawCapacityBar = false;
82 delete m_freeSpaceInfo.job;
83 m_freeSpaceInfo.lastUpdated.setRemainingTime(0);
84 m_freeSpaceInfo.size = 0;
85 m_freeSpaceInfo.used = 0;
86 m_freeSpaceInfo.usedRatio = 0;
87 }
88
89 void PlacesItemListWidget::polishEvent()
90 {
91 updateCapacityBar();
92
93 QGraphicsWidget::polishEvent();
94 }
95
96 void PlacesItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
97 {
98 KStandardItemListWidget::paint(painter, option, widget);
99
100 if (m_drawCapacityBar) {
101 const TextInfo* textInfo = m_textInfo.value("text");
102 if (textInfo) { // See KStandarItemListWidget::paint() for info on why we check textInfo.
103 painter->save();
104
105 const QRect capacityRect(
106 textInfo->pos.x(),
107 option->rect.top() + option->rect.height() - CAPACITYBAR_HEIGHT - CAPACITYBAR_MARGIN,
108 qMin((qreal)option->rect.width(), selectionRect().width()) - (textInfo->pos.x() - option->rect.left()),
109 CAPACITYBAR_HEIGHT
110 );
111
112 const QPalette pal = palette();
113 const QPalette::ColorGroup group = isActiveWindow() ? QPalette::Active : QPalette::Inactive;
114
115 // Background
116 const QColor bgColor = isSelected()
117 ? pal.color(group, QPalette::Highlight).darker(180)
118 : pal.color(group, QPalette::Window).darker(120);
119
120 painter->fillRect(capacityRect, bgColor);
121
122 // Fill
123 const QRect fillRect(capacityRect.x(), capacityRect.y(), capacityRect.width() * m_freeSpaceInfo.usedRatio, capacityRect.height());
124 if (m_freeSpaceInfo.usedRatio >= 0.95) { // More than 95% full!
125 const QColor dangerUsedColor = KColorScheme(group, KColorScheme::View).foreground(KColorScheme::NegativeText).color();
126 painter->fillRect(fillRect, dangerUsedColor);
127 } else {
128 const QPalette::ColorRole role = isSelected() ? QPalette::HighlightedText : QPalette::Highlight;
129 const QColor normalUsedColor = styleOption().palette.color(group, role);
130 painter->fillRect(fillRect, normalUsedColor);
131 }
132
133 painter->restore();
134 }
135 }
136
137 updateCapacityBar();
138 }