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