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