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);
134 setWhatsThis(xi18nc("@info:whatsthis Statusbar", "<para>This is "
135 "the <emphasis>Statusbar</emphasis>. It contains three elements "
136 "by default (left to right):<list><item>A <emphasis>text field"
137 "</emphasis> that displays the size of selected items. If only "
138 "one item is selected the name and type is shown as well.</item>"
139 "<item>A <emphasis>zoom slider</emphasis> that allows you "
140 "to adjust the size of the icons in the view.</item>"
141 "<item><emphasis>Space information</emphasis> about the "
142 "current storage device.</item></list></para>"));
145 DolphinStatusBar::~DolphinStatusBar()
149 void DolphinStatusBar::setText(const QString
& text
)
151 if (m_text
== text
) {
155 m_textTimestamp
= QTime::currentTime();
157 if (text
.isEmpty()) {
158 // Assure that the previous set text won't get
159 // cleared immediately.
160 m_resetToDefaultTextTimer
->start();
164 if (m_resetToDefaultTextTimer
->isActive()) {
165 m_resetToDefaultTextTimer
->start();
172 QString
DolphinStatusBar::text() const
177 void DolphinStatusBar::setProgressText(const QString
& text
)
179 m_progressTextLabel
->setText(text
);
182 QString
DolphinStatusBar::progressText() const
184 return m_progressTextLabel
->text();
187 void DolphinStatusBar::setProgress(int percent
)
189 // Show a busy indicator if a value < 0 is provided:
190 m_progressBar
->setMaximum((percent
< 0) ? 0 : 100);
192 percent
= qBound(0, percent
, 100);
193 const bool progressRestarted
= (percent
< 100) && (percent
< m_progress
);
194 m_progress
= percent
;
195 if (progressRestarted
&& !m_progressBar
->isVisible()) {
196 // Show the progress bar delayed: In the case if 100 % are reached within
197 // a short time, no progress bar will be shown at all.
198 m_showProgressBarTimer
->start();
201 m_progressBar
->setValue(m_progress
);
202 if (percent
== 100) {
203 // The end of the progress has been reached. Assure that the progress bar
204 // gets hidden and the extensions widgets get visible again.
205 m_showProgressBarTimer
->stop();
206 updateProgressInfo();
210 int DolphinStatusBar::progress() const
215 void DolphinStatusBar::resetToDefaultText()
218 if (currentTime
.msecsTo(m_textTimestamp
) < ResetToDefaultTimeout
) {
219 m_resetToDefaultTextTimer
->start();
221 m_resetToDefaultTextTimer
->stop();
222 slotResetToDefaultText();
226 void DolphinStatusBar::setDefaultText(const QString
& text
)
228 m_defaultText
= text
;
232 QString
DolphinStatusBar::defaultText() const
234 return m_defaultText
;
237 void DolphinStatusBar::setUrl(const QUrl
& url
)
239 m_spaceInfo
->setUrl(url
);
242 QUrl
DolphinStatusBar::url() const
244 return m_spaceInfo
->url();
247 void DolphinStatusBar::setZoomLevel(int zoomLevel
)
249 if (zoomLevel
!= m_zoomSlider
->value()) {
250 m_zoomSlider
->setValue(zoomLevel
);
254 int DolphinStatusBar::zoomLevel() const
256 return m_zoomSlider
->value();
259 void DolphinStatusBar::readSettings()
261 setExtensionsVisible(true);
264 void DolphinStatusBar::updateSpaceInfo()
266 m_spaceInfo
->update();
269 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent
* event
)
275 QAction
* showZoomSliderAction
= menu
.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
276 showZoomSliderAction
->setCheckable(true);
277 showZoomSliderAction
->setChecked(GeneralSettings::showZoomSlider());
279 QAction
* showSpaceInfoAction
= menu
.addAction(i18nc("@action:inmenu", "Show Space Information"));
280 showSpaceInfoAction
->setCheckable(true);
281 showSpaceInfoAction
->setChecked(GeneralSettings::showSpaceInfo());
283 const QAction
* action
= menu
.exec(QCursor::pos());
284 if (action
== showZoomSliderAction
) {
285 const bool visible
= showZoomSliderAction
->isChecked();
286 GeneralSettings::setShowZoomSlider(visible
);
287 m_zoomSlider
->setVisible(visible
);
288 } else if (action
== showSpaceInfoAction
) {
289 const bool visible
= showSpaceInfoAction
->isChecked();
290 GeneralSettings::setShowSpaceInfo(visible
);
291 m_spaceInfo
->setVisible(visible
);
295 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel
)
297 updateZoomSliderToolTip(zoomLevel
);
299 QPoint global
= m_zoomSlider
->rect().topLeft();
300 global
.ry() += m_zoomSlider
->height() / 2;
301 QHelpEvent
toolTipEvent(QEvent::ToolTip
, QPoint(0, 0), m_zoomSlider
->mapToGlobal(global
));
302 QApplication::sendEvent(m_zoomSlider
, &toolTipEvent
);
305 void DolphinStatusBar::updateProgressInfo()
307 if (m_progress
< 100) {
308 // Show the progress information and hide the extensions
309 m_stopButton
->show();
310 m_progressTextLabel
->show();
311 m_progressBar
->show();
312 setExtensionsVisible(false);
314 // Hide the progress information and show the extensions
315 m_stopButton
->hide();
316 m_progressTextLabel
->hide();
317 m_progressBar
->hide();
318 setExtensionsVisible(true);
322 void DolphinStatusBar::updateLabelText()
324 const QString text
= m_text
.isEmpty() ? m_defaultText
: m_text
;
325 m_label
->setText(text
);
328 void DolphinStatusBar::slotResetToDefaultText()
334 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel
)
336 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel
);
337 m_zoomSlider
->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size
));
340 void DolphinStatusBar::setExtensionsVisible(bool visible
)
342 bool showSpaceInfo
= visible
;
343 bool showZoomSlider
= visible
;
345 showSpaceInfo
= GeneralSettings::showSpaceInfo();
346 showZoomSlider
= GeneralSettings::showZoomSlider();
348 m_spaceInfo
->setVisible(showSpaceInfo
);
349 m_zoomSlider
->setVisible(showZoomSlider
);