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
);
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
);
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());
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
);
71 // Initialize space information
72 m_spaceInfo
= new StatusBarSpaceInfo(contentsContainer
);
73 connect(m_spaceInfo
, &StatusBarSpaceInfo::showMessage
, this, &DolphinStatusBar::showMessage
);
75 &StatusBarSpaceInfo::showInstallationProgress
,
77 [this](const QString
¤tlyRunningTaskTitle
, int installationProgressPercent
) {
78 showProgress(currentlyRunningTaskTitle
, installationProgressPercent
, CancelLoading::Disallowed
);
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"));
88 connect(m_stopButton
, &QToolButton::clicked
, this, &DolphinStatusBar::stopPressed
);
90 m_progressTextLabel
= new QLabel(contentsContainer
);
91 m_progressTextLabel
->hide();
93 m_progressBar
= new QProgressBar(contentsContainer
);
94 m_progressBar
->hide();
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
);
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
);
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
);
113 QFontMetrics
fontMetrics(m_label
->font());
115 m_label
->setFixedHeight(contentHeight
);
116 m_label
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
118 m_zoomSlider
->setMaximumWidth(fontMetrics
.averageCharWidth() * 15);
120 m_spaceInfo
->setFixedHeight(contentHeight
);
121 m_spaceInfo
->setMaximumWidth(fontMetrics
.averageCharWidth() * 25);
122 m_spaceInfo
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
124 m_progressBar
->setFixedHeight(zoomSliderHeight
);
125 m_progressBar
->setMaximumWidth(fontMetrics
.averageCharWidth() * 20);
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
);
138 setVisible(GeneralSettings::showStatusBar(), WithoutAnimation
);
139 setExtensionsVisible(true);
140 setWhatsThis(xi18nc("@info:whatsthis Statusbar",
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>"));
152 DolphinStatusBar::~DolphinStatusBar()
156 void DolphinStatusBar::setText(const QString
&text
)
158 if (m_text
== text
) {
162 m_textTimestamp
= QTime::currentTime();
165 // will update status bar text in 50ms
166 m_delayUpdateTimer
->start();
169 QString
DolphinStatusBar::text() const
174 void DolphinStatusBar::showProgress(const QString
¤tlyRunningTaskTitle
, int progressPercent
, CancelLoading cancelLoading
)
176 m_cancelLoading
= cancelLoading
;
178 // Show a busy indicator if a value < 0 is provided:
179 m_progressBar
->setMaximum((progressPercent
< 0) ? 0 : 100);
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();
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();
194 m_progress
= progressPercent
;
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();
204 m_progressTextLabel
->setText(currentlyRunningTaskTitle
);
207 QString
DolphinStatusBar::progressText() const
209 return m_progressTextLabel
->text();
212 int DolphinStatusBar::progress() const
217 void DolphinStatusBar::resetToDefaultText()
222 if (currentTime
.msecsTo(m_textTimestamp
) < UpdateDelay
) {
223 m_delayUpdateTimer
->start();
229 void DolphinStatusBar::setDefaultText(const QString
&text
)
231 m_defaultText
= text
;
235 QString
DolphinStatusBar::defaultText() const
237 return m_defaultText
;
240 void DolphinStatusBar::setUrl(const QUrl
&url
)
242 if (GeneralSettings::showSpaceInfo()) {
243 m_spaceInfo
->setUrl(url
);
247 QUrl
DolphinStatusBar::url() const
249 return m_spaceInfo
->url();
252 void DolphinStatusBar::setZoomLevel(int zoomLevel
)
254 if (zoomLevel
!= m_zoomSlider
->value()) {
255 m_zoomSlider
->setValue(zoomLevel
);
259 int DolphinStatusBar::zoomLevel() const
261 return m_zoomSlider
->value();
264 void DolphinStatusBar::readSettings()
266 setVisible(GeneralSettings::showStatusBar(), WithAnimation
);
267 setExtensionsVisible(true);
270 void DolphinStatusBar::updateSpaceInfo()
272 m_spaceInfo
->update();
275 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent
*event
)
281 QAction
*showZoomSliderAction
= menu
.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
282 showZoomSliderAction
->setCheckable(true);
283 showZoomSliderAction
->setChecked(GeneralSettings::showZoomSlider());
285 QAction
*showSpaceInfoAction
= menu
.addAction(i18nc("@action:inmenu", "Show Space Information"));
286 showSpaceInfoAction
->setCheckable(true);
287 showSpaceInfoAction
->setChecked(GeneralSettings::showSpaceInfo());
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
);
300 updateContentsMargins();
303 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel
)
305 updateZoomSliderToolTip(zoomLevel
);
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
);
313 void DolphinStatusBar::updateProgressInfo()
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);
322 // Hide the progress information and show the extensions
323 m_stopButton
->hide();
324 m_progressTextLabel
->hide();
325 m_progressBar
->hide();
326 setExtensionsVisible(true);
330 void DolphinStatusBar::updateLabelText()
332 const QString text
= m_text
.isEmpty() ? m_defaultText
: m_text
;
333 m_label
->setText(text
);
336 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel
)
338 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel
);
339 m_zoomSlider
->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size
));
342 void DolphinStatusBar::setExtensionsVisible(bool visible
)
344 bool showSpaceInfo
= visible
;
345 bool showZoomSlider
= visible
;
347 showSpaceInfo
= GeneralSettings::showSpaceInfo();
348 showZoomSlider
= GeneralSettings::showZoomSlider();
351 m_spaceInfo
->setShown(showSpaceInfo
);
352 m_spaceInfo
->setVisible(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"