]>
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 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
;
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() << ")";
68 void PlacesItemListWidget::resetCapacityBar()
70 m_isMountPoint
= false;
71 m_drawCapacityBar
= false;
72 m_capacityBarRatio
= 0;
75 void PlacesItemListWidget::polishEvent()
79 QGraphicsWidget::polishEvent();
82 void PlacesItemListWidget::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
84 KStandardItemListWidget::paint(painter
, option
, widget
);
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.
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()),
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);
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);
117 role
= isSelected() ? QPalette::HighlightedText
: QPalette::Highlight
;
118 QColor normalUsedColor
= styleOption().palette
.color(group
, role
);
120 QColor dangerUsedColor
= QColor::fromRgb(218, 38, 38);
123 painter
->fillRect(capacityRect
, bgColor
);
126 // const QRect outlineRect(capacityRect.x(), capacityRect.y(), capacityRect.width() - 1, capacityRect.height() - 1);
127 // painter->setPen(outlineColor);
128 // painter->drawRect(outlineRect);
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
);
135 painter
->fillRect(fillRect
, dangerUsedColor
);