]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/dolphinstatusbar.cpp
Improve Filelight installation UX
[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 connect(m_spaceInfo, &StatusBarSpaceInfo::showMessage, this, &DolphinStatusBar::showMessage);
74 connect(m_spaceInfo,
75 &StatusBarSpaceInfo::showInstallationProgress,
76 this,
77 [this](const QString &currentlyRunningTaskTitle, int installationProgressPercent) {
78 showProgress(currentlyRunningTaskTitle, installationProgressPercent, CancelLoading::Disallowed);
79 });
80
81 // Initialize progress information
82 m_stopButton = new QToolButton(contentsContainer);
83 m_stopButton->setIcon(QIcon::fromTheme(QStringLiteral("process-stop")));
84 m_stopButton->setAccessibleName(i18n("Stop"));
85 m_stopButton->setAutoRaise(true);
86 m_stopButton->setToolTip(i18nc("@tooltip", "Stop loading"));
87 m_stopButton->hide();
88 connect(m_stopButton, &QToolButton::clicked, this, &DolphinStatusBar::stopPressed);
89
90 m_progressTextLabel = new QLabel(contentsContainer);
91 m_progressTextLabel->hide();
92
93 m_progressBar = new QProgressBar(contentsContainer);
94 m_progressBar->hide();
95
96 m_showProgressBarTimer = new QTimer(this);
97 m_showProgressBarTimer->setInterval(500);
98 m_showProgressBarTimer->setSingleShot(true);
99 connect(m_showProgressBarTimer, &QTimer::timeout, this, &DolphinStatusBar::updateProgressInfo);
100
101 // initialize text updater delay timer
102 m_delayUpdateTimer = new QTimer(this);
103 m_delayUpdateTimer->setInterval(UpdateDelay);
104 m_delayUpdateTimer->setSingleShot(true);
105 connect(m_delayUpdateTimer, &QTimer::timeout, this, &DolphinStatusBar::updateLabelText);
106
107 // Initialize top layout and size policies
108 const int fontHeight = QFontMetrics(m_label->font()).height();
109 const int zoomSliderHeight = m_zoomSlider->minimumSizeHint().height();
110 const int buttonHeight = m_stopButton->height();
111 const int contentHeight = qMax(qMax(fontHeight, zoomSliderHeight), buttonHeight);
112
113 QFontMetrics fontMetrics(m_label->font());
114
115 m_label->setFixedHeight(contentHeight);
116 m_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
117
118 m_zoomSlider->setMaximumWidth(fontMetrics.averageCharWidth() * 15);
119
120 m_spaceInfo->setFixedHeight(contentHeight);
121 m_spaceInfo->setMaximumWidth(fontMetrics.averageCharWidth() * 25);
122 m_spaceInfo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
123
124 m_progressBar->setFixedHeight(zoomSliderHeight);
125 m_progressBar->setMaximumWidth(fontMetrics.averageCharWidth() * 20);
126
127 m_topLayout = new QHBoxLayout(contentsContainer);
128 updateContentsMargins();
129 m_topLayout->setSpacing(4);
130 m_topLayout->addWidget(m_label, 1);
131 m_topLayout->addWidget(m_zoomLabel);
132 m_topLayout->addWidget(m_zoomSlider, 1);
133 m_topLayout->addWidget(m_spaceInfo, 1);
134 m_topLayout->addWidget(m_stopButton);
135 m_topLayout->addWidget(m_progressTextLabel);
136 m_topLayout->addWidget(m_progressBar);
137
138 setVisible(GeneralSettings::showStatusBar(), WithoutAnimation);
139 setExtensionsVisible(true);
140 setWhatsThis(xi18nc("@info:whatsthis Statusbar",
141 "<para>This is "
142 "the <emphasis>Statusbar</emphasis>. It contains three elements "
143 "by default (left to right):<list><item>A <emphasis>text field"
144 "</emphasis> that displays the size of selected items. If only "
145 "one item is selected the name and type is shown as well.</item>"
146 "<item>A <emphasis>zoom slider</emphasis> that allows you "
147 "to adjust the size of the icons in the view.</item>"
148 "<item><emphasis>Space information</emphasis> about the "
149 "current storage device.</item></list></para>"));
150 }
151
152 DolphinStatusBar::~DolphinStatusBar()
153 {
154 }
155
156 void DolphinStatusBar::setText(const QString &text)
157 {
158 if (m_text == text) {
159 return;
160 }
161
162 m_textTimestamp = QTime::currentTime();
163
164 m_text = text;
165 // will update status bar text in 50ms
166 m_delayUpdateTimer->start();
167 }
168
169 QString DolphinStatusBar::text() const
170 {
171 return m_text;
172 }
173
174 void DolphinStatusBar::showProgress(const QString &currentlyRunningTaskTitle, int progressPercent, CancelLoading cancelLoading)
175 {
176 m_cancelLoading = cancelLoading;
177
178 // Show a busy indicator if a value < 0 is provided:
179 m_progressBar->setMaximum((progressPercent < 0) ? 0 : 100);
180
181 progressPercent = qBound(0, progressPercent, 100);
182 if (!m_progressBar->isVisible()) {
183 // Show the progress bar delayed: In the case if 100 % are reached within
184 // a short time, no progress bar will be shown at all.
185 if (!m_showProgressBarTimer->isActive()) {
186 m_showProgressBarTimer->start();
187 } else {
188 // The timer is already running. Should we restart it or keep it running?
189 if (m_progressTextLabel->text() != currentlyRunningTaskTitle || (progressPercent < 100 && progressPercent < m_progress)) {
190 m_showProgressBarTimer->start();
191 }
192 }
193 }
194 m_progress = progressPercent;
195
196 m_progressBar->setValue(m_progress);
197 if (progressPercent == 100) {
198 // The end of the progress has been reached. Assure that the progress bar
199 // gets hidden and the extensions widgets get visible again.
200 m_showProgressBarTimer->stop();
201 updateProgressInfo();
202 }
203
204 m_progressTextLabel->setText(currentlyRunningTaskTitle);
205 }
206
207 QString DolphinStatusBar::progressText() const
208 {
209 return m_progressTextLabel->text();
210 }
211
212 int DolphinStatusBar::progress() const
213 {
214 return m_progress;
215 }
216
217 void DolphinStatusBar::resetToDefaultText()
218 {
219 m_text.clear();
220
221 QTime currentTime;
222 if (currentTime.msecsTo(m_textTimestamp) < UpdateDelay) {
223 m_delayUpdateTimer->start();
224 } else {
225 updateLabelText();
226 }
227 }
228
229 void DolphinStatusBar::setDefaultText(const QString &text)
230 {
231 m_defaultText = text;
232 updateLabelText();
233 }
234
235 QString DolphinStatusBar::defaultText() const
236 {
237 return m_defaultText;
238 }
239
240 void DolphinStatusBar::setUrl(const QUrl &url)
241 {
242 if (GeneralSettings::showSpaceInfo()) {
243 m_spaceInfo->setUrl(url);
244 }
245 }
246
247 QUrl DolphinStatusBar::url() const
248 {
249 return m_spaceInfo->url();
250 }
251
252 void DolphinStatusBar::setZoomLevel(int zoomLevel)
253 {
254 if (zoomLevel != m_zoomSlider->value()) {
255 m_zoomSlider->setValue(zoomLevel);
256 }
257 }
258
259 int DolphinStatusBar::zoomLevel() const
260 {
261 return m_zoomSlider->value();
262 }
263
264 void DolphinStatusBar::readSettings()
265 {
266 setVisible(GeneralSettings::showStatusBar(), WithAnimation);
267 setExtensionsVisible(true);
268 }
269
270 void DolphinStatusBar::updateSpaceInfo()
271 {
272 m_spaceInfo->update();
273 }
274
275 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent *event)
276 {
277 Q_UNUSED(event)
278
279 QMenu menu(this);
280
281 QAction *showZoomSliderAction = menu.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
282 showZoomSliderAction->setCheckable(true);
283 showZoomSliderAction->setChecked(GeneralSettings::showZoomSlider());
284
285 QAction *showSpaceInfoAction = menu.addAction(i18nc("@action:inmenu", "Show Space Information"));
286 showSpaceInfoAction->setCheckable(true);
287 showSpaceInfoAction->setChecked(GeneralSettings::showSpaceInfo());
288
289 const QAction *action = menu.exec(event->reason() == QContextMenuEvent::Reason::Mouse ? QCursor::pos() : mapToGlobal(QPoint(width() / 2, height() / 2)));
290 if (action == showZoomSliderAction) {
291 const bool visible = showZoomSliderAction->isChecked();
292 GeneralSettings::setShowZoomSlider(visible);
293 m_zoomSlider->setVisible(visible);
294 m_zoomLabel->setVisible(visible);
295 } else if (action == showSpaceInfoAction) {
296 const bool visible = showSpaceInfoAction->isChecked();
297 GeneralSettings::setShowSpaceInfo(visible);
298 m_spaceInfo->setVisible(visible);
299 }
300 updateContentsMargins();
301 }
302
303 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel)
304 {
305 updateZoomSliderToolTip(zoomLevel);
306
307 QPoint global = m_zoomSlider->rect().topLeft();
308 global.ry() += m_zoomSlider->height() / 2;
309 QHelpEvent toolTipEvent(QEvent::ToolTip, QPoint(0, 0), m_zoomSlider->mapToGlobal(global));
310 QApplication::sendEvent(m_zoomSlider, &toolTipEvent);
311 }
312
313 void DolphinStatusBar::updateProgressInfo()
314 {
315 if (m_progress < 100) {
316 // Show the progress information and hide the extensions
317 m_stopButton->setVisible(m_cancelLoading == CancelLoading::Allowed);
318 m_progressTextLabel->show();
319 m_progressBar->show();
320 setExtensionsVisible(false);
321 } else {
322 // Hide the progress information and show the extensions
323 m_stopButton->hide();
324 m_progressTextLabel->hide();
325 m_progressBar->hide();
326 setExtensionsVisible(true);
327 }
328 }
329
330 void DolphinStatusBar::updateLabelText()
331 {
332 const QString text = m_text.isEmpty() ? m_defaultText : m_text;
333 m_label->setText(text);
334 }
335
336 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel)
337 {
338 const int size = ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel);
339 m_zoomSlider->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size));
340 }
341
342 void DolphinStatusBar::setExtensionsVisible(bool visible)
343 {
344 bool showSpaceInfo = visible;
345 bool showZoomSlider = visible;
346 if (visible) {
347 showSpaceInfo = GeneralSettings::showSpaceInfo();
348 showZoomSlider = GeneralSettings::showZoomSlider();
349 }
350
351 m_spaceInfo->setShown(showSpaceInfo);
352 m_spaceInfo->setVisible(showSpaceInfo);
353 m_zoomSlider->setVisible(showZoomSlider);
354 m_zoomLabel->setVisible(showZoomSlider);
355 updateContentsMargins();
356 }
357
358 void DolphinStatusBar::updateContentsMargins()
359 {
360 if (GeneralSettings::showSpaceInfo()) {
361 // 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.
362 m_topLayout->setContentsMargins(6, 0, 2, 0);
363 } else {
364 m_topLayout->setContentsMargins(6, 0, 6, 0);
365 }
366 }
367
368 void DolphinStatusBar::paintEvent(QPaintEvent *paintEvent)
369 {
370 Q_UNUSED(paintEvent)
371 QPainter p(this);
372 QStyleOption opt;
373 opt.initFrom(this);
374 style()->drawPrimitive(QStyle::PE_PanelStatusBar, &opt, &p, this);
375 }
376
377 int DolphinStatusBar::preferredHeight() const
378 {
379 return m_spaceInfo->height();
380 }
381
382 #include "moc_dolphinstatusbar.cpp"