]>
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 // Initialize text label
50 m_label
= new KSqueezedTextLabel(m_text
, this);
51 m_label
->setWordWrap(true);
52 m_label
->setTextFormat(Qt::PlainText
);
54 // Initialize zoom slider's explanatory label
55 m_zoomLabel
= new KSqueezedTextLabel(i18nc("Used as a noun, i.e. 'Here is the zoom level:'", "Zoom:"), this);
57 // Initialize zoom widget
58 m_zoomSlider
= new QSlider(Qt::Horizontal
, this);
59 m_zoomSlider
->setAccessibleName(i18n("Zoom"));
60 m_zoomSlider
->setAccessibleDescription(i18nc("Description for zoom-slider (accessibility)", "Sets the size of the file icons."));
61 m_zoomSlider
->setPageStep(1);
62 m_zoomSlider
->setRange(ZoomLevelInfo::minimumLevel(), ZoomLevelInfo::maximumLevel());
64 connect(m_zoomSlider
, &QSlider::valueChanged
, this, &DolphinStatusBar::zoomLevelChanged
);
65 connect(m_zoomSlider
, &QSlider::valueChanged
, this, &DolphinStatusBar::updateZoomSliderToolTip
);
66 connect(m_zoomSlider
, &QSlider::sliderMoved
, this, &DolphinStatusBar::showZoomSliderToolTip
);
68 // Initialize space information
69 m_spaceInfo
= new StatusBarSpaceInfo(this);
71 // Initialize progress information
72 m_stopButton
= new QToolButton(this);
73 m_stopButton
->setIcon(QIcon::fromTheme(QStringLiteral("process-stop")));
74 m_stopButton
->setAccessibleName(i18n("Stop"));
75 m_stopButton
->setAutoRaise(true);
76 m_stopButton
->setToolTip(i18nc("@tooltip", "Stop loading"));
78 connect(m_stopButton
, &QToolButton::clicked
, this, &DolphinStatusBar::stopPressed
);
80 m_progressTextLabel
= new QLabel(this);
81 m_progressTextLabel
->hide();
83 m_progressBar
= new QProgressBar(this);
84 m_progressBar
->hide();
86 m_showProgressBarTimer
= new QTimer(this);
87 m_showProgressBarTimer
->setInterval(500);
88 m_showProgressBarTimer
->setSingleShot(true);
89 connect(m_showProgressBarTimer
, &QTimer::timeout
, this, &DolphinStatusBar::updateProgressInfo
);
91 // initialize text updater delay timer
92 m_delayUpdateTimer
= new QTimer(this);
93 m_delayUpdateTimer
->setInterval(UpdateDelay
);
94 m_delayUpdateTimer
->setSingleShot(true);
95 connect(m_delayUpdateTimer
, &QTimer::timeout
, this, &DolphinStatusBar::updateLabelText
);
97 // Initialize top layout and size policies
98 const int fontHeight
= QFontMetrics(m_label
->font()).height();
99 const int zoomSliderHeight
= m_zoomSlider
->minimumSizeHint().height();
100 const int buttonHeight
= m_stopButton
->height();
101 const int contentHeight
= qMax(qMax(fontHeight
, zoomSliderHeight
), buttonHeight
);
103 QFontMetrics
fontMetrics(m_label
->font());
105 m_label
->setFixedHeight(contentHeight
);
106 m_label
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
108 m_zoomSlider
->setMaximumWidth(fontMetrics
.averageCharWidth() * 15);
110 m_spaceInfo
->setFixedHeight(contentHeight
);
111 m_spaceInfo
->setMaximumWidth(fontMetrics
.averageCharWidth() * 25);
112 m_spaceInfo
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
114 m_progressBar
->setFixedHeight(zoomSliderHeight
);
115 m_progressBar
->setMaximumWidth(fontMetrics
.averageCharWidth() * 20);
117 QHBoxLayout
*topLayout
= new QHBoxLayout(this);
118 updateContentsMargins();
119 topLayout
->setSpacing(4);
120 topLayout
->addWidget(m_label
, 1);
121 topLayout
->addWidget(m_zoomLabel
);
122 topLayout
->addWidget(m_zoomSlider
, 1);
123 topLayout
->addWidget(m_spaceInfo
, 1);
124 topLayout
->addWidget(m_stopButton
);
125 topLayout
->addWidget(m_progressTextLabel
);
126 topLayout
->addWidget(m_progressBar
);
128 setVisible(GeneralSettings::showStatusBar());
129 setExtensionsVisible(true);
130 setWhatsThis(xi18nc("@info:whatsthis Statusbar",
132 "the <emphasis>Statusbar</emphasis>. It contains three elements "
133 "by default (left to right):<list><item>A <emphasis>text field"
134 "</emphasis> that displays the size of selected items. If only "
135 "one item is selected the name and type is shown as well.</item>"
136 "<item>A <emphasis>zoom slider</emphasis> that allows you "
137 "to adjust the size of the icons in the view.</item>"
138 "<item><emphasis>Space information</emphasis> about the "
139 "current storage device.</item></list></para>"));
142 DolphinStatusBar::~DolphinStatusBar()
146 void DolphinStatusBar::setText(const QString
&text
)
148 if (m_text
== text
) {
152 m_textTimestamp
= QTime::currentTime();
155 // will update status bar text in 50ms
156 m_delayUpdateTimer
->start();
159 QString
DolphinStatusBar::text() const
164 void DolphinStatusBar::setProgressText(const QString
&text
)
166 m_progressTextLabel
->setText(text
);
169 QString
DolphinStatusBar::progressText() const
171 return m_progressTextLabel
->text();
174 void DolphinStatusBar::setProgress(int percent
)
176 // Show a busy indicator if a value < 0 is provided:
177 m_progressBar
->setMaximum((percent
< 0) ? 0 : 100);
179 percent
= qBound(0, percent
, 100);
180 const bool progressRestarted
= (percent
< 100) && (percent
< m_progress
);
181 m_progress
= percent
;
182 if (progressRestarted
&& !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 m_showProgressBarTimer
->start();
188 m_progressBar
->setValue(m_progress
);
189 if (percent
== 100) {
190 // The end of the progress has been reached. Assure that the progress bar
191 // gets hidden and the extensions widgets get visible again.
192 m_showProgressBarTimer
->stop();
193 updateProgressInfo();
197 int DolphinStatusBar::progress() const
202 void DolphinStatusBar::resetToDefaultText()
207 if (currentTime
.msecsTo(m_textTimestamp
) < UpdateDelay
) {
208 m_delayUpdateTimer
->start();
214 void DolphinStatusBar::setDefaultText(const QString
&text
)
216 m_defaultText
= text
;
220 QString
DolphinStatusBar::defaultText() const
222 return m_defaultText
;
225 void DolphinStatusBar::setUrl(const QUrl
&url
)
227 if (GeneralSettings::showSpaceInfo()) {
228 m_spaceInfo
->setUrl(url
);
232 QUrl
DolphinStatusBar::url() const
234 return m_spaceInfo
->url();
237 void DolphinStatusBar::setZoomLevel(int zoomLevel
)
239 if (zoomLevel
!= m_zoomSlider
->value()) {
240 m_zoomSlider
->setValue(zoomLevel
);
244 int DolphinStatusBar::zoomLevel() const
246 return m_zoomSlider
->value();
249 void DolphinStatusBar::readSettings()
251 setVisible(GeneralSettings::showStatusBar());
252 setExtensionsVisible(true);
255 void DolphinStatusBar::updateSpaceInfo()
257 m_spaceInfo
->update();
260 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent
*event
)
266 QAction
*showZoomSliderAction
= menu
.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
267 showZoomSliderAction
->setCheckable(true);
268 showZoomSliderAction
->setChecked(GeneralSettings::showZoomSlider());
270 QAction
*showSpaceInfoAction
= menu
.addAction(i18nc("@action:inmenu", "Show Space Information"));
271 showSpaceInfoAction
->setCheckable(true);
272 showSpaceInfoAction
->setChecked(GeneralSettings::showSpaceInfo());
274 const QAction
*action
= menu
.exec(event
->reason() == QContextMenuEvent::Reason::Mouse
? QCursor::pos() : mapToGlobal(QPoint(width() / 2, height() / 2)));
275 if (action
== showZoomSliderAction
) {
276 const bool visible
= showZoomSliderAction
->isChecked();
277 GeneralSettings::setShowZoomSlider(visible
);
278 m_zoomSlider
->setVisible(visible
);
279 m_zoomLabel
->setVisible(visible
);
280 } else if (action
== showSpaceInfoAction
) {
281 const bool visible
= showSpaceInfoAction
->isChecked();
282 GeneralSettings::setShowSpaceInfo(visible
);
283 m_spaceInfo
->setVisible(visible
);
285 updateContentsMargins();
288 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel
)
290 updateZoomSliderToolTip(zoomLevel
);
292 QPoint global
= m_zoomSlider
->rect().topLeft();
293 global
.ry() += m_zoomSlider
->height() / 2;
294 QHelpEvent
toolTipEvent(QEvent::ToolTip
, QPoint(0, 0), m_zoomSlider
->mapToGlobal(global
));
295 QApplication::sendEvent(m_zoomSlider
, &toolTipEvent
);
298 void DolphinStatusBar::updateProgressInfo()
300 if (m_progress
< 100) {
301 // Show the progress information and hide the extensions
302 m_stopButton
->show();
303 m_progressTextLabel
->show();
304 m_progressBar
->show();
305 setExtensionsVisible(false);
307 // Hide the progress information and show the extensions
308 m_stopButton
->hide();
309 m_progressTextLabel
->hide();
310 m_progressBar
->hide();
311 setExtensionsVisible(true);
315 void DolphinStatusBar::updateLabelText()
317 const QString text
= m_text
.isEmpty() ? m_defaultText
: m_text
;
318 m_label
->setText(text
);
321 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel
)
323 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel
);
324 m_zoomSlider
->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size
));
327 void DolphinStatusBar::setExtensionsVisible(bool visible
)
329 bool showSpaceInfo
= visible
;
330 bool showZoomSlider
= visible
;
332 showSpaceInfo
= GeneralSettings::showSpaceInfo();
333 showZoomSlider
= GeneralSettings::showZoomSlider();
336 m_spaceInfo
->setShown(showSpaceInfo
);
337 m_spaceInfo
->setVisible(showSpaceInfo
);
338 m_zoomSlider
->setVisible(showZoomSlider
);
339 m_zoomLabel
->setVisible(showZoomSlider
);
340 updateContentsMargins();
343 void DolphinStatusBar::updateContentsMargins()
345 if (GeneralSettings::showSpaceInfo()) {
346 // 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.
347 layout()->setContentsMargins(6, 0, 2, 0);
349 layout()->setContentsMargins(6, 0, 6, 0);
353 void DolphinStatusBar::paintEvent(QPaintEvent
*paintEvent
)
359 style()->drawPrimitive(QStyle::PE_PanelStatusBar
, &opt
, &p
, this);
362 #include "moc_dolphinstatusbar.cpp"