]>
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"
12 #include <QGraphicsView>
13 #include <QStyleOption>
15 #include <KColorScheme>
16 #include <KMountPoint>
18 #include <KIO/FileSystemFreeSpaceJob>
20 #define CAPACITYBAR_HEIGHT 2
21 #define CAPACITYBAR_MARGIN 2
24 PlacesItemListWidget::PlacesItemListWidget(KItemListWidgetInformant
* informant
, QGraphicsItem
* parent
) :
25 KStandardItemListWidget(informant
, parent
)
26 , m_drawCapacityBar(false)
30 PlacesItemListWidget::~PlacesItemListWidget()
34 bool PlacesItemListWidget::isHidden() const
36 return data().value("isHidden").toBool() ||
37 data().value("isGroupHidden").toBool();
40 QPalette::ColorRole
PlacesItemListWidget::normalTextColorRole() const
42 return QPalette::WindowText
;
45 void PlacesItemListWidget::updateCapacityBar()
47 const bool isDevice
= !data().value("udi").toString().isEmpty();
48 const QUrl url
= data().value("url").toUrl();
49 if (isDevice
&& url
.isLocalFile()) {
50 if (!m_freeSpaceInfo
.job
52 !m_freeSpaceInfo
.lastUpdated
.isValid()
53 || m_freeSpaceInfo
.lastUpdated
.secsTo(QDateTime::currentDateTimeUtc()) > 60
56 // qDebug() << "url:" << url;
57 m_freeSpaceInfo
.job
= KIO::fileSystemFreeSpace(url
);
60 &KIO::FileSystemFreeSpaceJob::result
,
62 [this](KIO::Job
*job
, KIO::filesize_t size
, KIO::filesize_t available
) {
63 // even if we receive an error we want to refresh lastUpdated to avoid repeatedly querying in this case
64 m_freeSpaceInfo
.lastUpdated
= QDateTime::currentDateTimeUtc();
70 m_freeSpaceInfo
.size
= size
;
71 m_freeSpaceInfo
.used
= size
- available
;
72 m_freeSpaceInfo
.usedRatio
= (qreal
)m_freeSpaceInfo
.used
/ (qreal
)m_freeSpaceInfo
.size
;
73 m_drawCapacityBar
= size
> 0;
74 // qDebug() << "job.url:" << data().value("url").toUrl();
75 // qDebug() << "job.size:" << m_freeSpaceInfo.size;
76 // qDebug() << "job.used:" << m_freeSpaceInfo.used;
77 // qDebug() << "job.ratio:" << m_freeSpaceInfo.usedRatio;
78 // qDebug() << "job.draw:" << m_drawCapacityBar;
84 // Job running or cache is still valid.
91 void PlacesItemListWidget::resetCapacityBar()
93 m_drawCapacityBar
= false;
94 m_freeSpaceInfo
.size
= 0;
95 m_freeSpaceInfo
.used
= 0;
96 m_freeSpaceInfo
.usedRatio
= 0;
99 void PlacesItemListWidget::polishEvent()
103 QGraphicsWidget::polishEvent();
106 void PlacesItemListWidget::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
108 KStandardItemListWidget::paint(painter
, option
, widget
);
110 if (m_drawCapacityBar
) {
111 const TextInfo
* textInfo
= m_textInfo
.value("text");
112 if (textInfo
) { // See KStandarItemListWidget::paint() for info on why we check textInfo.
115 const QRect
capacityRect(
117 option
->rect
.top() + option
->rect
.height() - CAPACITYBAR_HEIGHT
- CAPACITYBAR_MARGIN
,
118 qMin((qreal
)option
->rect
.width(), selectionRect().width()) - (textInfo
->pos
.x() - option
->rect
.left()),
122 const QPalette pal
= palette();
123 const QPalette::ColorGroup group
= isActiveWindow() ? QPalette::Active
: QPalette::Inactive
;
126 const QColor bgColor
= isSelected()
127 ? pal
.color(group
, QPalette::Highlight
).darker(180)
128 : pal
.color(group
, QPalette::Window
).darker(120);
130 painter
->fillRect(capacityRect
, bgColor
);
133 const QRect
fillRect(capacityRect
.x(), capacityRect
.y(), capacityRect
.width() * m_freeSpaceInfo
.usedRatio
, capacityRect
.height());
134 if (m_freeSpaceInfo
.usedRatio
>= 0.95) { // More than 95% full!
135 const QColor dangerUsedColor
= KColorScheme(group
, KColorScheme::View
).foreground(KColorScheme::NegativeText
).color();
136 painter
->fillRect(fillRect
, dangerUsedColor
);
138 const QPalette::ColorRole role
= isSelected() ? QPalette::HighlightedText
: QPalette::Highlight
;
139 const QColor normalUsedColor
= styleOption().palette
.color(group
, role
);
140 painter
->fillRect(fillRect
, normalUsedColor
);