]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/statusbarspaceinfo.cpp
3ac87925fcc3f4121018920f8becdfb3c1fa8751
[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::setUrl(const QUrl& url)
42 {
43 if (m_url != url) {
44 m_url = url;
45 if (m_observer) {
46 m_observer->setUrl(url);
47 }
48 }
49 }
50
51 QUrl StatusBarSpaceInfo::url() const
52 {
53 return m_url;
54 }
55
56 void StatusBarSpaceInfo::update()
57 {
58 if (m_observer) {
59 m_observer->update();
60 }
61 }
62
63 void StatusBarSpaceInfo::showEvent(QShowEvent* event)
64 {
65 KCapacityBar::showEvent(event);
66 m_observer.reset(new SpaceInfoObserver(m_url, this));
67 slotValuesChanged();
68 connect(m_observer.data(), &SpaceInfoObserver::valuesChanged, this, &StatusBarSpaceInfo::slotValuesChanged);
69 }
70
71 void StatusBarSpaceInfo::hideEvent(QHideEvent* event)
72 {
73 m_observer.reset();
74 KCapacityBar::hideEvent(event);
75 }
76
77 void StatusBarSpaceInfo::mousePressEvent(QMouseEvent* event)
78 {
79 if (event->button() == Qt::LeftButton) {
80 // Creates a menu with tools that help to find out more about free
81 // disk space for the given url.
82
83 // Note that this object must live long enough in case the user opens
84 // the "Configure..." dialog
85 KMoreToolsMenuFactory menuFactory(QStringLiteral("dolphin/statusbar-diskspace-menu"));
86 menuFactory.setParentWidget(this);
87 auto menu = menuFactory.createMenuFromGroupingNames(
88 { "disk-usage", "more:", "disk-partitions" }, m_url);
89
90 menu->exec(QCursor::pos());
91 }
92 }
93
94 void StatusBarSpaceInfo::slotValuesChanged()
95 {
96 Q_ASSERT(m_observer);
97 const quint64 size = m_observer->size();
98 if (size == 0) {
99 setText(i18nc("@info:status", "Unknown size"));
100 setValue(0);
101 update();
102 } else {
103 const quint64 available = m_observer->available();
104 const quint64 used = size - available;
105 const int percentUsed = qRound(100.0 * qreal(used) / qreal(size));
106
107 setText(i18nc("@info:status Free disk space", "%1 free", KIO::convertSize(available)));
108 setUpdatesEnabled(false);
109 setValue(percentUsed);
110 setUpdatesEnabled(true);
111 update();
112 }
113 }
114