]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placesitemlistwidget.cpp
Cleanup unused colors. Use const. Use KColorScheme danger.
[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 <QDebug>
10
11 #include <QDateTime>
12 #include <QGraphicsView>
13 #include <QStyleOption>
14
15 #include <KColorScheme>
16 #include <KMountPoint>
17
18 #include <KIO/FileSystemFreeSpaceJob>
19
20 #define CAPACITYBAR_HEIGHT 2
21 #define CAPACITYBAR_MARGIN 2
22
23
24 PlacesItemListWidget::PlacesItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent) :
25 KStandardItemListWidget(informant, parent)
26 , m_drawCapacityBar(false)
27 {
28 }
29
30 PlacesItemListWidget::~PlacesItemListWidget()
31 {
32 }
33
34 bool PlacesItemListWidget::isHidden() const
35 {
36 return data().value("isHidden").toBool() ||
37 data().value("isGroupHidden").toBool();
38 }
39
40 QPalette::ColorRole PlacesItemListWidget::normalTextColorRole() const
41 {
42 return QPalette::WindowText;
43 }
44
45 void PlacesItemListWidget::updateCapacityBar()
46 {
47 const bool isDevice = !data().value("udi").toString().isEmpty();
48 const QUrl url = data().value("url").toUrl();
49 if (isDevice && url.isLocalFile()) {
50 if (!m_freeSpaceInfo.job
51 && (
52 !m_freeSpaceInfo.lastUpdated.isValid()
53 || m_freeSpaceInfo.lastUpdated.secsTo(QDateTime::currentDateTimeUtc()) > 60
54 )
55 ) {
56 // qDebug() << "url:" << url;
57 m_freeSpaceInfo.job = KIO::fileSystemFreeSpace(url);
58 connect(
59 m_freeSpaceInfo.job,
60 &KIO::FileSystemFreeSpaceJob::result,
61 this,
62 [this](KIO::Job *job, KIO::filesize_t size, KIO::filesize_t available) {
63 // even if we receive an error we want to refresh lastUpdated to avoid repeatedly querying in this case
64 m_freeSpaceInfo.lastUpdated = QDateTime::currentDateTimeUtc();
65
66 if (job->error()) {
67 return;
68 }
69
70 m_freeSpaceInfo.size = size;
71 m_freeSpaceInfo.used = size - available;
72 m_freeSpaceInfo.usedRatio = (qreal)m_freeSpaceInfo.used / (qreal)m_freeSpaceInfo.size;
73 m_drawCapacityBar = size > 0;
74 // qDebug() << "job.url:" << data().value("url").toUrl();
75 // qDebug() << "job.size:" << m_freeSpaceInfo.size;
76 // qDebug() << "job.used:" << m_freeSpaceInfo.used;
77 // qDebug() << "job.ratio:" << m_freeSpaceInfo.usedRatio;
78 // qDebug() << "job.draw:" << m_drawCapacityBar;
79
80 update();
81 }
82 );
83 } else {
84 // Job running or cache is still valid.
85 }
86 } else {
87 resetCapacityBar();
88 }
89 }
90
91 void PlacesItemListWidget::resetCapacityBar()
92 {
93 m_drawCapacityBar = false;
94 m_freeSpaceInfo.size = 0;
95 m_freeSpaceInfo.used = 0;
96 m_freeSpaceInfo.usedRatio = 0;
97 }
98
99 void PlacesItemListWidget::polishEvent()
100 {
101 updateCapacityBar();
102
103 QGraphicsWidget::polishEvent();
104 }
105
106 void PlacesItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
107 {
108 KStandardItemListWidget::paint(painter, option, widget);
109
110 if (m_drawCapacityBar) {
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 }