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 <QProgressBar>
25 #include <QStyleOption>
27 #include <QToolButton>
31 const int UpdateDelay
= 50;
34 DolphinStatusBar::DolphinStatusBar(QWidget
*parent
)
35 : AnimatedHeightWidget(parent
)
39 , m_zoomLabel(nullptr)
40 , m_spaceInfo(nullptr)
41 , m_zoomSlider(nullptr)
42 , m_progressBar(nullptr)
43 , m_stopButton(nullptr)
45 , m_showProgressBarTimer(nullptr)
46 , m_delayUpdateTimer(nullptr)
49 setProperty("_breeze_statusbar_separator", true);
51 QWidget
*contentsContainer
= prepareContentsContainer();
53 // Initialize text label
54 m_label
= new KSqueezedTextLabel(m_text
, contentsContainer
);
55 m_label
->setTextFormat(Qt::PlainText
);
56 m_label
->setTextInteractionFlags(Qt::TextBrowserInteraction
| Qt::TextSelectableByKeyboard
); // for accessibility but also to allow copy-pasting this text.
58 // Initialize zoom slider's explanatory label
59 m_zoomLabel
= new KSqueezedTextLabel(i18nc("Used as a noun, i.e. 'Here is the zoom level:'", "Zoom:"), contentsContainer
);
61 // Initialize zoom widget
62 m_zoomSlider
= new QSlider(Qt::Horizontal
, contentsContainer
);
63 m_zoomSlider
->setAccessibleName(i18n("Zoom"));
64 m_zoomSlider
->setAccessibleDescription(i18nc("Description for zoom-slider (accessibility)", "Sets the size of the file icons."));
65 m_zoomSlider
->setPageStep(1);
66 m_zoomSlider
->setRange(ZoomLevelInfo::minimumLevel(), ZoomLevelInfo::maximumLevel());
68 connect(m_zoomSlider
, &QSlider::valueChanged
, this, &DolphinStatusBar::zoomLevelChanged
);
69 connect(m_zoomSlider
, &QSlider::valueChanged
, this, &DolphinStatusBar::updateZoomSliderToolTip
);
70 connect(m_zoomSlider
, &QSlider::sliderMoved
, this, &DolphinStatusBar::showZoomSliderToolTip
);
72 // Initialize space information
73 m_spaceInfo
= new StatusBarSpaceInfo(contentsContainer
);
74 connect(m_spaceInfo
, &StatusBarSpaceInfo::showMessage
, this, &DolphinStatusBar::showMessage
);
76 &StatusBarSpaceInfo::showInstallationProgress
,
78 [this](const QString
¤tlyRunningTaskTitle
, int installationProgressPercent
) {
79 showProgress(currentlyRunningTaskTitle
, installationProgressPercent
, CancelLoading::Disallowed
);
82 // Initialize progress information
83 m_stopButton
= new QToolButton(contentsContainer
);
84 m_stopButton
->setIcon(QIcon::fromTheme(QStringLiteral("process-stop")));
85 m_stopButton
->setAccessibleName(i18n("Stop"));
86 m_stopButton
->setAutoRaise(true);
87 m_stopButton
->setToolTip(i18nc("@tooltip", "Stop loading"));
89 connect(m_stopButton
, &QToolButton::clicked
, this, &DolphinStatusBar::stopPressed
);
91 m_progressTextLabel
= new QLabel(contentsContainer
);
92 m_progressTextLabel
->hide();
94 m_progressBar
= new QProgressBar(contentsContainer
);
95 m_progressBar
->hide();
97 m_showProgressBarTimer
= new QTimer(this);
98 m_showProgressBarTimer
->setInterval(500);
99 m_showProgressBarTimer
->setSingleShot(true);
100 connect(m_showProgressBarTimer
, &QTimer::timeout
, this, &DolphinStatusBar::updateProgressInfo
);
102 // initialize text updater delay timer
103 m_delayUpdateTimer
= new QTimer(this);
104 m_delayUpdateTimer
->setInterval(UpdateDelay
);
105 m_delayUpdateTimer
->setSingleShot(true);
106 connect(m_delayUpdateTimer
, &QTimer::timeout
, this, &DolphinStatusBar::updateLabelText
);
108 // Initialize top layout and size policies
109 const int fontHeight
= QFontMetrics(m_label
->font()).height();
110 const int zoomSliderHeight
= m_zoomSlider
->minimumSizeHint().height();
111 const int buttonHeight
= m_stopButton
->height();
112 const int contentHeight
= qMax(qMax(fontHeight
, zoomSliderHeight
), buttonHeight
);
114 QFontMetrics
fontMetrics(m_label
->font());
116 m_label
->setFixedHeight(contentHeight
);
117 m_label
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
119 m_zoomSlider
->setMaximumWidth(fontMetrics
.averageCharWidth() * 15);
121 m_spaceInfo
->setFixedHeight(contentHeight
);
122 m_spaceInfo
->setMaximumWidth(fontMetrics
.averageCharWidth() * 25);
123 m_spaceInfo
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
125 m_progressBar
->setFixedHeight(zoomSliderHeight
);
126 m_progressBar
->setMaximumWidth(fontMetrics
.averageCharWidth() * 20);
128 m_topLayout
= new QHBoxLayout(contentsContainer
);
129 updateContentsMargins();
130 m_topLayout
->setSpacing(4);
131 m_topLayout
->addWidget(m_label
, 1);
132 m_topLayout
->addWidget(m_zoomLabel
);
133 m_topLayout
->addWidget(m_zoomSlider
, 1);
134 m_topLayout
->addWidget(m_spaceInfo
, 1);
135 m_topLayout
->addWidget(m_stopButton
);
136 m_topLayout
->addWidget(m_progressTextLabel
);
137 m_topLayout
->addWidget(m_progressBar
);
139 setVisible(GeneralSettings::showStatusBar(), WithoutAnimation
);
140 setExtensionsVisible(true);
141 setWhatsThis(xi18nc("@info:whatsthis Statusbar",
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>"));
153 DolphinStatusBar::~DolphinStatusBar()
157 void DolphinStatusBar::setText(const QString
&text
)
159 if (m_text
== text
) {
163 m_textTimestamp
= QTime::currentTime();
166 // will update status bar text in 50ms
167 m_delayUpdateTimer
->start();
170 QString
DolphinStatusBar::text() const
175 void DolphinStatusBar::showProgress(const QString
¤tlyRunningTaskTitle
, int progressPercent
, CancelLoading cancelLoading
)
177 m_cancelLoading
= cancelLoading
;
179 // Show a busy indicator if a value < 0 is provided:
180 m_progressBar
->setMaximum((progressPercent
< 0) ? 0 : 100);
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();
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();
195 m_progress
= progressPercent
;
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();
205 m_progressTextLabel
->setText(currentlyRunningTaskTitle
);
208 QString
DolphinStatusBar::progressText() const
210 return m_progressTextLabel
->text();
213 int DolphinStatusBar::progress() const
218 void DolphinStatusBar::resetToDefaultText()
223 if (currentTime
.msecsTo(m_textTimestamp
) < UpdateDelay
) {
224 m_delayUpdateTimer
->start();
230 void DolphinStatusBar::setDefaultText(const QString
&text
)
232 m_defaultText
= text
;
236 QString
DolphinStatusBar::defaultText() const
238 return m_defaultText
;
241 void DolphinStatusBar::setUrl(const QUrl
&url
)
243 if (GeneralSettings::showSpaceInfo()) {
244 m_spaceInfo
->setUrl(url
);
248 QUrl
DolphinStatusBar::url() const
250 return m_spaceInfo
->url();
253 void DolphinStatusBar::setZoomLevel(int zoomLevel
)
255 if (zoomLevel
!= m_zoomSlider
->value()) {
256 m_zoomSlider
->setValue(zoomLevel
);
260 int DolphinStatusBar::zoomLevel() const
262 return m_zoomSlider
->value();
265 void DolphinStatusBar::readSettings()
267 setVisible(GeneralSettings::showStatusBar(), WithAnimation
);
268 setExtensionsVisible(true);
271 void DolphinStatusBar::updateSpaceInfo()
273 m_spaceInfo
->update();
276 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent
*event
)
282 QAction
*showZoomSliderAction
= menu
.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
283 showZoomSliderAction
->setCheckable(true);
284 showZoomSliderAction
->setChecked(GeneralSettings::showZoomSlider());
286 QAction
*showSpaceInfoAction
= menu
.addAction(i18nc("@action:inmenu", "Show Space Information"));
287 showSpaceInfoAction
->setCheckable(true);
288 showSpaceInfoAction
->setChecked(GeneralSettings::showSpaceInfo());
290 const QAction
*action
= menu
.exec(event
->reason() == QContextMenuEvent::Reason::Mouse
? QCursor::pos() : mapToGlobal(QPoint(width() / 2, height() / 2)));
291 if (action
== showZoomSliderAction
) {
292 const bool visible
= showZoomSliderAction
->isChecked();
293 GeneralSettings::setShowZoomSlider(visible
);
294 m_zoomSlider
->setVisible(visible
);
295 m_zoomLabel
->setVisible(visible
);
296 } else if (action
== showSpaceInfoAction
) {
297 const bool visible
= showSpaceInfoAction
->isChecked();
298 GeneralSettings::setShowSpaceInfo(visible
);
299 m_spaceInfo
->setShown(visible
);
301 updateContentsMargins();
304 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel
)
306 updateZoomSliderToolTip(zoomLevel
);
308 QPoint global
= m_zoomSlider
->rect().topLeft();
309 global
.ry() += m_zoomSlider
->height() / 2;
310 QHelpEvent
toolTipEvent(QEvent::ToolTip
, QPoint(0, 0), m_zoomSlider
->mapToGlobal(global
));
311 QApplication::sendEvent(m_zoomSlider
, &toolTipEvent
);
314 void DolphinStatusBar::updateProgressInfo()
316 if (m_progress
< 100) {
317 // Show the progress information and hide the extensions
318 m_stopButton
->setVisible(m_cancelLoading
== CancelLoading::Allowed
);
319 m_progressTextLabel
->show();
320 m_progressBar
->show();
321 setExtensionsVisible(false);
323 // Hide the progress information and show the extensions
324 m_stopButton
->hide();
325 m_progressTextLabel
->hide();
326 m_progressBar
->hide();
327 setExtensionsVisible(true);
331 void DolphinStatusBar::updateLabelText()
333 const QString text
= m_text
.isEmpty() ? m_defaultText
: m_text
;
334 m_label
->setText(text
);
337 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel
)
339 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel
);
340 m_zoomSlider
->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size
));
343 void DolphinStatusBar::setExtensionsVisible(bool visible
)
345 bool showSpaceInfo
= visible
;
346 bool showZoomSlider
= visible
;
348 showSpaceInfo
= GeneralSettings::showSpaceInfo();
349 showZoomSlider
= GeneralSettings::showZoomSlider();
352 m_spaceInfo
->setShown(showSpaceInfo
);
353 m_zoomSlider
->setVisible(showZoomSlider
);
354 m_zoomLabel
->setVisible(showZoomSlider
);
355 updateContentsMargins();
358 void DolphinStatusBar::updateContentsMargins()
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);
364 m_topLayout
->setContentsMargins(6, 0, 6, 0);
368 void DolphinStatusBar::paintEvent(QPaintEvent
*paintEvent
)
374 style()->drawPrimitive(QStyle::PE_PanelStatusBar
, &opt
, &p
, this);
377 int DolphinStatusBar::preferredHeight() const
379 return m_spaceInfo
->height();
382 #include "moc_dolphinstatusbar.cpp"