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>
26 #include <KSqueezedTextLabel>
29 #include "statusbarspaceinfo.h"
31 #include <QApplication>
32 #include <QHBoxLayout>
33 #include <QProgressBar>
34 #include <QToolButton>
37 #include <QTextDocument>
40 #include <views/dolphinview.h>
41 #include <views/zoomlevelinfo.h>
44 const int ResetToDefaultTimeout
= 1000;
47 DolphinStatusBar::DolphinStatusBar(QWidget
* parent
) :
53 m_zoomSlider(nullptr),
54 m_progressBar(nullptr),
55 m_stopButton(nullptr),
57 m_showProgressBarTimer(nullptr),
58 m_resetToDefaultTextTimer(nullptr),
61 // Initialize text label
62 m_label
= new KSqueezedTextLabel(m_text
, this);
63 m_label
->setWordWrap(true);
64 m_label
->setTextFormat(Qt::PlainText
);
66 // Initialize zoom widget
67 m_zoomSlider
= new QSlider(Qt::Horizontal
, this);
68 m_zoomSlider
->setAccessibleName(i18n("Zoom"));
69 m_zoomSlider
->setAccessibleDescription(i18nc("Description for zoom-slider (accessibility)", "Sets the size of the file icons."));
70 m_zoomSlider
->setPageStep(1);
71 m_zoomSlider
->setRange(ZoomLevelInfo::minimumLevel(), ZoomLevelInfo::maximumLevel());
73 connect(m_zoomSlider
, &QSlider::valueChanged
, this, &DolphinStatusBar::zoomLevelChanged
);
74 connect(m_zoomSlider
, &QSlider::valueChanged
, this, &DolphinStatusBar::updateZoomSliderToolTip
);
75 connect(m_zoomSlider
, &QSlider::sliderMoved
, this, &DolphinStatusBar::showZoomSliderToolTip
);
77 // Initialize space information
78 m_spaceInfo
= new StatusBarSpaceInfo(this);
80 // Initialize progress information
81 m_stopButton
= new QToolButton(this);
82 m_stopButton
->setIcon(QIcon::fromTheme(QStringLiteral("process-stop")));
83 m_stopButton
->setAccessibleName(i18n("Stop"));
84 m_stopButton
->setAutoRaise(true);
85 m_stopButton
->setToolTip(i18nc("@tooltip", "Stop loading"));
87 connect(m_stopButton
, &QToolButton::clicked
, this, &DolphinStatusBar::stopPressed
);
89 m_progressTextLabel
= new QLabel(this);
90 m_progressTextLabel
->hide();
92 m_progressBar
= new QProgressBar(this);
93 m_progressBar
->hide();
95 m_showProgressBarTimer
= new QTimer(this);
96 m_showProgressBarTimer
->setInterval(500);
97 m_showProgressBarTimer
->setSingleShot(true);
98 connect(m_showProgressBarTimer
, &QTimer::timeout
, this, &DolphinStatusBar::updateProgressInfo
);
100 m_resetToDefaultTextTimer
= new QTimer(this);
101 m_resetToDefaultTextTimer
->setInterval(ResetToDefaultTimeout
);
102 m_resetToDefaultTextTimer
->setSingleShot(true);
103 connect(m_resetToDefaultTextTimer
, &QTimer::timeout
, this, &DolphinStatusBar::slotResetToDefaultText
);
105 // Initialize top layout and size policies
106 const int fontHeight
= QFontMetrics(m_label
->font()).height();
107 const int zoomSliderHeight
= m_zoomSlider
->minimumSizeHint().height();
108 const int buttonHeight
= m_stopButton
->height();
109 const int contentHeight
= qMax(qMax(fontHeight
, zoomSliderHeight
), buttonHeight
);
111 QFontMetrics
fontMetrics(m_label
->font());
113 m_label
->setFixedHeight(contentHeight
);
114 m_label
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
116 m_zoomSlider
->setMaximumWidth(fontMetrics
.averageCharWidth() * 25);
118 m_spaceInfo
->setFixedHeight(zoomSliderHeight
);
119 m_spaceInfo
->setMaximumWidth(fontMetrics
.averageCharWidth() * 25);
120 m_spaceInfo
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
122 m_progressBar
->setFixedHeight(zoomSliderHeight
);
123 m_progressBar
->setMaximumWidth(fontMetrics
.averageCharWidth() * 25);
125 QHBoxLayout
* topLayout
= new QHBoxLayout(this);
126 topLayout
->setContentsMargins(2, 0, 2, 0);
127 topLayout
->setSpacing(4);
128 topLayout
->addWidget(m_label
, 1);
129 topLayout
->addWidget(m_zoomSlider
, 1);
130 topLayout
->addWidget(m_spaceInfo
, 1);
131 topLayout
->addWidget(m_stopButton
);
132 topLayout
->addWidget(m_progressTextLabel
);
133 topLayout
->addWidget(m_progressBar
);
135 setExtensionsVisible(true);
138 DolphinStatusBar::~DolphinStatusBar()
142 void DolphinStatusBar::setText(const QString
& text
)
144 if (m_text
== text
) {
148 m_textTimestamp
= QTime::currentTime();
150 if (text
.isEmpty()) {
151 // Assure that the previous set text won't get
152 // cleared immediatelly.
153 m_resetToDefaultTextTimer
->start();
157 if (m_resetToDefaultTextTimer
->isActive()) {
158 m_resetToDefaultTextTimer
->start();
165 QString
DolphinStatusBar::text() const
170 void DolphinStatusBar::setProgressText(const QString
& text
)
172 m_progressTextLabel
->setText(text
);
175 QString
DolphinStatusBar::progressText() const
177 return m_progressTextLabel
->text();
180 void DolphinStatusBar::setProgress(int percent
)
182 // Show a busy indicator if a value < 0 is provided:
183 m_progressBar
->setMaximum((percent
< 0) ? 0 : 100);
185 percent
= qBound(0, percent
, 100);
186 const bool progressRestarted
= (percent
< 100) && (percent
< m_progress
);
187 m_progress
= percent
;
188 if (progressRestarted
&& !m_progressBar
->isVisible()) {
189 // Show the progress bar delayed: In the case if 100 % are reached within
190 // a short time, no progress bar will be shown at all.
191 m_showProgressBarTimer
->start();
194 m_progressBar
->setValue(m_progress
);
195 if (percent
== 100) {
196 // The end of the progress has been reached. Assure that the progress bar
197 // gets hidden and the extensions widgets get visible again.
198 m_showProgressBarTimer
->stop();
199 updateProgressInfo();
203 int DolphinStatusBar::progress() const
208 void DolphinStatusBar::resetToDefaultText()
211 if (currentTime
.msecsTo(m_textTimestamp
) < ResetToDefaultTimeout
) {
212 m_resetToDefaultTextTimer
->start();
214 m_resetToDefaultTextTimer
->stop();
215 slotResetToDefaultText();
219 void DolphinStatusBar::setDefaultText(const QString
& text
)
221 m_defaultText
= text
;
225 QString
DolphinStatusBar::defaultText() const
227 return m_defaultText
;
230 void DolphinStatusBar::setUrl(const QUrl
& url
)
232 m_spaceInfo
->setUrl(url
);
235 QUrl
DolphinStatusBar::url() const
237 return m_spaceInfo
->url();
240 void DolphinStatusBar::setZoomLevel(int zoomLevel
)
242 if (zoomLevel
!= m_zoomSlider
->value()) {
243 m_zoomSlider
->setValue(zoomLevel
);
247 int DolphinStatusBar::zoomLevel() const
249 return m_zoomSlider
->value();
252 void DolphinStatusBar::readSettings()
254 setExtensionsVisible(true);
257 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent
* event
)
263 QAction
* showZoomSliderAction
= menu
.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
264 showZoomSliderAction
->setCheckable(true);
265 showZoomSliderAction
->setChecked(GeneralSettings::showZoomSlider());
267 QAction
* showSpaceInfoAction
= menu
.addAction(i18nc("@action:inmenu", "Show Space Information"));
268 showSpaceInfoAction
->setCheckable(true);
269 showSpaceInfoAction
->setChecked(GeneralSettings::showSpaceInfo());
271 const QAction
* action
= menu
.exec(QCursor::pos());
272 if (action
== showZoomSliderAction
) {
273 const bool visible
= showZoomSliderAction
->isChecked();
274 GeneralSettings::setShowZoomSlider(visible
);
275 m_zoomSlider
->setVisible(visible
);
276 } else if (action
== showSpaceInfoAction
) {
277 const bool visible
= showSpaceInfoAction
->isChecked();
278 GeneralSettings::setShowSpaceInfo(visible
);
279 m_spaceInfo
->setVisible(visible
);
283 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel
)
285 updateZoomSliderToolTip(zoomLevel
);
287 QPoint global
= m_zoomSlider
->rect().topLeft();
288 global
.ry() += m_zoomSlider
->height() / 2;
289 QHelpEvent
toolTipEvent(QEvent::ToolTip
, QPoint(0, 0), m_zoomSlider
->mapToGlobal(global
));
290 QApplication::sendEvent(m_zoomSlider
, &toolTipEvent
);
293 void DolphinStatusBar::updateProgressInfo()
295 if (m_progress
< 100) {
296 // Show the progress information and hide the extensions
297 m_stopButton
->show();
298 m_progressTextLabel
->show();
299 m_progressBar
->show();
300 setExtensionsVisible(false);
302 // Hide the progress information and show the extensions
303 m_stopButton
->hide();
304 m_progressTextLabel
->hide();
305 m_progressBar
->hide();
306 setExtensionsVisible(true);
310 void DolphinStatusBar::updateLabelText()
312 const QString text
= m_text
.isEmpty() ? m_defaultText
: m_text
;
313 m_label
->setText(text
);
316 void DolphinStatusBar::slotResetToDefaultText()
322 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel
)
324 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel
);
325 m_zoomSlider
->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size
));
328 void DolphinStatusBar::setExtensionsVisible(bool visible
)
330 bool showSpaceInfo
= visible
;
331 bool showZoomSlider
= visible
;
333 showSpaceInfo
= GeneralSettings::showSpaceInfo();
334 showZoomSlider
= GeneralSettings::showZoomSlider();
336 m_spaceInfo
->setVisible(showSpaceInfo
);
337 m_zoomSlider
->setVisible(showZoomSlider
);