]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/dolphinstatusbar.cpp
Change the icon text for Previous and Next toolbar buttons
[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, SIGNAL(valueChanged(int)), this, SIGNAL(zoomLevelChanged(int)));
76 connect(m_zoomSlider, SIGNAL(valueChanged(int)), this, SLOT(updateZoomSliderToolTip(int)));
77 connect(m_zoomSlider, SIGNAL(sliderMoved(int)), this, SLOT(showZoomSliderToolTip(int)));
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, SIGNAL(clicked()), this, SIGNAL(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, SIGNAL(timeout()), this, SLOT(updateProgressInfo()));
101
102 m_resetToDefaultTextTimer = new QTimer(this);
103 m_resetToDefaultTextTimer->setInterval(ResetToDefaultTimeout);
104 m_resetToDefaultTextTimer->setSingleShot(true);
105 connect(m_resetToDefaultTextTimer, SIGNAL(timeout()), this, SLOT(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 m_label->setFixedHeight(contentHeight);
113 m_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
114
115 m_zoomSlider->setFixedHeight(contentHeight);
116 m_zoomSlider->setMaximumWidth(150);
117
118 m_spaceInfo->setFixedHeight(contentHeight);
119 m_spaceInfo->setMaximumWidth(150);
120 m_spaceInfo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
121
122 m_progressBar->setFixedHeight(contentHeight);
123 m_progressBar->setMaximumWidth(150);
124
125 QHBoxLayout* topLayout = new QHBoxLayout(this);
126 topLayout->setMargin(0);
127 topLayout->setSpacing(4);
128 topLayout->addWidget(m_label);
129 topLayout->addWidget(m_zoomSlider);
130 topLayout->addWidget(m_spaceInfo);
131 topLayout->addWidget(m_stopButton);
132 topLayout->addWidget(m_progressTextLabel);
133 topLayout->addWidget(m_progressBar);
134
135 setExtensionsVisible(true);
136 }
137
138 DolphinStatusBar::~DolphinStatusBar()
139 {
140 }
141
142 void DolphinStatusBar::setText(const QString& text)
143 {
144 if (m_text == text) {
145 return;
146 }
147
148 m_textTimestamp = QTime::currentTime();
149
150 if (text.isEmpty()) {
151 // Assure that the previous set text won't get
152 // cleared immediatelly.
153 m_resetToDefaultTextTimer->start();
154 } else {
155 m_text = text;
156
157 if (m_resetToDefaultTextTimer->isActive()) {
158 m_resetToDefaultTextTimer->start();
159 }
160
161 updateLabelText();
162 }
163 }
164
165 QString DolphinStatusBar::text() const
166 {
167 return m_text;
168 }
169
170 void DolphinStatusBar::setProgressText(const QString& text)
171 {
172 m_progressTextLabel->setText(text);
173 }
174
175 QString DolphinStatusBar::progressText() const
176 {
177 return m_progressTextLabel->text();
178 }
179
180 void DolphinStatusBar::setProgress(int percent)
181 {
182 // Show a busy indicator if a value < 0 is provided:
183 m_progressBar->setMaximum((percent < 0) ? 0 : 100);
184
185 percent = qBound(0, percent, 100);
186 const bool progressRestarted = (percent < 100) && (percent < m_progress);
187 m_progress = percent;
188 if (progressRestarted && !m_progressBar->isVisible()) {
189 // Show the progress bar delayed: In the case if 100 % are reached within
190 // a short time, no progress bar will be shown at all.
191 m_showProgressBarTimer->start();
192 }
193
194 m_progressBar->setValue(m_progress);
195 if (percent == 100) {
196 // The end of the progress has been reached. Assure that the progress bar
197 // gets hidden and the extensions widgets get visible again.
198 m_showProgressBarTimer->stop();
199 updateProgressInfo();
200 }
201 }
202
203 int DolphinStatusBar::progress() const
204 {
205 return m_progress;
206 }
207
208 void DolphinStatusBar::resetToDefaultText()
209 {
210 QTime currentTime;
211 if (currentTime.msecsTo(m_textTimestamp) < ResetToDefaultTimeout) {
212 m_resetToDefaultTextTimer->start();
213 } else {
214 m_resetToDefaultTextTimer->stop();
215 slotResetToDefaultText();
216 }
217 }
218
219 void DolphinStatusBar::setDefaultText(const QString& text)
220 {
221 m_defaultText = text;
222 updateLabelText();
223 }
224
225 QString DolphinStatusBar::defaultText() const
226 {
227 return m_defaultText;
228 }
229
230 void DolphinStatusBar::setUrl(const KUrl& url)
231 {
232 m_spaceInfo->setUrl(url);
233 }
234
235 KUrl DolphinStatusBar::url() const
236 {
237 return m_spaceInfo->url();
238 }
239
240 void DolphinStatusBar::setZoomLevel(int zoomLevel)
241 {
242 if (zoomLevel != m_zoomSlider->value()) {
243 m_zoomSlider->setValue(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* 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 == 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 // Set status bar text and elide it if too long
323 QFontMetrics fontMetrics(m_label->font());
324 const QString elidedText = fontMetrics.elidedText(text, Qt::ElideRight, m_label->width());
325 m_label->setText(elidedText);
326
327 // If the text has been elided, set the original text as tooltip
328 if (text != elidedText) {
329 m_label->setToolTip(Qt::convertFromPlainText(text));
330 } else {
331 m_label->setToolTip(QString());
332 }
333 }
334
335 void DolphinStatusBar::slotResetToDefaultText()
336 {
337 m_text.clear();
338 updateLabelText();
339 }
340
341 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel)
342 {
343 const int size = ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel);
344 m_zoomSlider->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size));
345 }
346
347 void DolphinStatusBar::setExtensionsVisible(bool visible)
348 {
349 bool showSpaceInfo = visible;
350 bool showZoomSlider = visible;
351 if (visible) {
352 showSpaceInfo = GeneralSettings::showSpaceInfo();
353 showZoomSlider = GeneralSettings::showZoomSlider();
354 }
355 m_spaceInfo->setVisible(showSpaceInfo);
356 m_zoomSlider->setVisible(showZoomSlider);
357 }
358
359 #include "dolphinstatusbar.moc"