1 /***************************************************************************
2 * Copyright (C) 2006-2012 by Peter Penz <peter.penz19@gmail.com> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "dolphinstatusbar.h"
22 #include "dolphin_generalsettings.h"
24 #include <KIconLoader>
30 #include "statusbarspaceinfo.h"
32 #include <QApplication>
34 #include <QHBoxLayout>
36 #include <QProgressBar>
37 #include <QToolButton>
41 #include <views/dolphinview.h>
42 #include <views/zoomlevelinfo.h>
45 const int ResetToDefaultTimeout
= 1000;
48 DolphinStatusBar::DolphinStatusBar(QWidget
* parent
) :
58 m_showProgressBarTimer(0),
59 m_resetToDefaultTextTimer(0),
62 // Initialize text label
63 m_label
= new QLabel(this);
64 m_label
->setWordWrap(true);
65 m_label
->installEventFilter(this);
67 // Initialize zoom widget
68 m_zoomSlider
= new QSlider(Qt::Horizontal
, this);
69 m_zoomSlider
->setAccessibleName(i18n("Zoom slider"));
70 m_zoomSlider
->setPageStep(1);
71 m_zoomSlider
->setRange(ZoomLevelInfo::minimumLevel(), ZoomLevelInfo::maximumLevel());
73 connect(m_zoomSlider
, SIGNAL(valueChanged(int)), this, SIGNAL(zoomLevelChanged(int)));
74 connect(m_zoomSlider
, SIGNAL(sliderMoved(int)), this, SLOT(showZoomSliderToolTip(int)));
76 // Initialize space information
77 m_spaceInfo
= new StatusBarSpaceInfo(this);
79 // Initialize progress information
80 m_stopButton
= new QToolButton(this);
81 m_stopButton
->setIcon(KIcon("process-stop"));
82 m_stopButton
->setAccessibleName(i18n("Stop"));
83 m_stopButton
->setAutoRaise(true);
84 m_stopButton
->setToolTip(i18nc("@tooltip", "Stop loading"));
86 connect(m_stopButton
, SIGNAL(clicked()), this, SIGNAL(stopPressed()));
88 m_progressTextLabel
= new QLabel(this);
89 m_progressTextLabel
->hide();
91 m_progressBar
= new QProgressBar(this);
92 m_progressBar
->hide();
94 m_showProgressBarTimer
= new QTimer(this);
95 m_showProgressBarTimer
->setInterval(500);
96 m_showProgressBarTimer
->setSingleShot(true);
97 connect(m_showProgressBarTimer
, SIGNAL(timeout()), this, SLOT(updateProgressInfo()));
99 m_resetToDefaultTextTimer
= new QTimer(this);
100 m_resetToDefaultTextTimer
->setInterval(ResetToDefaultTimeout
);
101 m_resetToDefaultTextTimer
->setSingleShot(true);
102 connect(m_resetToDefaultTextTimer
, SIGNAL(timeout()), this, SLOT(slotResetToDefaultText()));
104 // Initialize top layout and size policies
105 const int fontHeight
= QFontMetrics(m_label
->font()).height();
106 const int zoomSliderHeight
= m_zoomSlider
->minimumSizeHint().height();
107 const int contentHeight
= qMax(fontHeight
, zoomSliderHeight
);
109 m_label
->setMinimumHeight(contentHeight
);
110 m_label
->setMaximumHeight(contentHeight
);
111 m_label
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
113 const QSize
size(150, contentHeight
);
114 applyFixedWidgetSize(m_spaceInfo
, size
);
115 applyFixedWidgetSize(m_progressBar
, size
);
116 applyFixedWidgetSize(m_zoomSlider
, size
);
118 QHBoxLayout
* topLayout
= new QHBoxLayout(this);
119 topLayout
->setMargin(0);
120 topLayout
->setSpacing(4);
121 topLayout
->addWidget(m_label
);
122 topLayout
->addWidget(m_zoomSlider
);
123 topLayout
->addWidget(m_spaceInfo
);
124 topLayout
->addWidget(m_stopButton
);
125 topLayout
->addWidget(m_progressTextLabel
);
126 topLayout
->addWidget(m_progressBar
);
128 setExtensionsVisible(true);
131 DolphinStatusBar::~DolphinStatusBar()
135 void DolphinStatusBar::setText(const QString
& text
)
137 if (m_text
== text
) {
141 m_textTimestamp
= QTime::currentTime();
143 if (text
.isEmpty()) {
144 // Assure that the previous set text won't get
145 // cleared immediatelly.
146 m_resetToDefaultTextTimer
->start();
150 if (m_resetToDefaultTextTimer
->isActive()) {
151 m_resetToDefaultTextTimer
->start();
158 QString
DolphinStatusBar::text() const
163 void DolphinStatusBar::setProgressText(const QString
& text
)
165 m_progressTextLabel
->setText(text
);
168 QString
DolphinStatusBar::progressText() const
170 return m_progressTextLabel
->text();
173 void DolphinStatusBar::setProgress(int percent
)
175 // Show a busy indicator if a value < 0 is provided:
176 m_progressBar
->setMaximum((percent
< 0) ? 0 : 100);
178 percent
= qBound(0, percent
, 100);
179 const bool progressRestarted
= (percent
< 100) && (percent
< m_progress
);
180 m_progress
= percent
;
181 if (progressRestarted
&& !m_progressBar
->isVisible()) {
182 // Show the progress bar delayed: In the case if 100 % are reached within
183 // a short time, no progress bar will be shown at all.
184 m_showProgressBarTimer
->start();
187 m_progressBar
->setValue(m_progress
);
188 if (percent
== 100) {
189 // The end of the progress has been reached. Assure that the progress bar
190 // gets hidden and the extensions widgets get visible again.
191 m_showProgressBarTimer
->stop();
192 updateProgressInfo();
196 int DolphinStatusBar::progress() const
201 void DolphinStatusBar::resetToDefaultText()
204 if (currentTime
.msecsTo(m_textTimestamp
) < ResetToDefaultTimeout
) {
205 m_resetToDefaultTextTimer
->start();
207 m_resetToDefaultTextTimer
->stop();
208 slotResetToDefaultText();
212 void DolphinStatusBar::setDefaultText(const QString
& text
)
214 m_defaultText
= text
;
218 QString
DolphinStatusBar::defaultText() const
220 return m_defaultText
;
223 void DolphinStatusBar::setUrl(const KUrl
& url
)
225 m_spaceInfo
->setUrl(url
);
228 KUrl
DolphinStatusBar::url() const
230 return m_spaceInfo
->url();
233 void DolphinStatusBar::setZoomLevel(int zoomLevel
)
235 if (zoomLevel
!= m_zoomSlider
->value()) {
236 m_zoomSlider
->setValue(zoomLevel
);
237 updateZoomSliderToolTip(zoomLevel
);
241 int DolphinStatusBar::zoomLevel() const
243 return m_zoomSlider
->value();
246 void DolphinStatusBar::readSettings()
248 setExtensionsVisible(true);
251 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent
* event
)
257 QAction
* copyAction
= menu
.addAction(i18nc("@action:inmenu", "Copy Text"));
258 QAction
* showZoomSliderAction
= menu
.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
259 showZoomSliderAction
->setCheckable(true);
260 showZoomSliderAction
->setChecked(GeneralSettings::showZoomSlider());
262 QAction
* showSpaceInfoAction
= menu
.addAction(i18nc("@action:inmenu", "Show Space Information"));
263 showSpaceInfoAction
->setCheckable(true);
264 showSpaceInfoAction
->setChecked(GeneralSettings::showSpaceInfo());
266 const QAction
* action
= menu
.exec(QCursor::pos());
267 if (action
== copyAction
) {
268 QMimeData
* mimeData
= new QMimeData();
269 mimeData
->setText(text());
270 QApplication::clipboard()->setMimeData(mimeData
);
271 } else if (action
== showZoomSliderAction
) {
272 const bool visible
= showZoomSliderAction
->isChecked();
273 GeneralSettings::setShowZoomSlider(visible
);
274 m_zoomSlider
->setVisible(visible
);
275 } else if (action
== showSpaceInfoAction
) {
276 const bool visible
= showSpaceInfoAction
->isChecked();
277 GeneralSettings::setShowSpaceInfo(visible
);
278 m_spaceInfo
->setVisible(visible
);
282 bool DolphinStatusBar::eventFilter(QObject
* obj
, QEvent
* event
)
284 if (obj
== m_label
&& event
->type() == QEvent::Resize
) {
287 return QWidget::eventFilter(obj
, event
);
290 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel
)
292 updateZoomSliderToolTip(zoomLevel
);
294 QPoint global
= m_zoomSlider
->rect().topLeft();
295 global
.ry() += m_zoomSlider
->height() / 2;
296 QHelpEvent
toolTipEvent(QEvent::ToolTip
, QPoint(0, 0), m_zoomSlider
->mapToGlobal(global
));
297 QApplication::sendEvent(m_zoomSlider
, &toolTipEvent
);
300 void DolphinStatusBar::updateProgressInfo()
302 if (m_progress
< 100) {
303 // Show the progress information and hide the extensions
304 m_stopButton
->show();
305 m_progressTextLabel
->show();
306 m_progressBar
->show();
307 setExtensionsVisible(false);
309 // Hide the progress information and show the extensions
310 m_stopButton
->hide();
311 m_progressTextLabel
->hide();
312 m_progressBar
->hide();
313 setExtensionsVisible(true);
317 void DolphinStatusBar::updateLabelText()
319 const QString text
= m_text
.isEmpty() ? m_defaultText
: m_text
;
321 QFontMetrics
fontMetrics(m_label
->font());
322 const QString elidedText
= fontMetrics
.elidedText(text
, Qt::ElideRight
, m_label
->width());
323 m_label
->setText(elidedText
);
324 m_label
->setToolTip(text
== elidedText
? QString() : text
);
327 void DolphinStatusBar::slotResetToDefaultText()
333 void DolphinStatusBar::setExtensionsVisible(bool visible
)
335 bool showSpaceInfo
= visible
;
336 bool showZoomSlider
= visible
;
338 showSpaceInfo
= GeneralSettings::showSpaceInfo();
339 showZoomSlider
= GeneralSettings::showZoomSlider();
341 m_spaceInfo
->setVisible(showSpaceInfo
);
342 m_zoomSlider
->setVisible(showZoomSlider
);
345 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel
)
347 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel
);
348 m_zoomSlider
->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size
));
351 void DolphinStatusBar::applyFixedWidgetSize(QWidget
* widget
, const QSize
& size
)
353 widget
->setMinimumSize(size
);
354 widget
->setMaximumSize(size
);
355 widget
->setSizePolicy(QSizePolicy::Fixed
, QSizePolicy::Fixed
);
358 #include "dolphinstatusbar.moc"