]>
cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placesitemlistwidget.cpp
2 * SPDX-FileCopyrightText: 2012 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "placesitemlistwidget.h"
11 #include <QGraphicsView>
12 #include <QStyleOption>
14 #include <KDiskFreeSpaceInfo>
15 #include <KMountPoint>
17 #define CAPACITYBAR_HEIGHT 2
18 #define CAPACITYBAR_MARGIN 2
21 PlacesItemListWidget::PlacesItemListWidget(KItemListWidgetInformant
* informant
, QGraphicsItem
* parent
) :
22 KStandardItemListWidget(informant
, parent
)
23 , m_isMountPoint(false)
24 , m_drawCapacityBar(false)
25 , m_capacityBarRatio(0)
29 PlacesItemListWidget::~PlacesItemListWidget()
33 bool PlacesItemListWidget::isHidden() const
35 return data().value("isHidden").toBool() ||
36 data().value("isGroupHidden").toBool();
39 QPalette::ColorRole
PlacesItemListWidget::normalTextColorRole() const
41 return QPalette::WindowText
;
44 void PlacesItemListWidget::updateCapacityBar()
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
;
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() << ")";
69 void PlacesItemListWidget::resetCapacityBar()
71 m_isMountPoint
= false;
72 m_drawCapacityBar
= false;
73 m_capacityBarRatio
= 0;
76 void PlacesItemListWidget::polishEvent()
80 QGraphicsWidget::polishEvent();
83 void PlacesItemListWidget::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
85 KStandardItemListWidget::paint(painter
, option
, widget
);
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.
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()),
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);
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);
118 role
= isSelected() ? QPalette::HighlightedText
: QPalette::Highlight
;
119 QColor normalUsedColor
= styleOption().palette
.color(group
, role
);
121 QColor dangerUsedColor
= QColor::fromRgb(218, 38, 38);
124 painter
->fillRect(capacityRect
, bgColor
);
127 // const QRect outlineRect(capacityRect.x(), capacityRect.y(), capacityRect.width() - 1, capacityRect.height() - 1);
128 // painter->setPen(outlineColor);
129 // painter->drawRect(outlineRect);
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
);
136 painter
->fillRect(fillRect
, dangerUsedColor
);