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