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