]>
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>
22 #include <QProgressBar>
24 #include <QTextDocument>
26 #include <QToolButton>
29 const int UpdateDelay
= 50;
32 DolphinStatusBar::DolphinStatusBar(QWidget
* parent
) :
39 m_zoomSlider(nullptr),
40 m_progressBar(nullptr),
41 m_stopButton(nullptr),
43 m_showProgressBarTimer(nullptr),
44 m_delayUpdateTimer(nullptr),
47 // Initialize text label
48 m_label
= new KSqueezedTextLabel(m_text
, this);
49 m_label
->setWordWrap(true);
50 m_label
->setTextFormat(Qt::PlainText
);
52 // Initialize zoom slider's explanatory label
53 m_zoomLabel
= new QLabel(i18nc("Used as a noun, i.e. 'Here is the zoom level:'","Zoom:"), this);
55 // Initialize zoom widget
56 m_zoomSlider
= new QSlider(Qt::Horizontal
, this);
57 m_zoomSlider
->setAccessibleName(i18n("Zoom"));
58 m_zoomSlider
->setAccessibleDescription(i18nc("Description for zoom-slider (accessibility)", "Sets the size of the file icons."));
59 m_zoomSlider
->setPageStep(1);
60 m_zoomSlider
->setRange(ZoomLevelInfo::minimumLevel(), ZoomLevelInfo::maximumLevel());
62 connect(m_zoomSlider
, &QSlider::valueChanged
, this, &DolphinStatusBar::zoomLevelChanged
);
63 connect(m_zoomSlider
, &QSlider::valueChanged
, this, &DolphinStatusBar::updateZoomSliderToolTip
);
64 connect(m_zoomSlider
, &QSlider::sliderMoved
, this, &DolphinStatusBar::showZoomSliderToolTip
);
66 // Initialize space information
67 m_spaceInfo
= new StatusBarSpaceInfo(this);
69 // Initialize progress information
70 m_stopButton
= new QToolButton(this);
71 m_stopButton
->setIcon(QIcon::fromTheme(QStringLiteral("process-stop")));
72 m_stopButton
->setAccessibleName(i18n("Stop"));
73 m_stopButton
->setAutoRaise(true);
74 m_stopButton
->setToolTip(i18nc("@tooltip", "Stop loading"));
76 connect(m_stopButton
, &QToolButton::clicked
, this, &DolphinStatusBar::stopPressed
);
78 m_progressTextLabel
= new QLabel(this);
79 m_progressTextLabel
->hide();
81 m_progressBar
= new QProgressBar(this);
82 m_progressBar
->hide();
84 m_showProgressBarTimer
= new QTimer(this);
85 m_showProgressBarTimer
->setInterval(500);
86 m_showProgressBarTimer
->setSingleShot(true);
87 connect(m_showProgressBarTimer
, &QTimer::timeout
, this, &DolphinStatusBar::updateProgressInfo
);
89 // initialize text updater delay timer
90 m_delayUpdateTimer
= new QTimer(this);
91 m_delayUpdateTimer
->setInterval(UpdateDelay
);
92 m_delayUpdateTimer
->setSingleShot(true);
93 connect(m_delayUpdateTimer
, &QTimer::timeout
,
94 this, &DolphinStatusBar::updateLabelText
);
96 // Initialize top layout and size policies
97 const int fontHeight
= QFontMetrics(m_label
->font()).height();
98 const int zoomSliderHeight
= m_zoomSlider
->minimumSizeHint().height();
99 const int buttonHeight
= m_stopButton
->height();
100 const int contentHeight
= qMax(qMax(fontHeight
, zoomSliderHeight
), buttonHeight
);
102 QFontMetrics
fontMetrics(m_label
->font());
104 m_label
->setFixedHeight(contentHeight
);
105 m_label
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
107 m_zoomSlider
->setMaximumWidth(fontMetrics
.averageCharWidth() * 15);
109 m_spaceInfo
->setFixedHeight(contentHeight
);
110 m_spaceInfo
->setMaximumWidth(fontMetrics
.averageCharWidth() * 25);
111 m_spaceInfo
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
113 m_progressBar
->setFixedHeight(zoomSliderHeight
);
114 m_progressBar
->setMaximumWidth(fontMetrics
.averageCharWidth() * 20);
116 QHBoxLayout
* topLayout
= new QHBoxLayout(this);
117 topLayout
->setContentsMargins(2, 0, 2, 0);
118 topLayout
->setSpacing(4);
119 topLayout
->addWidget(m_label
, 1);
120 topLayout
->addWidget(m_zoomLabel
);
121 topLayout
->addWidget(m_zoomSlider
, 1);
122 topLayout
->addWidget(m_spaceInfo
, 1);
123 topLayout
->addWidget(m_stopButton
);
124 topLayout
->addWidget(m_progressTextLabel
);
125 topLayout
->addWidget(m_progressBar
);
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 setExtensionsVisible(true);
251 void DolphinStatusBar::updateSpaceInfo()
253 m_spaceInfo
->update();
256 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent
* event
)
262 QAction
* showZoomSliderAction
= menu
.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
263 showZoomSliderAction
->setCheckable(true);
264 showZoomSliderAction
->setChecked(GeneralSettings::showZoomSlider());
266 QAction
* showSpaceInfoAction
= menu
.addAction(i18nc("@action:inmenu", "Show Space Information"));
267 showSpaceInfoAction
->setCheckable(true);
268 showSpaceInfoAction
->setChecked(GeneralSettings::showSpaceInfo());
270 const QAction
* action
= menu
.exec(QCursor::pos());
271 if (action
== showZoomSliderAction
) {
272 const bool visible
= showZoomSliderAction
->isChecked();
273 GeneralSettings::setShowZoomSlider(visible
);
274 m_zoomSlider
->setVisible(visible
);
275 m_zoomLabel
->setVisible(visible
);
276 } else if (action
== showSpaceInfoAction
) {
277 const bool visible
= showSpaceInfoAction
->isChecked();
278 GeneralSettings::setShowSpaceInfo(visible
);
279 m_spaceInfo
->setVisible(visible
);
283 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel
)
285 updateZoomSliderToolTip(zoomLevel
);
287 QPoint global
= m_zoomSlider
->rect().topLeft();
288 global
.ry() += m_zoomSlider
->height() / 2;
289 QHelpEvent
toolTipEvent(QEvent::ToolTip
, QPoint(0, 0), m_zoomSlider
->mapToGlobal(global
));
290 QApplication::sendEvent(m_zoomSlider
, &toolTipEvent
);
293 void DolphinStatusBar::updateProgressInfo()
295 if (m_progress
< 100) {
296 // Show the progress information and hide the extensions
297 m_stopButton
->show();
298 m_progressTextLabel
->show();
299 m_progressBar
->show();
300 setExtensionsVisible(false);
302 // Hide the progress information and show the extensions
303 m_stopButton
->hide();
304 m_progressTextLabel
->hide();
305 m_progressBar
->hide();
306 setExtensionsVisible(true);
310 void DolphinStatusBar::updateLabelText()
312 const QString text
= m_text
.isEmpty() ? m_defaultText
: m_text
;
313 m_label
->setText(text
);
316 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel
)
318 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel
);
319 m_zoomSlider
->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size
));
322 void DolphinStatusBar::setExtensionsVisible(bool visible
)
324 bool showStatusBar
= visible
;
325 bool showSpaceInfo
= visible
;
326 bool showZoomSlider
= visible
;
328 showStatusBar
= GeneralSettings::showStatusBar();
329 showSpaceInfo
= GeneralSettings::showSpaceInfo();
330 showZoomSlider
= GeneralSettings::showZoomSlider();
332 setVisible(showStatusBar
);
333 m_spaceInfo
->setShown(showSpaceInfo
);
334 m_spaceInfo
->setVisible(showSpaceInfo
);
335 m_zoomSlider
->setVisible(showZoomSlider
);
336 m_zoomLabel
->setVisible(showZoomSlider
);