]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/dolphinstatusbar.cpp
31d4ab81a58a396dffe5e6876b7958f52b77d46c
[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 <QMenu>
27
28 #include "statusbarspaceinfo.h"
29
30 #include <QApplication>
31 #include <QHBoxLayout>
32 #include <QLabel>
33 #include <QProgressBar>
34 #include <QToolButton>
35 #include <QTime>
36 #include <QTimer>
37 #include <QSlider>
38 #include <QTextDocument>
39 #include <QHelpEvent>
40
41 #include <views/dolphinview.h>
42 #include <views/zoomlevelinfo.h>
43
44 namespace {
45 const int ResetToDefaultTimeout = 1000;
46 }
47
48 DolphinStatusBar::DolphinStatusBar(QWidget* parent) :
49 QWidget(parent),
50 m_text(),
51 m_defaultText(),
52 m_label(0),
53 m_spaceInfo(0),
54 m_zoomSlider(0),
55 m_progressBar(0),
56 m_stopButton(0),
57 m_progress(100),
58 m_showProgressBarTimer(0),
59 m_resetToDefaultTextTimer(0),
60 m_textTimestamp()
61 {
62 // Initialize text label
63 m_label = new QLabel(this);
64 m_label->setWordWrap(true);
65 m_label->setTextFormat(Qt::PlainText);
66 m_label->installEventFilter(this);
67
68 // Initialize zoom widget
69 m_zoomSlider = new QSlider(Qt::Horizontal, this);
70 m_zoomSlider->setAccessibleName(i18n("Zoom"));
71 m_zoomSlider->setAccessibleDescription(i18nc("Description for zoom-slider (accessibility)", "Sets the size of the file icons."));
72 m_zoomSlider->setPageStep(1);
73 m_zoomSlider->setRange(ZoomLevelInfo::minimumLevel(), ZoomLevelInfo::maximumLevel());
74
75 connect(m_zoomSlider, &QSlider::valueChanged, this, &DolphinStatusBar::zoomLevelChanged);
76 connect(m_zoomSlider, &QSlider::valueChanged, this, &DolphinStatusBar::updateZoomSliderToolTip);
77 connect(m_zoomSlider, &QSlider::sliderMoved, this, &DolphinStatusBar::showZoomSliderToolTip);
78
79 // Initialize space information
80 m_spaceInfo = new StatusBarSpaceInfo(this);
81
82 // Initialize progress information
83 m_stopButton = new QToolButton(this);
84 m_stopButton->setIcon(QIcon::fromTheme(QStringLiteral("process-stop")));
85 m_stopButton->setAccessibleName(i18n("Stop"));
86 m_stopButton->setAutoRaise(true);
87 m_stopButton->setToolTip(i18nc("@tooltip", "Stop loading"));
88 m_stopButton->hide();
89 connect(m_stopButton, &QToolButton::clicked, this, &DolphinStatusBar::stopPressed);
90
91 m_progressTextLabel = new QLabel(this);
92 m_progressTextLabel->hide();
93
94 m_progressBar = new QProgressBar(this);
95 m_progressBar->hide();
96
97 m_showProgressBarTimer = new QTimer(this);
98 m_showProgressBarTimer->setInterval(500);
99 m_showProgressBarTimer->setSingleShot(true);
100 connect(m_showProgressBarTimer, &QTimer::timeout, this, &DolphinStatusBar::updateProgressInfo);
101
102 m_resetToDefaultTextTimer = new QTimer(this);
103 m_resetToDefaultTextTimer->setInterval(ResetToDefaultTimeout);
104 m_resetToDefaultTextTimer->setSingleShot(true);
105 connect(m_resetToDefaultTextTimer, &QTimer::timeout, this, &DolphinStatusBar::slotResetToDefaultText);
106
107 // Initialize top layout and size policies
108 const int fontHeight = QFontMetrics(m_label->font()).height();
109 const int zoomSliderHeight = m_zoomSlider->minimumSizeHint().height();
110 const int buttonHeight = m_stopButton->height();
111 const int contentHeight = qMax(qMax(fontHeight, zoomSliderHeight), buttonHeight);
112
113 QFontMetrics fontMetrics(m_label->font());
114
115 m_label->setFixedHeight(contentHeight);
116 m_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
117
118 m_zoomSlider->setMaximumWidth(fontMetrics.averageCharWidth() * 25);
119
120 m_spaceInfo->setFixedHeight(zoomSliderHeight);
121 m_spaceInfo->setMaximumWidth(fontMetrics.averageCharWidth() * 25);
122 m_spaceInfo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
123
124 m_progressBar->setFixedHeight(zoomSliderHeight);
125 m_progressBar->setMaximumWidth(fontMetrics.averageCharWidth() * 25);
126
127 QHBoxLayout* topLayout = new QHBoxLayout(this);
128 topLayout->setContentsMargins(2, 0, 2, 0);
129 topLayout->setSpacing(4);
130 topLayout->addWidget(m_label);
131 topLayout->addWidget(m_zoomSlider);
132 topLayout->addWidget(m_spaceInfo);
133 topLayout->addWidget(m_stopButton);
134 topLayout->addWidget(m_progressTextLabel);
135 topLayout->addWidget(m_progressBar);
136
137 setExtensionsVisible(true);
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 if (text.isEmpty()) {
153 // Assure that the previous set text won't get
154 // cleared immediatelly.
155 m_resetToDefaultTextTimer->start();
156 } else {
157 m_text = text;
158
159 if (m_resetToDefaultTextTimer->isActive()) {
160 m_resetToDefaultTextTimer->start();
161 }
162
163 updateLabelText();
164 }
165 }
166
167 QString DolphinStatusBar::text() const
168 {
169 return m_text;
170 }
171
172 void DolphinStatusBar::setProgressText(const QString& text)
173 {
174 m_progressTextLabel->setText(text);
175 }
176
177 QString DolphinStatusBar::progressText() const
178 {
179 return m_progressTextLabel->text();
180 }
181
182 void DolphinStatusBar::setProgress(int percent)
183 {
184 // Show a busy indicator if a value < 0 is provided:
185 m_progressBar->setMaximum((percent < 0) ? 0 : 100);
186
187 percent = qBound(0, percent, 100);
188 const bool progressRestarted = (percent < 100) && (percent < m_progress);
189 m_progress = percent;
190 if (progressRestarted && !m_progressBar->isVisible()) {
191 // Show the progress bar delayed: In the case if 100 % are reached within
192 // a short time, no progress bar will be shown at all.
193 m_showProgressBarTimer->start();
194 }
195
196 m_progressBar->setValue(m_progress);
197 if (percent == 100) {
198 // The end of the progress has been reached. Assure that the progress bar
199 // gets hidden and the extensions widgets get visible again.
200 m_showProgressBarTimer->stop();
201 updateProgressInfo();
202 }
203 }
204
205 int DolphinStatusBar::progress() const
206 {
207 return m_progress;
208 }
209
210 void DolphinStatusBar::resetToDefaultText()
211 {
212 QTime currentTime;
213 if (currentTime.msecsTo(m_textTimestamp) < ResetToDefaultTimeout) {
214 m_resetToDefaultTextTimer->start();
215 } else {
216 m_resetToDefaultTextTimer->stop();
217 slotResetToDefaultText();
218 }
219 }
220
221 void DolphinStatusBar::setDefaultText(const QString& text)
222 {
223 m_defaultText = text;
224 updateLabelText();
225 }
226
227 QString DolphinStatusBar::defaultText() const
228 {
229 return m_defaultText;
230 }
231
232 void DolphinStatusBar::setUrl(const QUrl& url)
233 {
234 m_spaceInfo->setUrl(url);
235 }
236
237 QUrl DolphinStatusBar::url() const
238 {
239 return m_spaceInfo->url();
240 }
241
242 void DolphinStatusBar::setZoomLevel(int zoomLevel)
243 {
244 if (zoomLevel != m_zoomSlider->value()) {
245 m_zoomSlider->setValue(zoomLevel);
246 }
247 }
248
249 int DolphinStatusBar::zoomLevel() const
250 {
251 return m_zoomSlider->value();
252 }
253
254 void DolphinStatusBar::readSettings()
255 {
256 setExtensionsVisible(true);
257 }
258
259 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent* event)
260 {
261 Q_UNUSED(event);
262
263 QMenu menu(this);
264
265 QAction* showZoomSliderAction = menu.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
266 showZoomSliderAction->setCheckable(true);
267 showZoomSliderAction->setChecked(GeneralSettings::showZoomSlider());
268
269 QAction* showSpaceInfoAction = menu.addAction(i18nc("@action:inmenu", "Show Space Information"));
270 showSpaceInfoAction->setCheckable(true);
271 showSpaceInfoAction->setChecked(GeneralSettings::showSpaceInfo());
272
273 const QAction* action = menu.exec(QCursor::pos());
274 if (action == showZoomSliderAction) {
275 const bool visible = showZoomSliderAction->isChecked();
276 GeneralSettings::setShowZoomSlider(visible);
277 m_zoomSlider->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 bool DolphinStatusBar::eventFilter(QObject* obj, QEvent* event)
286 {
287 if (obj == m_label && event->type() == QEvent::Resize) {
288 updateLabelText();
289 }
290 return QWidget::eventFilter(obj, event);
291 }
292
293 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel)
294 {
295 updateZoomSliderToolTip(zoomLevel);
296
297 QPoint global = m_zoomSlider->rect().topLeft();
298 global.ry() += m_zoomSlider->height() / 2;
299 QHelpEvent toolTipEvent(QEvent::ToolTip, QPoint(0, 0), m_zoomSlider->mapToGlobal(global));
300 QApplication::sendEvent(m_zoomSlider, &toolTipEvent);
301 }
302
303 void DolphinStatusBar::updateProgressInfo()
304 {
305 if (m_progress < 100) {
306 // Show the progress information and hide the extensions
307 m_stopButton->show();
308 m_progressTextLabel->show();
309 m_progressBar->show();
310 setExtensionsVisible(false);
311 } else {
312 // Hide the progress information and show the extensions
313 m_stopButton->hide();
314 m_progressTextLabel->hide();
315 m_progressBar->hide();
316 setExtensionsVisible(true);
317 }
318 }
319
320 void DolphinStatusBar::updateLabelText()
321 {
322 const QString text = m_text.isEmpty() ? m_defaultText : m_text;
323
324 // Set status bar text and elide it if too long
325 QFontMetrics fontMetrics(m_label->font());
326 const QString elidedText = fontMetrics.elidedText(text, Qt::ElideRight, m_label->width());
327 m_label->setText(elidedText);
328
329 // If the text has been elided, set the original text as tooltip
330 if (text != elidedText) {
331 m_label->setToolTip(Qt::convertFromPlainText(text));
332 } else {
333 m_label->setToolTip(QString());
334 }
335 }
336
337 void DolphinStatusBar::slotResetToDefaultText()
338 {
339 m_text.clear();
340 updateLabelText();
341 }
342
343 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel)
344 {
345 const int size = ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel);
346 m_zoomSlider->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size));
347 }
348
349 void DolphinStatusBar::setExtensionsVisible(bool visible)
350 {
351 bool showSpaceInfo = visible;
352 bool showZoomSlider = visible;
353 if (visible) {
354 showSpaceInfo = GeneralSettings::showSpaceInfo();
355 showZoomSlider = GeneralSettings::showZoomSlider();
356 }
357 m_spaceInfo->setVisible(showSpaceInfo);
358 m_zoomSlider->setVisible(showZoomSlider);
359 }
360