2 * SPDX-FileCopyrightText: 2006-2012 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "dolphinstatusbar.h"
9 #include "dolphin_generalsettings.h"
10 #include "statusbarspaceinfo.h"
11 #include "views/dolphinview.h"
12 #include "views/zoomlevelinfo.h"
14 #include <KLocalizedString>
15 #include <KSqueezedTextLabel>
17 #include <QApplication>
18 #include <QHBoxLayout>
23 #include <QPainterPath>
24 #include <QProgressBar>
26 #include <QStyleOption>
28 #include <QToolButton>
32 const int UpdateDelay
= 50;
35 DolphinStatusBar::DolphinStatusBar(QWidget
*parent
)
36 : AnimatedHeightWidget(parent
)
40 , m_zoomLabel(nullptr)
41 , m_spaceInfo(nullptr)
42 , m_zoomSlider(nullptr)
43 , m_progressBar(nullptr)
44 , m_stopButton(nullptr)
46 , m_showProgressBarTimer(nullptr)
47 , m_delayUpdateTimer(nullptr)
50 setProperty("_breeze_statusbar_separator", true);
52 QWidget
*contentsContainer
= prepareContentsContainer();
53 contentsContainer
->setContentsMargins(0, 0, 0, 0);
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.
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
);
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());
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
);
74 // Initialize space information
75 m_spaceInfo
= new StatusBarSpaceInfo(contentsContainer
);
76 connect(m_spaceInfo
, &StatusBarSpaceInfo::showMessage
, this, &DolphinStatusBar::showMessage
);
78 &StatusBarSpaceInfo::showInstallationProgress
,
80 [this](const QString
¤tlyRunningTaskTitle
, int installationProgressPercent
) {
81 showProgress(currentlyRunningTaskTitle
, installationProgressPercent
, CancelLoading::Disallowed
);
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"));
91 connect(m_stopButton
, &QToolButton::clicked
, this, &DolphinStatusBar::stopPressed
);
93 m_progressTextLabel
= new QLabel(contentsContainer
);
94 m_progressTextLabel
->hide();
96 m_progressBar
= new QProgressBar(contentsContainer
);
97 m_progressBar
->hide();
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
);
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
);
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
);
116 QFontMetrics
fontMetrics(m_label
->font());
118 m_label
->setFixedHeight(contentHeight
);
119 m_label
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
121 m_zoomSlider
->setMaximumWidth(fontMetrics
.averageCharWidth() * 15);
123 m_spaceInfo
->setFixedHeight(contentHeight
);
124 m_spaceInfo
->setMaximumWidth(fontMetrics
.averageCharWidth() * 25);
125 m_spaceInfo
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
127 m_progressBar
->setFixedHeight(zoomSliderHeight
);
128 m_progressBar
->setMaximumWidth(fontMetrics
.averageCharWidth() * 20);
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
);
142 setWhatsThis(xi18nc("@info:whatsthis Statusbar",
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>"));
154 DolphinStatusBar::~DolphinStatusBar()
158 void DolphinStatusBar::setText(const QString
&text
)
160 if (m_text
== text
) {
164 m_textTimestamp
= QTime::currentTime();
167 // will update status bar text in 50ms
168 m_delayUpdateTimer
->start();
171 QString
DolphinStatusBar::text() const
176 void DolphinStatusBar::showProgress(const QString
¤tlyRunningTaskTitle
, int progressPercent
, CancelLoading cancelLoading
)
178 m_cancelLoading
= cancelLoading
;
180 // Show a busy indicator if a value < 0 is provided:
181 m_progressBar
->setMaximum((progressPercent
< 0) ? 0 : 100);
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();
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();
196 m_progress
= progressPercent
;
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();
206 m_progressTextLabel
->setText(currentlyRunningTaskTitle
);
207 updateWidthToContent();
210 QString
DolphinStatusBar::progressText() const
212 return m_progressTextLabel
->text();
215 int DolphinStatusBar::progress() const
220 void DolphinStatusBar::resetToDefaultText()
225 if (currentTime
.msecsTo(m_textTimestamp
) < UpdateDelay
) {
226 m_delayUpdateTimer
->start();
232 void DolphinStatusBar::setDefaultText(const QString
&text
)
234 m_defaultText
= text
;
238 QString
DolphinStatusBar::defaultText() const
240 return m_defaultText
;
243 void DolphinStatusBar::setUrl(const QUrl
&url
)
245 if (GeneralSettings::showStatusBar() == GeneralSettings::EnumShowStatusBar::FullWidth
&& m_spaceInfo
&& m_spaceInfo
->url() != url
) {
246 m_spaceInfo
->setUrl(url
);
251 QUrl
DolphinStatusBar::url() const
253 return m_spaceInfo
->url();
256 void DolphinStatusBar::setZoomLevel(int zoomLevel
)
258 if (zoomLevel
!= m_zoomSlider
->value()) {
259 m_zoomSlider
->setValue(zoomLevel
);
263 int DolphinStatusBar::zoomLevel() const
265 return m_zoomSlider
->value();
268 void DolphinStatusBar::readSettings()
271 setExtensionsVisible(true);
274 void DolphinStatusBar::updateSpaceInfo()
276 m_spaceInfo
->update();
279 void DolphinStatusBar::updateWidthToContent()
281 if (GeneralSettings::showStatusBar() == GeneralSettings::EnumShowStatusBar::Small
) {
282 QStyleOptionSlider opt
;
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
);
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
));
300 Q_EMIT
widthUpdated();
303 setFixedWidth(QWIDGETSIZE_MAX
);
304 Q_EMIT
widthUpdated();
308 int DolphinStatusBar::clippingAmount() const
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;
317 void DolphinStatusBar::updateMode()
319 switch (GeneralSettings::showStatusBar()) {
320 case GeneralSettings::EnumShowStatusBar::Small
:
322 m_spaceInfo
->setShown(false);
323 m_zoomSlider
->setVisible(false);
324 m_zoomLabel
->setVisible(false);
325 setVisible(true, WithAnimation
);
327 case GeneralSettings::EnumShowStatusBar::FullWidth
:
329 m_spaceInfo
->setShown(true);
330 setVisible(true, WithAnimation
);
332 case GeneralSettings::EnumShowStatusBar::Disabled
:
334 setVisible(false, WithoutAnimation
);
337 Q_EMIT
modeUpdated();
338 updateWidthToContent();
341 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent
*event
)
345 // Do not show the context menu on small statusbar.
346 if (GeneralSettings::showStatusBar() == GeneralSettings::EnumShowStatusBar::Small
) {
352 QAction
*showZoomSliderAction
= menu
.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
353 showZoomSliderAction
->setCheckable(true);
354 showZoomSliderAction
->setChecked(GeneralSettings::showZoomSlider());
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
);
363 updateContentsMargins();
366 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel
)
368 updateZoomSliderToolTip(zoomLevel
);
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
);
376 void DolphinStatusBar::updateProgressInfo()
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);
385 // Hide the progress information and show the extensions
386 m_stopButton
->hide();
387 m_progressTextLabel
->hide();
388 m_progressBar
->hide();
389 setExtensionsVisible(true);
391 updateWidthToContent();
394 void DolphinStatusBar::updateLabelText()
396 const QString text
= m_text
.isEmpty() ? m_defaultText
: m_text
;
397 m_label
->setText(text
);
398 updateWidthToContent();
401 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel
)
403 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel
);
404 m_zoomSlider
->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size
));
407 void DolphinStatusBar::setExtensionsVisible(bool visible
)
409 bool showZoomSlider
= visible
;
411 showZoomSlider
= GeneralSettings::showZoomSlider() && GeneralSettings::showStatusBar() == GeneralSettings::EnumShowStatusBar::FullWidth
;
414 m_zoomSlider
->setVisible(showZoomSlider
);
415 m_zoomLabel
->setVisible(showZoomSlider
);
416 updateContentsMargins();
419 void DolphinStatusBar::updateContentsMargins()
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);
425 // Add extra margins to toplayout to avoid clipping too early.
426 m_topLayout
->setContentsMargins(clippingAmount() * 2, 0, clippingAmount(), 0);
428 setContentsMargins(0, 0, 0, 0);
431 void DolphinStatusBar::paintEvent(QPaintEvent
*paintEvent
)
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
;
442 // Adjust the rectangle to be a bit larger, then clip the left and bottom border off.
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());
448 opt
.rect
= rect().adjusted(-clippingAmount(), 0, 0, clippingAmount());
449 clipRect
= QRect(opt
.rect
.topLeft(), opt
.rect
.bottomRight()).adjusted(clippingAmount(), 0, 0, -clippingAmount());
451 path
.addRect(clipRect
);
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);
460 // Draw regular statusbar.
462 style()->drawPrimitive(QStyle::PE_PanelStatusBar
, &opt
, &p
, this);
466 int DolphinStatusBar::preferredHeight() const
468 return m_spaceInfo
->height();
471 #include "moc_dolphinstatusbar.cpp"