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"
25 #include <KLocalizedString>
28 #include "statusbarspaceinfo.h"
30 #include <QApplication>
31 #include <QHBoxLayout>
33 #include <QProgressBar>
34 #include <QToolButton>
38 #include <QTextDocument>
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(QIcon::fromTheme(QStringLiteral("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 buttonHeight
= m_stopButton
->height();
111 const int contentHeight
= qMax(qMax(fontHeight
, zoomSliderHeight
), buttonHeight
);
113 QFontMetrics
fontMetrics(m_label
->font());
115 m_label
->setFixedHeight(contentHeight
);
116 m_label
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
118 m_zoomSlider
->setMaximumWidth(fontMetrics
.averageCharWidth() * 25);
120 m_spaceInfo
->setFixedHeight(zoomSliderHeight
);
121 m_spaceInfo
->setMaximumWidth(fontMetrics
.averageCharWidth() * 25);
122 m_spaceInfo
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
124 m_progressBar
->setFixedHeight(zoomSliderHeight
);
125 m_progressBar
->setMaximumWidth(fontMetrics
.averageCharWidth() * 25);
127 QHBoxLayout
* topLayout
= new QHBoxLayout(this);
128 topLayout
->setContentsMargins(2, 0, 2, 0);
129 topLayout
->setSpacing(4);
130 topLayout
->addWidget(m_label
);
131 topLayout
->addWidget(m_zoomSlider
);
132 topLayout
->addWidget(m_spaceInfo
);
133 topLayout
->addWidget(m_stopButton
);
134 topLayout
->addWidget(m_progressTextLabel
);
135 topLayout
->addWidget(m_progressBar
);
137 setExtensionsVisible(true);
140 DolphinStatusBar::~DolphinStatusBar()
144 void DolphinStatusBar::setText(const QString
& text
)
146 if (m_text
== text
) {
150 m_textTimestamp
= QTime::currentTime();
152 if (text
.isEmpty()) {
153 // Assure that the previous set text won't get
154 // cleared immediatelly.
155 m_resetToDefaultTextTimer
->start();
159 if (m_resetToDefaultTextTimer
->isActive()) {
160 m_resetToDefaultTextTimer
->start();
167 QString
DolphinStatusBar::text() const
172 void DolphinStatusBar::setProgressText(const QString
& text
)
174 m_progressTextLabel
->setText(text
);
177 QString
DolphinStatusBar::progressText() const
179 return m_progressTextLabel
->text();
182 void DolphinStatusBar::setProgress(int percent
)
184 // Show a busy indicator if a value < 0 is provided:
185 m_progressBar
->setMaximum((percent
< 0) ? 0 : 100);
187 percent
= qBound(0, percent
, 100);
188 const bool progressRestarted
= (percent
< 100) && (percent
< m_progress
);
189 m_progress
= percent
;
190 if (progressRestarted
&& !m_progressBar
->isVisible()) {
191 // Show the progress bar delayed: In the case if 100 % are reached within
192 // a short time, no progress bar will be shown at all.
193 m_showProgressBarTimer
->start();
196 m_progressBar
->setValue(m_progress
);
197 if (percent
== 100) {
198 // The end of the progress has been reached. Assure that the progress bar
199 // gets hidden and the extensions widgets get visible again.
200 m_showProgressBarTimer
->stop();
201 updateProgressInfo();
205 int DolphinStatusBar::progress() const
210 void DolphinStatusBar::resetToDefaultText()
213 if (currentTime
.msecsTo(m_textTimestamp
) < ResetToDefaultTimeout
) {
214 m_resetToDefaultTextTimer
->start();
216 m_resetToDefaultTextTimer
->stop();
217 slotResetToDefaultText();
221 void DolphinStatusBar::setDefaultText(const QString
& text
)
223 m_defaultText
= text
;
227 QString
DolphinStatusBar::defaultText() const
229 return m_defaultText
;
232 void DolphinStatusBar::setUrl(const QUrl
& url
)
234 m_spaceInfo
->setUrl(url
);
237 QUrl
DolphinStatusBar::url() const
239 return m_spaceInfo
->url();
242 void DolphinStatusBar::setZoomLevel(int zoomLevel
)
244 if (zoomLevel
!= m_zoomSlider
->value()) {
245 m_zoomSlider
->setValue(zoomLevel
);
249 int DolphinStatusBar::zoomLevel() const
251 return m_zoomSlider
->value();
254 void DolphinStatusBar::readSettings()
256 setExtensionsVisible(true);
259 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent
* event
)
265 QAction
* showZoomSliderAction
= menu
.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
266 showZoomSliderAction
->setCheckable(true);
267 showZoomSliderAction
->setChecked(GeneralSettings::showZoomSlider());
269 QAction
* showSpaceInfoAction
= menu
.addAction(i18nc("@action:inmenu", "Show Space Information"));
270 showSpaceInfoAction
->setCheckable(true);
271 showSpaceInfoAction
->setChecked(GeneralSettings::showSpaceInfo());
273 const QAction
* action
= menu
.exec(QCursor::pos());
274 if (action
== showZoomSliderAction
) {
275 const bool visible
= showZoomSliderAction
->isChecked();
276 GeneralSettings::setShowZoomSlider(visible
);
277 m_zoomSlider
->setVisible(visible
);
278 } else if (action
== showSpaceInfoAction
) {
279 const bool visible
= showSpaceInfoAction
->isChecked();
280 GeneralSettings::setShowSpaceInfo(visible
);
281 m_spaceInfo
->setVisible(visible
);
285 bool DolphinStatusBar::eventFilter(QObject
* obj
, QEvent
* event
)
287 if (obj
== m_label
&& event
->type() == QEvent::Resize
) {
290 return QWidget::eventFilter(obj
, event
);
293 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel
)
295 updateZoomSliderToolTip(zoomLevel
);
297 QPoint global
= m_zoomSlider
->rect().topLeft();
298 global
.ry() += m_zoomSlider
->height() / 2;
299 QHelpEvent
toolTipEvent(QEvent::ToolTip
, QPoint(0, 0), m_zoomSlider
->mapToGlobal(global
));
300 QApplication::sendEvent(m_zoomSlider
, &toolTipEvent
);
303 void DolphinStatusBar::updateProgressInfo()
305 if (m_progress
< 100) {
306 // Show the progress information and hide the extensions
307 m_stopButton
->show();
308 m_progressTextLabel
->show();
309 m_progressBar
->show();
310 setExtensionsVisible(false);
312 // Hide the progress information and show the extensions
313 m_stopButton
->hide();
314 m_progressTextLabel
->hide();
315 m_progressBar
->hide();
316 setExtensionsVisible(true);
320 void DolphinStatusBar::updateLabelText()
322 const QString text
= m_text
.isEmpty() ? m_defaultText
: m_text
;
324 // Set status bar text and elide it if too long
325 QFontMetrics
fontMetrics(m_label
->font());
326 const QString elidedText
= fontMetrics
.elidedText(text
, Qt::ElideRight
, m_label
->width());
327 m_label
->setText(elidedText
);
329 // If the text has been elided, set the original text as tooltip
330 if (text
!= elidedText
) {
331 m_label
->setToolTip(Qt::convertFromPlainText(text
));
333 m_label
->setToolTip(QString());
337 void DolphinStatusBar::slotResetToDefaultText()
343 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel
)
345 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel
);
346 m_zoomSlider
->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size
));
349 void DolphinStatusBar::setExtensionsVisible(bool visible
)
351 bool showSpaceInfo
= visible
;
352 bool showZoomSlider
= visible
;
354 showSpaceInfo
= GeneralSettings::showSpaceInfo();
355 showZoomSlider
= GeneralSettings::showZoomSlider();
357 m_spaceInfo
->setVisible(showSpaceInfo
);
358 m_zoomSlider
->setVisible(showZoomSlider
);