]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/statusbarspaceinfo.cpp
Avoid wrapping text in the status bar
[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 <KCapacityBar>
12 #include <KIO/ApplicationLauncherJob>
13 #include <KIO/Global>
14 #include <KLocalizedString>
15 #include <KService>
16
17 #include <QHBoxLayout>
18 #include <QMenu>
19 #include <QMouseEvent>
20 #include <QStorageInfo>
21 #include <QToolButton>
22
23 StatusBarSpaceInfo::StatusBarSpaceInfo(QWidget *parent)
24 : QWidget(parent)
25 , m_observer(nullptr)
26 {
27 m_capacityBar = new KCapacityBar(KCapacityBar::DrawTextInline, this);
28 m_textInfoButton = new QToolButton(this);
29 m_textInfoButton->setAutoRaise(true);
30 m_textInfoButton->setPopupMode(QToolButton::InstantPopup);
31 m_buttonMenu = new QMenu(this);
32 m_textInfoButton->setMenu(m_buttonMenu);
33 connect(m_buttonMenu, &QMenu::aboutToShow, this, &StatusBarSpaceInfo::updateMenu);
34
35 auto layout = new QHBoxLayout(this);
36 // We reduce the outside margin of the flat button so it visually has the same margin as the status bar text label on the other end of the bar.
37 layout->setContentsMargins(2, -1, 0, -1); // "-1" makes it so the fixed height won't be ignored.
38 layout->addWidget(m_capacityBar);
39 layout->addWidget(m_textInfoButton);
40 }
41
42 StatusBarSpaceInfo::~StatusBarSpaceInfo()
43 {
44 }
45
46 void StatusBarSpaceInfo::setShown(bool shown)
47 {
48 m_shown = shown;
49 if (!m_shown) {
50 hide();
51 m_ready = false;
52 }
53 }
54
55 void StatusBarSpaceInfo::setUrl(const QUrl &url)
56 {
57 if (m_url != url) {
58 m_url = url;
59 m_ready = false;
60 if (m_observer) {
61 m_observer.reset(new SpaceInfoObserver(m_url, this));
62 connect(m_observer.data(), &SpaceInfoObserver::valuesChanged, this, &StatusBarSpaceInfo::slotValuesChanged);
63 }
64 }
65 }
66
67 QUrl StatusBarSpaceInfo::url() const
68 {
69 return m_url;
70 }
71
72 void StatusBarSpaceInfo::update()
73 {
74 if (m_observer) {
75 m_observer->update();
76 }
77 }
78
79 void StatusBarSpaceInfo::showEvent(QShowEvent *event)
80 {
81 if (m_shown) {
82 if (m_ready) {
83 QWidget::showEvent(event);
84 }
85
86 if (m_observer.isNull()) {
87 m_observer.reset(new SpaceInfoObserver(m_url, this));
88 connect(m_observer.data(), &SpaceInfoObserver::valuesChanged, this, &StatusBarSpaceInfo::slotValuesChanged);
89 }
90 }
91 }
92
93 void StatusBarSpaceInfo::hideEvent(QHideEvent *event)
94 {
95 if (m_ready) {
96 m_observer.reset();
97 m_ready = false;
98 }
99 QWidget::hideEvent(event);
100 }
101
102 QSize StatusBarSpaceInfo::minimumSizeHint() const
103 {
104 return QSize();
105 }
106
107 void StatusBarSpaceInfo::updateMenu()
108 {
109 m_buttonMenu->clear();
110
111 // Creates a menu with tools that help to find out more about free
112 // disk space for the given url.
113
114 const KService::Ptr filelight = KService::serviceByDesktopName(QStringLiteral("org.kde.filelight"));
115 const KService::Ptr kdiskfree = KService::serviceByDesktopName(QStringLiteral("org.kde.kdf"));
116
117 if (!filelight && !kdiskfree) {
118 // nothing to show
119 return;
120 }
121
122 if (filelight) {
123 QAction *filelightFolderAction = m_buttonMenu->addAction(QIcon::fromTheme(QStringLiteral("filelight")), i18n("Disk Usage Statistics - current folder"));
124
125 m_buttonMenu->connect(filelightFolderAction, &QAction::triggered, m_buttonMenu, [this, filelight](bool) {
126 auto *job = new KIO::ApplicationLauncherJob(filelight);
127 job->setUrls({m_url});
128 job->start();
129 });
130
131 // For remote URLs like FTP analyzing the device makes no sense
132 if (m_url.isLocalFile()) {
133 QAction *filelightDiskAction =
134 m_buttonMenu->addAction(QIcon::fromTheme(QStringLiteral("filelight")), i18n("Disk Usage Statistics - current device"));
135
136 m_buttonMenu->connect(filelightDiskAction, &QAction::triggered, m_buttonMenu, [this, filelight](bool) {
137 const QStorageInfo info(m_url.toLocalFile());
138
139 if (info.isValid() && info.isReady()) {
140 auto *job = new KIO::ApplicationLauncherJob(filelight);
141 job->setUrls({QUrl::fromLocalFile(info.rootPath())});
142 job->start();
143 }
144 });
145 }
146
147 QAction *filelightAllAction = m_buttonMenu->addAction(QIcon::fromTheme(QStringLiteral("filelight")), i18n("Disk Usage Statistics - all devices"));
148
149 m_buttonMenu->connect(filelightAllAction, &QAction::triggered, m_buttonMenu, [this, filelight](bool) {
150 const QStorageInfo info(m_url.toLocalFile());
151
152 if (info.isValid() && info.isReady()) {
153 auto *job = new KIO::ApplicationLauncherJob(filelight);
154 job->start();
155 }
156 });
157 }
158
159 if (kdiskfree) {
160 QAction *kdiskfreeAction = m_buttonMenu->addAction(QIcon::fromTheme(QStringLiteral("kdf")), i18n("KDiskFree"));
161
162 connect(kdiskfreeAction, &QAction::triggered, this, [kdiskfree] {
163 auto *job = new KIO::ApplicationLauncherJob(kdiskfree);
164 job->start();
165 });
166 }
167 }
168
169 void StatusBarSpaceInfo::slotValuesChanged()
170 {
171 Q_ASSERT(m_observer);
172 const quint64 size = m_observer->size();
173
174 if (!m_shown || size == 0) {
175 hide();
176 return;
177 }
178
179 m_ready = true;
180
181 const quint64 available = m_observer->available();
182 const quint64 used = size - available;
183 const int percentUsed = qRound(100.0 * qreal(used) / qreal(size));
184
185 m_textInfoButton->setText(i18nc("@info:status Free disk space", "%1 free", KIO::convertSize(available)));
186 setToolTip(i18nc("tooltip:status Free disk space", "%1 free out of %2 (%3% used)", KIO::convertSize(available), KIO::convertSize(size), percentUsed));
187 m_textInfoButton->setToolTip(i18nc("@info:tooltip for the free disk space button",
188 "%1 free out of %2 (%3% used)\nPress to manage disk space usage.",
189 KIO::convertSize(available),
190 KIO::convertSize(size),
191 percentUsed));
192 setUpdatesEnabled(false);
193 m_capacityBar->setValue(percentUsed);
194 setUpdatesEnabled(true);
195
196 if (!isVisible()) {
197 show();
198 } else {
199 update();
200 }
201 }
202
203 #include "moc_statusbarspaceinfo.cpp"