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