]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/dolphinstatusbar.cpp
Offer installing Filelight if no disk usage analyzer was found
[dolphin.git] / src / statusbar / dolphinstatusbar.cpp
1 /*
2 * SPDX-FileCopyrightText: 2006-2012 Peter Penz <peter.penz19@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "dolphinstatusbar.h"
8
9 #include "dolphin_generalsettings.h"
10 #include "statusbarspaceinfo.h"
11 #include "views/dolphinview.h"
12 #include "views/zoomlevelinfo.h"
13
14 #include <KLocalizedString>
15 #include <KSqueezedTextLabel>
16
17 #include <QApplication>
18 #include <QHBoxLayout>
19 #include <QHelpEvent>
20 #include <QIcon>
21 #include <QMenu>
22 #include <QPainter>
23 #include <QProgressBar>
24 #include <QSlider>
25 #include <QStyleOption>
26 #include <QTimer>
27 #include <QToolButton>
28
29 namespace
30 {
31 const int UpdateDelay = 50;
32 }
33
34 DolphinStatusBar::DolphinStatusBar(QWidget *parent)
35 : AnimatedHeightWidget(parent)
36 , m_text()
37 , m_defaultText()
38 , m_label(nullptr)
39 , m_zoomLabel(nullptr)
40 , m_spaceInfo(nullptr)
41 , m_zoomSlider(nullptr)
42 , m_progressBar(nullptr)
43 , m_stopButton(nullptr)
44 , m_progress(100)
45 , m_showProgressBarTimer(nullptr)
46 , m_delayUpdateTimer(nullptr)
47 , m_textTimestamp()
48 {
49 setProperty("_breeze_statusbar_separator", true);
50
51 QWidget *contentsContainer = prepareContentsContainer();
52
53 // Initialize text label
54 m_label = new KSqueezedTextLabel(m_text, contentsContainer);
55 m_label->setTextFormat(Qt::PlainText);
56
57 // Initialize zoom slider's explanatory label
58 m_zoomLabel = new KSqueezedTextLabel(i18nc("Used as a noun, i.e. 'Here is the zoom level:'", "Zoom:"), contentsContainer);
59
60 // Initialize zoom widget
61 m_zoomSlider = new QSlider(Qt::Horizontal, contentsContainer);
62 m_zoomSlider->setAccessibleName(i18n("Zoom"));
63 m_zoomSlider->setAccessibleDescription(i18nc("Description for zoom-slider (accessibility)", "Sets the size of the file icons."));
64 m_zoomSlider->setPageStep(1);
65 m_zoomSlider->setRange(ZoomLevelInfo::minimumLevel(), ZoomLevelInfo::maximumLevel());
66
67 connect(m_zoomSlider, &QSlider::valueChanged, this, &DolphinStatusBar::zoomLevelChanged);
68 connect(m_zoomSlider, &QSlider::valueChanged, this, &DolphinStatusBar::updateZoomSliderToolTip);
69 connect(m_zoomSlider, &QSlider::sliderMoved, this, &DolphinStatusBar::showZoomSliderToolTip);
70
71 // Initialize space information
72 m_spaceInfo = new StatusBarSpaceInfo(contentsContainer);
73
74 // Initialize progress information
75 m_stopButton = new QToolButton(contentsContainer);
76 m_stopButton->setIcon(QIcon::fromTheme(QStringLiteral("process-stop")));
77 m_stopButton->setAccessibleName(i18n("Stop"));
78 m_stopButton->setAutoRaise(true);
79 m_stopButton->setToolTip(i18nc("@tooltip", "Stop loading"));
80 m_stopButton->hide();
81 connect(m_stopButton, &QToolButton::clicked, this, &DolphinStatusBar::stopPressed);
82
83 m_progressTextLabel = new QLabel(contentsContainer);
84 m_progressTextLabel->hide();
85
86 m_progressBar = new QProgressBar(contentsContainer);
87 m_progressBar->hide();
88
89 m_showProgressBarTimer = new QTimer(this);
90 m_showProgressBarTimer->setInterval(500);
91 m_showProgressBarTimer->setSingleShot(true);
92 connect(m_showProgressBarTimer, &QTimer::timeout, this, &DolphinStatusBar::updateProgressInfo);
93
94 // initialize text updater delay timer
95 m_delayUpdateTimer = new QTimer(this);
96 m_delayUpdateTimer->setInterval(UpdateDelay);
97 m_delayUpdateTimer->setSingleShot(true);
98 connect(m_delayUpdateTimer, &QTimer::timeout, this, &DolphinStatusBar::updateLabelText);
99
100 // Initialize top layout and size policies
101 const int fontHeight = QFontMetrics(m_label->font()).height();
102 const int zoomSliderHeight = m_zoomSlider->minimumSizeHint().height();
103 const int buttonHeight = m_stopButton->height();
104 const int contentHeight = qMax(qMax(fontHeight, zoomSliderHeight), buttonHeight);
105
106 QFontMetrics fontMetrics(m_label->font());
107
108 m_label->setFixedHeight(contentHeight);
109 m_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
110
111 m_zoomSlider->setMaximumWidth(fontMetrics.averageCharWidth() * 15);
112
113 m_spaceInfo->setFixedHeight(contentHeight);
114 m_spaceInfo->setMaximumWidth(fontMetrics.averageCharWidth() * 25);
115 m_spaceInfo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
116
117 m_progressBar->setFixedHeight(zoomSliderHeight);
118 m_progressBar->setMaximumWidth(fontMetrics.averageCharWidth() * 20);
119
120 m_topLayout = new QHBoxLayout(contentsContainer);
121 updateContentsMargins();
122 m_topLayout->setSpacing(4);
123 m_topLayout->addWidget(m_label, 1);
124 m_topLayout->addWidget(m_zoomLabel);
125 m_topLayout->addWidget(m_zoomSlider, 1);
126 m_topLayout->addWidget(m_spaceInfo, 1);
127 m_topLayout->addWidget(m_stopButton);
128 m_topLayout->addWidget(m_progressTextLabel);
129 m_topLayout->addWidget(m_progressBar);
130
131 setVisible(GeneralSettings::showStatusBar(), WithoutAnimation);
132 setExtensionsVisible(true);
133 setWhatsThis(xi18nc("@info:whatsthis Statusbar",
134 "<para>This is "
135 "the <emphasis>Statusbar</emphasis>. It contains three elements "
136 "by default (left to right):<list><item>A <emphasis>text field"
137 "</emphasis> that displays the size of selected items. If only "
138 "one item is selected the name and type is shown as well.</item>"
139 "<item>A <emphasis>zoom slider</emphasis> that allows you "
140 "to adjust the size of the icons in the view.</item>"
141 "<item><emphasis>Space information</emphasis> about the "
142 "current storage device.</item></list></para>"));
143 }
144
145 DolphinStatusBar::~DolphinStatusBar()
146 {
147 }
148
149 void DolphinStatusBar::setText(const QString &text)
150 {
151 if (m_text == text) {
152 return;
153 }
154
155 m_textTimestamp = QTime::currentTime();
156
157 m_text = text;
158 // will update status bar text in 50ms
159 m_delayUpdateTimer->start();
160 }
161
162 QString DolphinStatusBar::text() const
163 {
164 return m_text;
165 }
166
167 void DolphinStatusBar::setProgressText(const QString &text)
168 {
169 m_progressTextLabel->setText(text);
170 }
171
172 QString DolphinStatusBar::progressText() const
173 {
174 return m_progressTextLabel->text();
175 }
176
177 void DolphinStatusBar::setProgress(int percent)
178 {
179 // Show a busy indicator if a value < 0 is provided:
180 m_progressBar->setMaximum((percent < 0) ? 0 : 100);
181
182 percent = qBound(0, percent, 100);
183 const bool progressRestarted = (percent < 100) && (percent < m_progress);
184 m_progress = percent;
185 if (progressRestarted && !m_progressBar->isVisible()) {
186 // Show the progress bar delayed: In the case if 100 % are reached within
187 // a short time, no progress bar will be shown at all.
188 m_showProgressBarTimer->start();
189 }
190
191 m_progressBar->setValue(m_progress);
192 if (percent == 100) {
193 // The end of the progress has been reached. Assure that the progress bar
194 // gets hidden and the extensions widgets get visible again.
195 m_showProgressBarTimer->stop();
196 updateProgressInfo();
197 }
198 }
199
200 int DolphinStatusBar::progress() const
201 {
202 return m_progress;
203 }
204
205 void DolphinStatusBar::resetToDefaultText()
206 {
207 m_text.clear();
208
209 QTime currentTime;
210 if (currentTime.msecsTo(m_textTimestamp) < UpdateDelay) {
211 m_delayUpdateTimer->start();
212 } else {
213 updateLabelText();
214 }
215 }
216
217 void DolphinStatusBar::setDefaultText(const QString &text)
218 {
219 m_defaultText = text;
220 updateLabelText();
221 }
222
223 QString DolphinStatusBar::defaultText() const
224 {
225 return m_defaultText;
226 }
227
228 void DolphinStatusBar::setUrl(const QUrl &url)
229 {
230 if (GeneralSettings::showSpaceInfo()) {
231 m_spaceInfo->setUrl(url);
232 }
233 }
234
235 QUrl DolphinStatusBar::url() const
236 {
237 return m_spaceInfo->url();
238 }
239
240 void DolphinStatusBar::setZoomLevel(int zoomLevel)
241 {
242 if (zoomLevel != m_zoomSlider->value()) {
243 m_zoomSlider->setValue(zoomLevel);
244 }
245 }
246
247 int DolphinStatusBar::zoomLevel() const
248 {
249 return m_zoomSlider->value();
250 }
251
252 void DolphinStatusBar::readSettings()
253 {
254 setVisible(GeneralSettings::showStatusBar(), WithAnimation);
255 setExtensionsVisible(true);
256 }
257
258 void DolphinStatusBar::updateSpaceInfo()
259 {
260 m_spaceInfo->update();
261 }
262
263 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent *event)
264 {
265 Q_UNUSED(event)
266
267 QMenu menu(this);
268
269 QAction *showZoomSliderAction = menu.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
270 showZoomSliderAction->setCheckable(true);
271 showZoomSliderAction->setChecked(GeneralSettings::showZoomSlider());
272
273 QAction *showSpaceInfoAction = menu.addAction(i18nc("@action:inmenu", "Show Space Information"));
274 showSpaceInfoAction->setCheckable(true);
275 showSpaceInfoAction->setChecked(GeneralSettings::showSpaceInfo());
276
277 const QAction *action = menu.exec(event->reason() == QContextMenuEvent::Reason::Mouse ? QCursor::pos() : mapToGlobal(QPoint(width() / 2, height() / 2)));
278 if (action == showZoomSliderAction) {
279 const bool visible = showZoomSliderAction->isChecked();
280 GeneralSettings::setShowZoomSlider(visible);
281 m_zoomSlider->setVisible(visible);
282 m_zoomLabel->setVisible(visible);
283 } else if (action == showSpaceInfoAction) {
284 const bool visible = showSpaceInfoAction->isChecked();
285 GeneralSettings::setShowSpaceInfo(visible);
286 m_spaceInfo->setVisible(visible);
287 }
288 updateContentsMargins();
289 }
290
291 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel)
292 {
293 updateZoomSliderToolTip(zoomLevel);
294
295 QPoint global = m_zoomSlider->rect().topLeft();
296 global.ry() += m_zoomSlider->height() / 2;
297 QHelpEvent toolTipEvent(QEvent::ToolTip, QPoint(0, 0), m_zoomSlider->mapToGlobal(global));
298 QApplication::sendEvent(m_zoomSlider, &toolTipEvent);
299 }
300
301 void DolphinStatusBar::updateProgressInfo()
302 {
303 if (m_progress < 100) {
304 // Show the progress information and hide the extensions
305 m_stopButton->show();
306 m_progressTextLabel->show();
307 m_progressBar->show();
308 setExtensionsVisible(false);
309 } else {
310 // Hide the progress information and show the extensions
311 m_stopButton->hide();
312 m_progressTextLabel->hide();
313 m_progressBar->hide();
314 setExtensionsVisible(true);
315 }
316 }
317
318 void DolphinStatusBar::updateLabelText()
319 {
320 const QString text = m_text.isEmpty() ? m_defaultText : m_text;
321 m_label->setText(text);
322 }
323
324 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel)
325 {
326 const int size = ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel);
327 m_zoomSlider->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size));
328 }
329
330 void DolphinStatusBar::setExtensionsVisible(bool visible)
331 {
332 bool showSpaceInfo = visible;
333 bool showZoomSlider = visible;
334 if (visible) {
335 showSpaceInfo = GeneralSettings::showSpaceInfo();
336 showZoomSlider = GeneralSettings::showZoomSlider();
337 }
338
339 m_spaceInfo->setShown(showSpaceInfo);
340 m_spaceInfo->setVisible(showSpaceInfo);
341 m_zoomSlider->setVisible(showZoomSlider);
342 m_zoomLabel->setVisible(showZoomSlider);
343 updateContentsMargins();
344 }
345
346 void DolphinStatusBar::updateContentsMargins()
347 {
348 if (GeneralSettings::showSpaceInfo()) {
349 // We reduce the outside margin for the flat button so it visually has the same margin as the status bar text label on the other end of the bar.
350 m_topLayout->setContentsMargins(6, 0, 2, 0);
351 } else {
352 m_topLayout->setContentsMargins(6, 0, 6, 0);
353 }
354 }
355
356 void DolphinStatusBar::paintEvent(QPaintEvent *paintEvent)
357 {
358 Q_UNUSED(paintEvent)
359 QPainter p(this);
360 QStyleOption opt;
361 opt.initFrom(this);
362 style()->drawPrimitive(QStyle::PE_PanelStatusBar, &opt, &p, this);
363 }
364
365 int DolphinStatusBar::preferredHeight() const
366 {
367 return m_spaceInfo->height();
368 }
369
370 #include "moc_dolphinstatusbar.cpp"