]>
cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/dolphinstatusbar.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz *
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. *
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. *
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 ***************************************************************************/
21 #include "dolphinstatusbar.h"
23 #include "dolphin_generalsettings.h"
25 #include <KIconLoader>
31 #include "settings/dolphinsettings.h"
32 #include "statusbarspaceinfo.h"
34 #include <QApplication>
36 #include <QHBoxLayout>
38 #include <QProgressBar>
39 #include <QToolButton>
43 #include <views/dolphinview.h>
44 #include <views/zoomlevelinfo.h>
46 DolphinStatusBar::DolphinStatusBar(QWidget
* parent
, DolphinView
* view
) :
55 m_showProgressBarTimer(0),
58 connect(m_view
, SIGNAL(urlChanged(KUrl
)),
59 this, SLOT(updateSpaceInfoContent(KUrl
)));
61 // Initialize message label
62 m_messageLabel
= new KonqStatusBarMessageLabel(this);
64 // Initialize zoom widget
65 m_zoomSlider
= new QSlider(Qt::Horizontal
, this);
66 m_zoomSlider
->setPageStep(1);
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());
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)));
78 // Initialize space information
79 m_spaceInfo
= new StatusBarSpaceInfo(this);
80 m_spaceInfo
->setUrl(m_view
->url());
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);
89 connect(m_stopButton
, SIGNAL(clicked()), this, SIGNAL(stopPressed()));
91 m_progressText
= new QLabel(this);
92 m_progressText
->hide();
94 m_progressBar
= new QProgressBar(this);
95 m_progressBar
->hide();
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()));
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
);
107 m_messageLabel
->setMinimumTextHeight(contentHeight
);
109 m_spaceInfo
->setMaximumSize(200, contentHeight
- 5);
110 m_spaceInfo
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
112 m_progressBar
->setMaximumSize(200, contentHeight
);
113 m_zoomSlider
->setMaximumSize(150, contentHeight
);
114 m_zoomSlider
->setMinimumWidth(30);
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
);
126 setExtensionsVisible(true);
129 DolphinStatusBar::~DolphinStatusBar()
133 void DolphinStatusBar::setMessage(const QString
& msg
,
136 int timeout
= 1000; // Timeout in milliseconds until default
137 // messages may overwrite other messages.
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()) {
146 message
= m_messageLabel
->defaultText();
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
157 const QTime currentTime
= QTime::currentTime();
158 const bool skipMessage
= (type
== Default
) &&
159 m_messageTimeStamp
.isValid() &&
160 (m_messageTimeStamp
.msecsTo(currentTime
) < timeout
);
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.
168 m_messageLabel
->setMessage(message
, konqType
);
169 if (type
!= Default
) {
170 m_messageTimeStamp
= currentTime
;
174 DolphinStatusBar::Type
DolphinStatusBar::type() const
176 return static_cast<Type
>(m_messageLabel
->type());
179 QString
DolphinStatusBar::message() const
181 return m_messageLabel
->text();
184 void DolphinStatusBar::setProgressText(const QString
& text
)
186 m_progressText
->setText(text
);
189 int DolphinStatusBar::progress() const
194 QString
DolphinStatusBar::progressText() const
196 return m_progressText
->text();
199 void DolphinStatusBar::setProgress(int percent
)
201 // Show a busy indicator if a value < 0 is provided:
202 m_progressBar
->setMaximum((percent
< 0) ? 0 : 100);
206 } else if (percent
> 100) {
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
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();
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();
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
);
241 void DolphinStatusBar::clear()
243 setMessage(m_messageLabel
->defaultText(), Default
);
246 void DolphinStatusBar::setDefaultText(const QString
& text
)
248 m_messageLabel
->setDefaultText(text
);
251 QString
DolphinStatusBar::defaultText() const
253 return m_messageLabel
->defaultText();
256 void DolphinStatusBar::refresh()
258 setExtensionsVisible(true);
261 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent
* event
)
267 QAction
* copyAction
= 0;
270 case OperationCompleted
:
272 copyAction
= menu
.addAction(i18nc("@action:inmenu", "Copy Information Message"));
275 copyAction
= menu
.addAction(i18nc("@action:inmenu", "Copy Error Message"));
280 GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
282 QAction
* showZoomSliderAction
= menu
.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
283 showZoomSliderAction
->setCheckable(true);
284 showZoomSliderAction
->setChecked(settings
->showZoomSlider());
286 QAction
* showSpaceInfoAction
= menu
.addAction(i18nc("@action:inmenu", "Show Space Information"));
287 showSpaceInfoAction
->setCheckable(true);
288 showSpaceInfoAction
->setChecked(settings
->showSpaceInfo());
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
);
306 void DolphinStatusBar::updateSpaceInfoContent(const KUrl
& url
)
308 m_spaceInfo
->setUrl(url
);
311 void DolphinStatusBar::setZoomLevel(int zoomLevel
)
313 m_view
->setZoomLevel(zoomLevel
);
314 updateZoomSliderToolTip(zoomLevel
);
317 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel
)
319 updateZoomSliderToolTip(zoomLevel
);
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
);
327 void DolphinStatusBar::slotZoomLevelChanged(int current
, int previous
)
330 m_zoomSlider
->setValue(current
);
333 void DolphinStatusBar::updateProgressInfo()
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);
340 m_stopButton
->show();
341 m_progressText
->show();
342 m_progressBar
->show();
345 // Hide the progress information and show the extensions
346 m_stopButton
->hide();
347 m_progressText
->hide();
348 m_progressBar
->hide();
349 setExtensionsVisible(true);
353 void DolphinStatusBar::setExtensionsVisible(bool visible
)
355 bool showSpaceInfo
= visible
;
356 bool showZoomSlider
= visible
;
358 const GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
359 showSpaceInfo
= settings
->showSpaceInfo();
360 showZoomSlider
= settings
->showZoomSlider();
362 m_spaceInfo
->setVisible(showSpaceInfo
);
363 m_zoomSlider
->setVisible(showZoomSlider
);
366 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel
)
368 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel
);
369 m_zoomSlider
->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size
));
372 #include "dolphinstatusbar.moc"