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