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