]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/statusbarspaceinfo.cpp
Merge branch 'release/20.04'
[dolphin.git] / src / statusbar / statusbarspaceinfo.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) and *
3 * and Patrice Tremblay *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "statusbarspaceinfo.h"
22
23 #include "spaceinfoobserver.h"
24
25 #include <KLocalizedString>
26 #include <KNS3/KMoreToolsMenuFactory>
27
28 #include <QMouseEvent>
29
30 StatusBarSpaceInfo::StatusBarSpaceInfo(QWidget* parent) :
31 KCapacityBar(KCapacityBar::DrawTextInline, parent),
32 m_observer(nullptr)
33 {
34 setCursor(Qt::PointingHandCursor);
35 }
36
37 StatusBarSpaceInfo::~StatusBarSpaceInfo()
38 {
39 }
40
41 void StatusBarSpaceInfo::setShown(bool shown)
42 {
43 m_shown = shown;
44 if (!m_shown) {
45 hide();
46 m_ready = false;
47 }
48 }
49
50 void StatusBarSpaceInfo::setUrl(const QUrl& url)
51 {
52 if (m_url != url) {
53 m_url = url;
54 m_ready = false;
55 if (m_observer) {
56 m_observer.reset(new SpaceInfoObserver(m_url, this));
57 connect(m_observer.data(), &SpaceInfoObserver::valuesChanged, this, &StatusBarSpaceInfo::slotValuesChanged);
58 }
59 }
60 }
61
62 QUrl StatusBarSpaceInfo::url() const
63 {
64 return m_url;
65 }
66
67 void StatusBarSpaceInfo::update()
68 {
69 if (m_observer) {
70 m_observer->update();
71 }
72 }
73
74 void StatusBarSpaceInfo::showEvent(QShowEvent* event)
75 {
76 if (m_shown) {
77 if (m_ready) {
78 KCapacityBar::showEvent(event);
79 }
80
81 if (m_observer.isNull()) {
82 m_observer.reset(new SpaceInfoObserver(m_url, this));
83 connect(m_observer.data(), &SpaceInfoObserver::valuesChanged, this, &StatusBarSpaceInfo::slotValuesChanged);
84 }
85 }
86 }
87
88 void StatusBarSpaceInfo::hideEvent(QHideEvent* event)
89 {
90 if (m_ready) {
91 m_observer.reset();
92 m_ready = false;
93 }
94 KCapacityBar::hideEvent(event);
95 }
96
97 void StatusBarSpaceInfo::mousePressEvent(QMouseEvent* event)
98 {
99 if (event->button() == Qt::LeftButton) {
100 // Creates a menu with tools that help to find out more about free
101 // disk space for the given url.
102
103 // Note that this object must live long enough in case the user opens
104 // the "Configure..." dialog
105 KMoreToolsMenuFactory menuFactory(QStringLiteral("dolphin/statusbar-diskspace-menu"));
106 menuFactory.setParentWidget(this);
107 auto menu = menuFactory.createMenuFromGroupingNames(
108 { "disk-usage", "more:", "disk-partitions" }, m_url);
109
110 menu->exec(QCursor::pos());
111 }
112 }
113
114 void StatusBarSpaceInfo::slotValuesChanged()
115 {
116 Q_ASSERT(m_observer);
117 const quint64 size = m_observer->size();
118
119 if (!m_shown || size == 0) {
120 hide();
121 return;
122 }
123
124 m_ready = true;
125
126 const quint64 available = m_observer->available();
127 const quint64 used = size - available;
128 const int percentUsed = qRound(100.0 * qreal(used) / qreal(size));
129
130 setText(i18nc("@info:status Free disk space", "%1 free", KIO::convertSize(available)));
131 setUpdatesEnabled(false);
132 setValue(percentUsed);
133 setUpdatesEnabled(true);
134
135 if (!isVisible()) {
136 show();
137 } else {
138 update();
139 }
140 }
141