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