]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/dolphinstatusbar.cpp
Apply 1 suggestion(s) to 1 file(s)
[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 <QPainterPath>
24 #include <QProgressBar>
25 #include <QSlider>
26 #include <QStyleOption>
27 #include <QTimer>
28 #include <QToolButton>
29
30 namespace
31 {
32 const int UpdateDelay = 50;
33 }
34
35 DolphinStatusBar::DolphinStatusBar(QWidget *parent)
36 : AnimatedHeightWidget(parent)
37 , m_text()
38 , m_defaultText()
39 , m_label(nullptr)
40 , m_zoomLabel(nullptr)
41 , m_spaceInfo(nullptr)
42 , m_zoomSlider(nullptr)
43 , m_progressBar(nullptr)
44 , m_stopButton(nullptr)
45 , m_progress(100)
46 , m_showProgressBarTimer(nullptr)
47 , m_delayUpdateTimer(nullptr)
48 , m_textTimestamp()
49 {
50 setProperty("_breeze_statusbar_separator", true);
51
52 QWidget *contentsContainer = prepareContentsContainer();
53
54 // Initialize text label
55 m_label = new KSqueezedTextLabel(m_text, contentsContainer);
56 m_label->setTextFormat(Qt::PlainText);
57 m_label->setTextInteractionFlags(Qt::TextBrowserInteraction | Qt::TextSelectableByKeyboard); // for accessibility but also to allow copy-pasting this text.
58
59 // Initialize zoom slider's explanatory label
60 m_zoomLabel = new KSqueezedTextLabel(i18nc("Used as a noun, i.e. 'Here is the zoom level:'", "Zoom:"), contentsContainer);
61
62 // Initialize zoom widget
63 m_zoomSlider = new QSlider(Qt::Horizontal, contentsContainer);
64 m_zoomSlider->setAccessibleName(i18n("Zoom"));
65 m_zoomSlider->setAccessibleDescription(i18nc("Description for zoom-slider (accessibility)", "Sets the size of the file icons."));
66 m_zoomSlider->setPageStep(1);
67 m_zoomSlider->setRange(ZoomLevelInfo::minimumLevel(), ZoomLevelInfo::maximumLevel());
68
69 connect(m_zoomSlider, &QSlider::valueChanged, this, &DolphinStatusBar::zoomLevelChanged);
70 connect(m_zoomSlider, &QSlider::valueChanged, this, &DolphinStatusBar::updateZoomSliderToolTip);
71 connect(m_zoomSlider, &QSlider::sliderMoved, this, &DolphinStatusBar::showZoomSliderToolTip);
72
73 // Initialize space information
74 m_spaceInfo = new StatusBarSpaceInfo(contentsContainer);
75 connect(m_spaceInfo, &StatusBarSpaceInfo::showMessage, this, &DolphinStatusBar::showMessage);
76 connect(m_spaceInfo,
77 &StatusBarSpaceInfo::showInstallationProgress,
78 this,
79 [this](const QString &currentlyRunningTaskTitle, int installationProgressPercent) {
80 showProgress(currentlyRunningTaskTitle, installationProgressPercent, CancelLoading::Disallowed);
81 });
82
83 // Initialize progress information
84 m_stopButton = new QToolButton(contentsContainer);
85 m_stopButton->setIcon(QIcon::fromTheme(QStringLiteral("process-stop")));
86 m_stopButton->setAccessibleName(i18n("Stop"));
87 m_stopButton->setAutoRaise(true);
88 m_stopButton->setToolTip(i18nc("@tooltip", "Stop loading"));
89 m_stopButton->hide();
90 connect(m_stopButton, &QToolButton::clicked, this, &DolphinStatusBar::stopPressed);
91
92 m_progressTextLabel = new QLabel(contentsContainer);
93 m_progressTextLabel->hide();
94
95 m_progressBar = new QProgressBar(contentsContainer);
96 m_progressBar->hide();
97
98 m_showProgressBarTimer = new QTimer(this);
99 m_showProgressBarTimer->setInterval(500);
100 m_showProgressBarTimer->setSingleShot(true);
101 connect(m_showProgressBarTimer, &QTimer::timeout, this, &DolphinStatusBar::updateProgressInfo);
102
103 // initialize text updater delay timer
104 m_delayUpdateTimer = new QTimer(this);
105 m_delayUpdateTimer->setInterval(UpdateDelay);
106 m_delayUpdateTimer->setSingleShot(true);
107 connect(m_delayUpdateTimer, &QTimer::timeout, this, &DolphinStatusBar::updateLabelText);
108
109 // Initialize top layout and size policies
110 const int fontHeight = QFontMetrics(m_label->font()).height();
111 const int zoomSliderHeight = m_zoomSlider->minimumSizeHint().height();
112 const int buttonHeight = m_stopButton->height();
113 const int contentHeight = qMax(qMax(fontHeight, zoomSliderHeight), buttonHeight);
114
115 QFontMetrics fontMetrics(m_label->font());
116
117 m_label->setFixedHeight(contentHeight);
118 m_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
119
120 m_zoomSlider->setMaximumWidth(fontMetrics.averageCharWidth() * 15);
121
122 m_spaceInfo->setFixedHeight(contentHeight);
123 m_spaceInfo->setMaximumWidth(fontMetrics.averageCharWidth() * 25);
124 m_spaceInfo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
125
126 m_progressBar->setFixedHeight(zoomSliderHeight);
127 m_progressBar->setMaximumWidth(fontMetrics.averageCharWidth() * 20);
128
129 m_topLayout = new QHBoxLayout(contentsContainer);
130 updateContentsMargins();
131 m_topLayout->setSpacing(4);
132 m_topLayout->addWidget(m_label, 1);
133 m_topLayout->addWidget(m_zoomLabel);
134 m_topLayout->addWidget(m_zoomSlider, 1);
135 m_topLayout->addWidget(m_spaceInfo, 1);
136 m_topLayout->addWidget(m_stopButton);
137 m_topLayout->addWidget(m_progressTextLabel);
138 m_topLayout->addWidget(m_progressBar);
139
140 readSettings();
141 setWhatsThis(xi18nc("@info:whatsthis Statusbar",
142 "<para>This is "
143 "the <emphasis>Statusbar</emphasis>. It contains three elements "
144 "by default (left to right):<list><item>A <emphasis>text field"
145 "</emphasis> that displays the size of selected items. If only "
146 "one item is selected the name and type is shown as well.</item>"
147 "<item>A <emphasis>zoom slider</emphasis> that allows you "
148 "to adjust the size of the icons in the view.</item>"
149 "<item><emphasis>Space information</emphasis> about the "
150 "current storage device.</item></list></para>"));
151 }
152
153 DolphinStatusBar::~DolphinStatusBar()
154 {
155 }
156
157 void DolphinStatusBar::setText(const QString &text)
158 {
159 if (m_text == text) {
160 return;
161 }
162
163 m_textTimestamp = QTime::currentTime();
164
165 m_text = text;
166 // will update status bar text in 50ms
167 m_delayUpdateTimer->start();
168 }
169
170 QString DolphinStatusBar::text() const
171 {
172 return m_text;
173 }
174
175 void DolphinStatusBar::showProgress(const QString &currentlyRunningTaskTitle, int progressPercent, CancelLoading cancelLoading)
176 {
177 m_cancelLoading = cancelLoading;
178
179 // Show a busy indicator if a value < 0 is provided:
180 m_progressBar->setMaximum((progressPercent < 0) ? 0 : 100);
181
182 progressPercent = qBound(0, progressPercent, 100);
183 if (!m_progressBar->isVisible()) {
184 // Show the progress bar delayed: In the case if 100 % are reached within
185 // a short time, no progress bar will be shown at all.
186 if (!m_showProgressBarTimer->isActive()) {
187 m_showProgressBarTimer->start();
188 } else {
189 // The timer is already running. Should we restart it or keep it running?
190 if (m_progressTextLabel->text() != currentlyRunningTaskTitle || (progressPercent < 100 && progressPercent < m_progress)) {
191 m_showProgressBarTimer->start();
192 }
193 }
194 }
195 m_progress = progressPercent;
196
197 m_progressBar->setValue(m_progress);
198 if (progressPercent == 100) {
199 // The end of the progress has been reached. Assure that the progress bar
200 // gets hidden and the extensions widgets get visible again.
201 m_showProgressBarTimer->stop();
202 updateProgressInfo();
203 }
204
205 m_progressTextLabel->setText(currentlyRunningTaskTitle);
206 updateWidthToContent();
207 }
208
209 QString DolphinStatusBar::progressText() const
210 {
211 return m_progressTextLabel->text();
212 }
213
214 int DolphinStatusBar::progress() const
215 {
216 return m_progress;
217 }
218
219 void DolphinStatusBar::resetToDefaultText()
220 {
221 m_text.clear();
222
223 QTime currentTime;
224 if (currentTime.msecsTo(m_textTimestamp) < UpdateDelay) {
225 m_delayUpdateTimer->start();
226 } else {
227 updateLabelText();
228 }
229 }
230
231 void DolphinStatusBar::setDefaultText(const QString &text)
232 {
233 m_defaultText = text;
234 updateLabelText();
235 }
236
237 QString DolphinStatusBar::defaultText() const
238 {
239 return m_defaultText;
240 }
241
242 void DolphinStatusBar::setUrl(const QUrl &url)
243 {
244 if (GeneralSettings::showStatusBar() == GeneralSettings::EnumShowStatusBar::FullWidth && m_spaceInfo && m_spaceInfo->url() != url) {
245 m_spaceInfo->setUrl(url);
246 Q_EMIT urlChanged();
247 }
248 }
249
250 QUrl DolphinStatusBar::url() const
251 {
252 return m_spaceInfo->url();
253 }
254
255 void DolphinStatusBar::setZoomLevel(int zoomLevel)
256 {
257 if (zoomLevel != m_zoomSlider->value()) {
258 m_zoomSlider->setValue(zoomLevel);
259 }
260 }
261
262 int DolphinStatusBar::zoomLevel() const
263 {
264 return m_zoomSlider->value();
265 }
266
267 void DolphinStatusBar::readSettings()
268 {
269 updateMode();
270 setExtensionsVisible(true);
271 }
272
273 void DolphinStatusBar::updateSpaceInfo()
274 {
275 m_spaceInfo->update();
276 }
277
278 void DolphinStatusBar::updateWidthToContent()
279 {
280 if (GeneralSettings::showStatusBar() == GeneralSettings::EnumShowStatusBar::Small) {
281 QStyleOptionSlider opt;
282 opt.initFrom(this);
283 opt.orientation = Qt::Vertical;
284 const QSize labelSize = QFontMetrics(font()).size(Qt::TextSingleLine, m_label->fullText());
285 // Make sure minimum height takes clipping into account.
286 setMinimumHeight(m_label->height() + clippingAmount());
287 const int scrollbarWidth = style()->pixelMetric(QStyle::PM_ScrollBarExtent, &opt, this);
288 // Make sure maximumViewWidth does not go below 0.
289 const int maximumViewWidth = qMax(0, parentWidget()->width() - scrollbarWidth);
290 if (m_stopButton->isVisible() || m_progressTextLabel->isVisible() || m_progressBar->isVisible()) {
291 // Use maximum width when interactable elements are shown, to keep them
292 // from "jumping around" when user tries to interact with them.
293 setFixedWidth(maximumViewWidth);
294 } else {
295 const int contentWidth = labelSize.width() + 15;
296 setFixedWidth(qMin(contentWidth, maximumViewWidth));
297 }
298 Q_EMIT widthUpdated();
299 } else {
300 setMinimumHeight(0);
301 setFixedWidth(QWIDGETSIZE_MAX);
302 Q_EMIT widthUpdated();
303 }
304 }
305
306 int DolphinStatusBar::clippingAmount() const
307 {
308 QStyleOption opt;
309 opt.initFrom(this);
310 // Add 2 for extra padding due to how QRect coordinates work.
311 const int val = 2 + style()->pixelMetric(QStyle::PM_SplitterWidth, &opt, this) * 2;
312 return val;
313 }
314
315 void DolphinStatusBar::updateMode()
316 {
317 switch (GeneralSettings::showStatusBar()) {
318 case GeneralSettings::EnumShowStatusBar::Small:
319 setEnabled(true);
320 m_spaceInfo->setShown(false);
321 m_zoomSlider->setVisible(false);
322 m_zoomLabel->setVisible(false);
323 setVisible(true, WithAnimation);
324 break;
325 case GeneralSettings::EnumShowStatusBar::FullWidth:
326 setEnabled(true);
327 m_spaceInfo->setShown(true);
328 setVisible(true, WithAnimation);
329 break;
330 case GeneralSettings::EnumShowStatusBar::Disabled:
331 setEnabled(false);
332 setVisible(false, WithoutAnimation);
333 break;
334 }
335 Q_EMIT modeUpdated();
336 updateWidthToContent();
337 }
338
339 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent *event)
340 {
341 Q_UNUSED(event)
342
343 // Do not show the context menu on small statusbar.
344 if (GeneralSettings::showStatusBar() == GeneralSettings::EnumShowStatusBar::Small) {
345 return;
346 }
347
348 QMenu menu(this);
349
350 QAction *showZoomSliderAction = menu.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
351 showZoomSliderAction->setCheckable(true);
352 showZoomSliderAction->setChecked(GeneralSettings::showZoomSlider());
353
354 const QAction *action = menu.exec(event->reason() == QContextMenuEvent::Reason::Mouse ? QCursor::pos() : mapToGlobal(QPoint(width() / 2, height() / 2)));
355 if (action == showZoomSliderAction) {
356 const bool visible = showZoomSliderAction->isChecked();
357 GeneralSettings::setShowZoomSlider(visible);
358 m_zoomSlider->setVisible(visible);
359 m_zoomLabel->setVisible(visible);
360 }
361 updateContentsMargins();
362 }
363
364 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel)
365 {
366 updateZoomSliderToolTip(zoomLevel);
367
368 QPoint global = m_zoomSlider->rect().topLeft();
369 global.ry() += m_zoomSlider->height() / 2;
370 QHelpEvent toolTipEvent(QEvent::ToolTip, QPoint(0, 0), m_zoomSlider->mapToGlobal(global));
371 QApplication::sendEvent(m_zoomSlider, &toolTipEvent);
372 }
373
374 void DolphinStatusBar::updateProgressInfo()
375 {
376 if (m_progress < 100) {
377 // Show the progress information and hide the extensions
378 m_stopButton->setVisible(m_cancelLoading == CancelLoading::Allowed);
379 m_progressTextLabel->show();
380 m_progressBar->show();
381 setExtensionsVisible(false);
382 } else {
383 // Hide the progress information and show the extensions
384 m_stopButton->hide();
385 m_progressTextLabel->hide();
386 m_progressBar->hide();
387 setExtensionsVisible(true);
388 }
389 updateWidthToContent();
390 }
391
392 void DolphinStatusBar::updateLabelText()
393 {
394 const QString text = m_text.isEmpty() ? m_defaultText : m_text;
395 m_label->setText(text);
396 updateWidthToContent();
397 }
398
399 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel)
400 {
401 const int size = ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel);
402 m_zoomSlider->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size));
403 }
404
405 void DolphinStatusBar::setExtensionsVisible(bool visible)
406 {
407 bool showZoomSlider = visible;
408 if (visible) {
409 showZoomSlider = GeneralSettings::showZoomSlider() && GeneralSettings::showStatusBar() == GeneralSettings::EnumShowStatusBar::FullWidth;
410 }
411
412 m_zoomSlider->setVisible(showZoomSlider);
413 m_zoomLabel->setVisible(showZoomSlider);
414 updateContentsMargins();
415 }
416
417 void DolphinStatusBar::updateContentsMargins()
418 {
419 if (GeneralSettings::showStatusBar() == GeneralSettings::EnumShowStatusBar::FullWidth) {
420 // 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.
421 m_topLayout->setContentsMargins(6, 0, 2, 0);
422 } else {
423 // Add extra margins to toplayout to avoid clipping too early.
424 m_topLayout->setContentsMargins(clippingAmount() * 2, 0, clippingAmount(), clippingAmount());
425 }
426 setContentsMargins(0, 0, 0, 0);
427 }
428
429 void DolphinStatusBar::paintEvent(QPaintEvent *paintEvent)
430 {
431 Q_UNUSED(paintEvent)
432 QPainter p(this);
433 QStyleOption opt;
434 opt.initFrom(this);
435 // Draw statusbar only if there is text.
436 if (GeneralSettings::showStatusBar() == GeneralSettings::EnumShowStatusBar::Small) {
437 if (m_label && !m_label->fullText().isEmpty()) {
438 opt.state = QStyle::State_Sunken;
439 QPainterPath path;
440 // Clip the left and bottom border off.
441 QRect clipRect;
442 if (layoutDirection() == Qt::RightToLeft) {
443 clipRect = QRect(opt.rect.topLeft(), opt.rect.bottomRight()).adjusted(0, 0, -clippingAmount(), -clippingAmount());
444 } else {
445 clipRect = QRect(opt.rect.topLeft(), opt.rect.bottomRight()).adjusted(clippingAmount(), 0, 0, -clippingAmount());
446 }
447 path.addRect(clipRect);
448 p.setClipPath(path);
449 opt.palette.setColor(QPalette::Base, palette().window().color());
450 style()->drawPrimitive(QStyle::PE_Frame, &opt, &p, this);
451 }
452 }
453 // Draw regular statusbar.
454 else {
455 style()->drawPrimitive(QStyle::PE_PanelStatusBar, &opt, &p, this);
456 }
457 }
458
459 int DolphinStatusBar::preferredHeight() const
460 {
461 return m_spaceInfo->height();
462 }
463
464 #include "moc_dolphinstatusbar.cpp"