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