]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/dolphinstatusbar.cpp
Never show status bar context menu away from status bar
[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 {
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, 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",
129 "<para>This is "
130 "the <emphasis>Statusbar</emphasis>. It contains three elements "
131 "by default (left to right):<list><item>A <emphasis>text field"
132 "</emphasis> that displays the size of selected items. If only "
133 "one item is selected the name and type is shown as well.</item>"
134 "<item>A <emphasis>zoom slider</emphasis> that allows you "
135 "to adjust the size of the icons in the view.</item>"
136 "<item><emphasis>Space information</emphasis> about the "
137 "current storage device.</item></list></para>"));
138 }
139
140 DolphinStatusBar::~DolphinStatusBar()
141 {
142 }
143
144 void DolphinStatusBar::setText(const QString &text)
145 {
146 if (m_text == text) {
147 return;
148 }
149
150 m_textTimestamp = QTime::currentTime();
151
152 m_text = text;
153 // will update status bar text in 50ms
154 m_delayUpdateTimer->start();
155 }
156
157 QString DolphinStatusBar::text() const
158 {
159 return m_text;
160 }
161
162 void DolphinStatusBar::setProgressText(const QString &text)
163 {
164 m_progressTextLabel->setText(text);
165 }
166
167 QString DolphinStatusBar::progressText() const
168 {
169 return m_progressTextLabel->text();
170 }
171
172 void DolphinStatusBar::setProgress(int percent)
173 {
174 // Show a busy indicator if a value < 0 is provided:
175 m_progressBar->setMaximum((percent < 0) ? 0 : 100);
176
177 percent = qBound(0, percent, 100);
178 const bool progressRestarted = (percent < 100) && (percent < m_progress);
179 m_progress = percent;
180 if (progressRestarted && !m_progressBar->isVisible()) {
181 // Show the progress bar delayed: In the case if 100 % are reached within
182 // a short time, no progress bar will be shown at all.
183 m_showProgressBarTimer->start();
184 }
185
186 m_progressBar->setValue(m_progress);
187 if (percent == 100) {
188 // The end of the progress has been reached. Assure that the progress bar
189 // gets hidden and the extensions widgets get visible again.
190 m_showProgressBarTimer->stop();
191 updateProgressInfo();
192 }
193 }
194
195 int DolphinStatusBar::progress() const
196 {
197 return m_progress;
198 }
199
200 void DolphinStatusBar::resetToDefaultText()
201 {
202 m_text.clear();
203
204 QTime currentTime;
205 if (currentTime.msecsTo(m_textTimestamp) < UpdateDelay) {
206 m_delayUpdateTimer->start();
207 } else {
208 updateLabelText();
209 }
210 }
211
212 void DolphinStatusBar::setDefaultText(const QString &text)
213 {
214 m_defaultText = text;
215 updateLabelText();
216 }
217
218 QString DolphinStatusBar::defaultText() const
219 {
220 return m_defaultText;
221 }
222
223 void DolphinStatusBar::setUrl(const QUrl &url)
224 {
225 if (GeneralSettings::showSpaceInfo()) {
226 m_spaceInfo->setUrl(url);
227 }
228 }
229
230 QUrl DolphinStatusBar::url() const
231 {
232 return m_spaceInfo->url();
233 }
234
235 void DolphinStatusBar::setZoomLevel(int zoomLevel)
236 {
237 if (zoomLevel != m_zoomSlider->value()) {
238 m_zoomSlider->setValue(zoomLevel);
239 }
240 }
241
242 int DolphinStatusBar::zoomLevel() const
243 {
244 return m_zoomSlider->value();
245 }
246
247 void DolphinStatusBar::readSettings()
248 {
249 setVisible(GeneralSettings::showStatusBar());
250 setExtensionsVisible(true);
251 }
252
253 void DolphinStatusBar::updateSpaceInfo()
254 {
255 m_spaceInfo->update();
256 }
257
258 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent *event)
259 {
260 Q_UNUSED(event)
261
262 QMenu menu(this);
263
264 QAction *showZoomSliderAction = menu.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
265 showZoomSliderAction->setCheckable(true);
266 showZoomSliderAction->setChecked(GeneralSettings::showZoomSlider());
267
268 QAction *showSpaceInfoAction = menu.addAction(i18nc("@action:inmenu", "Show Space Information"));
269 showSpaceInfoAction->setCheckable(true);
270 showSpaceInfoAction->setChecked(GeneralSettings::showSpaceInfo());
271
272 const QAction *action = menu.exec(event->reason() == QContextMenuEvent::Reason::Mouse ? QCursor::pos() : mapToGlobal(QPoint(width() / 2, height() / 2)));
273 if (action == showZoomSliderAction) {
274 const bool visible = showZoomSliderAction->isChecked();
275 GeneralSettings::setShowZoomSlider(visible);
276 m_zoomSlider->setVisible(visible);
277 m_zoomLabel->setVisible(visible);
278 } else if (action == showSpaceInfoAction) {
279 const bool visible = showSpaceInfoAction->isChecked();
280 GeneralSettings::setShowSpaceInfo(visible);
281 m_spaceInfo->setVisible(visible);
282 }
283 }
284
285 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel)
286 {
287 updateZoomSliderToolTip(zoomLevel);
288
289 QPoint global = m_zoomSlider->rect().topLeft();
290 global.ry() += m_zoomSlider->height() / 2;
291 QHelpEvent toolTipEvent(QEvent::ToolTip, QPoint(0, 0), m_zoomSlider->mapToGlobal(global));
292 QApplication::sendEvent(m_zoomSlider, &toolTipEvent);
293 }
294
295 void DolphinStatusBar::updateProgressInfo()
296 {
297 if (m_progress < 100) {
298 // Show the progress information and hide the extensions
299 m_stopButton->show();
300 m_progressTextLabel->show();
301 m_progressBar->show();
302 setExtensionsVisible(false);
303 } else {
304 // Hide the progress information and show the extensions
305 m_stopButton->hide();
306 m_progressTextLabel->hide();
307 m_progressBar->hide();
308 setExtensionsVisible(true);
309 }
310 }
311
312 void DolphinStatusBar::updateLabelText()
313 {
314 const QString text = m_text.isEmpty() ? m_defaultText : m_text;
315 m_label->setText(text);
316 }
317
318 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel)
319 {
320 const int size = ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel);
321 m_zoomSlider->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size));
322 }
323
324 void DolphinStatusBar::setExtensionsVisible(bool visible)
325 {
326 bool showSpaceInfo = visible;
327 bool showZoomSlider = visible;
328 if (visible) {
329 showSpaceInfo = GeneralSettings::showSpaceInfo();
330 showZoomSlider = GeneralSettings::showZoomSlider();
331 }
332
333 m_spaceInfo->setShown(showSpaceInfo);
334 m_spaceInfo->setVisible(showSpaceInfo);
335 m_zoomSlider->setVisible(showZoomSlider);
336 m_zoomLabel->setVisible(showZoomSlider);
337 }
338
339 #include "moc_dolphinstatusbar.cpp"