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