]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/dolphinstatusbar.cpp
Merge branch 'master' into frameworks
[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 <KIconLoader>
25 #include <KIcon>
26 #include <KLocale>
27 #include <KMenu>
28 #include <KVBox>
29
30 #include "statusbarspaceinfo.h"
31
32 #include <QApplication>
33 #include <QHBoxLayout>
34 #include <QLabel>
35 #include <QProgressBar>
36 #include <QTextDocument>
37 #include <QToolButton>
38 #include <QTime>
39 #include <QTimer>
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(KIcon("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 contentHeight = qMax(fontHeight, zoomSliderHeight);
111
112 QFontMetrics fontMetrics(m_label->font());
113
114 m_label->setFixedHeight(contentHeight);
115 m_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
116
117 m_zoomSlider->setMaximumWidth(fontMetrics.averageCharWidth() * 15);
118
119 m_spaceInfo->setFixedHeight(contentHeight);
120 m_spaceInfo->setMaximumWidth(fontMetrics.averageCharWidth() * 15);
121 m_spaceInfo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
122
123 m_progressBar->setFixedHeight(contentHeight);
124 m_progressBar->setMaximumWidth(fontMetrics.averageCharWidth() * 15);
125
126 QHBoxLayout* topLayout = new QHBoxLayout(this);
127 topLayout->setMargin(0);
128 topLayout->setSpacing(4);
129 topLayout->addWidget(m_label);
130 topLayout->addWidget(m_zoomSlider);
131 topLayout->addWidget(m_spaceInfo);
132 topLayout->addWidget(m_stopButton);
133 topLayout->addWidget(m_progressTextLabel);
134 topLayout->addWidget(m_progressBar);
135
136 setExtensionsVisible(true);
137 }
138
139 DolphinStatusBar::~DolphinStatusBar()
140 {
141 }
142
143 void DolphinStatusBar::setText(const QString& text)
144 {
145 if (m_text == text) {
146 return;
147 }
148
149 m_textTimestamp = QTime::currentTime();
150
151 if (text.isEmpty()) {
152 // Assure that the previous set text won't get
153 // cleared immediatelly.
154 m_resetToDefaultTextTimer->start();
155 } else {
156 m_text = text;
157
158 if (m_resetToDefaultTextTimer->isActive()) {
159 m_resetToDefaultTextTimer->start();
160 }
161
162 updateLabelText();
163 }
164 }
165
166 QString DolphinStatusBar::text() const
167 {
168 return m_text;
169 }
170
171 void DolphinStatusBar::setProgressText(const QString& text)
172 {
173 m_progressTextLabel->setText(text);
174 }
175
176 QString DolphinStatusBar::progressText() const
177 {
178 return m_progressTextLabel->text();
179 }
180
181 void DolphinStatusBar::setProgress(int percent)
182 {
183 // Show a busy indicator if a value < 0 is provided:
184 m_progressBar->setMaximum((percent < 0) ? 0 : 100);
185
186 percent = qBound(0, percent, 100);
187 const bool progressRestarted = (percent < 100) && (percent < m_progress);
188 m_progress = percent;
189 if (progressRestarted && !m_progressBar->isVisible()) {
190 // Show the progress bar delayed: In the case if 100 % are reached within
191 // a short time, no progress bar will be shown at all.
192 m_showProgressBarTimer->start();
193 }
194
195 m_progressBar->setValue(m_progress);
196 if (percent == 100) {
197 // The end of the progress has been reached. Assure that the progress bar
198 // gets hidden and the extensions widgets get visible again.
199 m_showProgressBarTimer->stop();
200 updateProgressInfo();
201 }
202 }
203
204 int DolphinStatusBar::progress() const
205 {
206 return m_progress;
207 }
208
209 void DolphinStatusBar::resetToDefaultText()
210 {
211 QTime currentTime;
212 if (currentTime.msecsTo(m_textTimestamp) < ResetToDefaultTimeout) {
213 m_resetToDefaultTextTimer->start();
214 } else {
215 m_resetToDefaultTextTimer->stop();
216 slotResetToDefaultText();
217 }
218 }
219
220 void DolphinStatusBar::setDefaultText(const QString& text)
221 {
222 m_defaultText = text;
223 updateLabelText();
224 }
225
226 QString DolphinStatusBar::defaultText() const
227 {
228 return m_defaultText;
229 }
230
231 void DolphinStatusBar::setUrl(const KUrl& url)
232 {
233 m_spaceInfo->setUrl(url);
234 }
235
236 KUrl DolphinStatusBar::url() const
237 {
238 return m_spaceInfo->url();
239 }
240
241 void DolphinStatusBar::setZoomLevel(int zoomLevel)
242 {
243 if (zoomLevel != m_zoomSlider->value()) {
244 m_zoomSlider->setValue(zoomLevel);
245 }
246 }
247
248 int DolphinStatusBar::zoomLevel() const
249 {
250 return m_zoomSlider->value();
251 }
252
253 void DolphinStatusBar::readSettings()
254 {
255 setExtensionsVisible(true);
256 }
257
258 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent* event)
259 {
260 Q_UNUSED(event);
261
262 KMenu 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(QCursor::pos());
273 if (action == showZoomSliderAction) {
274 const bool visible = showZoomSliderAction->isChecked();
275 GeneralSettings::setShowZoomSlider(visible);
276 m_zoomSlider->setVisible(visible);
277 } else if (action == showSpaceInfoAction) {
278 const bool visible = showSpaceInfoAction->isChecked();
279 GeneralSettings::setShowSpaceInfo(visible);
280 m_spaceInfo->setVisible(visible);
281 }
282 }
283
284 bool DolphinStatusBar::eventFilter(QObject* obj, QEvent* event)
285 {
286 if (obj == m_label && event->type() == QEvent::Resize) {
287 updateLabelText();
288 }
289 return QWidget::eventFilter(obj, event);
290 }
291
292 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel)
293 {
294 updateZoomSliderToolTip(zoomLevel);
295
296 QPoint global = m_zoomSlider->rect().topLeft();
297 global.ry() += m_zoomSlider->height() / 2;
298 QHelpEvent toolTipEvent(QEvent::ToolTip, QPoint(0, 0), m_zoomSlider->mapToGlobal(global));
299 QApplication::sendEvent(m_zoomSlider, &toolTipEvent);
300 }
301
302 void DolphinStatusBar::updateProgressInfo()
303 {
304 if (m_progress < 100) {
305 // Show the progress information and hide the extensions
306 m_stopButton->show();
307 m_progressTextLabel->show();
308 m_progressBar->show();
309 setExtensionsVisible(false);
310 } else {
311 // Hide the progress information and show the extensions
312 m_stopButton->hide();
313 m_progressTextLabel->hide();
314 m_progressBar->hide();
315 setExtensionsVisible(true);
316 }
317 }
318
319 void DolphinStatusBar::updateLabelText()
320 {
321 const QString text = m_text.isEmpty() ? m_defaultText : m_text;
322
323 // Set status bar text and elide it if too long
324 QFontMetrics fontMetrics(m_label->font());
325 const QString elidedText = fontMetrics.elidedText(text, Qt::ElideRight, m_label->width());
326 m_label->setText(elidedText);
327
328 // If the text has been elided, set the original text as tooltip
329 if (text != elidedText) {
330 m_label->setToolTip(Qt::convertFromPlainText(text));
331 } else {
332 m_label->setToolTip(QString());
333 }
334 }
335
336 void DolphinStatusBar::slotResetToDefaultText()
337 {
338 m_text.clear();
339 updateLabelText();
340 }
341
342 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel)
343 {
344 const int size = ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel);
345 m_zoomSlider->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size));
346 }
347
348 void DolphinStatusBar::setExtensionsVisible(bool visible)
349 {
350 bool showSpaceInfo = visible;
351 bool showZoomSlider = visible;
352 if (visible) {
353 showSpaceInfo = GeneralSettings::showSpaceInfo();
354 showZoomSlider = GeneralSettings::showZoomSlider();
355 }
356 m_spaceInfo->setVisible(showSpaceInfo);
357 m_zoomSlider->setVisible(showZoomSlider);
358 }
359