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