]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/dolphinstatusbar.cpp
5a3aa58577ccbf4d07820ef91c277cacdf746a22
[dolphin.git] / src / statusbar / dolphinstatusbar.cpp
1 /*
2 * SPDX-FileCopyrightText: 2006-2012 Peter Penz <peter.penz19@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "dolphinstatusbar.h"
8
9 #include "dolphin_generalsettings.h"
10 #include "statusbarspaceinfo.h"
11 #include "views/dolphinview.h"
12 #include "views/zoomlevelinfo.h"
13
14 #include <KLocalizedString>
15 #include <KSqueezedTextLabel>
16
17 #include <QApplication>
18 #include <QHBoxLayout>
19 #include <QHelpEvent>
20 #include <QIcon>
21 #include <QMenu>
22 #include <QPainter>
23 #include <QProgressBar>
24 #include <QSlider>
25 #include <QStyleOption>
26 #include <QTimer>
27 #include <QToolButton>
28
29 namespace
30 {
31 const int UpdateDelay = 50;
32 }
33
34 DolphinStatusBar::DolphinStatusBar(QWidget *parent)
35 : AnimatedHeightWidget(parent)
36 , m_text()
37 , m_defaultText()
38 , m_label(nullptr)
39 , m_zoomLabel(nullptr)
40 , m_spaceInfo(nullptr)
41 , m_zoomSlider(nullptr)
42 , m_progressBar(nullptr)
43 , m_stopButton(nullptr)
44 , m_progress(100)
45 , m_showProgressBarTimer(nullptr)
46 , m_delayUpdateTimer(nullptr)
47 , m_textTimestamp()
48 {
49 setProperty("_breeze_statusbar_separator", true);
50
51 QWidget *contentsContainer = prepareContentsContainer();
52
53 // Initialize text label
54 m_label = new KSqueezedTextLabel(m_text, contentsContainer);
55 m_label->setTextFormat(Qt::PlainText);
56
57 // Initialize zoom slider's explanatory label
58 m_zoomLabel = new KSqueezedTextLabel(i18nc("Used as a noun, i.e. 'Here is the zoom level:'", "Zoom:"), contentsContainer);
59
60 // Initialize zoom widget
61 m_zoomSlider = new QSlider(Qt::Horizontal, contentsContainer);
62 m_zoomSlider->setAccessibleName(i18n("Zoom"));
63 m_zoomSlider->setAccessibleDescription(i18nc("Description for zoom-slider (accessibility)", "Sets the size of the file icons."));
64 m_zoomSlider->setPageStep(1);
65 m_zoomSlider->setRange(ZoomLevelInfo::minimumLevel(), ZoomLevelInfo::maximumLevel());
66
67 connect(m_zoomSlider, &QSlider::valueChanged, this, &DolphinStatusBar::zoomLevelChanged);
68 connect(m_zoomSlider, &QSlider::valueChanged, this, &DolphinStatusBar::updateZoomSliderToolTip);
69 connect(m_zoomSlider, &QSlider::sliderMoved, this, &DolphinStatusBar::showZoomSliderToolTip);
70
71 // Initialize space information
72 m_spaceInfo = new StatusBarSpaceInfo(contentsContainer);
73
74 // Initialize progress information
75 m_stopButton = new QToolButton(contentsContainer);
76 m_stopButton->setIcon(QIcon::fromTheme(QStringLiteral("process-stop")));
77 m_stopButton->setAccessibleName(i18n("Stop"));
78 m_stopButton->setAutoRaise(true);
79 m_stopButton->setToolTip(i18nc("@tooltip", "Stop loading"));
80 m_stopButton->hide();
81 connect(m_stopButton, &QToolButton::clicked, this, &DolphinStatusBar::stopPressed);
82
83 m_progressTextLabel = new QLabel(contentsContainer);
84 m_progressTextLabel->hide();
85
86 m_progressBar = new QProgressBar(contentsContainer);
87 m_progressBar->hide();
88
89 m_showProgressBarTimer = new QTimer(this);
90 m_showProgressBarTimer->setInterval(500);
91 m_showProgressBarTimer->setSingleShot(true);
92 connect(m_showProgressBarTimer, &QTimer::timeout, this, &DolphinStatusBar::updateProgressInfo);
93
94 // initialize text updater delay timer
95 m_delayUpdateTimer = new QTimer(this);
96 m_delayUpdateTimer->setInterval(UpdateDelay);
97 m_delayUpdateTimer->setSingleShot(true);
98 connect(m_delayUpdateTimer, &QTimer::timeout, this, &DolphinStatusBar::updateLabelText);
99
100 // Initialize top layout and size policies
101 const int fontHeight = QFontMetrics(m_label->font()).height();
102 const int zoomSliderHeight = m_zoomSlider->minimumSizeHint().height();
103 const int buttonHeight = m_stopButton->height();
104 const int contentHeight = qMax(qMax(fontHeight, zoomSliderHeight), buttonHeight);
105
106 QFontMetrics fontMetrics(m_label->font());
107
108 m_label->setFixedHeight(contentHeight);
109 m_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
110
111 m_zoomSlider->setMaximumWidth(fontMetrics.averageCharWidth() * 15);
112
113 m_spaceInfo->setFixedHeight(contentHeight);
114 m_spaceInfo->setMaximumWidth(fontMetrics.averageCharWidth() * 25);
115 m_spaceInfo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
116
117 m_progressBar->setFixedHeight(zoomSliderHeight);
118 m_progressBar->setMaximumWidth(fontMetrics.averageCharWidth() * 20);
119
120 m_topLayout = new QHBoxLayout(contentsContainer);
121 updateContentsMargins();
122 m_topLayout->setSpacing(4);
123 m_topLayout->addWidget(m_label, 1);
124 m_topLayout->addWidget(m_zoomLabel);
125 m_topLayout->addWidget(m_zoomSlider, 1);
126 m_topLayout->addWidget(m_spaceInfo, 1);
127 m_topLayout->addWidget(m_stopButton);
128 m_topLayout->addWidget(m_progressTextLabel);
129 m_topLayout->addWidget(m_progressBar);
130
131 setVisible(GeneralSettings::showStatusBar(), WithoutAnimation);
132 setExtensionsVisible(true);
133 setWhatsThis(xi18nc("@info:whatsthis Statusbar",
134 "<para>This is "
135 "the <emphasis>Statusbar</emphasis>. It contains three elements "
136 "by default (left to right):<list><item>A <emphasis>text field"
137 "</emphasis> that displays the size of selected items. If only "
138 "one item is selected the name and type is shown as well.</item>"
139 "<item>A <emphasis>zoom slider</emphasis> that allows you "
140 "to adjust the size of the icons in the view.</item>"
141 "<item><emphasis>Space information</emphasis> about the "
142 "current storage device.</item></list></para>"));
143 }
144
145 DolphinStatusBar::~DolphinStatusBar()
146 {
147 }
148
149 void DolphinStatusBar::setText(const QString &text)
150 {
151 if (m_text == text) {
152 return;
153 }
154
155 m_textTimestamp = QTime::currentTime();
156
157 m_text = text;
158 // will update status bar text in 50ms
159 m_delayUpdateTimer->start();
160 }
161
162 QString DolphinStatusBar::text() const
163 {
164 return m_text;
165 }
166
167 void DolphinStatusBar::showProgress(const QString &currentlyRunningTaskTitle, int progressPercent, CancelLoading cancelLoading)
168 {
169 m_cancelLoading = cancelLoading;
170
171 // Show a busy indicator if a value < 0 is provided:
172 m_progressBar->setMaximum((progressPercent < 0) ? 0 : 100);
173
174 progressPercent = qBound(0, progressPercent, 100);
175 if (!m_progressBar->isVisible()) {
176 // Show the progress bar delayed: In the case if 100 % are reached within
177 // a short time, no progress bar will be shown at all.
178 if (!m_showProgressBarTimer->isActive()) {
179 m_showProgressBarTimer->start();
180 } else {
181 // The timer is already running. Should we restart it or keep it running?
182 if (m_progressTextLabel->text() != currentlyRunningTaskTitle || (progressPercent < 100 && progressPercent < m_progress)) {
183 m_showProgressBarTimer->start();
184 }
185 }
186 }
187 m_progress = progressPercent;
188
189 m_progressBar->setValue(m_progress);
190 if (progressPercent == 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();
195 }
196
197 m_progressTextLabel->setText(currentlyRunningTaskTitle);
198 }
199
200 QString DolphinStatusBar::progressText() const
201 {
202 return m_progressTextLabel->text();
203 }
204
205 int DolphinStatusBar::progress() const
206 {
207 return m_progress;
208 }
209
210 void DolphinStatusBar::resetToDefaultText()
211 {
212 m_text.clear();
213
214 QTime currentTime;
215 if (currentTime.msecsTo(m_textTimestamp) < UpdateDelay) {
216 m_delayUpdateTimer->start();
217 } else {
218 updateLabelText();
219 }
220 }
221
222 void DolphinStatusBar::setDefaultText(const QString &text)
223 {
224 m_defaultText = text;
225 updateLabelText();
226 }
227
228 QString DolphinStatusBar::defaultText() const
229 {
230 return m_defaultText;
231 }
232
233 void DolphinStatusBar::setUrl(const QUrl &url)
234 {
235 if (GeneralSettings::showSpaceInfo()) {
236 m_spaceInfo->setUrl(url);
237 }
238 }
239
240 QUrl DolphinStatusBar::url() const
241 {
242 return m_spaceInfo->url();
243 }
244
245 void DolphinStatusBar::setZoomLevel(int zoomLevel)
246 {
247 if (zoomLevel != m_zoomSlider->value()) {
248 m_zoomSlider->setValue(zoomLevel);
249 }
250 }
251
252 int DolphinStatusBar::zoomLevel() const
253 {
254 return m_zoomSlider->value();
255 }
256
257 void DolphinStatusBar::readSettings()
258 {
259 setVisible(GeneralSettings::showStatusBar(), WithAnimation);
260 setExtensionsVisible(true);
261 }
262
263 void DolphinStatusBar::updateSpaceInfo()
264 {
265 m_spaceInfo->update();
266 }
267
268 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent *event)
269 {
270 Q_UNUSED(event)
271
272 QMenu menu(this);
273
274 QAction *showZoomSliderAction = menu.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
275 showZoomSliderAction->setCheckable(true);
276 showZoomSliderAction->setChecked(GeneralSettings::showZoomSlider());
277
278 QAction *showSpaceInfoAction = menu.addAction(i18nc("@action:inmenu", "Show Space Information"));
279 showSpaceInfoAction->setCheckable(true);
280 showSpaceInfoAction->setChecked(GeneralSettings::showSpaceInfo());
281
282 const QAction *action = menu.exec(event->reason() == QContextMenuEvent::Reason::Mouse ? QCursor::pos() : mapToGlobal(QPoint(width() / 2, height() / 2)));
283 if (action == showZoomSliderAction) {
284 const bool visible = showZoomSliderAction->isChecked();
285 GeneralSettings::setShowZoomSlider(visible);
286 m_zoomSlider->setVisible(visible);
287 m_zoomLabel->setVisible(visible);
288 } else if (action == showSpaceInfoAction) {
289 const bool visible = showSpaceInfoAction->isChecked();
290 GeneralSettings::setShowSpaceInfo(visible);
291 m_spaceInfo->setVisible(visible);
292 }
293 updateContentsMargins();
294 }
295
296 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel)
297 {
298 updateZoomSliderToolTip(zoomLevel);
299
300 QPoint global = m_zoomSlider->rect().topLeft();
301 global.ry() += m_zoomSlider->height() / 2;
302 QHelpEvent toolTipEvent(QEvent::ToolTip, QPoint(0, 0), m_zoomSlider->mapToGlobal(global));
303 QApplication::sendEvent(m_zoomSlider, &toolTipEvent);
304 }
305
306 void DolphinStatusBar::updateProgressInfo()
307 {
308 if (m_progress < 100) {
309 // Show the progress information and hide the extensions
310 m_stopButton->setVisible(m_cancelLoading == CancelLoading::Allowed);
311 m_progressTextLabel->show();
312 m_progressBar->show();
313 setExtensionsVisible(false);
314 } else {
315 // Hide the progress information and show the extensions
316 m_stopButton->hide();
317 m_progressTextLabel->hide();
318 m_progressBar->hide();
319 setExtensionsVisible(true);
320 }
321 }
322
323 void DolphinStatusBar::updateLabelText()
324 {
325 const QString text = m_text.isEmpty() ? m_defaultText : m_text;
326 m_label->setText(text);
327 }
328
329 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel)
330 {
331 const int size = ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel);
332 m_zoomSlider->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size));
333 }
334
335 void DolphinStatusBar::setExtensionsVisible(bool visible)
336 {
337 bool showSpaceInfo = visible;
338 bool showZoomSlider = visible;
339 if (visible) {
340 showSpaceInfo = GeneralSettings::showSpaceInfo();
341 showZoomSlider = GeneralSettings::showZoomSlider();
342 }
343
344 m_spaceInfo->setShown(showSpaceInfo);
345 m_spaceInfo->setVisible(showSpaceInfo);
346 m_zoomSlider->setVisible(showZoomSlider);
347 m_zoomLabel->setVisible(showZoomSlider);
348 updateContentsMargins();
349 }
350
351 void DolphinStatusBar::updateContentsMargins()
352 {
353 if (GeneralSettings::showSpaceInfo()) {
354 // 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.
355 m_topLayout->setContentsMargins(6, 0, 2, 0);
356 } else {
357 m_topLayout->setContentsMargins(6, 0, 6, 0);
358 }
359 }
360
361 void DolphinStatusBar::paintEvent(QPaintEvent *paintEvent)
362 {
363 Q_UNUSED(paintEvent)
364 QPainter p(this);
365 QStyleOption opt;
366 opt.initFrom(this);
367 style()->drawPrimitive(QStyle::PE_PanelStatusBar, &opt, &p, this);
368 }
369
370 int DolphinStatusBar::preferredHeight() const
371 {
372 return m_spaceInfo->height();
373 }
374
375 #include "moc_dolphinstatusbar.cpp"