]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/dolphinstatusbar.cpp
14672dff079869f0704ed3615c31b6b0b8d9e996
[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
24 #include <QIcon>
25 #include <KLocalizedString>
26 #include <KSqueezedTextLabel>
27 #include <QMenu>
28
29 #include "statusbarspaceinfo.h"
30
31 #include <QApplication>
32 #include <QHBoxLayout>
33 #include <QProgressBar>
34 #include <QToolButton>
35 #include <QTimer>
36 #include <QSlider>
37 #include <QTextDocument>
38 #include <QHelpEvent>
39
40 #include <views/dolphinview.h>
41 #include <views/zoomlevelinfo.h>
42
43 namespace {
44 const int ResetToDefaultTimeout = 1000;
45 }
46
47 DolphinStatusBar::DolphinStatusBar(QWidget* parent) :
48 QWidget(parent),
49 m_text(),
50 m_defaultText(),
51 m_label(nullptr),
52 m_spaceInfo(nullptr),
53 m_zoomSlider(nullptr),
54 m_progressBar(nullptr),
55 m_stopButton(nullptr),
56 m_progress(100),
57 m_showProgressBarTimer(nullptr),
58 m_resetToDefaultTextTimer(nullptr),
59 m_textTimestamp()
60 {
61 // Initialize text label
62 m_label = new KSqueezedTextLabel(m_text, this);
63 m_label->setWordWrap(true);
64 m_label->setTextFormat(Qt::PlainText);
65
66 // Initialize zoom widget
67 m_zoomSlider = new QSlider(Qt::Horizontal, this);
68 m_zoomSlider->setAccessibleName(i18n("Zoom"));
69 m_zoomSlider->setAccessibleDescription(i18nc("Description for zoom-slider (accessibility)", "Sets the size of the file icons."));
70 m_zoomSlider->setPageStep(1);
71 m_zoomSlider->setRange(ZoomLevelInfo::minimumLevel(), ZoomLevelInfo::maximumLevel());
72
73 connect(m_zoomSlider, &QSlider::valueChanged, this, &DolphinStatusBar::zoomLevelChanged);
74 connect(m_zoomSlider, &QSlider::valueChanged, this, &DolphinStatusBar::updateZoomSliderToolTip);
75 connect(m_zoomSlider, &QSlider::sliderMoved, this, &DolphinStatusBar::showZoomSliderToolTip);
76
77 // Initialize space information
78 m_spaceInfo = new StatusBarSpaceInfo(this);
79
80 // Initialize progress information
81 m_stopButton = new QToolButton(this);
82 m_stopButton->setIcon(QIcon::fromTheme(QStringLiteral("process-stop")));
83 m_stopButton->setAccessibleName(i18n("Stop"));
84 m_stopButton->setAutoRaise(true);
85 m_stopButton->setToolTip(i18nc("@tooltip", "Stop loading"));
86 m_stopButton->hide();
87 connect(m_stopButton, &QToolButton::clicked, this, &DolphinStatusBar::stopPressed);
88
89 m_progressTextLabel = new QLabel(this);
90 m_progressTextLabel->hide();
91
92 m_progressBar = new QProgressBar(this);
93 m_progressBar->hide();
94
95 m_showProgressBarTimer = new QTimer(this);
96 m_showProgressBarTimer->setInterval(500);
97 m_showProgressBarTimer->setSingleShot(true);
98 connect(m_showProgressBarTimer, &QTimer::timeout, this, &DolphinStatusBar::updateProgressInfo);
99
100 m_resetToDefaultTextTimer = new QTimer(this);
101 m_resetToDefaultTextTimer->setInterval(ResetToDefaultTimeout);
102 m_resetToDefaultTextTimer->setSingleShot(true);
103 connect(m_resetToDefaultTextTimer, &QTimer::timeout, this, &DolphinStatusBar::slotResetToDefaultText);
104
105 // Initialize top layout and size policies
106 const int fontHeight = QFontMetrics(m_label->font()).height();
107 const int zoomSliderHeight = m_zoomSlider->minimumSizeHint().height();
108 const int buttonHeight = m_stopButton->height();
109 const int contentHeight = qMax(qMax(fontHeight, zoomSliderHeight), buttonHeight);
110
111 QFontMetrics fontMetrics(m_label->font());
112
113 m_label->setFixedHeight(contentHeight);
114 m_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
115
116 m_zoomSlider->setMaximumWidth(fontMetrics.averageCharWidth() * 25);
117
118 m_spaceInfo->setFixedHeight(zoomSliderHeight);
119 m_spaceInfo->setMaximumWidth(fontMetrics.averageCharWidth() * 25);
120 m_spaceInfo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
121
122 m_progressBar->setFixedHeight(zoomSliderHeight);
123 m_progressBar->setMaximumWidth(fontMetrics.averageCharWidth() * 25);
124
125 QHBoxLayout* topLayout = new QHBoxLayout(this);
126 topLayout->setContentsMargins(2, 0, 2, 0);
127 topLayout->setSpacing(4);
128 topLayout->addWidget(m_label, 1);
129 topLayout->addWidget(m_zoomSlider, 1);
130 topLayout->addWidget(m_spaceInfo, 1);
131 topLayout->addWidget(m_stopButton);
132 topLayout->addWidget(m_progressTextLabel);
133 topLayout->addWidget(m_progressBar);
134
135 setExtensionsVisible(true);
136 }
137
138 DolphinStatusBar::~DolphinStatusBar()
139 {
140 }
141
142 void DolphinStatusBar::setText(const QString& text)
143 {
144 if (m_text == text) {
145 return;
146 }
147
148 m_textTimestamp = QTime::currentTime();
149
150 if (text.isEmpty()) {
151 // Assure that the previous set text won't get
152 // cleared immediatelly.
153 m_resetToDefaultTextTimer->start();
154 } else {
155 m_text = text;
156
157 if (m_resetToDefaultTextTimer->isActive()) {
158 m_resetToDefaultTextTimer->start();
159 }
160
161 updateLabelText();
162 }
163 }
164
165 QString DolphinStatusBar::text() const
166 {
167 return m_text;
168 }
169
170 void DolphinStatusBar::setProgressText(const QString& text)
171 {
172 m_progressTextLabel->setText(text);
173 }
174
175 QString DolphinStatusBar::progressText() const
176 {
177 return m_progressTextLabel->text();
178 }
179
180 void DolphinStatusBar::setProgress(int percent)
181 {
182 // Show a busy indicator if a value < 0 is provided:
183 m_progressBar->setMaximum((percent < 0) ? 0 : 100);
184
185 percent = qBound(0, percent, 100);
186 const bool progressRestarted = (percent < 100) && (percent < m_progress);
187 m_progress = percent;
188 if (progressRestarted && !m_progressBar->isVisible()) {
189 // Show the progress bar delayed: In the case if 100 % are reached within
190 // a short time, no progress bar will be shown at all.
191 m_showProgressBarTimer->start();
192 }
193
194 m_progressBar->setValue(m_progress);
195 if (percent == 100) {
196 // The end of the progress has been reached. Assure that the progress bar
197 // gets hidden and the extensions widgets get visible again.
198 m_showProgressBarTimer->stop();
199 updateProgressInfo();
200 }
201 }
202
203 int DolphinStatusBar::progress() const
204 {
205 return m_progress;
206 }
207
208 void DolphinStatusBar::resetToDefaultText()
209 {
210 QTime currentTime;
211 if (currentTime.msecsTo(m_textTimestamp) < ResetToDefaultTimeout) {
212 m_resetToDefaultTextTimer->start();
213 } else {
214 m_resetToDefaultTextTimer->stop();
215 slotResetToDefaultText();
216 }
217 }
218
219 void DolphinStatusBar::setDefaultText(const QString& text)
220 {
221 m_defaultText = text;
222 updateLabelText();
223 }
224
225 QString DolphinStatusBar::defaultText() const
226 {
227 return m_defaultText;
228 }
229
230 void DolphinStatusBar::setUrl(const QUrl& url)
231 {
232 m_spaceInfo->setUrl(url);
233 }
234
235 QUrl DolphinStatusBar::url() const
236 {
237 return m_spaceInfo->url();
238 }
239
240 void DolphinStatusBar::setZoomLevel(int zoomLevel)
241 {
242 if (zoomLevel != m_zoomSlider->value()) {
243 m_zoomSlider->setValue(zoomLevel);
244 }
245 }
246
247 int DolphinStatusBar::zoomLevel() const
248 {
249 return m_zoomSlider->value();
250 }
251
252 void DolphinStatusBar::readSettings()
253 {
254 setExtensionsVisible(true);
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 } else if (action == showSpaceInfoAction) {
277 const bool visible = showSpaceInfoAction->isChecked();
278 GeneralSettings::setShowSpaceInfo(visible);
279 m_spaceInfo->setVisible(visible);
280 }
281 }
282
283 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel)
284 {
285 updateZoomSliderToolTip(zoomLevel);
286
287 QPoint global = m_zoomSlider->rect().topLeft();
288 global.ry() += m_zoomSlider->height() / 2;
289 QHelpEvent toolTipEvent(QEvent::ToolTip, QPoint(0, 0), m_zoomSlider->mapToGlobal(global));
290 QApplication::sendEvent(m_zoomSlider, &toolTipEvent);
291 }
292
293 void DolphinStatusBar::updateProgressInfo()
294 {
295 if (m_progress < 100) {
296 // Show the progress information and hide the extensions
297 m_stopButton->show();
298 m_progressTextLabel->show();
299 m_progressBar->show();
300 setExtensionsVisible(false);
301 } else {
302 // Hide the progress information and show the extensions
303 m_stopButton->hide();
304 m_progressTextLabel->hide();
305 m_progressBar->hide();
306 setExtensionsVisible(true);
307 }
308 }
309
310 void DolphinStatusBar::updateLabelText()
311 {
312 const QString text = m_text.isEmpty() ? m_defaultText : m_text;
313 m_label->setText(text);
314 }
315
316 void DolphinStatusBar::slotResetToDefaultText()
317 {
318 m_text.clear();
319 updateLabelText();
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 m_spaceInfo->setVisible(showSpaceInfo);
337 m_zoomSlider->setVisible(showZoomSlider);
338 }
339