]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/dolphinstatusbar.cpp
Merge remote-tracking branch 'origin/KDE/4.11'
[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->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, SIGNAL(valueChanged(int)), this, SIGNAL(zoomLevelChanged(int)));
76 connect(m_zoomSlider, SIGNAL(sliderMoved(int)), this, SLOT(showZoomSliderToolTip(int)));
77
78 // Initialize space information
79 m_spaceInfo = new StatusBarSpaceInfo(this);
80
81 // Initialize progress information
82 m_stopButton = new QToolButton(this);
83 m_stopButton->setIcon(KIcon("process-stop"));
84 m_stopButton->setAccessibleName(i18n("Stop"));
85 m_stopButton->setAutoRaise(true);
86 m_stopButton->setToolTip(i18nc("@tooltip", "Stop loading"));
87 m_stopButton->hide();
88 connect(m_stopButton, SIGNAL(clicked()), this, SIGNAL(stopPressed()));
89
90 m_progressTextLabel = new QLabel(this);
91 m_progressTextLabel->hide();
92
93 m_progressBar = new QProgressBar(this);
94 m_progressBar->hide();
95
96 m_showProgressBarTimer = new QTimer(this);
97 m_showProgressBarTimer->setInterval(500);
98 m_showProgressBarTimer->setSingleShot(true);
99 connect(m_showProgressBarTimer, SIGNAL(timeout()), this, SLOT(updateProgressInfo()));
100
101 m_resetToDefaultTextTimer = new QTimer(this);
102 m_resetToDefaultTextTimer->setInterval(ResetToDefaultTimeout);
103 m_resetToDefaultTextTimer->setSingleShot(true);
104 connect(m_resetToDefaultTextTimer, SIGNAL(timeout()), this, SLOT(slotResetToDefaultText()));
105
106 // Initialize top layout and size policies
107 const int fontHeight = QFontMetrics(m_label->font()).height();
108 const int zoomSliderHeight = m_zoomSlider->minimumSizeHint().height();
109 const int contentHeight = qMax(fontHeight, zoomSliderHeight);
110
111 m_label->setFixedHeight(contentHeight);
112 m_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
113
114 m_zoomSlider->setFixedHeight(contentHeight);
115 m_zoomSlider->setMaximumWidth(150);
116
117 m_spaceInfo->setFixedHeight(contentHeight);
118 m_spaceInfo->setMaximumWidth(150);
119 m_spaceInfo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
120
121 m_progressBar->setFixedHeight(contentHeight);
122 m_progressBar->setMaximumWidth(150);
123
124 QHBoxLayout* topLayout = new QHBoxLayout(this);
125 topLayout->setMargin(0);
126 topLayout->setSpacing(4);
127 topLayout->addWidget(m_label);
128 topLayout->addWidget(m_zoomSlider);
129 topLayout->addWidget(m_spaceInfo);
130 topLayout->addWidget(m_stopButton);
131 topLayout->addWidget(m_progressTextLabel);
132 topLayout->addWidget(m_progressBar);
133
134 setExtensionsVisible(true);
135 }
136
137 DolphinStatusBar::~DolphinStatusBar()
138 {
139 }
140
141 void DolphinStatusBar::setText(const QString& text)
142 {
143 if (m_text == text) {
144 return;
145 }
146
147 m_textTimestamp = QTime::currentTime();
148
149 if (text.isEmpty()) {
150 // Assure that the previous set text won't get
151 // cleared immediatelly.
152 m_resetToDefaultTextTimer->start();
153 } else {
154 m_text = text;
155
156 if (m_resetToDefaultTextTimer->isActive()) {
157 m_resetToDefaultTextTimer->start();
158 }
159
160 updateLabelText();
161 }
162 }
163
164 QString DolphinStatusBar::text() const
165 {
166 return m_text;
167 }
168
169 void DolphinStatusBar::setProgressText(const QString& text)
170 {
171 m_progressTextLabel->setText(text);
172 }
173
174 QString DolphinStatusBar::progressText() const
175 {
176 return m_progressTextLabel->text();
177 }
178
179 void DolphinStatusBar::setProgress(int percent)
180 {
181 // Show a busy indicator if a value < 0 is provided:
182 m_progressBar->setMaximum((percent < 0) ? 0 : 100);
183
184 percent = qBound(0, percent, 100);
185 const bool progressRestarted = (percent < 100) && (percent < m_progress);
186 m_progress = percent;
187 if (progressRestarted && !m_progressBar->isVisible()) {
188 // Show the progress bar delayed: In the case if 100 % are reached within
189 // a short time, no progress bar will be shown at all.
190 m_showProgressBarTimer->start();
191 }
192
193 m_progressBar->setValue(m_progress);
194 if (percent == 100) {
195 // The end of the progress has been reached. Assure that the progress bar
196 // gets hidden and the extensions widgets get visible again.
197 m_showProgressBarTimer->stop();
198 updateProgressInfo();
199 }
200 }
201
202 int DolphinStatusBar::progress() const
203 {
204 return m_progress;
205 }
206
207 void DolphinStatusBar::resetToDefaultText()
208 {
209 QTime currentTime;
210 if (currentTime.msecsTo(m_textTimestamp) < ResetToDefaultTimeout) {
211 m_resetToDefaultTextTimer->start();
212 } else {
213 m_resetToDefaultTextTimer->stop();
214 slotResetToDefaultText();
215 }
216 }
217
218 void DolphinStatusBar::setDefaultText(const QString& text)
219 {
220 m_defaultText = text;
221 updateLabelText();
222 }
223
224 QString DolphinStatusBar::defaultText() const
225 {
226 return m_defaultText;
227 }
228
229 void DolphinStatusBar::setUrl(const KUrl& url)
230 {
231 m_spaceInfo->setUrl(url);
232 }
233
234 KUrl DolphinStatusBar::url() const
235 {
236 return m_spaceInfo->url();
237 }
238
239 void DolphinStatusBar::setZoomLevel(int zoomLevel)
240 {
241 if (zoomLevel != m_zoomSlider->value()) {
242 m_zoomSlider->setValue(zoomLevel);
243 updateZoomSliderToolTip(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 KMenu menu(this);
262
263 QAction* copyAction = menu.addAction(i18nc("@action:inmenu", "Copy Text"));
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 == copyAction) {
274 QMimeData* mimeData = new QMimeData();
275 mimeData->setText(text());
276 QApplication::clipboard()->setMimeData(mimeData);
277 } else if (action == showZoomSliderAction) {
278 const bool visible = showZoomSliderAction->isChecked();
279 GeneralSettings::setShowZoomSlider(visible);
280 m_zoomSlider->setVisible(visible);
281 } else if (action == showSpaceInfoAction) {
282 const bool visible = showSpaceInfoAction->isChecked();
283 GeneralSettings::setShowSpaceInfo(visible);
284 m_spaceInfo->setVisible(visible);
285 }
286 }
287
288 bool DolphinStatusBar::eventFilter(QObject* obj, QEvent* event)
289 {
290 if (obj == m_label && event->type() == QEvent::Resize) {
291 updateLabelText();
292 }
293 return QWidget::eventFilter(obj, event);
294 }
295
296 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel)
297 {
298 updateZoomSliderToolTip(zoomLevel);
299
300 QPoint global = m_zoomSlider->rect().topLeft();
301 global.ry() += m_zoomSlider->height() / 2;
302 QHelpEvent toolTipEvent(QEvent::ToolTip, QPoint(0, 0), m_zoomSlider->mapToGlobal(global));
303 QApplication::sendEvent(m_zoomSlider, &toolTipEvent);
304 }
305
306 void DolphinStatusBar::updateProgressInfo()
307 {
308 if (m_progress < 100) {
309 // Show the progress information and hide the extensions
310 m_stopButton->show();
311 m_progressTextLabel->show();
312 m_progressBar->show();
313 setExtensionsVisible(false);
314 } else {
315 // Hide the progress information and show the extensions
316 m_stopButton->hide();
317 m_progressTextLabel->hide();
318 m_progressBar->hide();
319 setExtensionsVisible(true);
320 }
321 }
322
323 void DolphinStatusBar::updateLabelText()
324 {
325 const QString text = m_text.isEmpty() ? m_defaultText : m_text;
326
327 QFontMetrics fontMetrics(m_label->font());
328 const QString elidedText = fontMetrics.elidedText(text, Qt::ElideRight, m_label->width());
329 m_label->setText(elidedText);
330 m_label->setToolTip(text == elidedText ? QString() : text);
331 }
332
333 void DolphinStatusBar::slotResetToDefaultText()
334 {
335 m_text.clear();
336 updateLabelText();
337 }
338
339 void DolphinStatusBar::setExtensionsVisible(bool visible)
340 {
341 bool showSpaceInfo = visible;
342 bool showZoomSlider = visible;
343 if (visible) {
344 showSpaceInfo = GeneralSettings::showSpaceInfo();
345 showZoomSlider = GeneralSettings::showZoomSlider();
346 }
347 m_spaceInfo->setVisible(showSpaceInfo);
348 m_zoomSlider->setVisible(showZoomSlider);
349 }
350
351 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel)
352 {
353 const int size = ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel);
354 m_zoomSlider->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size));
355 }
356
357 #include "dolphinstatusbar.moc"