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