]>
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
)
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
->setWordWrap(true);
56 m_label
->setTextFormat(Qt::PlainText
);
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
);
75 // Initialize progress information
76 m_stopButton
= new QToolButton(contentsContainer
);
77 m_stopButton
->setIcon(QIcon::fromTheme(QStringLiteral("process-stop")));
78 m_stopButton
->setAccessibleName(i18n("Stop"));
79 m_stopButton
->setAutoRaise(true);
80 m_stopButton
->setToolTip(i18nc("@tooltip", "Stop loading"));
82 connect(m_stopButton
, &QToolButton::clicked
, this, &DolphinStatusBar::stopPressed
);
84 m_progressTextLabel
= new QLabel(contentsContainer
);
85 m_progressTextLabel
->hide();
87 m_progressBar
= new QProgressBar(contentsContainer
);
88 m_progressBar
->hide();
90 m_showProgressBarTimer
= new QTimer(this);
91 m_showProgressBarTimer
->setInterval(500);
92 m_showProgressBarTimer
->setSingleShot(true);
93 connect(m_showProgressBarTimer
, &QTimer::timeout
, this, &DolphinStatusBar::updateProgressInfo
);
95 // initialize text updater delay timer
96 m_delayUpdateTimer
= new QTimer(this);
97 m_delayUpdateTimer
->setInterval(UpdateDelay
);
98 m_delayUpdateTimer
->setSingleShot(true);
99 connect(m_delayUpdateTimer
, &QTimer::timeout
, this, &DolphinStatusBar::updateLabelText
);
101 // Initialize top layout and size policies
102 const int fontHeight
= QFontMetrics(m_label
->font()).height();
103 const int zoomSliderHeight
= m_zoomSlider
->minimumSizeHint().height();
104 const int buttonHeight
= m_stopButton
->height();
105 const int contentHeight
= qMax(qMax(fontHeight
, zoomSliderHeight
), buttonHeight
);
107 QFontMetrics
fontMetrics(m_label
->font());
109 m_label
->setFixedHeight(contentHeight
);
110 m_label
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
112 m_zoomSlider
->setMaximumWidth(fontMetrics
.averageCharWidth() * 15);
114 m_spaceInfo
->setFixedHeight(contentHeight
);
115 m_spaceInfo
->setMaximumWidth(fontMetrics
.averageCharWidth() * 25);
116 m_spaceInfo
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
118 m_progressBar
->setFixedHeight(zoomSliderHeight
);
119 m_progressBar
->setMaximumWidth(fontMetrics
.averageCharWidth() * 20);
121 m_topLayout
= new QHBoxLayout(contentsContainer
);
122 updateContentsMargins();
123 m_topLayout
->setSpacing(4);
124 m_topLayout
->addWidget(m_label
, 1);
125 m_topLayout
->addWidget(m_zoomLabel
);
126 m_topLayout
->addWidget(m_zoomSlider
, 1);
127 m_topLayout
->addWidget(m_spaceInfo
, 1);
128 m_topLayout
->addWidget(m_stopButton
);
129 m_topLayout
->addWidget(m_progressTextLabel
);
130 m_topLayout
->addWidget(m_progressBar
);
132 setVisible(GeneralSettings::showStatusBar(), WithoutAnimation
);
133 setExtensionsVisible(true);
134 setWhatsThis(xi18nc("@info:whatsthis Statusbar",
136 "the <emphasis>Statusbar</emphasis>. It contains three elements "
137 "by default (left to right):<list><item>A <emphasis>text field"
138 "</emphasis> that displays the size of selected items. If only "
139 "one item is selected the name and type is shown as well.</item>"
140 "<item>A <emphasis>zoom slider</emphasis> that allows you "
141 "to adjust the size of the icons in the view.</item>"
142 "<item><emphasis>Space information</emphasis> about the "
143 "current storage device.</item></list></para>"));
146 DolphinStatusBar::~DolphinStatusBar()
150 void DolphinStatusBar::setText(const QString
&text
)
152 if (m_text
== text
) {
156 m_textTimestamp
= QTime::currentTime();
159 // will update status bar text in 50ms
160 m_delayUpdateTimer
->start();
163 QString
DolphinStatusBar::text() const
168 void DolphinStatusBar::setProgressText(const QString
&text
)
170 m_progressTextLabel
->setText(text
);
173 QString
DolphinStatusBar::progressText() const
175 return m_progressTextLabel
->text();
178 void DolphinStatusBar::setProgress(int percent
)
180 // Show a busy indicator if a value < 0 is provided:
181 m_progressBar
->setMaximum((percent
< 0) ? 0 : 100);
183 percent
= qBound(0, percent
, 100);
184 const bool progressRestarted
= (percent
< 100) && (percent
< m_progress
);
185 m_progress
= percent
;
186 if (progressRestarted
&& !m_progressBar
->isVisible()) {
187 // Show the progress bar delayed: In the case if 100 % are reached within
188 // a short time, no progress bar will be shown at all.
189 m_showProgressBarTimer
->start();
192 m_progressBar
->setValue(m_progress
);
193 if (percent
== 100) {
194 // The end of the progress has been reached. Assure that the progress bar
195 // gets hidden and the extensions widgets get visible again.
196 m_showProgressBarTimer
->stop();
197 updateProgressInfo();
201 int DolphinStatusBar::progress() const
206 void DolphinStatusBar::resetToDefaultText()
211 if (currentTime
.msecsTo(m_textTimestamp
) < UpdateDelay
) {
212 m_delayUpdateTimer
->start();
218 void DolphinStatusBar::setDefaultText(const QString
&text
)
220 m_defaultText
= text
;
224 QString
DolphinStatusBar::defaultText() const
226 return m_defaultText
;
229 void DolphinStatusBar::setUrl(const QUrl
&url
)
231 if (GeneralSettings::showSpaceInfo()) {
232 m_spaceInfo
->setUrl(url
);
236 QUrl
DolphinStatusBar::url() const
238 return m_spaceInfo
->url();
241 void DolphinStatusBar::setZoomLevel(int zoomLevel
)
243 if (zoomLevel
!= m_zoomSlider
->value()) {
244 m_zoomSlider
->setValue(zoomLevel
);
248 int DolphinStatusBar::zoomLevel() const
250 return m_zoomSlider
->value();
253 void DolphinStatusBar::readSettings()
255 setVisible(GeneralSettings::showStatusBar(), WithAnimation
);
256 setExtensionsVisible(true);
259 void DolphinStatusBar::updateSpaceInfo()
261 m_spaceInfo
->update();
264 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent
*event
)
270 QAction
*showZoomSliderAction
= menu
.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
271 showZoomSliderAction
->setCheckable(true);
272 showZoomSliderAction
->setChecked(GeneralSettings::showZoomSlider());
274 QAction
*showSpaceInfoAction
= menu
.addAction(i18nc("@action:inmenu", "Show Space Information"));
275 showSpaceInfoAction
->setCheckable(true);
276 showSpaceInfoAction
->setChecked(GeneralSettings::showSpaceInfo());
278 const QAction
*action
= menu
.exec(event
->reason() == QContextMenuEvent::Reason::Mouse
? QCursor::pos() : mapToGlobal(QPoint(width() / 2, height() / 2)));
279 if (action
== showZoomSliderAction
) {
280 const bool visible
= showZoomSliderAction
->isChecked();
281 GeneralSettings::setShowZoomSlider(visible
);
282 m_zoomSlider
->setVisible(visible
);
283 m_zoomLabel
->setVisible(visible
);
284 } else if (action
== showSpaceInfoAction
) {
285 const bool visible
= showSpaceInfoAction
->isChecked();
286 GeneralSettings::setShowSpaceInfo(visible
);
287 m_spaceInfo
->setVisible(visible
);
289 updateContentsMargins();
292 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel
)
294 updateZoomSliderToolTip(zoomLevel
);
296 QPoint global
= m_zoomSlider
->rect().topLeft();
297 global
.ry() += m_zoomSlider
->height() / 2;
298 QHelpEvent
toolTipEvent(QEvent::ToolTip
, QPoint(0, 0), m_zoomSlider
->mapToGlobal(global
));
299 QApplication::sendEvent(m_zoomSlider
, &toolTipEvent
);
302 void DolphinStatusBar::updateProgressInfo()
304 if (m_progress
< 100) {
305 // Show the progress information and hide the extensions
306 m_stopButton
->show();
307 m_progressTextLabel
->show();
308 m_progressBar
->show();
309 setExtensionsVisible(false);
311 // Hide the progress information and show the extensions
312 m_stopButton
->hide();
313 m_progressTextLabel
->hide();
314 m_progressBar
->hide();
315 setExtensionsVisible(true);
319 void DolphinStatusBar::updateLabelText()
321 const QString text
= m_text
.isEmpty() ? m_defaultText
: m_text
;
322 m_label
->setText(text
);
325 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel
)
327 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel
);
328 m_zoomSlider
->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size
));
331 void DolphinStatusBar::setExtensionsVisible(bool visible
)
333 bool showSpaceInfo
= visible
;
334 bool showZoomSlider
= visible
;
336 showSpaceInfo
= GeneralSettings::showSpaceInfo();
337 showZoomSlider
= GeneralSettings::showZoomSlider();
340 m_spaceInfo
->setShown(showSpaceInfo
);
341 m_spaceInfo
->setVisible(showSpaceInfo
);
342 m_zoomSlider
->setVisible(showZoomSlider
);
343 m_zoomLabel
->setVisible(showZoomSlider
);
344 updateContentsMargins();
347 void DolphinStatusBar::updateContentsMargins()
349 if (GeneralSettings::showSpaceInfo()) {
350 // 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.
351 m_topLayout
->setContentsMargins(6, 0, 2, 0);
353 m_topLayout
->setContentsMargins(6, 0, 6, 0);
357 void DolphinStatusBar::paintEvent(QPaintEvent
*paintEvent
)
363 style()->drawPrimitive(QStyle::PE_PanelStatusBar
, &opt
, &p
, this);
366 int DolphinStatusBar::preferredHeight() const
368 return m_spaceInfo
->height();
371 #include "moc_dolphinstatusbar.cpp"