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