]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placesitemlistwidget.cpp
Only show capacity bar for devices (has a udi).
[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 <QGraphicsView>
12 #include <QStyleOption>
13
14 #include <KDiskFreeSpaceInfo>
15 #include <KMountPoint>
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_isMountPoint(false)
24 , m_drawCapacityBar(false)
25 , m_capacityBarRatio(0)
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 bool isDevice = !data().value("udi").toString().isEmpty();
47 const QUrl url = data().value("url").toUrl();
48 if (isDevice && url.isLocalFile()) {
49 const QString mountPointPath = url.toLocalFile();
50 qDebug() << "url:" << mountPointPath;
51 KMountPoint::Ptr mp = KMountPoint::currentMountPoints().findByPath(mountPointPath);
52 m_isMountPoint = (mp && mp->mountPoint() == mountPointPath);
53 qDebug() << " isMountPoint:" << m_isMountPoint;
54 if (m_isMountPoint) {
55 const KDiskFreeSpaceInfo info = KDiskFreeSpaceInfo::freeSpaceInfo(mountPointPath);
56 m_drawCapacityBar = info.size() != 0;
57 m_capacityBarRatio = (qreal)info.used() / (qreal)info.size();
58 qDebug() << " capacityBarRatio:" << m_capacityBarRatio << "(" << info.used() << "/" << info.size() << ")";
59
60 // update();
61 return;
62 }
63 }
64
65 // else
66 resetCapacityBar();
67 }
68
69 void PlacesItemListWidget::resetCapacityBar()
70 {
71 m_isMountPoint = false;
72 m_drawCapacityBar = false;
73 m_capacityBarRatio = 0;
74 }
75
76 void PlacesItemListWidget::polishEvent()
77 {
78 updateCapacityBar();
79
80 QGraphicsWidget::polishEvent();
81 }
82
83 void PlacesItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
84 {
85 KStandardItemListWidget::paint(painter, option, widget);
86
87 if (m_drawCapacityBar) {
88 const TextInfo* textInfo = m_textInfo.value("text");
89 if (textInfo) { // See KStandarItemListWidget::paint() for info on why we check textInfo.
90 painter->save();
91
92 QRect capacityRect(
93 textInfo->pos.x(),
94 option->rect.top() + option->rect.height() - CAPACITYBAR_HEIGHT - CAPACITYBAR_MARGIN,
95 qMin((qreal)option->rect.width(), selectionRect().width()) - (textInfo->pos.x() - option->rect.left()),
96 CAPACITYBAR_HEIGHT
97 );
98
99 const QPalette pal = palette();
100 const QPalette::ColorGroup group = isActiveWindow() ? QPalette::Active : QPalette::Inactive;
101 // QColor bgColor = QColor::fromRgb(230, 230, 230);
102 // QColor outlineColor = QColor::fromRgb(208, 208, 208);
103 // QColor bgColor = QColor::fromRgb(0, 230, 0);
104 // QColor outlineColor = QColor::fromRgb(208, 0, 0, 127);
105 // QColor normalUsedColor = QColor::fromRgb(38, 160, 218);
106 // QColor dangerUsedColor = QColor::fromRgb(218, 38, 38);
107 // QColor bgColor = pal.base().color().darker(130);
108 // QColor outlineColor = pal.base().color().darker(150);
109
110 QPalette::ColorRole role;
111 // role = isSelected() ? QPalette::Highlight : QPalette::Window;
112 // QColor bgColor = styleOption().palette.color(group, role).darker(150);
113 // QColor outlineColor = styleOption().palette.color(group, role).darker(170);
114 QColor bgColor = isSelected()
115 ? styleOption().palette.color(group, QPalette::Highlight).darker(180)
116 : styleOption().palette.color(group, QPalette::Window).darker(120);
117
118 role = isSelected() ? QPalette::HighlightedText : QPalette::Highlight;
119 QColor normalUsedColor = styleOption().palette.color(group, role);
120
121 QColor dangerUsedColor = QColor::fromRgb(218, 38, 38);
122
123 // Background
124 painter->fillRect(capacityRect, bgColor);
125
126 // Outline
127 // const QRect outlineRect(capacityRect.x(), capacityRect.y(), capacityRect.width() - 1, capacityRect.height() - 1);
128 // painter->setPen(outlineColor);
129 // painter->drawRect(outlineRect);
130
131 // Fill
132 const QRect fillRect(capacityRect.x(), capacityRect.y(), capacityRect.width() * m_capacityBarRatio, capacityRect.height());
133 if (m_capacityBarRatio < 0.95) { // Fill
134 painter->fillRect(fillRect, normalUsedColor);
135 } else {
136 painter->fillRect(fillRect, dangerUsedColor);
137 }
138
139 painter->restore();
140 }
141 }
142 }