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