]>
cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/dolphinstatusbar.cpp
2 * SPDX-FileCopyrightText: 2006-2012 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "dolphinstatusbar.h"
9 #include "dolphin_generalsettings.h"
10 #include "statusbarspaceinfo.h"
11 #include "views/dolphinview.h"
12 #include "views/zoomlevelinfo.h"
14 #include <KLocalizedString>
15 #include <KSqueezedTextLabel>
17 #include <QApplication>
18 #include <QHBoxLayout>
22 #include <QProgressBar>
24 #include <QTextDocument>
26 #include <QToolButton>
29 const int UpdateDelay
= 50;
32 DolphinStatusBar::DolphinStatusBar(QWidget
* parent
) :
38 m_zoomSlider(nullptr),
39 m_progressBar(nullptr),
40 m_stopButton(nullptr),
42 m_showProgressBarTimer(nullptr),
43 m_delayUpdateTimer(nullptr),
46 // Initialize text label
47 m_label
= new KSqueezedTextLabel(m_text
, this);
48 m_label
->setWordWrap(true);
49 m_label
->setTextFormat(Qt::PlainText
);
51 // Initialize zoom widget
52 m_zoomSlider
= new QSlider(Qt::Horizontal
, this);
53 m_zoomSlider
->setAccessibleName(i18n("Zoom"));
54 m_zoomSlider
->setAccessibleDescription(i18nc("Description for zoom-slider (accessibility)", "Sets the size of the file icons."));
55 m_zoomSlider
->setPageStep(1);
56 m_zoomSlider
->setRange(ZoomLevelInfo::minimumLevel(), ZoomLevelInfo::maximumLevel());
58 connect(m_zoomSlider
, &QSlider::valueChanged
, this, &DolphinStatusBar::zoomLevelChanged
);
59 connect(m_zoomSlider
, &QSlider::valueChanged
, this, &DolphinStatusBar::updateZoomSliderToolTip
);
60 connect(m_zoomSlider
, &QSlider::sliderMoved
, this, &DolphinStatusBar::showZoomSliderToolTip
);
62 // Initialize space information
63 m_spaceInfo
= new StatusBarSpaceInfo(this);
65 // Initialize progress information
66 m_stopButton
= new QToolButton(this);
67 m_stopButton
->setIcon(QIcon::fromTheme(QStringLiteral("process-stop")));
68 m_stopButton
->setAccessibleName(i18n("Stop"));
69 m_stopButton
->setAutoRaise(true);
70 m_stopButton
->setToolTip(i18nc("@tooltip", "Stop loading"));
72 connect(m_stopButton
, &QToolButton::clicked
, this, &DolphinStatusBar::stopPressed
);
74 m_progressTextLabel
= new QLabel(this);
75 m_progressTextLabel
->hide();
77 m_progressBar
= new QProgressBar(this);
78 m_progressBar
->hide();
80 m_showProgressBarTimer
= new QTimer(this);
81 m_showProgressBarTimer
->setInterval(500);
82 m_showProgressBarTimer
->setSingleShot(true);
83 connect(m_showProgressBarTimer
, &QTimer::timeout
, this, &DolphinStatusBar::updateProgressInfo
);
85 // initialize text updater delay timer
86 m_delayUpdateTimer
= new QTimer(this);
87 m_delayUpdateTimer
->setInterval(UpdateDelay
);
88 m_delayUpdateTimer
->setSingleShot(true);
89 connect(m_delayUpdateTimer
, &QTimer::timeout
,
90 this, &DolphinStatusBar::updateLabelText
);
92 // Initialize top layout and size policies
93 const int fontHeight
= QFontMetrics(m_label
->font()).height();
94 const int zoomSliderHeight
= m_zoomSlider
->minimumSizeHint().height();
95 const int buttonHeight
= m_stopButton
->height();
96 const int contentHeight
= qMax(qMax(fontHeight
, zoomSliderHeight
), buttonHeight
);
98 QFontMetrics
fontMetrics(m_label
->font());
100 m_label
->setFixedHeight(contentHeight
);
101 m_label
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
103 m_zoomSlider
->setMaximumWidth(fontMetrics
.averageCharWidth() * 25);
105 m_spaceInfo
->setFixedHeight(contentHeight
);
106 m_spaceInfo
->setMaximumWidth(fontMetrics
.averageCharWidth() * 25);
107 m_spaceInfo
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
109 m_progressBar
->setFixedHeight(zoomSliderHeight
);
110 m_progressBar
->setMaximumWidth(fontMetrics
.averageCharWidth() * 25);
112 QHBoxLayout
* topLayout
= new QHBoxLayout(this);
113 topLayout
->setContentsMargins(2, 0, 2, 0);
114 topLayout
->setSpacing(4);
115 topLayout
->addWidget(m_label
, 1);
116 topLayout
->addWidget(m_zoomSlider
, 1);
117 topLayout
->addWidget(m_spaceInfo
, 1);
118 topLayout
->addWidget(m_stopButton
);
119 topLayout
->addWidget(m_progressTextLabel
);
120 topLayout
->addWidget(m_progressBar
);
122 setExtensionsVisible(true);
123 setWhatsThis(xi18nc("@info:whatsthis Statusbar", "<para>This is "
124 "the <emphasis>Statusbar</emphasis>. It contains three elements "
125 "by default (left to right):<list><item>A <emphasis>text field"
126 "</emphasis> that displays the size of selected items. If only "
127 "one item is selected the name and type is shown as well.</item>"
128 "<item>A <emphasis>zoom slider</emphasis> that allows you "
129 "to adjust the size of the icons in the view.</item>"
130 "<item><emphasis>Space information</emphasis> about the "
131 "current storage device.</item></list></para>"));
134 DolphinStatusBar::~DolphinStatusBar()
138 void DolphinStatusBar::setText(const QString
& text
)
140 if (m_text
== text
) {
144 m_textTimestamp
= QTime::currentTime();
147 // will update status bar text in 50ms
148 m_delayUpdateTimer
->start();
151 QString
DolphinStatusBar::text() const
156 void DolphinStatusBar::setProgressText(const QString
& text
)
158 m_progressTextLabel
->setText(text
);
161 QString
DolphinStatusBar::progressText() const
163 return m_progressTextLabel
->text();
166 void DolphinStatusBar::setProgress(int percent
)
168 // Show a busy indicator if a value < 0 is provided:
169 m_progressBar
->setMaximum((percent
< 0) ? 0 : 100);
171 percent
= qBound(0, percent
, 100);
172 const bool progressRestarted
= (percent
< 100) && (percent
< m_progress
);
173 m_progress
= percent
;
174 if (progressRestarted
&& !m_progressBar
->isVisible()) {
175 // Show the progress bar delayed: In the case if 100 % are reached within
176 // a short time, no progress bar will be shown at all.
177 m_showProgressBarTimer
->start();
180 m_progressBar
->setValue(m_progress
);
181 if (percent
== 100) {
182 // The end of the progress has been reached. Assure that the progress bar
183 // gets hidden and the extensions widgets get visible again.
184 m_showProgressBarTimer
->stop();
185 updateProgressInfo();
189 int DolphinStatusBar::progress() const
194 void DolphinStatusBar::resetToDefaultText()
199 if (currentTime
.msecsTo(m_textTimestamp
) < UpdateDelay
) {
200 m_delayUpdateTimer
->start();
206 void DolphinStatusBar::setDefaultText(const QString
& text
)
208 m_defaultText
= text
;
212 QString
DolphinStatusBar::defaultText() const
214 return m_defaultText
;
217 void DolphinStatusBar::setUrl(const QUrl
& url
)
219 if (GeneralSettings::showSpaceInfo()) {
220 m_spaceInfo
->setUrl(url
);
224 QUrl
DolphinStatusBar::url() const
226 return m_spaceInfo
->url();
229 void DolphinStatusBar::setZoomLevel(int zoomLevel
)
231 if (zoomLevel
!= m_zoomSlider
->value()) {
232 m_zoomSlider
->setValue(zoomLevel
);
236 int DolphinStatusBar::zoomLevel() const
238 return m_zoomSlider
->value();
241 void DolphinStatusBar::readSettings()
243 setExtensionsVisible(true);
246 void DolphinStatusBar::updateSpaceInfo()
248 m_spaceInfo
->update();
251 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent
* event
)
257 QAction
* showZoomSliderAction
= menu
.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
258 showZoomSliderAction
->setCheckable(true);
259 showZoomSliderAction
->setChecked(GeneralSettings::showZoomSlider());
261 QAction
* showSpaceInfoAction
= menu
.addAction(i18nc("@action:inmenu", "Show Space Information"));
262 showSpaceInfoAction
->setCheckable(true);
263 showSpaceInfoAction
->setChecked(GeneralSettings::showSpaceInfo());
265 const QAction
* action
= menu
.exec(QCursor::pos());
266 if (action
== showZoomSliderAction
) {
267 const bool visible
= showZoomSliderAction
->isChecked();
268 GeneralSettings::setShowZoomSlider(visible
);
269 m_zoomSlider
->setVisible(visible
);
270 } else if (action
== showSpaceInfoAction
) {
271 const bool visible
= showSpaceInfoAction
->isChecked();
272 GeneralSettings::setShowSpaceInfo(visible
);
273 m_spaceInfo
->setVisible(visible
);
277 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel
)
279 updateZoomSliderToolTip(zoomLevel
);
281 QPoint global
= m_zoomSlider
->rect().topLeft();
282 global
.ry() += m_zoomSlider
->height() / 2;
283 QHelpEvent
toolTipEvent(QEvent::ToolTip
, QPoint(0, 0), m_zoomSlider
->mapToGlobal(global
));
284 QApplication::sendEvent(m_zoomSlider
, &toolTipEvent
);
287 void DolphinStatusBar::updateProgressInfo()
289 if (m_progress
< 100) {
290 // Show the progress information and hide the extensions
291 m_stopButton
->show();
292 m_progressTextLabel
->show();
293 m_progressBar
->show();
294 setExtensionsVisible(false);
296 // Hide the progress information and show the extensions
297 m_stopButton
->hide();
298 m_progressTextLabel
->hide();
299 m_progressBar
->hide();
300 setExtensionsVisible(true);
304 void DolphinStatusBar::updateLabelText()
306 const QString text
= m_text
.isEmpty() ? m_defaultText
: m_text
;
307 m_label
->setText(text
);
310 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel
)
312 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel
);
313 m_zoomSlider
->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size
));
316 void DolphinStatusBar::setExtensionsVisible(bool visible
)
318 bool showSpaceInfo
= visible
;
319 bool showZoomSlider
= visible
;
321 showSpaceInfo
= GeneralSettings::showSpaceInfo();
322 showZoomSlider
= GeneralSettings::showZoomSlider();
324 m_spaceInfo
->setShown(showSpaceInfo
);
325 m_spaceInfo
->setVisible(showSpaceInfo
);
326 m_zoomSlider
->setVisible(showZoomSlider
);