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>
33 #include <QHBoxLayout>
35 #include <QProgressBar>
36 #include <QTextDocument>
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
->setTextFormat(Qt::PlainText
);
66 m_label
->installEventFilter(this);
68 // Initialize zoom widget
69 m_zoomSlider
= new QSlider(Qt::Horizontal
, this);
70 m_zoomSlider
->setAccessibleName(i18n("Zoom"));
71 m_zoomSlider
->setAccessibleDescription(i18nc("Description for zoom-slider (accessibility)", "Sets the size of the file icons."));
72 m_zoomSlider
->setPageStep(1);
73 m_zoomSlider
->setRange(ZoomLevelInfo::minimumLevel(), ZoomLevelInfo::maximumLevel());
75 connect(m_zoomSlider
, &QSlider::valueChanged
, this, &DolphinStatusBar::zoomLevelChanged
);
76 connect(m_zoomSlider
, &QSlider::valueChanged
, this, &DolphinStatusBar::updateZoomSliderToolTip
);
77 connect(m_zoomSlider
, &QSlider::sliderMoved
, this, &DolphinStatusBar::showZoomSliderToolTip
);
79 // Initialize space information
80 m_spaceInfo
= new StatusBarSpaceInfo(this);
82 // Initialize progress information
83 m_stopButton
= new QToolButton(this);
84 m_stopButton
->setIcon(KIcon("process-stop"));
85 m_stopButton
->setAccessibleName(i18n("Stop"));
86 m_stopButton
->setAutoRaise(true);
87 m_stopButton
->setToolTip(i18nc("@tooltip", "Stop loading"));
89 connect(m_stopButton
, &QToolButton::clicked
, this, &DolphinStatusBar::stopPressed
);
91 m_progressTextLabel
= new QLabel(this);
92 m_progressTextLabel
->hide();
94 m_progressBar
= new QProgressBar(this);
95 m_progressBar
->hide();
97 m_showProgressBarTimer
= new QTimer(this);
98 m_showProgressBarTimer
->setInterval(500);
99 m_showProgressBarTimer
->setSingleShot(true);
100 connect(m_showProgressBarTimer
, &QTimer::timeout
, this, &DolphinStatusBar::updateProgressInfo
);
102 m_resetToDefaultTextTimer
= new QTimer(this);
103 m_resetToDefaultTextTimer
->setInterval(ResetToDefaultTimeout
);
104 m_resetToDefaultTextTimer
->setSingleShot(true);
105 connect(m_resetToDefaultTextTimer
, &QTimer::timeout
, this, &DolphinStatusBar::slotResetToDefaultText
);
107 // Initialize top layout and size policies
108 const int fontHeight
= QFontMetrics(m_label
->font()).height();
109 const int zoomSliderHeight
= m_zoomSlider
->minimumSizeHint().height();
110 const int contentHeight
= qMax(fontHeight
, zoomSliderHeight
);
112 QFontMetrics
fontMetrics(m_label
->font());
114 m_label
->setFixedHeight(contentHeight
);
115 m_label
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
117 m_zoomSlider
->setMaximumWidth(fontMetrics
.averageCharWidth() * 15);
119 m_spaceInfo
->setFixedHeight(contentHeight
);
120 m_spaceInfo
->setMaximumWidth(fontMetrics
.averageCharWidth() * 15);
121 m_spaceInfo
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
123 m_progressBar
->setFixedHeight(contentHeight
);
124 m_progressBar
->setMaximumWidth(fontMetrics
.averageCharWidth() * 15);
126 QHBoxLayout
* topLayout
= new QHBoxLayout(this);
127 topLayout
->setMargin(0);
128 topLayout
->setSpacing(4);
129 topLayout
->addWidget(m_label
);
130 topLayout
->addWidget(m_zoomSlider
);
131 topLayout
->addWidget(m_spaceInfo
);
132 topLayout
->addWidget(m_stopButton
);
133 topLayout
->addWidget(m_progressTextLabel
);
134 topLayout
->addWidget(m_progressBar
);
136 setExtensionsVisible(true);
139 DolphinStatusBar::~DolphinStatusBar()
143 void DolphinStatusBar::setText(const QString
& text
)
145 if (m_text
== text
) {
149 m_textTimestamp
= QTime::currentTime();
151 if (text
.isEmpty()) {
152 // Assure that the previous set text won't get
153 // cleared immediatelly.
154 m_resetToDefaultTextTimer
->start();
158 if (m_resetToDefaultTextTimer
->isActive()) {
159 m_resetToDefaultTextTimer
->start();
166 QString
DolphinStatusBar::text() const
171 void DolphinStatusBar::setProgressText(const QString
& text
)
173 m_progressTextLabel
->setText(text
);
176 QString
DolphinStatusBar::progressText() const
178 return m_progressTextLabel
->text();
181 void DolphinStatusBar::setProgress(int percent
)
183 // Show a busy indicator if a value < 0 is provided:
184 m_progressBar
->setMaximum((percent
< 0) ? 0 : 100);
186 percent
= qBound(0, percent
, 100);
187 const bool progressRestarted
= (percent
< 100) && (percent
< m_progress
);
188 m_progress
= percent
;
189 if (progressRestarted
&& !m_progressBar
->isVisible()) {
190 // Show the progress bar delayed: In the case if 100 % are reached within
191 // a short time, no progress bar will be shown at all.
192 m_showProgressBarTimer
->start();
195 m_progressBar
->setValue(m_progress
);
196 if (percent
== 100) {
197 // The end of the progress has been reached. Assure that the progress bar
198 // gets hidden and the extensions widgets get visible again.
199 m_showProgressBarTimer
->stop();
200 updateProgressInfo();
204 int DolphinStatusBar::progress() const
209 void DolphinStatusBar::resetToDefaultText()
212 if (currentTime
.msecsTo(m_textTimestamp
) < ResetToDefaultTimeout
) {
213 m_resetToDefaultTextTimer
->start();
215 m_resetToDefaultTextTimer
->stop();
216 slotResetToDefaultText();
220 void DolphinStatusBar::setDefaultText(const QString
& text
)
222 m_defaultText
= text
;
226 QString
DolphinStatusBar::defaultText() const
228 return m_defaultText
;
231 void DolphinStatusBar::setUrl(const KUrl
& url
)
233 m_spaceInfo
->setUrl(url
);
236 KUrl
DolphinStatusBar::url() const
238 return m_spaceInfo
->url();
241 void DolphinStatusBar::setZoomLevel(int zoomLevel
)
243 if (zoomLevel
!= m_zoomSlider
->value()) {
244 m_zoomSlider
->setValue(zoomLevel
);
248 int DolphinStatusBar::zoomLevel() const
250 return m_zoomSlider
->value();
253 void DolphinStatusBar::readSettings()
255 setExtensionsVisible(true);
258 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent
* event
)
264 QAction
* showZoomSliderAction
= menu
.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
265 showZoomSliderAction
->setCheckable(true);
266 showZoomSliderAction
->setChecked(GeneralSettings::showZoomSlider());
268 QAction
* showSpaceInfoAction
= menu
.addAction(i18nc("@action:inmenu", "Show Space Information"));
269 showSpaceInfoAction
->setCheckable(true);
270 showSpaceInfoAction
->setChecked(GeneralSettings::showSpaceInfo());
272 const QAction
* action
= menu
.exec(QCursor::pos());
273 if (action
== showZoomSliderAction
) {
274 const bool visible
= showZoomSliderAction
->isChecked();
275 GeneralSettings::setShowZoomSlider(visible
);
276 m_zoomSlider
->setVisible(visible
);
277 } else if (action
== showSpaceInfoAction
) {
278 const bool visible
= showSpaceInfoAction
->isChecked();
279 GeneralSettings::setShowSpaceInfo(visible
);
280 m_spaceInfo
->setVisible(visible
);
284 bool DolphinStatusBar::eventFilter(QObject
* obj
, QEvent
* event
)
286 if (obj
== m_label
&& event
->type() == QEvent::Resize
) {
289 return QWidget::eventFilter(obj
, event
);
292 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel
)
294 updateZoomSliderToolTip(zoomLevel
);
296 QPoint global
= m_zoomSlider
->rect().topLeft();
297 global
.ry() += m_zoomSlider
->height() / 2;
298 QHelpEvent
toolTipEvent(QEvent::ToolTip
, QPoint(0, 0), m_zoomSlider
->mapToGlobal(global
));
299 QApplication::sendEvent(m_zoomSlider
, &toolTipEvent
);
302 void DolphinStatusBar::updateProgressInfo()
304 if (m_progress
< 100) {
305 // Show the progress information and hide the extensions
306 m_stopButton
->show();
307 m_progressTextLabel
->show();
308 m_progressBar
->show();
309 setExtensionsVisible(false);
311 // Hide the progress information and show the extensions
312 m_stopButton
->hide();
313 m_progressTextLabel
->hide();
314 m_progressBar
->hide();
315 setExtensionsVisible(true);
319 void DolphinStatusBar::updateLabelText()
321 const QString text
= m_text
.isEmpty() ? m_defaultText
: m_text
;
323 // Set status bar text and elide it if too long
324 QFontMetrics
fontMetrics(m_label
->font());
325 const QString elidedText
= fontMetrics
.elidedText(text
, Qt::ElideRight
, m_label
->width());
326 m_label
->setText(elidedText
);
328 // If the text has been elided, set the original text as tooltip
329 if (text
!= elidedText
) {
330 m_label
->setToolTip(Qt::convertFromPlainText(text
));
332 m_label
->setToolTip(QString());
336 void DolphinStatusBar::slotResetToDefaultText()
342 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel
)
344 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel
);
345 m_zoomSlider
->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size
));
348 void DolphinStatusBar::setExtensionsVisible(bool visible
)
350 bool showSpaceInfo
= visible
;
351 bool showZoomSlider
= visible
;
353 showSpaceInfo
= GeneralSettings::showSpaceInfo();
354 showZoomSlider
= GeneralSettings::showZoomSlider();
356 m_spaceInfo
->setVisible(showSpaceInfo
);
357 m_zoomSlider
->setVisible(showZoomSlider
);