]>
cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/dolphinstatusbar.cpp
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
)
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);
50 // Initialize text label
51 m_label
= new KSqueezedTextLabel(m_text
, this);
52 m_label
->setWordWrap(true);
53 m_label
->setTextFormat(Qt::PlainText
);
55 // Initialize zoom slider's explanatory label
56 m_zoomLabel
= new KSqueezedTextLabel(i18nc("Used as a noun, i.e. 'Here is the zoom level:'", "Zoom:"), this);
58 // Initialize zoom widget
59 m_zoomSlider
= new QSlider(Qt::Horizontal
, this);
60 m_zoomSlider
->setAccessibleName(i18n("Zoom"));
61 m_zoomSlider
->setAccessibleDescription(i18nc("Description for zoom-slider (accessibility)", "Sets the size of the file icons."));
62 m_zoomSlider
->setPageStep(1);
63 m_zoomSlider
->setRange(ZoomLevelInfo::minimumLevel(), ZoomLevelInfo::maximumLevel());
65 connect(m_zoomSlider
, &QSlider::valueChanged
, this, &DolphinStatusBar::zoomLevelChanged
);
66 connect(m_zoomSlider
, &QSlider::valueChanged
, this, &DolphinStatusBar::updateZoomSliderToolTip
);
67 connect(m_zoomSlider
, &QSlider::sliderMoved
, this, &DolphinStatusBar::showZoomSliderToolTip
);
69 // Initialize space information
70 m_spaceInfo
= new StatusBarSpaceInfo(this);
72 // Initialize progress information
73 m_stopButton
= new QToolButton(this);
74 m_stopButton
->setIcon(QIcon::fromTheme(QStringLiteral("process-stop")));
75 m_stopButton
->setAccessibleName(i18n("Stop"));
76 m_stopButton
->setAutoRaise(true);
77 m_stopButton
->setToolTip(i18nc("@tooltip", "Stop loading"));
79 connect(m_stopButton
, &QToolButton::clicked
, this, &DolphinStatusBar::stopPressed
);
81 m_progressTextLabel
= new QLabel(this);
82 m_progressTextLabel
->hide();
84 m_progressBar
= new QProgressBar(this);
85 m_progressBar
->hide();
87 m_showProgressBarTimer
= new QTimer(this);
88 m_showProgressBarTimer
->setInterval(500);
89 m_showProgressBarTimer
->setSingleShot(true);
90 connect(m_showProgressBarTimer
, &QTimer::timeout
, this, &DolphinStatusBar::updateProgressInfo
);
92 // initialize text updater delay timer
93 m_delayUpdateTimer
= new QTimer(this);
94 m_delayUpdateTimer
->setInterval(UpdateDelay
);
95 m_delayUpdateTimer
->setSingleShot(true);
96 connect(m_delayUpdateTimer
, &QTimer::timeout
, this, &DolphinStatusBar::updateLabelText
);
98 // Initialize top layout and size policies
99 const int fontHeight
= QFontMetrics(m_label
->font()).height();
100 const int zoomSliderHeight
= m_zoomSlider
->minimumSizeHint().height();
101 const int buttonHeight
= m_stopButton
->height();
102 const int contentHeight
= qMax(qMax(fontHeight
, zoomSliderHeight
), buttonHeight
);
104 QFontMetrics
fontMetrics(m_label
->font());
106 m_label
->setFixedHeight(contentHeight
);
107 m_label
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
109 m_zoomSlider
->setMaximumWidth(fontMetrics
.averageCharWidth() * 15);
111 m_spaceInfo
->setFixedHeight(contentHeight
);
112 m_spaceInfo
->setMaximumWidth(fontMetrics
.averageCharWidth() * 25);
113 m_spaceInfo
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
115 m_progressBar
->setFixedHeight(zoomSliderHeight
);
116 m_progressBar
->setMaximumWidth(fontMetrics
.averageCharWidth() * 20);
118 QHBoxLayout
*topLayout
= new QHBoxLayout(this);
119 updateContentsMargins();
120 topLayout
->setSpacing(4);
121 topLayout
->addWidget(m_label
, 1);
122 topLayout
->addWidget(m_zoomLabel
);
123 topLayout
->addWidget(m_zoomSlider
, 1);
124 topLayout
->addWidget(m_spaceInfo
, 1);
125 topLayout
->addWidget(m_stopButton
);
126 topLayout
->addWidget(m_progressTextLabel
);
127 topLayout
->addWidget(m_progressBar
);
129 setVisible(GeneralSettings::showStatusBar());
130 setExtensionsVisible(true);
131 setWhatsThis(xi18nc("@info:whatsthis Statusbar",
133 "the <emphasis>Statusbar</emphasis>. It contains three elements "
134 "by default (left to right):<list><item>A <emphasis>text field"
135 "</emphasis> that displays the size of selected items. If only "
136 "one item is selected the name and type is shown as well.</item>"
137 "<item>A <emphasis>zoom slider</emphasis> that allows you "
138 "to adjust the size of the icons in the view.</item>"
139 "<item><emphasis>Space information</emphasis> about the "
140 "current storage device.</item></list></para>"));
143 DolphinStatusBar::~DolphinStatusBar()
147 void DolphinStatusBar::setText(const QString
&text
)
149 if (m_text
== text
) {
153 m_textTimestamp
= QTime::currentTime();
156 // will update status bar text in 50ms
157 m_delayUpdateTimer
->start();
160 QString
DolphinStatusBar::text() const
165 void DolphinStatusBar::setProgressText(const QString
&text
)
167 m_progressTextLabel
->setText(text
);
170 QString
DolphinStatusBar::progressText() const
172 return m_progressTextLabel
->text();
175 void DolphinStatusBar::setProgress(int percent
)
177 // Show a busy indicator if a value < 0 is provided:
178 m_progressBar
->setMaximum((percent
< 0) ? 0 : 100);
180 percent
= qBound(0, percent
, 100);
181 const bool progressRestarted
= (percent
< 100) && (percent
< m_progress
);
182 m_progress
= percent
;
183 if (progressRestarted
&& !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 m_showProgressBarTimer
->start();
189 m_progressBar
->setValue(m_progress
);
190 if (percent
== 100) {
191 // The end of the progress has been reached. Assure that the progress bar
192 // gets hidden and the extensions widgets get visible again.
193 m_showProgressBarTimer
->stop();
194 updateProgressInfo();
198 int DolphinStatusBar::progress() const
203 void DolphinStatusBar::resetToDefaultText()
208 if (currentTime
.msecsTo(m_textTimestamp
) < UpdateDelay
) {
209 m_delayUpdateTimer
->start();
215 void DolphinStatusBar::setDefaultText(const QString
&text
)
217 m_defaultText
= text
;
221 QString
DolphinStatusBar::defaultText() const
223 return m_defaultText
;
226 void DolphinStatusBar::setUrl(const QUrl
&url
)
228 if (GeneralSettings::showSpaceInfo()) {
229 m_spaceInfo
->setUrl(url
);
233 QUrl
DolphinStatusBar::url() const
235 return m_spaceInfo
->url();
238 void DolphinStatusBar::setZoomLevel(int zoomLevel
)
240 if (zoomLevel
!= m_zoomSlider
->value()) {
241 m_zoomSlider
->setValue(zoomLevel
);
245 int DolphinStatusBar::zoomLevel() const
247 return m_zoomSlider
->value();
250 void DolphinStatusBar::readSettings()
252 setVisible(GeneralSettings::showStatusBar());
253 setExtensionsVisible(true);
256 void DolphinStatusBar::updateSpaceInfo()
258 m_spaceInfo
->update();
261 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent
*event
)
267 QAction
*showZoomSliderAction
= menu
.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
268 showZoomSliderAction
->setCheckable(true);
269 showZoomSliderAction
->setChecked(GeneralSettings::showZoomSlider());
271 QAction
*showSpaceInfoAction
= menu
.addAction(i18nc("@action:inmenu", "Show Space Information"));
272 showSpaceInfoAction
->setCheckable(true);
273 showSpaceInfoAction
->setChecked(GeneralSettings::showSpaceInfo());
275 const QAction
*action
= menu
.exec(event
->reason() == QContextMenuEvent::Reason::Mouse
? QCursor::pos() : mapToGlobal(QPoint(width() / 2, height() / 2)));
276 if (action
== showZoomSliderAction
) {
277 const bool visible
= showZoomSliderAction
->isChecked();
278 GeneralSettings::setShowZoomSlider(visible
);
279 m_zoomSlider
->setVisible(visible
);
280 m_zoomLabel
->setVisible(visible
);
281 } else if (action
== showSpaceInfoAction
) {
282 const bool visible
= showSpaceInfoAction
->isChecked();
283 GeneralSettings::setShowSpaceInfo(visible
);
284 m_spaceInfo
->setVisible(visible
);
286 updateContentsMargins();
289 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel
)
291 updateZoomSliderToolTip(zoomLevel
);
293 QPoint global
= m_zoomSlider
->rect().topLeft();
294 global
.ry() += m_zoomSlider
->height() / 2;
295 QHelpEvent
toolTipEvent(QEvent::ToolTip
, QPoint(0, 0), m_zoomSlider
->mapToGlobal(global
));
296 QApplication::sendEvent(m_zoomSlider
, &toolTipEvent
);
299 void DolphinStatusBar::updateProgressInfo()
301 if (m_progress
< 100) {
302 // Show the progress information and hide the extensions
303 m_stopButton
->show();
304 m_progressTextLabel
->show();
305 m_progressBar
->show();
306 setExtensionsVisible(false);
308 // Hide the progress information and show the extensions
309 m_stopButton
->hide();
310 m_progressTextLabel
->hide();
311 m_progressBar
->hide();
312 setExtensionsVisible(true);
316 void DolphinStatusBar::updateLabelText()
318 const QString text
= m_text
.isEmpty() ? m_defaultText
: m_text
;
319 m_label
->setText(text
);
322 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel
)
324 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel
);
325 m_zoomSlider
->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size
));
328 void DolphinStatusBar::setExtensionsVisible(bool visible
)
330 bool showSpaceInfo
= visible
;
331 bool showZoomSlider
= visible
;
333 showSpaceInfo
= GeneralSettings::showSpaceInfo();
334 showZoomSlider
= GeneralSettings::showZoomSlider();
337 m_spaceInfo
->setShown(showSpaceInfo
);
338 m_spaceInfo
->setVisible(showSpaceInfo
);
339 m_zoomSlider
->setVisible(showZoomSlider
);
340 m_zoomLabel
->setVisible(showZoomSlider
);
341 updateContentsMargins();
344 void DolphinStatusBar::updateContentsMargins()
346 if (GeneralSettings::showSpaceInfo()) {
347 // 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.
348 layout()->setContentsMargins(6, 0, 2, 0);
350 layout()->setContentsMargins(6, 0, 6, 0);
354 void DolphinStatusBar::paintEvent(QPaintEvent
*paintEvent
)
360 style()->drawPrimitive(QStyle::PE_PanelStatusBar
, &opt
, &p
, this);
363 #include "moc_dolphinstatusbar.cpp"