]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/dolphinstatusbar.cpp
Merged very early alpha-version of Dolphin 2.0
[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 "settings/dolphinsettings.h"
32 #include "statusbarspaceinfo.h"
33
34 #include <QApplication>
35 #include <QClipboard>
36 #include <QHBoxLayout>
37 #include <QLabel>
38 #include <QProgressBar>
39 #include <QToolButton>
40 #include <QTime>
41 #include <QTimer>
42
43 #include <views/dolphinview.h>
44 #include <views/zoomlevelinfo.h>
45
46 DolphinStatusBar::DolphinStatusBar(QWidget* parent, DolphinView* view) :
47 QWidget(parent),
48 m_view(view),
49 m_messageLabel(0),
50 m_spaceInfo(0),
51 m_zoomSlider(0),
52 m_progressBar(0),
53 m_stopButton(0),
54 m_progress(100),
55 m_showProgressBarTimer(0),
56 m_messageTimeStamp()
57 {
58 connect(m_view, SIGNAL(urlChanged(const KUrl&)),
59 this, SLOT(updateSpaceInfoContent(const KUrl&)));
60
61 // Initialize message label
62 m_messageLabel = new KonqStatusBarMessageLabel(this);
63
64 // Initialize zoom widget
65 m_zoomSlider = new QSlider(Qt::Horizontal, this);
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
78 // Initialize space information
79 m_spaceInfo = new StatusBarSpaceInfo(this);
80 m_spaceInfo->setUrl(m_view->url());
81
82 // Initialize progress information
83 m_stopButton = new QToolButton(this);
84 m_stopButton->setIcon(KIcon("process-stop"));
85 // TODO: Add tooltip for KDE SC 4.7.0, if new strings are allowed again
86 m_stopButton->setAutoRaise(true);
87 m_stopButton->hide();
88 connect(m_stopButton, SIGNAL(clicked()), this, SIGNAL(stopPressed()));
89
90 m_progressText = new QLabel(this);
91 m_progressText->hide();
92
93 m_progressBar = new QProgressBar(this);
94 m_progressBar->hide();
95
96 m_showProgressBarTimer = new QTimer(this);
97 m_showProgressBarTimer->setInterval(1000);
98 m_showProgressBarTimer->setSingleShot(true);
99 connect(m_showProgressBarTimer, SIGNAL(timeout()), this, SLOT(updateProgressInfo()));
100
101 // Initialize top layout and size policies
102 const int fontHeight = QFontMetrics(m_messageLabel->font()).height();
103 const int zoomSliderHeight = m_zoomSlider->minimumSizeHint().height();
104 const int contentHeight = qMax(fontHeight, zoomSliderHeight);
105
106 m_messageLabel->setMinimumTextHeight(contentHeight);
107
108 m_spaceInfo->setMaximumSize(200, contentHeight - 5);
109 m_spaceInfo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
110
111 m_progressBar->setMaximumSize(200, contentHeight);
112 m_zoomSlider->setMaximumSize(150, contentHeight);
113 m_zoomSlider->setMinimumWidth(30);
114
115 QHBoxLayout* topLayout = new QHBoxLayout(this);
116 topLayout->setMargin(0);
117 topLayout->setSpacing(4);
118 topLayout->addWidget(m_messageLabel);
119 topLayout->addWidget(m_zoomSlider);
120 topLayout->addWidget(m_spaceInfo);
121 topLayout->addWidget(m_stopButton);
122 topLayout->addWidget(m_progressText);
123 topLayout->addWidget(m_progressBar);
124
125 setExtensionsVisible(true);
126 }
127
128 DolphinStatusBar::~DolphinStatusBar()
129 {
130 }
131
132 void DolphinStatusBar::setMessage(const QString& msg,
133 Type type)
134 {
135 int timeout = 1000; // Timeout in milliseconds until default
136 // messages may overwrite other messages.
137
138 QString message = msg;
139 if (message.isEmpty()) {
140 // Show the default text as fallback. An empty text indicates
141 // a clearing of the information message.
142 if (m_messageLabel->defaultText().isEmpty()) {
143 return;
144 }
145 message = m_messageLabel->defaultText();
146 type = Default;
147 timeout = 0;
148 }
149
150 KonqStatusBarMessageLabel::Type konqType = static_cast<KonqStatusBarMessageLabel::Type>(type);
151 if ((message == m_messageLabel->text()) && (konqType == m_messageLabel->type())) {
152 // the message is already shown
153 return;
154 }
155
156 const QTime currentTime = QTime::currentTime();
157 const bool skipMessage = (type == Default) &&
158 m_messageTimeStamp.isValid() &&
159 (m_messageTimeStamp.msecsTo(currentTime) < timeout);
160 if (skipMessage) {
161 // A non-default message is shown just for a very short time. Don't hide
162 // the message by a default message, so that the user gets the chance to
163 // read the information.
164 return;
165 }
166
167 m_messageLabel->setMessage(message, konqType);
168 if (type != Default) {
169 m_messageTimeStamp = currentTime;
170 }
171 }
172
173 DolphinStatusBar::Type DolphinStatusBar::type() const
174 {
175 return static_cast<Type>(m_messageLabel->type());
176 }
177
178 QString DolphinStatusBar::message() const
179 {
180 return m_messageLabel->text();
181 }
182
183 void DolphinStatusBar::setProgressText(const QString& text)
184 {
185 m_progressText->setText(text);
186 }
187
188 int DolphinStatusBar::progress() const
189 {
190 return m_progress;
191 }
192
193 QString DolphinStatusBar::progressText() const
194 {
195 return m_progressText->text();
196 }
197
198 void DolphinStatusBar::setProgress(int percent)
199 {
200 // Show a busy indicator if a value < 0 is provided:
201 m_progressBar->setMaximum((percent < 0) ? 0 : 100);
202
203 if (percent < 0) {
204 percent = 0;
205 } else if (percent > 100) {
206 percent = 100;
207 }
208
209 const bool progressRestarted = (percent < 100) && (percent < m_progress);
210 m_progress = percent;
211 if (m_messageLabel->type() == KonqStatusBarMessageLabel::Error) {
212 // Don't update any widget or status bar text if an
213 // error message is shown
214 return;
215 }
216
217 if (progressRestarted && !m_progressBar->isVisible()) {
218 // Show the progress bar delayed: In the case if 100 % are reached within
219 // a short time, no progress bar will be shown at all.
220 m_showProgressBarTimer->start();
221 }
222
223 m_progressBar->setValue(m_progress);
224 if (percent == 100) {
225 // The end of the progress has been reached. Assure that the progress bar
226 // gets hidden and the extensions widgets get visible again.
227 m_showProgressBarTimer->stop();
228 updateProgressInfo();
229 }
230
231 const QString defaultText = m_messageLabel->defaultText();
232 const QString msg(m_messageLabel->text());
233 if ((percent == 0) && !msg.isEmpty()) {
234 setMessage(QString(), Default);
235 } else if ((percent == 100) && (msg != defaultText)) {
236 setMessage(defaultText, Default);
237 }
238 }
239
240 void DolphinStatusBar::clear()
241 {
242 setMessage(m_messageLabel->defaultText(), Default);
243 }
244
245 void DolphinStatusBar::setDefaultText(const QString& text)
246 {
247 m_messageLabel->setDefaultText(text);
248 }
249
250 QString DolphinStatusBar::defaultText() const
251 {
252 return m_messageLabel->defaultText();
253 }
254
255 void DolphinStatusBar::refresh()
256 {
257 setExtensionsVisible(true);
258 }
259
260 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent* event)
261 {
262 Q_UNUSED(event);
263
264 KMenu menu(this);
265
266 QAction* copyAction = 0;
267 switch (type()) {
268 case Default:
269 case OperationCompleted:
270 case Information:
271 copyAction = menu.addAction(i18nc("@action:inmenu", "Copy Information Message"));
272 break;
273 case Error:
274 copyAction = menu.addAction(i18nc("@action:inmenu", "Copy Error Message"));
275 break;
276 default: break;
277 }
278
279 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
280
281 QAction* showZoomSliderAction = menu.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
282 showZoomSliderAction->setCheckable(true);
283 showZoomSliderAction->setChecked(settings->showZoomSlider());
284
285 QAction* showSpaceInfoAction = menu.addAction(i18nc("@action:inmenu", "Show Space Information"));
286 showSpaceInfoAction->setCheckable(true);
287 showSpaceInfoAction->setChecked(settings->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 settings->setShowZoomSlider(visible);
297 m_zoomSlider->setVisible(visible);
298 } else if (action == showSpaceInfoAction) {
299 const bool visible = showSpaceInfoAction->isChecked();
300 settings->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::updateProgressInfo()
333 {
334 const bool isErrorShown = (m_messageLabel->type() == KonqStatusBarMessageLabel::Error);
335 if (m_progress < 100) {
336 // Show the progress information and hide the extensions
337 setExtensionsVisible(false);
338 if (!isErrorShown) {
339 m_stopButton->show();
340 m_progressText->show();
341 m_progressBar->show();
342 }
343 } else {
344 // Hide the progress information and show the extensions
345 m_stopButton->hide();
346 m_progressText->hide();
347 m_progressBar->hide();
348 setExtensionsVisible(true);
349 }
350 }
351
352 void DolphinStatusBar::setExtensionsVisible(bool visible)
353 {
354 bool showSpaceInfo = visible;
355 bool showZoomSlider = visible;
356 if (visible) {
357 const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
358 showSpaceInfo = settings->showSpaceInfo();
359 showZoomSlider = settings->showZoomSlider();
360 }
361 m_spaceInfo->setVisible(showSpaceInfo);
362 m_zoomSlider->setVisible(showZoomSlider);
363 }
364
365 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel)
366 {
367 const int size = ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel);
368 m_zoomSlider->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size));
369 }
370
371 #include "dolphinstatusbar.moc"