]>
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>
11 #include <KColorScheme>
13 #include <Solid/Device>
14 #include <Solid/NetworkShare>
16 #define CAPACITYBAR_HEIGHT 2
17 #define CAPACITYBAR_MARGIN 2
18 #define CAPACITYBAR_CACHE_TTL 60000
21 PlacesItemListWidget::PlacesItemListWidget(KItemListWidgetInformant
* informant
, QGraphicsItem
* parent
) :
22 KStandardItemListWidget(informant
, parent
)
23 , m_drawCapacityBar(false)
27 PlacesItemListWidget::~PlacesItemListWidget()
31 bool PlacesItemListWidget::isHidden() const
33 return data().value("isHidden").toBool() ||
34 data().value("isGroupHidden").toBool();
37 QPalette::ColorRole
PlacesItemListWidget::normalTextColorRole() const
39 return QPalette::WindowText
;
42 void PlacesItemListWidget::updateCapacityBar()
44 const QString udi
= data().value("udi").toString();
49 const Solid::Device device
= Solid::Device(udi
);
50 if (device
.isDeviceInterface(Solid::DeviceInterface::NetworkShare
)
51 || device
.isDeviceInterface(Solid::DeviceInterface::OpticalDrive
)
52 || device
.isDeviceInterface(Solid::DeviceInterface::OpticalDisc
)) {
56 const QUrl url
= data().value("url").toUrl();
58 if (url
.isEmpty() || m_freeSpaceInfo
.job
|| !m_freeSpaceInfo
.lastUpdated
.hasExpired()) {
59 // No url, job running or cache is still valid.
63 m_freeSpaceInfo
.job
= KIO::fileSystemFreeSpace(url
);
66 &KIO::FileSystemFreeSpaceJob::result
,
68 [this](KIO::Job
*job
, KIO::filesize_t size
, KIO::filesize_t available
) {
69 // even if we receive an error we want to refresh lastUpdated to avoid repeatedly querying in this case
70 m_freeSpaceInfo
.lastUpdated
.setRemainingTime(CAPACITYBAR_CACHE_TTL
);
76 m_freeSpaceInfo
.size
= size
;
77 m_freeSpaceInfo
.used
= size
- available
;
78 m_freeSpaceInfo
.usedRatio
= (qreal
)m_freeSpaceInfo
.used
/ (qreal
)m_freeSpaceInfo
.size
;
79 m_drawCapacityBar
= size
> 0;
86 void PlacesItemListWidget::resetCapacityBar()
88 m_drawCapacityBar
= false;
89 delete m_freeSpaceInfo
.job
;
90 m_freeSpaceInfo
.lastUpdated
.setRemainingTime(0);
91 m_freeSpaceInfo
.size
= 0;
92 m_freeSpaceInfo
.used
= 0;
93 m_freeSpaceInfo
.usedRatio
= 0;
96 void PlacesItemListWidget::polishEvent()
100 QGraphicsWidget::polishEvent();
103 void PlacesItemListWidget::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
105 KStandardItemListWidget::paint(painter
, option
, widget
);
107 // We check if option=nullptr since it is null when the place is dragged (Bug #430441)
108 if (m_drawCapacityBar
&& option
) {
109 const TextInfo
* textInfo
= m_textInfo
.value("text");
110 if (textInfo
) { // See KStandarItemListWidget::paint() for info on why we check textInfo.
113 const QRect
capacityRect(
115 option
->rect
.top() + option
->rect
.height() - CAPACITYBAR_HEIGHT
- CAPACITYBAR_MARGIN
,
116 qMin((qreal
)option
->rect
.width(), selectionRect().width()) - (textInfo
->pos
.x() - option
->rect
.left()),
120 const QPalette pal
= palette();
121 const QPalette::ColorGroup group
= isActiveWindow() ? QPalette::Active
: QPalette::Inactive
;
124 const QColor bgColor
= isSelected()
125 ? pal
.color(group
, QPalette::Highlight
).darker(180)
126 : pal
.color(group
, QPalette::Window
).darker(120);
128 painter
->fillRect(capacityRect
, bgColor
);
131 const QRect
fillRect(capacityRect
.x(), capacityRect
.y(), capacityRect
.width() * m_freeSpaceInfo
.usedRatio
, capacityRect
.height());
132 if (m_freeSpaceInfo
.usedRatio
>= 0.95) { // More than 95% full!
133 const QColor dangerUsedColor
= KColorScheme(group
, KColorScheme::View
).foreground(KColorScheme::NegativeText
).color();
134 painter
->fillRect(fillRect
, dangerUsedColor
);
136 const QPalette::ColorRole role
= isSelected() ? QPalette::HighlightedText
: QPalette::Highlight
;
137 const QColor normalUsedColor
= styleOption().palette
.color(group
, role
);
138 painter
->fillRect(fillRect
, normalUsedColor
);