]>
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"
9 #include <QStyleOption>
12 #include <KColorScheme>
14 #include <KIO/FileSystemFreeSpaceJob>
15 #include <Solid/Device>
16 #include <Solid/NetworkShare>
18 #define CAPACITYBAR_HEIGHT 2
19 #define CAPACITYBAR_MARGIN 2
20 #define CAPACITYBAR_CACHE_TTL 60000
23 PlacesItemListWidget::PlacesItemListWidget(KItemListWidgetInformant
* informant
, QGraphicsItem
* parent
) :
24 KStandardItemListWidget(informant
, parent
)
25 , m_drawCapacityBar(false)
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 QString udi
= data().value("udi").toString();
51 const Solid::Device device
= Solid::Device(udi
);
52 if (device
.isDeviceInterface(Solid::DeviceInterface::NetworkShare
)
53 || device
.isDeviceInterface(Solid::DeviceInterface::OpticalDrive
)
54 || device
.isDeviceInterface(Solid::DeviceInterface::OpticalDisc
)) {
58 const QUrl url
= data().value("url").toUrl();
60 if (url
.isEmpty() || m_freeSpaceInfo
.job
|| !m_freeSpaceInfo
.lastUpdated
.hasExpired()) {
61 // No url, job running or cache is still valid.
65 m_freeSpaceInfo
.job
= KIO::fileSystemFreeSpace(url
);
68 &KIO::FileSystemFreeSpaceJob::result
,
70 [this](KIO::Job
*job
, KIO::filesize_t size
, KIO::filesize_t available
) {
71 // even if we receive an error we want to refresh lastUpdated to avoid repeatedly querying in this case
72 m_freeSpaceInfo
.lastUpdated
.setRemainingTime(CAPACITYBAR_CACHE_TTL
);
78 m_freeSpaceInfo
.size
= size
;
79 m_freeSpaceInfo
.used
= size
- available
;
80 m_freeSpaceInfo
.usedRatio
= (qreal
)m_freeSpaceInfo
.used
/ (qreal
)m_freeSpaceInfo
.size
;
81 m_drawCapacityBar
= size
> 0;
88 void PlacesItemListWidget::resetCapacityBar()
90 m_drawCapacityBar
= false;
91 delete m_freeSpaceInfo
.job
;
92 m_freeSpaceInfo
.lastUpdated
.setRemainingTime(0);
93 m_freeSpaceInfo
.size
= 0;
94 m_freeSpaceInfo
.used
= 0;
95 m_freeSpaceInfo
.usedRatio
= 0;
98 void PlacesItemListWidget::polishEvent()
102 QGraphicsWidget::polishEvent();
105 void PlacesItemListWidget::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
107 KStandardItemListWidget::paint(painter
, option
, widget
);
109 if (m_drawCapacityBar
) {
110 const TextInfo
* textInfo
= m_textInfo
.value("text");
111 if (textInfo
) { // See KStandarItemListWidget::paint() for info on why we check textInfo.
114 const QRect
capacityRect(
116 option
->rect
.top() + option
->rect
.height() - CAPACITYBAR_HEIGHT
- CAPACITYBAR_MARGIN
,
117 qMin((qreal
)option
->rect
.width(), selectionRect().width()) - (textInfo
->pos
.x() - option
->rect
.left()),
121 const QPalette pal
= palette();
122 const QPalette::ColorGroup group
= isActiveWindow() ? QPalette::Active
: QPalette::Inactive
;
125 const QColor bgColor
= isSelected()
126 ? pal
.color(group
, QPalette::Highlight
).darker(180)
127 : pal
.color(group
, QPalette::Window
).darker(120);
129 painter
->fillRect(capacityRect
, bgColor
);
132 const QRect
fillRect(capacityRect
.x(), capacityRect
.y(), capacityRect
.width() * m_freeSpaceInfo
.usedRatio
, capacityRect
.height());
133 if (m_freeSpaceInfo
.usedRatio
>= 0.95) { // More than 95% full!
134 const QColor dangerUsedColor
= KColorScheme(group
, KColorScheme::View
).foreground(KColorScheme::NegativeText
).color();
135 painter
->fillRect(fillRect
, dangerUsedColor
);
137 const QPalette::ColorRole role
= isSelected() ? QPalette::HighlightedText
: QPalette::Highlight
;
138 const QColor normalUsedColor
= styleOption().palette
.color(group
, role
);
139 painter
->fillRect(fillRect
, normalUsedColor
);