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