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