]>
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>
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 bool isDevice
= !data().value("udi").toString().isEmpty();
45 const QUrl url
= data().value("url").toUrl();
46 if (!(isDevice
&& url
.isLocalFile())) {
51 if (m_freeSpaceInfo
.job
|| !m_freeSpaceInfo
.lastUpdated
.hasExpired()) {
52 // Job running or cache is still valid.
56 m_freeSpaceInfo
.job
= KIO::fileSystemFreeSpace(url
);
59 &KIO::FileSystemFreeSpaceJob::result
,
61 [this](KIO::Job
*job
, KIO::filesize_t size
, KIO::filesize_t available
) {
62 // even if we receive an error we want to refresh lastUpdated to avoid repeatedly querying in this case
63 m_freeSpaceInfo
.lastUpdated
.setRemainingTime(CAPACITYBAR_CACHE_TTL
);
69 m_freeSpaceInfo
.size
= size
;
70 m_freeSpaceInfo
.used
= size
- available
;
71 m_freeSpaceInfo
.usedRatio
= (qreal
)m_freeSpaceInfo
.used
/ (qreal
)m_freeSpaceInfo
.size
;
72 m_drawCapacityBar
= size
> 0;
79 void PlacesItemListWidget::resetCapacityBar()
81 m_drawCapacityBar
= false;
82 delete m_freeSpaceInfo
.job
;
83 m_freeSpaceInfo
.lastUpdated
.setRemainingTime(0);
84 m_freeSpaceInfo
.size
= 0;
85 m_freeSpaceInfo
.used
= 0;
86 m_freeSpaceInfo
.usedRatio
= 0;
89 void PlacesItemListWidget::polishEvent()
93 QGraphicsWidget::polishEvent();
96 void PlacesItemListWidget::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
98 KStandardItemListWidget::paint(painter
, option
, widget
);
100 if (m_drawCapacityBar
) {
101 const TextInfo
* textInfo
= m_textInfo
.value("text");
102 if (textInfo
) { // See KStandarItemListWidget::paint() for info on why we check textInfo.
105 const QRect
capacityRect(
107 option
->rect
.top() + option
->rect
.height() - CAPACITYBAR_HEIGHT
- CAPACITYBAR_MARGIN
,
108 qMin((qreal
)option
->rect
.width(), selectionRect().width()) - (textInfo
->pos
.x() - option
->rect
.left()),
112 const QPalette pal
= palette();
113 const QPalette::ColorGroup group
= isActiveWindow() ? QPalette::Active
: QPalette::Inactive
;
116 const QColor bgColor
= isSelected()
117 ? pal
.color(group
, QPalette::Highlight
).darker(180)
118 : pal
.color(group
, QPalette::Window
).darker(120);
120 painter
->fillRect(capacityRect
, bgColor
);
123 const QRect
fillRect(capacityRect
.x(), capacityRect
.y(), capacityRect
.width() * m_freeSpaceInfo
.usedRatio
, capacityRect
.height());
124 if (m_freeSpaceInfo
.usedRatio
>= 0.95) { // More than 95% full!
125 const QColor dangerUsedColor
= KColorScheme(group
, KColorScheme::View
).foreground(KColorScheme::NegativeText
).color();
126 painter
->fillRect(fillRect
, dangerUsedColor
);
128 const QPalette::ColorRole role
= isSelected() ? QPalette::HighlightedText
: QPalette::Highlight
;
129 const QColor normalUsedColor
= styleOption().palette
.color(group
, role
);
130 painter
->fillRect(fillRect
, normalUsedColor
);