]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/dolphinstatusbar.cpp
c733ebe027bf8dd0915a79852de67ed362878adb
[dolphin.git] / src / statusbar / dolphinstatusbar.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz *
3 * peter.penz@gmx.at *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "dolphinstatusbar.h"
22
23 #include "dolphin_generalsettings.h"
24
25 #include <KIconLoader>
26 #include <KIcon>
27 #include <KLocale>
28 #include <KMenu>
29 #include <KVBox>
30
31 #include "statusbarspaceinfo.h"
32
33 #include <QApplication>
34 #include <QClipboard>
35 #include <QHBoxLayout>
36 #include <QLabel>
37 #include <QProgressBar>
38 #include <QToolButton>
39 #include <QTime>
40 #include <QTimer>
41
42 #include <views/dolphinview.h>
43 #include <views/zoomlevelinfo.h>
44
45 DolphinStatusBar::DolphinStatusBar(QWidget* parent, DolphinView* view) :
46 QWidget(parent),
47 m_view(view),
48 m_messageLabel(0),
49 m_spaceInfo(0),
50 m_zoomSlider(0),
51 m_progressBar(0),
52 m_stopButton(0),
53 m_progress(100),
54 m_showProgressBarTimer(0),
55 m_messageTimeStamp()
56 {
57 connect(m_view, SIGNAL(urlChanged(KUrl)),
58 this, SLOT(updateSpaceInfoContent(KUrl)));
59
60 // Initialize message label
61 m_messageLabel = new KonqStatusBarMessageLabel(this);
62
63 // Initialize zoom widget
64 m_zoomSlider = new QSlider(Qt::Horizontal, this);
65 m_zoomSlider->setAccessibleName(i18n("Zoom slider"));
66 m_zoomSlider->setPageStep(1);
67
68 const int min = ZoomLevelInfo::minimumLevel();
69 const int max = ZoomLevelInfo::maximumLevel();
70 m_zoomSlider->setRange(min, max);
71 m_zoomSlider->setValue(view->zoomLevel());
72 updateZoomSliderToolTip(view->zoomLevel());
73
74 connect(m_zoomSlider, SIGNAL(valueChanged(int)), this, SLOT(setZoomLevel(int)));
75 connect(m_zoomSlider, SIGNAL(sliderMoved(int)), this, SLOT(showZoomSliderToolTip(int)));
76 connect(m_view, SIGNAL(zoomLevelChanged(int,int)), this, SLOT(slotZoomLevelChanged(int,int)));
77 connect(m_view, SIGNAL(previewsShownChanged(bool)), this, SLOT(slotPreviewsShownChanged(bool)));
78
79 // Initialize space information
80 m_spaceInfo = new StatusBarSpaceInfo(this);
81 m_spaceInfo->setUrl(m_view->url());
82
83 // Initialize progress information
84 m_stopButton = new QToolButton(this);
85 m_stopButton->setIcon(KIcon("process-stop"));
86 m_stopButton->setAccessibleName(i18n("Stop"));
87 // TODO: Add tooltip for KDE SC 4.7.0, if new strings are allowed again
88 m_stopButton->setAutoRaise(true);
89 m_stopButton->hide();
90 connect(m_stopButton, SIGNAL(clicked()), this, SIGNAL(stopPressed()));
91
92 m_progressText = new QLabel(this);
93 m_progressText->hide();
94
95 m_progressBar = new QProgressBar(this);
96 m_progressBar->hide();
97
98 m_showProgressBarTimer = new QTimer(this);
99 m_showProgressBarTimer->setInterval(1000);
100 m_showProgressBarTimer->setSingleShot(true);
101 connect(m_showProgressBarTimer, SIGNAL(timeout()), this, SLOT(updateProgressInfo()));
102
103 // Initialize top layout and size policies
104 const int fontHeight = QFontMetrics(m_messageLabel->font()).height();
105 const int zoomSliderHeight = m_zoomSlider->minimumSizeHint().height();
106 const int contentHeight = qMax(fontHeight, zoomSliderHeight);
107
108 m_messageLabel->setMinimumTextHeight(contentHeight);
109
110 m_spaceInfo->setMaximumSize(200, contentHeight - 5);
111 m_spaceInfo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
112
113 m_progressBar->setMaximumSize(200, contentHeight);
114 m_zoomSlider->setMaximumSize(150, contentHeight);
115 m_zoomSlider->setMinimumWidth(30);
116
117 QHBoxLayout* topLayout = new QHBoxLayout(this);
118 topLayout->setMargin(0);
119 topLayout->setSpacing(4);
120 topLayout->addWidget(m_messageLabel);
121 topLayout->addWidget(m_zoomSlider);
122 topLayout->addWidget(m_spaceInfo);
123 topLayout->addWidget(m_stopButton);
124 topLayout->addWidget(m_progressText);
125 topLayout->addWidget(m_progressBar);
126
127 setExtensionsVisible(true);
128 }
129
130 DolphinStatusBar::~DolphinStatusBar()
131 {
132 }
133
134 void DolphinStatusBar::setMessage(const QString& msg,
135 Type type)
136 {
137 int timeout = 1000; // Timeout in milliseconds until default
138 // messages may overwrite other messages.
139
140 QString message = msg;
141 if (message.isEmpty()) {
142 // Show the default text as fallback. An empty text indicates
143 // a clearing of the information message.
144 if (m_messageLabel->defaultText().isEmpty()) {
145 return;
146 }
147 message = m_messageLabel->defaultText();
148 type = Default;
149 timeout = 0;
150 }
151
152 KonqStatusBarMessageLabel::Type konqType = static_cast<KonqStatusBarMessageLabel::Type>(type);
153 if ((message == m_messageLabel->text()) && (konqType == m_messageLabel->type())) {
154 // the message is already shown
155 return;
156 }
157
158 const QTime currentTime = QTime::currentTime();
159 const bool skipMessage = (type == Default) &&
160 m_messageTimeStamp.isValid() &&
161 (m_messageTimeStamp.msecsTo(currentTime) < timeout);
162 if (skipMessage) {
163 // A non-default message is shown just for a very short time. Don't hide
164 // the message by a default message, so that the user gets the chance to
165 // read the information.
166 return;
167 }
168
169 m_messageLabel->setMessage(message, konqType);
170 if (type != Default) {
171 m_messageTimeStamp = currentTime;
172 }
173 }
174
175 DolphinStatusBar::Type DolphinStatusBar::type() const
176 {
177 return static_cast<Type>(m_messageLabel->type());
178 }
179
180 QString DolphinStatusBar::message() const
181 {
182 return m_messageLabel->text();
183 }
184
185 void DolphinStatusBar::setProgressText(const QString& text)
186 {
187 m_progressText->setText(text);
188 }
189
190 int DolphinStatusBar::progress() const
191 {
192 return m_progress;
193 }
194
195 QString DolphinStatusBar::progressText() const
196 {
197 return m_progressText->text();
198 }
199
200 void DolphinStatusBar::setProgress(int percent)
201 {
202 // Show a busy indicator if a value < 0 is provided:
203 m_progressBar->setMaximum((percent < 0) ? 0 : 100);
204
205 if (percent < 0) {
206 percent = 0;
207 } else if (percent > 100) {
208 percent = 100;
209 }
210
211 const bool progressRestarted = (percent < 100) && (percent < m_progress);
212 m_progress = percent;
213 if (m_messageLabel->type() == KonqStatusBarMessageLabel::Error) {
214 // Don't update any widget or status bar text if an
215 // error message is shown
216 return;
217 }
218
219 if (progressRestarted && !m_progressBar->isVisible()) {
220 // Show the progress bar delayed: In the case if 100 % are reached within
221 // a short time, no progress bar will be shown at all.
222 m_showProgressBarTimer->start();
223 }
224
225 m_progressBar->setValue(m_progress);
226 if (percent == 100) {
227 // The end of the progress has been reached. Assure that the progress bar
228 // gets hidden and the extensions widgets get visible again.
229 m_showProgressBarTimer->stop();
230 updateProgressInfo();
231 }
232
233 const QString defaultText = m_messageLabel->defaultText();
234 const QString msg(m_messageLabel->text());
235 if ((percent == 0) && !msg.isEmpty()) {
236 setMessage(QString(), Default);
237 } else if ((percent == 100) && (msg != defaultText)) {
238 setMessage(defaultText, Default);
239 }
240 }
241
242 void DolphinStatusBar::clear()
243 {
244 setMessage(m_messageLabel->defaultText(), Default);
245 }
246
247 void DolphinStatusBar::setDefaultText(const QString& text)
248 {
249 m_messageLabel->setDefaultText(text);
250 }
251
252 QString DolphinStatusBar::defaultText() const
253 {
254 return m_messageLabel->defaultText();
255 }
256
257 void DolphinStatusBar::refresh()
258 {
259 setExtensionsVisible(true);
260 }
261
262 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent* event)
263 {
264 Q_UNUSED(event);
265
266 KMenu menu(this);
267
268 QAction* copyAction = 0;
269 switch (type()) {
270 case Default:
271 case OperationCompleted:
272 case Information:
273 copyAction = menu.addAction(i18nc("@action:inmenu", "Copy Information Message"));
274 break;
275 case Error:
276 copyAction = menu.addAction(i18nc("@action:inmenu", "Copy Error Message"));
277 break;
278 default: break;
279 }
280
281 QAction* showZoomSliderAction = menu.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
282 showZoomSliderAction->setCheckable(true);
283 showZoomSliderAction->setChecked(GeneralSettings::showZoomSlider());
284
285 QAction* showSpaceInfoAction = menu.addAction(i18nc("@action:inmenu", "Show Space Information"));
286 showSpaceInfoAction->setCheckable(true);
287 showSpaceInfoAction->setChecked(GeneralSettings::showSpaceInfo());
288
289 const QAction* action = menu.exec(QCursor::pos());
290 if (action == copyAction) {
291 QMimeData* mimeData = new QMimeData();
292 mimeData->setText(message());
293 QApplication::clipboard()->setMimeData(mimeData);
294 } else if (action == showZoomSliderAction) {
295 const bool visible = showZoomSliderAction->isChecked();
296 GeneralSettings::setShowZoomSlider(visible);
297 m_zoomSlider->setVisible(visible);
298 } else if (action == showSpaceInfoAction) {
299 const bool visible = showSpaceInfoAction->isChecked();
300 GeneralSettings::setShowSpaceInfo(visible);
301 m_spaceInfo->setVisible(visible);
302 }
303 }
304
305 void DolphinStatusBar::updateSpaceInfoContent(const KUrl& url)
306 {
307 m_spaceInfo->setUrl(url);
308 }
309
310 void DolphinStatusBar::setZoomLevel(int zoomLevel)
311 {
312 m_view->setZoomLevel(zoomLevel);
313 updateZoomSliderToolTip(zoomLevel);
314 }
315
316 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel)
317 {
318 updateZoomSliderToolTip(zoomLevel);
319
320 QPoint global = m_zoomSlider->rect().topLeft();
321 global.ry() += m_zoomSlider->height() / 2;
322 QHelpEvent toolTipEvent(QEvent::ToolTip, QPoint(0, 0), m_zoomSlider->mapToGlobal(global));
323 QApplication::sendEvent(m_zoomSlider, &toolTipEvent);
324 }
325
326 void DolphinStatusBar::slotZoomLevelChanged(int current, int previous)
327 {
328 Q_UNUSED(previous);
329 m_zoomSlider->setValue(current);
330 }
331
332 void DolphinStatusBar::slotPreviewsShownChanged(bool shown)
333 {
334 Q_UNUSED(shown);
335 // The zoom level might be different with/without previews -> update the zoom slider.
336 m_zoomSlider->setValue(m_view->zoomLevel());
337 }
338
339 void DolphinStatusBar::updateProgressInfo()
340 {
341 const bool isErrorShown = (m_messageLabel->type() == KonqStatusBarMessageLabel::Error);
342 if (m_progress < 100) {
343 // Show the progress information and hide the extensions
344 setExtensionsVisible(false);
345 if (!isErrorShown) {
346 m_stopButton->show();
347 m_progressText->show();
348 m_progressBar->show();
349 }
350 } else {
351 // Hide the progress information and show the extensions
352 m_stopButton->hide();
353 m_progressText->hide();
354 m_progressBar->hide();
355 setExtensionsVisible(true);
356 }
357 }
358
359 void DolphinStatusBar::setExtensionsVisible(bool visible)
360 {
361 bool showSpaceInfo = visible;
362 bool showZoomSlider = visible;
363 if (visible) {
364 showSpaceInfo = GeneralSettings::showSpaceInfo();
365 showZoomSlider = GeneralSettings::showZoomSlider();
366 }
367 m_spaceInfo->setVisible(showSpaceInfo);
368 m_zoomSlider->setVisible(showZoomSlider);
369 }
370
371 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel)
372 {
373 const int size = ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel);
374 m_zoomSlider->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size));
375 }
376
377 #include "dolphinstatusbar.moc"