]>
cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/dolphinstatusbar.cpp
70ebe0c3cd338da7d2eb228892404c91202fd163
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>
22 #include <QProgressBar>
25 #include <QToolButton>
28 const int UpdateDelay
= 50;
31 DolphinStatusBar::DolphinStatusBar(QWidget
* parent
) :
38 m_zoomSlider(nullptr),
39 m_progressBar(nullptr),
40 m_stopButton(nullptr),
42 m_showProgressBarTimer(nullptr),
43 m_delayUpdateTimer(nullptr),
46 // Initialize text label
47 m_label
= new KSqueezedTextLabel(m_text
, this);
48 m_label
->setWordWrap(true);
49 m_label
->setTextFormat(Qt::PlainText
);
51 // Initialize zoom slider's explanatory label
52 m_zoomLabel
= new QLabel(i18nc("Used as a noun, i.e. 'Here is the zoom level:'","Zoom:"), this);
54 // Initialize zoom widget
55 m_zoomSlider
= new QSlider(Qt::Horizontal
, this);
56 m_zoomSlider
->setAccessibleName(i18n("Zoom"));
57 m_zoomSlider
->setAccessibleDescription(i18nc("Description for zoom-slider (accessibility)", "Sets the size of the file icons."));
58 m_zoomSlider
->setPageStep(1);
59 m_zoomSlider
->setRange(ZoomLevelInfo::minimumLevel(), ZoomLevelInfo::maximumLevel());
61 connect(m_zoomSlider
, &QSlider::valueChanged
, this, &DolphinStatusBar::zoomLevelChanged
);
62 connect(m_zoomSlider
, &QSlider::valueChanged
, this, &DolphinStatusBar::updateZoomSliderToolTip
);
63 connect(m_zoomSlider
, &QSlider::sliderMoved
, this, &DolphinStatusBar::showZoomSliderToolTip
);
65 // Initialize space information
66 m_spaceInfo
= new StatusBarSpaceInfo(this);
68 // Initialize progress information
69 m_stopButton
= new QToolButton(this);
70 m_stopButton
->setIcon(QIcon::fromTheme(QStringLiteral("process-stop")));
71 m_stopButton
->setAccessibleName(i18n("Stop"));
72 m_stopButton
->setAutoRaise(true);
73 m_stopButton
->setToolTip(i18nc("@tooltip", "Stop loading"));
75 connect(m_stopButton
, &QToolButton::clicked
, this, &DolphinStatusBar::stopPressed
);
77 m_progressTextLabel
= new QLabel(this);
78 m_progressTextLabel
->hide();
80 m_progressBar
= new QProgressBar(this);
81 m_progressBar
->hide();
83 m_showProgressBarTimer
= new QTimer(this);
84 m_showProgressBarTimer
->setInterval(500);
85 m_showProgressBarTimer
->setSingleShot(true);
86 connect(m_showProgressBarTimer
, &QTimer::timeout
, this, &DolphinStatusBar::updateProgressInfo
);
88 // initialize text updater delay timer
89 m_delayUpdateTimer
= new QTimer(this);
90 m_delayUpdateTimer
->setInterval(UpdateDelay
);
91 m_delayUpdateTimer
->setSingleShot(true);
92 connect(m_delayUpdateTimer
, &QTimer::timeout
,
93 this, &DolphinStatusBar::updateLabelText
);
95 // Initialize top layout and size policies
96 const int fontHeight
= QFontMetrics(m_label
->font()).height();
97 const int zoomSliderHeight
= m_zoomSlider
->minimumSizeHint().height();
98 const int buttonHeight
= m_stopButton
->height();
99 const int contentHeight
= qMax(qMax(fontHeight
, zoomSliderHeight
), buttonHeight
);
101 QFontMetrics
fontMetrics(m_label
->font());
103 m_label
->setFixedHeight(contentHeight
);
104 m_label
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
106 m_zoomSlider
->setMaximumWidth(fontMetrics
.averageCharWidth() * 15);
108 m_spaceInfo
->setFixedHeight(contentHeight
);
109 m_spaceInfo
->setMaximumWidth(fontMetrics
.averageCharWidth() * 25);
110 m_spaceInfo
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
112 m_progressBar
->setFixedHeight(zoomSliderHeight
);
113 m_progressBar
->setMaximumWidth(fontMetrics
.averageCharWidth() * 20);
115 QHBoxLayout
* topLayout
= new QHBoxLayout(this);
116 topLayout
->setContentsMargins(2, 0, 2, 0);
117 topLayout
->setSpacing(4);
118 topLayout
->addWidget(m_label
, 1);
119 topLayout
->addWidget(m_zoomLabel
);
120 topLayout
->addWidget(m_zoomSlider
, 1);
121 topLayout
->addWidget(m_spaceInfo
, 1);
122 topLayout
->addWidget(m_stopButton
);
123 topLayout
->addWidget(m_progressTextLabel
);
124 topLayout
->addWidget(m_progressBar
);
126 setVisible(GeneralSettings::showStatusBar());
127 setExtensionsVisible(true);
128 setWhatsThis(xi18nc("@info:whatsthis Statusbar", "<para>This is "
129 "the <emphasis>Statusbar</emphasis>. It contains three elements "
130 "by default (left to right):<list><item>A <emphasis>text field"
131 "</emphasis> that displays the size of selected items. If only "
132 "one item is selected the name and type is shown as well.</item>"
133 "<item>A <emphasis>zoom slider</emphasis> that allows you "
134 "to adjust the size of the icons in the view.</item>"
135 "<item><emphasis>Space information</emphasis> about the "
136 "current storage device.</item></list></para>"));
139 DolphinStatusBar::~DolphinStatusBar()
143 void DolphinStatusBar::setText(const QString
& text
)
145 if (m_text
== text
) {
149 m_textTimestamp
= QTime::currentTime();
152 // will update status bar text in 50ms
153 m_delayUpdateTimer
->start();
156 QString
DolphinStatusBar::text() const
161 void DolphinStatusBar::setProgressText(const QString
& text
)
163 m_progressTextLabel
->setText(text
);
166 QString
DolphinStatusBar::progressText() const
168 return m_progressTextLabel
->text();
171 void DolphinStatusBar::setProgress(int percent
)
173 // Show a busy indicator if a value < 0 is provided:
174 m_progressBar
->setMaximum((percent
< 0) ? 0 : 100);
176 percent
= qBound(0, percent
, 100);
177 const bool progressRestarted
= (percent
< 100) && (percent
< m_progress
);
178 m_progress
= percent
;
179 if (progressRestarted
&& !m_progressBar
->isVisible()) {
180 // Show the progress bar delayed: In the case if 100 % are reached within
181 // a short time, no progress bar will be shown at all.
182 m_showProgressBarTimer
->start();
185 m_progressBar
->setValue(m_progress
);
186 if (percent
== 100) {
187 // The end of the progress has been reached. Assure that the progress bar
188 // gets hidden and the extensions widgets get visible again.
189 m_showProgressBarTimer
->stop();
190 updateProgressInfo();
194 int DolphinStatusBar::progress() const
199 void DolphinStatusBar::resetToDefaultText()
204 if (currentTime
.msecsTo(m_textTimestamp
) < UpdateDelay
) {
205 m_delayUpdateTimer
->start();
211 void DolphinStatusBar::setDefaultText(const QString
& text
)
213 m_defaultText
= text
;
217 QString
DolphinStatusBar::defaultText() const
219 return m_defaultText
;
222 void DolphinStatusBar::setUrl(const QUrl
& url
)
224 if (GeneralSettings::showSpaceInfo()) {
225 m_spaceInfo
->setUrl(url
);
229 QUrl
DolphinStatusBar::url() const
231 return m_spaceInfo
->url();
234 void DolphinStatusBar::setZoomLevel(int zoomLevel
)
236 if (zoomLevel
!= m_zoomSlider
->value()) {
237 m_zoomSlider
->setValue(zoomLevel
);
241 int DolphinStatusBar::zoomLevel() const
243 return m_zoomSlider
->value();
246 void DolphinStatusBar::readSettings()
248 setVisible(GeneralSettings::showStatusBar());
249 setExtensionsVisible(true);
252 void DolphinStatusBar::updateSpaceInfo()
254 m_spaceInfo
->update();
257 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent
* event
)
263 QAction
* showZoomSliderAction
= menu
.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
264 showZoomSliderAction
->setCheckable(true);
265 showZoomSliderAction
->setChecked(GeneralSettings::showZoomSlider());
267 QAction
* showSpaceInfoAction
= menu
.addAction(i18nc("@action:inmenu", "Show Space Information"));
268 showSpaceInfoAction
->setCheckable(true);
269 showSpaceInfoAction
->setChecked(GeneralSettings::showSpaceInfo());
271 const QAction
* action
= menu
.exec(QCursor::pos());
272 if (action
== showZoomSliderAction
) {
273 const bool visible
= showZoomSliderAction
->isChecked();
274 GeneralSettings::setShowZoomSlider(visible
);
275 m_zoomSlider
->setVisible(visible
);
276 m_zoomLabel
->setVisible(visible
);
277 } else if (action
== showSpaceInfoAction
) {
278 const bool visible
= showSpaceInfoAction
->isChecked();
279 GeneralSettings::setShowSpaceInfo(visible
);
280 m_spaceInfo
->setVisible(visible
);
284 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel
)
286 updateZoomSliderToolTip(zoomLevel
);
288 QPoint global
= m_zoomSlider
->rect().topLeft();
289 global
.ry() += m_zoomSlider
->height() / 2;
290 QHelpEvent
toolTipEvent(QEvent::ToolTip
, QPoint(0, 0), m_zoomSlider
->mapToGlobal(global
));
291 QApplication::sendEvent(m_zoomSlider
, &toolTipEvent
);
294 void DolphinStatusBar::updateProgressInfo()
296 if (m_progress
< 100) {
297 // Show the progress information and hide the extensions
298 m_stopButton
->show();
299 m_progressTextLabel
->show();
300 m_progressBar
->show();
301 setExtensionsVisible(false);
303 // Hide the progress information and show the extensions
304 m_stopButton
->hide();
305 m_progressTextLabel
->hide();
306 m_progressBar
->hide();
307 setExtensionsVisible(true);
311 void DolphinStatusBar::updateLabelText()
313 const QString text
= m_text
.isEmpty() ? m_defaultText
: m_text
;
314 m_label
->setText(text
);
317 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel
)
319 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel
);
320 m_zoomSlider
->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size
));
323 void DolphinStatusBar::setExtensionsVisible(bool visible
)
325 bool showSpaceInfo
= visible
;
326 bool showZoomSlider
= visible
;
328 showSpaceInfo
= GeneralSettings::showSpaceInfo();
329 showZoomSlider
= GeneralSettings::showZoomSlider();
332 m_spaceInfo
->setShown(showSpaceInfo
);
333 m_spaceInfo
->setVisible(showSpaceInfo
);
334 m_zoomSlider
->setVisible(showZoomSlider
);
335 m_zoomLabel
->setVisible(showZoomSlider
);