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