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