]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placesitemlistwidget.cpp
52b3baf309063857a7e75b1e8017407e998e11e4
[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 <QGraphicsView>
10 #include <QStyleOption>
11
12 #include <KDiskFreeSpaceInfo>
13 #include <KMountPoint>
14
15 #define CAPACITYBAR_HEIGHT 2
16 #define CAPACITYBAR_MARGIN 2
17
18
19 PlacesItemListWidget::PlacesItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent) :
20 KStandardItemListWidget(informant, parent)
21 {
22 }
23
24 PlacesItemListWidget::~PlacesItemListWidget()
25 {
26 }
27
28 bool PlacesItemListWidget::isHidden() const
29 {
30 return data().value("isHidden").toBool() ||
31 data().value("isGroupHidden").toBool();
32 }
33
34 QPalette::ColorRole PlacesItemListWidget::normalTextColorRole() const
35 {
36 return QPalette::WindowText;
37 }
38
39 void PlacesItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
40 {
41 KStandardItemListWidget::paint(painter, option, widget);
42
43 bool drawCapacityBar = false;
44 const QUrl url = data().value("url").toUrl();
45 if (url.isLocalFile()) {
46 const QString mountPointPath = url.toLocalFile();
47 KMountPoint::Ptr mp = KMountPoint::currentMountPoints().findByPath(mountPointPath);
48 bool isMountPoint = (mp && mp->mountPoint() == mountPointPath);
49
50 if (isMountPoint) {
51 const KDiskFreeSpaceInfo info = KDiskFreeSpaceInfo::freeSpaceInfo(mountPointPath);
52 drawCapacityBar = info.size() != 0;
53 if (drawCapacityBar) {
54 const TextInfo* textInfo = m_textInfo.value("text");
55 if (textInfo) { // See KStandarItemListWidget::paint() for info on why we check textInfo.
56 painter->save();
57
58 QRect capacityRect(
59 textInfo->pos.x(),
60 option->rect.top() + option->rect.height() - CAPACITYBAR_HEIGHT - CAPACITYBAR_MARGIN,
61 qMin((qreal)option->rect.width(), selectionRect().width()) - (textInfo->pos.x() - option->rect.left()),
62 CAPACITYBAR_HEIGHT
63 );
64
65 const qreal ratio = (qreal)info.used() / (qreal)info.size();
66 // qDebug() << "ratio:" << ratio << "(" << info.used() << "/" << info.size() << ")";
67
68 const QPalette pal = palette();
69 const QPalette::ColorGroup group = isActiveWindow() ? QPalette::Active : QPalette::Inactive;
70 // QColor bgColor = QColor::fromRgb(230, 230, 230);
71 // QColor outlineColor = QColor::fromRgb(208, 208, 208);
72 // QColor bgColor = QColor::fromRgb(0, 230, 0);
73 // QColor outlineColor = QColor::fromRgb(208, 0, 0, 127);
74 // QColor normalUsedColor = QColor::fromRgb(38, 160, 218);
75 // QColor dangerUsedColor = QColor::fromRgb(218, 38, 38);
76 // QColor bgColor = pal.base().color().darker(130);
77 // QColor outlineColor = pal.base().color().darker(150);
78
79 QPalette::ColorRole role;
80 // role = isSelected() ? QPalette::Highlight : QPalette::Window;
81 // QColor bgColor = styleOption().palette.color(group, role).darker(150);
82 // QColor outlineColor = styleOption().palette.color(group, role).darker(170);
83 QColor bgColor = isSelected()
84 ? styleOption().palette.color(group, QPalette::Highlight).darker(180)
85 : styleOption().palette.color(group, QPalette::Window).darker(120);
86
87 role = isSelected() ? QPalette::HighlightedText : QPalette::Highlight;
88 QColor normalUsedColor = styleOption().palette.color(group, role);
89
90 QColor dangerUsedColor = QColor::fromRgb(218, 38, 38);
91
92 // Background
93 painter->fillRect(capacityRect, bgColor);
94
95 // Outline
96 // const QRect outlineRect(capacityRect.x(), capacityRect.y(), capacityRect.width() - 1, capacityRect.height() - 1);
97 // painter->setPen(outlineColor);
98 // painter->drawRect(outlineRect);
99
100 // Fill
101 const QRect fillRect(capacityRect.x(), capacityRect.y(), capacityRect.width() * ratio, capacityRect.height());
102 if (ratio < 0.95) { // Fill
103 painter->fillRect(fillRect, normalUsedColor);
104 } else {
105 painter->fillRect(fillRect, dangerUsedColor);
106 }
107
108 painter->restore();
109 }
110 }
111 }
112 }
113 }