]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/dolphinstatusbar.cpp
70ebe0c3cd338da7d2eb228892404c91202fd163
[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 <QTimer>
25 #include <QToolButton>
26
27 namespace {
28 const int UpdateDelay = 50;
29 }
30
31 DolphinStatusBar::DolphinStatusBar(QWidget* parent) :
32 QWidget(parent),
33 m_text(),
34 m_defaultText(),
35 m_label(nullptr),
36 m_zoomLabel(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 slider's explanatory label
52 m_zoomLabel = new QLabel(i18nc("Used as a noun, i.e. 'Here is the zoom level:'","Zoom:"), this);
53
54 // Initialize zoom widget
55 m_zoomSlider = new QSlider(Qt::Horizontal, this);
56 m_zoomSlider->setAccessibleName(i18n("Zoom"));
57 m_zoomSlider->setAccessibleDescription(i18nc("Description for zoom-slider (accessibility)", "Sets the size of the file icons."));
58 m_zoomSlider->setPageStep(1);
59 m_zoomSlider->setRange(ZoomLevelInfo::minimumLevel(), ZoomLevelInfo::maximumLevel());
60
61 connect(m_zoomSlider, &QSlider::valueChanged, this, &DolphinStatusBar::zoomLevelChanged);
62 connect(m_zoomSlider, &QSlider::valueChanged, this, &DolphinStatusBar::updateZoomSliderToolTip);
63 connect(m_zoomSlider, &QSlider::sliderMoved, this, &DolphinStatusBar::showZoomSliderToolTip);
64
65 // Initialize space information
66 m_spaceInfo = new StatusBarSpaceInfo(this);
67
68 // Initialize progress information
69 m_stopButton = new QToolButton(this);
70 m_stopButton->setIcon(QIcon::fromTheme(QStringLiteral("process-stop")));
71 m_stopButton->setAccessibleName(i18n("Stop"));
72 m_stopButton->setAutoRaise(true);
73 m_stopButton->setToolTip(i18nc("@tooltip", "Stop loading"));
74 m_stopButton->hide();
75 connect(m_stopButton, &QToolButton::clicked, this, &DolphinStatusBar::stopPressed);
76
77 m_progressTextLabel = new QLabel(this);
78 m_progressTextLabel->hide();
79
80 m_progressBar = new QProgressBar(this);
81 m_progressBar->hide();
82
83 m_showProgressBarTimer = new QTimer(this);
84 m_showProgressBarTimer->setInterval(500);
85 m_showProgressBarTimer->setSingleShot(true);
86 connect(m_showProgressBarTimer, &QTimer::timeout, this, &DolphinStatusBar::updateProgressInfo);
87
88 // initialize text updater delay timer
89 m_delayUpdateTimer = new QTimer(this);
90 m_delayUpdateTimer->setInterval(UpdateDelay);
91 m_delayUpdateTimer->setSingleShot(true);
92 connect(m_delayUpdateTimer, &QTimer::timeout,
93 this, &DolphinStatusBar::updateLabelText);
94
95 // Initialize top layout and size policies
96 const int fontHeight = QFontMetrics(m_label->font()).height();
97 const int zoomSliderHeight = m_zoomSlider->minimumSizeHint().height();
98 const int buttonHeight = m_stopButton->height();
99 const int contentHeight = qMax(qMax(fontHeight, zoomSliderHeight), buttonHeight);
100
101 QFontMetrics fontMetrics(m_label->font());
102
103 m_label->setFixedHeight(contentHeight);
104 m_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
105
106 m_zoomSlider->setMaximumWidth(fontMetrics.averageCharWidth() * 15);
107
108 m_spaceInfo->setFixedHeight(contentHeight);
109 m_spaceInfo->setMaximumWidth(fontMetrics.averageCharWidth() * 25);
110 m_spaceInfo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
111
112 m_progressBar->setFixedHeight(zoomSliderHeight);
113 m_progressBar->setMaximumWidth(fontMetrics.averageCharWidth() * 20);
114
115 QHBoxLayout* topLayout = new QHBoxLayout(this);
116 topLayout->setContentsMargins(2, 0, 2, 0);
117 topLayout->setSpacing(4);
118 topLayout->addWidget(m_label, 1);
119 topLayout->addWidget(m_zoomLabel);
120 topLayout->addWidget(m_zoomSlider, 1);
121 topLayout->addWidget(m_spaceInfo, 1);
122 topLayout->addWidget(m_stopButton);
123 topLayout->addWidget(m_progressTextLabel);
124 topLayout->addWidget(m_progressBar);
125
126 setVisible(GeneralSettings::showStatusBar());
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 setVisible(GeneralSettings::showStatusBar());
249 setExtensionsVisible(true);
250 }
251
252 void DolphinStatusBar::updateSpaceInfo()
253 {
254 m_spaceInfo->update();
255 }
256
257 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent* event)
258 {
259 Q_UNUSED(event)
260
261 QMenu menu(this);
262
263 QAction* showZoomSliderAction = menu.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
264 showZoomSliderAction->setCheckable(true);
265 showZoomSliderAction->setChecked(GeneralSettings::showZoomSlider());
266
267 QAction* showSpaceInfoAction = menu.addAction(i18nc("@action:inmenu", "Show Space Information"));
268 showSpaceInfoAction->setCheckable(true);
269 showSpaceInfoAction->setChecked(GeneralSettings::showSpaceInfo());
270
271 const QAction* action = menu.exec(QCursor::pos());
272 if (action == showZoomSliderAction) {
273 const bool visible = showZoomSliderAction->isChecked();
274 GeneralSettings::setShowZoomSlider(visible);
275 m_zoomSlider->setVisible(visible);
276 m_zoomLabel->setVisible(visible);
277 } else if (action == showSpaceInfoAction) {
278 const bool visible = showSpaceInfoAction->isChecked();
279 GeneralSettings::setShowSpaceInfo(visible);
280 m_spaceInfo->setVisible(visible);
281 }
282 }
283
284 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel)
285 {
286 updateZoomSliderToolTip(zoomLevel);
287
288 QPoint global = m_zoomSlider->rect().topLeft();
289 global.ry() += m_zoomSlider->height() / 2;
290 QHelpEvent toolTipEvent(QEvent::ToolTip, QPoint(0, 0), m_zoomSlider->mapToGlobal(global));
291 QApplication::sendEvent(m_zoomSlider, &toolTipEvent);
292 }
293
294 void DolphinStatusBar::updateProgressInfo()
295 {
296 if (m_progress < 100) {
297 // Show the progress information and hide the extensions
298 m_stopButton->show();
299 m_progressTextLabel->show();
300 m_progressBar->show();
301 setExtensionsVisible(false);
302 } else {
303 // Hide the progress information and show the extensions
304 m_stopButton->hide();
305 m_progressTextLabel->hide();
306 m_progressBar->hide();
307 setExtensionsVisible(true);
308 }
309 }
310
311 void DolphinStatusBar::updateLabelText()
312 {
313 const QString text = m_text.isEmpty() ? m_defaultText : m_text;
314 m_label->setText(text);
315 }
316
317 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel)
318 {
319 const int size = ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel);
320 m_zoomSlider->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size));
321 }
322
323 void DolphinStatusBar::setExtensionsVisible(bool visible)
324 {
325 bool showSpaceInfo = visible;
326 bool showZoomSlider = visible;
327 if (visible) {
328 showSpaceInfo = GeneralSettings::showSpaceInfo();
329 showZoomSlider = GeneralSettings::showZoomSlider();
330 }
331
332 m_spaceInfo->setShown(showSpaceInfo);
333 m_spaceInfo->setVisible(showSpaceInfo);
334 m_zoomSlider->setVisible(showZoomSlider);
335 m_zoomLabel->setVisible(showZoomSlider);
336 }
337