]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/dolphinstatusbar.cpp
Merge remote-tracking branch 'origin/KDE/4.9'
[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 <QClipboard>
34 #include <QHBoxLayout>
35 #include <QLabel>
36 #include <QProgressBar>
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->installEventFilter(this);
66
67 // Initialize zoom widget
68 m_zoomSlider = new QSlider(Qt::Horizontal, this);
69 m_zoomSlider->setAccessibleName(i18n("Zoom"));
70 m_zoomSlider->setAccessibleDescription(i18nc("Description for zoom-slider (accessibility)", "Sets the size of the file icons."));
71 m_zoomSlider->setPageStep(1);
72 m_zoomSlider->setRange(ZoomLevelInfo::minimumLevel(), ZoomLevelInfo::maximumLevel());
73
74 connect(m_zoomSlider, SIGNAL(valueChanged(int)), this, SIGNAL(zoomLevelChanged(int)));
75 connect(m_zoomSlider, SIGNAL(sliderMoved(int)), this, SLOT(showZoomSliderToolTip(int)));
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(KIcon("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, SIGNAL(clicked()), this, SIGNAL(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, SIGNAL(timeout()), this, SLOT(updateProgressInfo()));
99
100 m_resetToDefaultTextTimer = new QTimer(this);
101 m_resetToDefaultTextTimer->setInterval(ResetToDefaultTimeout);
102 m_resetToDefaultTextTimer->setSingleShot(true);
103 connect(m_resetToDefaultTextTimer, SIGNAL(timeout()), this, SLOT(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 contentHeight = qMax(fontHeight, zoomSliderHeight);
109
110 m_label->setMinimumHeight(contentHeight);
111 m_label->setMaximumHeight(contentHeight);
112 m_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
113
114 const QSize size(150, contentHeight);
115 applyFixedWidgetSize(m_spaceInfo, size);
116 applyFixedWidgetSize(m_progressBar, size);
117 applyFixedWidgetSize(m_zoomSlider, size);
118
119 QHBoxLayout* topLayout = new QHBoxLayout(this);
120 topLayout->setMargin(0);
121 topLayout->setSpacing(4);
122 topLayout->addWidget(m_label);
123 topLayout->addWidget(m_zoomSlider);
124 topLayout->addWidget(m_spaceInfo);
125 topLayout->addWidget(m_stopButton);
126 topLayout->addWidget(m_progressTextLabel);
127 topLayout->addWidget(m_progressBar);
128
129 setExtensionsVisible(true);
130 }
131
132 DolphinStatusBar::~DolphinStatusBar()
133 {
134 }
135
136 void DolphinStatusBar::setText(const QString& text)
137 {
138 if (m_text == text) {
139 return;
140 }
141
142 m_textTimestamp = QTime::currentTime();
143
144 if (text.isEmpty()) {
145 // Assure that the previous set text won't get
146 // cleared immediatelly.
147 m_resetToDefaultTextTimer->start();
148 } else {
149 m_text = text;
150
151 if (m_resetToDefaultTextTimer->isActive()) {
152 m_resetToDefaultTextTimer->start();
153 }
154
155 updateLabelText();
156 }
157 }
158
159 QString DolphinStatusBar::text() const
160 {
161 return m_text;
162 }
163
164 void DolphinStatusBar::setProgressText(const QString& text)
165 {
166 m_progressTextLabel->setText(text);
167 }
168
169 QString DolphinStatusBar::progressText() const
170 {
171 return m_progressTextLabel->text();
172 }
173
174 void DolphinStatusBar::setProgress(int percent)
175 {
176 // Show a busy indicator if a value < 0 is provided:
177 m_progressBar->setMaximum((percent < 0) ? 0 : 100);
178
179 percent = qBound(0, percent, 100);
180 const bool progressRestarted = (percent < 100) && (percent < m_progress);
181 m_progress = percent;
182 if (progressRestarted && !m_progressBar->isVisible()) {
183 // Show the progress bar delayed: In the case if 100 % are reached within
184 // a short time, no progress bar will be shown at all.
185 m_showProgressBarTimer->start();
186 }
187
188 m_progressBar->setValue(m_progress);
189 if (percent == 100) {
190 // The end of the progress has been reached. Assure that the progress bar
191 // gets hidden and the extensions widgets get visible again.
192 m_showProgressBarTimer->stop();
193 updateProgressInfo();
194 }
195 }
196
197 int DolphinStatusBar::progress() const
198 {
199 return m_progress;
200 }
201
202 void DolphinStatusBar::resetToDefaultText()
203 {
204 QTime currentTime;
205 if (currentTime.msecsTo(m_textTimestamp) < ResetToDefaultTimeout) {
206 m_resetToDefaultTextTimer->start();
207 } else {
208 m_resetToDefaultTextTimer->stop();
209 slotResetToDefaultText();
210 }
211 }
212
213 void DolphinStatusBar::setDefaultText(const QString& text)
214 {
215 m_defaultText = text;
216 updateLabelText();
217 }
218
219 QString DolphinStatusBar::defaultText() const
220 {
221 return m_defaultText;
222 }
223
224 void DolphinStatusBar::setUrl(const KUrl& url)
225 {
226 m_spaceInfo->setUrl(url);
227 }
228
229 KUrl DolphinStatusBar::url() const
230 {
231 return m_spaceInfo->url();
232 }
233
234 void DolphinStatusBar::setZoomLevel(int zoomLevel)
235 {
236 if (zoomLevel != m_zoomSlider->value()) {
237 m_zoomSlider->setValue(zoomLevel);
238 updateZoomSliderToolTip(zoomLevel);
239 }
240 }
241
242 int DolphinStatusBar::zoomLevel() const
243 {
244 return m_zoomSlider->value();
245 }
246
247 void DolphinStatusBar::readSettings()
248 {
249 setExtensionsVisible(true);
250 }
251
252 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent* event)
253 {
254 Q_UNUSED(event);
255
256 KMenu menu(this);
257
258 QAction* copyAction = menu.addAction(i18nc("@action:inmenu", "Copy Text"));
259 QAction* showZoomSliderAction = menu.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
260 showZoomSliderAction->setCheckable(true);
261 showZoomSliderAction->setChecked(GeneralSettings::showZoomSlider());
262
263 QAction* showSpaceInfoAction = menu.addAction(i18nc("@action:inmenu", "Show Space Information"));
264 showSpaceInfoAction->setCheckable(true);
265 showSpaceInfoAction->setChecked(GeneralSettings::showSpaceInfo());
266
267 const QAction* action = menu.exec(QCursor::pos());
268 if (action == copyAction) {
269 QMimeData* mimeData = new QMimeData();
270 mimeData->setText(text());
271 QApplication::clipboard()->setMimeData(mimeData);
272 } else 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 bool DolphinStatusBar::eventFilter(QObject* obj, QEvent* event)
284 {
285 if (obj == m_label && event->type() == QEvent::Resize) {
286 updateLabelText();
287 }
288 return QWidget::eventFilter(obj, event);
289 }
290
291 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel)
292 {
293 updateZoomSliderToolTip(zoomLevel);
294
295 QPoint global = m_zoomSlider->rect().topLeft();
296 global.ry() += m_zoomSlider->height() / 2;
297 QHelpEvent toolTipEvent(QEvent::ToolTip, QPoint(0, 0), m_zoomSlider->mapToGlobal(global));
298 QApplication::sendEvent(m_zoomSlider, &toolTipEvent);
299 }
300
301 void DolphinStatusBar::updateProgressInfo()
302 {
303 if (m_progress < 100) {
304 // Show the progress information and hide the extensions
305 m_stopButton->show();
306 m_progressTextLabel->show();
307 m_progressBar->show();
308 setExtensionsVisible(false);
309 } else {
310 // Hide the progress information and show the extensions
311 m_stopButton->hide();
312 m_progressTextLabel->hide();
313 m_progressBar->hide();
314 setExtensionsVisible(true);
315 }
316 }
317
318 void DolphinStatusBar::updateLabelText()
319 {
320 const QString text = m_text.isEmpty() ? m_defaultText : m_text;
321
322 QFontMetrics fontMetrics(m_label->font());
323 const QString elidedText = fontMetrics.elidedText(text, Qt::ElideRight, m_label->width());
324 m_label->setText(elidedText);
325 m_label->setToolTip(text == elidedText ? QString() : text);
326 }
327
328 void DolphinStatusBar::slotResetToDefaultText()
329 {
330 m_text.clear();
331 updateLabelText();
332 }
333
334 void DolphinStatusBar::setExtensionsVisible(bool visible)
335 {
336 bool showSpaceInfo = visible;
337 bool showZoomSlider = visible;
338 if (visible) {
339 showSpaceInfo = GeneralSettings::showSpaceInfo();
340 showZoomSlider = GeneralSettings::showZoomSlider();
341 }
342 m_spaceInfo->setVisible(showSpaceInfo);
343 m_zoomSlider->setVisible(showZoomSlider);
344 }
345
346 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel)
347 {
348 const int size = ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel);
349 m_zoomSlider->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size));
350 }
351
352 void DolphinStatusBar::applyFixedWidgetSize(QWidget* widget, const QSize& size)
353 {
354 widget->setMinimumSize(size);
355 widget->setMaximumSize(size);
356 widget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
357 }
358
359 #include "dolphinstatusbar.moc"