]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/dolphinstatusbar.cpp
1. Also use a time out when waiting for the view's selectionChanged signal.
[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.h>
26 #include <kicon.h>
27 #include <klocale.h>
28 #include <kmenu.h>
29 #include <kvbox.h>
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 <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_zoomWidget(0),
51 m_zoomOut(0),
52 m_zoomSlider(0),
53 m_zoomIn(0),
54 m_progressBar(0),
55 m_stopButton(0),
56 m_progress(100),
57 m_messageTimeStamp()
58 {
59 connect(m_view, SIGNAL(urlChanged(const KUrl&)),
60 this, SLOT(updateSpaceInfoContent(const KUrl&)));
61
62 // Initialize message label
63 m_messageLabel = new KonqStatusBarMessageLabel(this);
64
65 // Initialize zoom slider
66 m_zoomWidget = new QWidget(this);
67
68 m_zoomOut = new QToolButton(m_zoomWidget);
69 m_zoomOut->setIcon(KIcon("zoom-out"));
70 m_zoomOut->setAutoRaise(true);
71
72 m_zoomSlider = new QSlider(Qt::Horizontal, m_zoomWidget);
73 m_zoomSlider->setPageStep(1);
74
75 const int min = ZoomLevelInfo::minimumLevel();
76 const int max = ZoomLevelInfo::maximumLevel();
77 m_zoomSlider->setRange(min, max);
78 m_zoomSlider->setValue(view->zoomLevel());
79 updateZoomSliderToolTip(view->zoomLevel());
80
81 m_zoomIn = new QToolButton(m_zoomWidget);
82 m_zoomIn->setIcon(KIcon("zoom-in"));
83 m_zoomIn->setAutoRaise(true);
84
85 // Initialize zoom widget layout
86 QHBoxLayout* zoomWidgetLayout = new QHBoxLayout(m_zoomWidget);
87 zoomWidgetLayout->setSpacing(0);
88 zoomWidgetLayout->setMargin(0);
89 zoomWidgetLayout->addWidget(m_zoomOut);
90 zoomWidgetLayout->addWidget(m_zoomSlider);
91 zoomWidgetLayout->addWidget(m_zoomIn);
92
93 connect(m_zoomSlider, SIGNAL(valueChanged(int)), this, SLOT(setZoomLevel(int)));
94 connect(m_zoomSlider, SIGNAL(sliderMoved(int)), this, SLOT(showZoomSliderToolTip(int)));
95 connect(m_view, SIGNAL(zoomLevelChanged(int)), m_zoomSlider, SLOT(setValue(int)));
96 connect(m_zoomOut, SIGNAL(clicked()), this, SLOT(zoomOut()));
97 connect(m_zoomIn, SIGNAL(clicked()), this, SLOT(zoomIn()));
98
99 // Initialize space information
100 m_spaceInfo = new StatusBarSpaceInfo(this);
101 m_spaceInfo->setUrl(m_view->url());
102
103 // Initialize progress information
104 m_stopButton = new QToolButton(this);
105 m_stopButton->setIcon(KIcon("process-stop"));
106 // TODO: Add tooltip for KDE SC 4.7.0, if new strings are allowed again
107 m_stopButton->setAutoRaise(true);
108 m_stopButton->hide();
109 connect(m_stopButton, SIGNAL(clicked()), this, SIGNAL(stopPressed()));
110
111 m_progressText = new QLabel(this);
112 m_progressText->hide();
113
114 m_progressBar = new QProgressBar(this);
115 m_progressBar->hide();
116
117 // Initialize top layout and size policies
118 const int fontHeight = QFontMetrics(m_messageLabel->font()).height();
119 const int zoomWidgetHeight = m_zoomWidget->minimumSizeHint().height();
120 const int contentHeight = qMax(fontHeight, zoomWidgetHeight);
121
122 m_messageLabel->setMinimumTextHeight(contentHeight);
123
124 m_spaceInfo->setMaximumSize(200, contentHeight - 5);
125 m_spaceInfo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
126
127 m_progressBar->setMaximumSize(200, contentHeight);
128 m_zoomWidget->setMaximumSize(150, contentHeight);
129 m_zoomSlider->setMinimumWidth(30);
130
131 QHBoxLayout* topLayout = new QHBoxLayout(this);
132 topLayout->setMargin(0);
133 topLayout->setSpacing(4);
134 topLayout->addWidget(m_messageLabel);
135 topLayout->addWidget(m_zoomWidget);
136 topLayout->addWidget(m_spaceInfo);
137 topLayout->addWidget(m_stopButton);
138 topLayout->addWidget(m_progressText);
139 topLayout->addWidget(m_progressBar);
140
141 setExtensionsVisible(true);
142 }
143
144 DolphinStatusBar::~DolphinStatusBar()
145 {
146 }
147
148 void DolphinStatusBar::setMessage(const QString& msg,
149 Type type)
150 {
151 int timeout = 1000; // Timeout in milliseconds until default
152 // messages may overwrite other messages.
153
154 QString message = msg;
155 if (message.isEmpty()) {
156 // Show the default text as fallback. An empty text indicates
157 // a clearing of the information message.
158 if (m_messageLabel->defaultText().isEmpty()) {
159 return;
160 }
161 message = m_messageLabel->defaultText();
162 type = Default;
163 timeout = 0;
164 }
165
166 KonqStatusBarMessageLabel::Type konqType = static_cast<KonqStatusBarMessageLabel::Type>(type);
167 if ((message == m_messageLabel->text()) && (konqType == m_messageLabel->type())) {
168 // the message is already shown
169 return;
170 }
171
172 const QTime currentTime = QTime::currentTime();
173 const bool skipMessage = (type == Default) &&
174 m_messageTimeStamp.isValid() &&
175 (m_messageTimeStamp.msecsTo(currentTime) < timeout);
176 if (skipMessage) {
177 // A non-default message is shown just for a very short time. Don't hide
178 // the message by a default message, so that the user gets the chance to
179 // read the information.
180 return;
181 }
182
183 m_messageLabel->setMessage(message, konqType);
184 if (type != Default) {
185 m_messageTimeStamp = currentTime;
186 }
187 }
188
189 DolphinStatusBar::Type DolphinStatusBar::type() const
190 {
191 return static_cast<Type>(m_messageLabel->type());
192 }
193
194 QString DolphinStatusBar::message() const
195 {
196 return m_messageLabel->text();
197 }
198
199 void DolphinStatusBar::setProgressText(const QString& text)
200 {
201 m_progressText->setText(text);
202 }
203
204 int DolphinStatusBar::progress() const
205 {
206 return m_progress;
207 }
208
209 QString DolphinStatusBar::progressText() const
210 {
211 return m_progressText->text();
212 }
213
214 void DolphinStatusBar::setProgress(int percent)
215 {
216 // Show a busy indicator if a value < 0 is provided:
217 m_progressBar->setMaximum((percent < 0) ? 0 : 100);
218
219 if (percent < 0) {
220 percent = 0;
221 } else if (percent > 100) {
222 percent = 100;
223 }
224
225 m_progress = percent;
226 if (m_messageLabel->type() == KonqStatusBarMessageLabel::Error) {
227 // Don't update any widget or status bar text if an
228 // error message is shown
229 return;
230 }
231
232 m_progressBar->setValue(m_progress);
233 if (!m_progressBar->isVisible() || (percent == 100)) {
234 updateProgressInfo();
235 }
236
237 const QString defaultText = m_messageLabel->defaultText();
238 const QString msg(m_messageLabel->text());
239 if ((percent == 0) && !msg.isEmpty()) {
240 setMessage(QString(), Default);
241 } else if ((percent == 100) && (msg != defaultText)) {
242 setMessage(defaultText, Default);
243 }
244 }
245
246 void DolphinStatusBar::clear()
247 {
248 setMessage(m_messageLabel->defaultText(), Default);
249 }
250
251 void DolphinStatusBar::setDefaultText(const QString& text)
252 {
253 m_messageLabel->setDefaultText(text);
254 }
255
256 QString DolphinStatusBar::defaultText() const
257 {
258 return m_messageLabel->defaultText();
259 }
260
261 void DolphinStatusBar::refresh()
262 {
263 setExtensionsVisible(true);
264 }
265
266 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent* event)
267 {
268 Q_UNUSED(event);
269
270 KMenu menu(this);
271
272 QAction* copyAction = 0;
273 switch (type()) {
274 case Default:
275 case OperationCompleted:
276 case Information:
277 copyAction = menu.addAction(i18nc("@action:inmenu", "Copy Information Message"));
278 break;
279 case Error:
280 copyAction = menu.addAction(i18nc("@action:inmenu", "Copy Error Message"));
281 break;
282 default: break;
283 }
284
285 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
286
287 QAction* showZoomSliderAction = menu.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
288 showZoomSliderAction->setCheckable(true);
289 showZoomSliderAction->setChecked(settings->showZoomSlider());
290
291 QAction* showSpaceInfoAction = menu.addAction(i18nc("@action:inmenu", "Show Space Information"));
292 showSpaceInfoAction->setCheckable(true);
293 showSpaceInfoAction->setChecked(settings->showSpaceInfo());
294
295 const QAction* action = menu.exec(QCursor::pos());
296 if (action == copyAction) {
297 QMimeData* mimeData = new QMimeData();
298 mimeData->setText(message());
299 QApplication::clipboard()->setMimeData(mimeData);
300 } else if (action == showZoomSliderAction) {
301 const bool visible = showZoomSliderAction->isChecked();
302 settings->setShowZoomSlider(visible);
303 m_zoomWidget->setVisible(visible);
304 } else if (action == showSpaceInfoAction) {
305 const bool visible = showSpaceInfoAction->isChecked();
306 settings->setShowSpaceInfo(visible);
307 m_spaceInfo->setVisible(visible);
308 }
309 }
310
311 void DolphinStatusBar::updateSpaceInfoContent(const KUrl& url)
312 {
313 m_spaceInfo->setUrl(url);
314 }
315
316 void DolphinStatusBar::setZoomLevel(int zoomLevel)
317 {
318 m_zoomOut->setEnabled(zoomLevel > m_zoomSlider->minimum());
319 m_zoomIn->setEnabled(zoomLevel < m_zoomSlider->maximum());
320 m_view->setZoomLevel(zoomLevel);
321 updateZoomSliderToolTip(zoomLevel);
322 }
323
324 void DolphinStatusBar::zoomOut()
325 {
326 const int value = m_zoomSlider->value();
327 m_zoomSlider->setValue(value - 1);
328 }
329
330 void DolphinStatusBar::zoomIn()
331 {
332 const int value = m_zoomSlider->value();
333 m_zoomSlider->setValue(value + 1);
334 }
335
336 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel)
337 {
338 updateZoomSliderToolTip(zoomLevel);
339
340 QPoint global = m_zoomSlider->rect().topLeft();
341 global.ry() += m_zoomSlider->height() / 2;
342 QHelpEvent toolTipEvent(QEvent::ToolTip, QPoint(0, 0), m_zoomSlider->mapToGlobal(global));
343 QApplication::sendEvent(m_zoomSlider, &toolTipEvent);
344 }
345
346 void DolphinStatusBar::updateProgressInfo()
347 {
348 const bool isErrorShown = (m_messageLabel->type() == KonqStatusBarMessageLabel::Error);
349 if (m_progress < 100) {
350 // Show the progress information and hide the extensions
351 setExtensionsVisible(false);
352 if (!isErrorShown) {
353 m_stopButton->show();
354 m_progressText->show();
355 m_progressBar->show();
356 }
357 } else {
358 // Hide the progress information and show the extensions
359 m_stopButton->hide();
360 m_progressText->hide();
361 m_progressBar->hide();
362 setExtensionsVisible(true);
363 }
364 }
365
366 void DolphinStatusBar::setExtensionsVisible(bool visible)
367 {
368 bool showSpaceInfo = visible;
369 bool showZoomWidget = visible;
370 if (visible) {
371 const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
372 showSpaceInfo = settings->showSpaceInfo();
373 showZoomWidget = settings->showZoomSlider();
374 }
375 m_spaceInfo->setVisible(showSpaceInfo);
376 m_zoomWidget->setVisible(showZoomWidget);
377 }
378
379 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel)
380 {
381 const int size = ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel);
382 m_zoomSlider->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size));
383 }
384
385 #include "dolphinstatusbar.moc"