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"
23 #include "statusbarspaceinfo.h"
24 #include "views/dolphinview.h"
25 #include "views/zoomlevelinfo.h"
27 #include <KLocalizedString>
28 #include <KSqueezedTextLabel>
30 #include <QApplication>
31 #include <QHBoxLayout>
35 #include <QProgressBar>
37 #include <QTextDocument>
39 #include <QToolButton>
42 const int ResetToDefaultTimeout
= 1000;
45 DolphinStatusBar::DolphinStatusBar(QWidget
* parent
) :
51 m_zoomSlider(nullptr),
52 m_progressBar(nullptr),
53 m_stopButton(nullptr),
55 m_showProgressBarTimer(nullptr),
56 m_resetToDefaultTextTimer(nullptr),
59 // Initialize text label
60 m_label
= new KSqueezedTextLabel(m_text
, this);
61 m_label
->setWordWrap(true);
62 m_label
->setTextFormat(Qt::PlainText
);
64 // Initialize zoom widget
65 m_zoomSlider
= new QSlider(Qt::Horizontal
, this);
66 m_zoomSlider
->setAccessibleName(i18n("Zoom"));
67 m_zoomSlider
->setAccessibleDescription(i18nc("Description for zoom-slider (accessibility)", "Sets the size of the file icons."));
68 m_zoomSlider
->setPageStep(1);
69 m_zoomSlider
->setRange(ZoomLevelInfo::minimumLevel(), ZoomLevelInfo::maximumLevel());
71 connect(m_zoomSlider
, &QSlider::valueChanged
, this, &DolphinStatusBar::zoomLevelChanged
);
72 connect(m_zoomSlider
, &QSlider::valueChanged
, this, &DolphinStatusBar::updateZoomSliderToolTip
);
73 connect(m_zoomSlider
, &QSlider::sliderMoved
, this, &DolphinStatusBar::showZoomSliderToolTip
);
75 // Initialize space information
76 m_spaceInfo
= new StatusBarSpaceInfo(this);
78 // Initialize progress information
79 m_stopButton
= new QToolButton(this);
80 m_stopButton
->setIcon(QIcon::fromTheme(QStringLiteral("process-stop")));
81 m_stopButton
->setAccessibleName(i18n("Stop"));
82 m_stopButton
->setAutoRaise(true);
83 m_stopButton
->setToolTip(i18nc("@tooltip", "Stop loading"));
85 connect(m_stopButton
, &QToolButton::clicked
, this, &DolphinStatusBar::stopPressed
);
87 m_progressTextLabel
= new QLabel(this);
88 m_progressTextLabel
->hide();
90 m_progressBar
= new QProgressBar(this);
91 m_progressBar
->hide();
93 m_showProgressBarTimer
= new QTimer(this);
94 m_showProgressBarTimer
->setInterval(500);
95 m_showProgressBarTimer
->setSingleShot(true);
96 connect(m_showProgressBarTimer
, &QTimer::timeout
, this, &DolphinStatusBar::updateProgressInfo
);
98 m_resetToDefaultTextTimer
= new QTimer(this);
99 m_resetToDefaultTextTimer
->setInterval(ResetToDefaultTimeout
);
100 m_resetToDefaultTextTimer
->setSingleShot(true);
101 connect(m_resetToDefaultTextTimer
, &QTimer::timeout
, this, &DolphinStatusBar::slotResetToDefaultText
);
103 // Initialize top layout and size policies
104 const int fontHeight
= QFontMetrics(m_label
->font()).height();
105 const int zoomSliderHeight
= m_zoomSlider
->minimumSizeHint().height();
106 const int buttonHeight
= m_stopButton
->height();
107 const int contentHeight
= qMax(qMax(fontHeight
, zoomSliderHeight
), buttonHeight
);
109 QFontMetrics
fontMetrics(m_label
->font());
111 m_label
->setFixedHeight(contentHeight
);
112 m_label
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
114 m_zoomSlider
->setMaximumWidth(fontMetrics
.averageCharWidth() * 25);
116 m_spaceInfo
->setFixedHeight(zoomSliderHeight
);
117 m_spaceInfo
->setMaximumWidth(fontMetrics
.averageCharWidth() * 25);
118 m_spaceInfo
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
120 m_progressBar
->setFixedHeight(zoomSliderHeight
);
121 m_progressBar
->setMaximumWidth(fontMetrics
.averageCharWidth() * 25);
123 QHBoxLayout
* topLayout
= new QHBoxLayout(this);
124 topLayout
->setContentsMargins(2, 0, 2, 0);
125 topLayout
->setSpacing(4);
126 topLayout
->addWidget(m_label
, 1);
127 topLayout
->addWidget(m_zoomSlider
, 1);
128 topLayout
->addWidget(m_spaceInfo
, 1);
129 topLayout
->addWidget(m_stopButton
);
130 topLayout
->addWidget(m_progressTextLabel
);
131 topLayout
->addWidget(m_progressBar
);
133 setExtensionsVisible(true);
136 DolphinStatusBar::~DolphinStatusBar()
140 void DolphinStatusBar::setText(const QString
& text
)
142 if (m_text
== text
) {
146 m_textTimestamp
= QTime::currentTime();
148 if (text
.isEmpty()) {
149 // Assure that the previous set text won't get
150 // cleared immediatelly.
151 m_resetToDefaultTextTimer
->start();
155 if (m_resetToDefaultTextTimer
->isActive()) {
156 m_resetToDefaultTextTimer
->start();
163 QString
DolphinStatusBar::text() const
168 void DolphinStatusBar::setProgressText(const QString
& text
)
170 m_progressTextLabel
->setText(text
);
173 QString
DolphinStatusBar::progressText() const
175 return m_progressTextLabel
->text();
178 void DolphinStatusBar::setProgress(int percent
)
180 // Show a busy indicator if a value < 0 is provided:
181 m_progressBar
->setMaximum((percent
< 0) ? 0 : 100);
183 percent
= qBound(0, percent
, 100);
184 const bool progressRestarted
= (percent
< 100) && (percent
< m_progress
);
185 m_progress
= percent
;
186 if (progressRestarted
&& !m_progressBar
->isVisible()) {
187 // Show the progress bar delayed: In the case if 100 % are reached within
188 // a short time, no progress bar will be shown at all.
189 m_showProgressBarTimer
->start();
192 m_progressBar
->setValue(m_progress
);
193 if (percent
== 100) {
194 // The end of the progress has been reached. Assure that the progress bar
195 // gets hidden and the extensions widgets get visible again.
196 m_showProgressBarTimer
->stop();
197 updateProgressInfo();
201 int DolphinStatusBar::progress() const
206 void DolphinStatusBar::resetToDefaultText()
209 if (currentTime
.msecsTo(m_textTimestamp
) < ResetToDefaultTimeout
) {
210 m_resetToDefaultTextTimer
->start();
212 m_resetToDefaultTextTimer
->stop();
213 slotResetToDefaultText();
217 void DolphinStatusBar::setDefaultText(const QString
& text
)
219 m_defaultText
= text
;
223 QString
DolphinStatusBar::defaultText() const
225 return m_defaultText
;
228 void DolphinStatusBar::setUrl(const QUrl
& url
)
230 m_spaceInfo
->setUrl(url
);
233 QUrl
DolphinStatusBar::url() const
235 return m_spaceInfo
->url();
238 void DolphinStatusBar::setZoomLevel(int zoomLevel
)
240 if (zoomLevel
!= m_zoomSlider
->value()) {
241 m_zoomSlider
->setValue(zoomLevel
);
245 int DolphinStatusBar::zoomLevel() const
247 return m_zoomSlider
->value();
250 void DolphinStatusBar::readSettings()
252 setExtensionsVisible(true);
255 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent
* event
)
261 QAction
* showZoomSliderAction
= menu
.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
262 showZoomSliderAction
->setCheckable(true);
263 showZoomSliderAction
->setChecked(GeneralSettings::showZoomSlider());
265 QAction
* showSpaceInfoAction
= menu
.addAction(i18nc("@action:inmenu", "Show Space Information"));
266 showSpaceInfoAction
->setCheckable(true);
267 showSpaceInfoAction
->setChecked(GeneralSettings::showSpaceInfo());
269 const QAction
* action
= menu
.exec(QCursor::pos());
270 if (action
== showZoomSliderAction
) {
271 const bool visible
= showZoomSliderAction
->isChecked();
272 GeneralSettings::setShowZoomSlider(visible
);
273 m_zoomSlider
->setVisible(visible
);
274 } else if (action
== showSpaceInfoAction
) {
275 const bool visible
= showSpaceInfoAction
->isChecked();
276 GeneralSettings::setShowSpaceInfo(visible
);
277 m_spaceInfo
->setVisible(visible
);
281 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel
)
283 updateZoomSliderToolTip(zoomLevel
);
285 QPoint global
= m_zoomSlider
->rect().topLeft();
286 global
.ry() += m_zoomSlider
->height() / 2;
287 QHelpEvent
toolTipEvent(QEvent::ToolTip
, QPoint(0, 0), m_zoomSlider
->mapToGlobal(global
));
288 QApplication::sendEvent(m_zoomSlider
, &toolTipEvent
);
291 void DolphinStatusBar::updateProgressInfo()
293 if (m_progress
< 100) {
294 // Show the progress information and hide the extensions
295 m_stopButton
->show();
296 m_progressTextLabel
->show();
297 m_progressBar
->show();
298 setExtensionsVisible(false);
300 // Hide the progress information and show the extensions
301 m_stopButton
->hide();
302 m_progressTextLabel
->hide();
303 m_progressBar
->hide();
304 setExtensionsVisible(true);
308 void DolphinStatusBar::updateLabelText()
310 const QString text
= m_text
.isEmpty() ? m_defaultText
: m_text
;
311 m_label
->setText(text
);
314 void DolphinStatusBar::slotResetToDefaultText()
320 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel
)
322 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel
);
323 m_zoomSlider
->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size
));
326 void DolphinStatusBar::setExtensionsVisible(bool visible
)
328 bool showSpaceInfo
= visible
;
329 bool showZoomSlider
= visible
;
331 showSpaceInfo
= GeneralSettings::showSpaceInfo();
332 showZoomSlider
= GeneralSettings::showZoomSlider();
334 m_spaceInfo
->setVisible(showSpaceInfo
);
335 m_zoomSlider
->setVisible(showZoomSlider
);