]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/statusbarspaceinfo.cpp
1debaec2976f97b5bd12e09b20299760a067d3e7
[dolphin.git] / src / statusbar / statusbarspaceinfo.cpp
1 /*
2 * SPDX-FileCopyrightText: 2006 Peter Penz (peter.penz@gmx.at) and Patrice Tremblay
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "statusbarspaceinfo.h"
8
9 #include "spaceinfoobserver.h"
10
11 #include <KLocalizedString>
12 #include <KMoreToolsMenuFactory>
13
14 #include <KIO/Global>
15 #include <QMouseEvent>
16
17 StatusBarSpaceInfo::StatusBarSpaceInfo(QWidget* parent) :
18 KCapacityBar(KCapacityBar::DrawTextInline, parent),
19 m_observer(nullptr)
20 {
21 setCursor(Qt::PointingHandCursor);
22 }
23
24 StatusBarSpaceInfo::~StatusBarSpaceInfo()
25 {
26 }
27
28 void StatusBarSpaceInfo::setShown(bool shown)
29 {
30 m_shown = shown;
31 if (!m_shown) {
32 hide();
33 m_ready = false;
34 }
35 }
36
37 void StatusBarSpaceInfo::setUrl(const QUrl& url)
38 {
39 if (m_url != url) {
40 m_url = url;
41 m_ready = false;
42 if (m_observer) {
43 m_observer.reset(new SpaceInfoObserver(m_url, this));
44 connect(m_observer.data(), &SpaceInfoObserver::valuesChanged, this, &StatusBarSpaceInfo::slotValuesChanged);
45 }
46 }
47 }
48
49 QUrl StatusBarSpaceInfo::url() const
50 {
51 return m_url;
52 }
53
54 void StatusBarSpaceInfo::update()
55 {
56 if (m_observer) {
57 m_observer->update();
58 }
59 }
60
61 void StatusBarSpaceInfo::showEvent(QShowEvent* event)
62 {
63 if (m_shown) {
64 if (m_ready) {
65 KCapacityBar::showEvent(event);
66 }
67
68 if (m_observer.isNull()) {
69 m_observer.reset(new SpaceInfoObserver(m_url, this));
70 connect(m_observer.data(), &SpaceInfoObserver::valuesChanged, this, &StatusBarSpaceInfo::slotValuesChanged);
71 }
72 }
73 }
74
75 void StatusBarSpaceInfo::hideEvent(QHideEvent* event)
76 {
77 if (m_ready) {
78 m_observer.reset();
79 m_ready = false;
80 }
81 KCapacityBar::hideEvent(event);
82 }
83
84 void StatusBarSpaceInfo::mousePressEvent(QMouseEvent* event)
85 {
86 if (event->button() == Qt::LeftButton) {
87 // Creates a menu with tools that help to find out more about free
88 // disk space for the given url.
89
90 // Note that this object must live long enough in case the user opens
91 // the "Configure..." dialog
92 KMoreToolsMenuFactory menuFactory(QStringLiteral("dolphin/statusbar-diskspace-menu"));
93 menuFactory.setParentWidget(this);
94 auto menu = menuFactory.createMenuFromGroupingNames(
95 { "disk-usage", "more:", "disk-partitions" }, m_url);
96
97 menu->exec(QCursor::pos());
98 }
99 }
100
101 void StatusBarSpaceInfo::slotValuesChanged()
102 {
103 Q_ASSERT(m_observer);
104 const quint64 size = m_observer->size();
105
106 if (!m_shown || size == 0) {
107 hide();
108 return;
109 }
110
111 m_ready = true;
112
113 const quint64 available = m_observer->available();
114 const quint64 used = size - available;
115 const int percentUsed = qRound(100.0 * qreal(used) / qreal(size));
116
117 setText(i18nc("@info:status Free disk space", "%1 free", KIO::convertSize(available)));
118 setToolTip(i18nc("tooltip:status Free disk space", "%1 free out of %2 (%3% used)", KIO::convertSize(available), KIO::convertSize(size), percentUsed));
119 setUpdatesEnabled(false);
120 setValue(percentUsed);
121 setUpdatesEnabled(true);
122
123 if (!isVisible()) {
124 show();
125 } else {
126 update();
127 }
128 }
129