]>
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>
23 #include <QProgressBar>
25 #include <QStyleOption>
27 #include <QToolButton>
31 const int UpdateDelay
= 50;
34 DolphinStatusBar::DolphinStatusBar(QWidget
*parent
)
39 , m_zoomLabel(nullptr)
40 , m_spaceInfo(nullptr)
41 , m_zoomSlider(nullptr)
42 , m_progressBar(nullptr)
43 , m_stopButton(nullptr)
45 , m_showProgressBarTimer(nullptr)
46 , m_delayUpdateTimer(nullptr)
49 setContentsMargins(4, 0, 4, 0);
51 // Initialize text label
52 m_label
= new KSqueezedTextLabel(m_text
, this);
53 m_label
->setWordWrap(true);
54 m_label
->setTextFormat(Qt::PlainText
);
56 // Initialize zoom slider's explanatory label
57 m_zoomLabel
= new QLabel(i18nc("Used as a noun, i.e. 'Here is the zoom level:'", "Zoom:"), this);
59 // Initialize zoom widget
60 m_zoomSlider
= new QSlider(Qt::Horizontal
, this);
61 m_zoomSlider
->setAccessibleName(i18n("Zoom"));
62 m_zoomSlider
->setAccessibleDescription(i18nc("Description for zoom-slider (accessibility)", "Sets the size of the file icons."));
63 m_zoomSlider
->setPageStep(1);
64 m_zoomSlider
->setRange(ZoomLevelInfo::minimumLevel(), ZoomLevelInfo::maximumLevel());
66 connect(m_zoomSlider
, &QSlider::valueChanged
, this, &DolphinStatusBar::zoomLevelChanged
);
67 connect(m_zoomSlider
, &QSlider::valueChanged
, this, &DolphinStatusBar::updateZoomSliderToolTip
);
68 connect(m_zoomSlider
, &QSlider::sliderMoved
, this, &DolphinStatusBar::showZoomSliderToolTip
);
70 // Initialize space information
71 m_spaceInfo
= new StatusBarSpaceInfo(this);
73 // Initialize progress information
74 m_stopButton
= new QToolButton(this);
75 m_stopButton
->setIcon(QIcon::fromTheme(QStringLiteral("process-stop")));
76 m_stopButton
->setAccessibleName(i18n("Stop"));
77 m_stopButton
->setAutoRaise(true);
78 m_stopButton
->setToolTip(i18nc("@tooltip", "Stop loading"));
80 connect(m_stopButton
, &QToolButton::clicked
, this, &DolphinStatusBar::stopPressed
);
82 m_progressTextLabel
= new QLabel(this);
83 m_progressTextLabel
->hide();
85 m_progressBar
= new QProgressBar(this);
86 m_progressBar
->hide();
88 m_showProgressBarTimer
= new QTimer(this);
89 m_showProgressBarTimer
->setInterval(500);
90 m_showProgressBarTimer
->setSingleShot(true);
91 connect(m_showProgressBarTimer
, &QTimer::timeout
, this, &DolphinStatusBar::updateProgressInfo
);
93 // initialize text updater delay timer
94 m_delayUpdateTimer
= new QTimer(this);
95 m_delayUpdateTimer
->setInterval(UpdateDelay
);
96 m_delayUpdateTimer
->setSingleShot(true);
97 connect(m_delayUpdateTimer
, &QTimer::timeout
, this, &DolphinStatusBar::updateLabelText
);
99 // Initialize top layout and size policies
100 const int fontHeight
= QFontMetrics(m_label
->font()).height();
101 const int zoomSliderHeight
= m_zoomSlider
->minimumSizeHint().height();
102 const int buttonHeight
= m_stopButton
->height();
103 const int contentHeight
= qMax(qMax(fontHeight
, zoomSliderHeight
), buttonHeight
);
105 QFontMetrics
fontMetrics(m_label
->font());
107 m_label
->setFixedHeight(contentHeight
);
108 m_label
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
110 m_zoomSlider
->setMaximumWidth(fontMetrics
.averageCharWidth() * 15);
112 m_spaceInfo
->setFixedHeight(contentHeight
);
113 m_spaceInfo
->setMaximumWidth(fontMetrics
.averageCharWidth() * 25);
114 m_spaceInfo
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
116 m_progressBar
->setFixedHeight(zoomSliderHeight
);
117 m_progressBar
->setMaximumWidth(fontMetrics
.averageCharWidth() * 20);
119 QHBoxLayout
*topLayout
= new QHBoxLayout(this);
120 topLayout
->setContentsMargins(2, 0, 2, 0);
121 topLayout
->setSpacing(4);
122 topLayout
->addWidget(m_label
, 1);
123 topLayout
->addWidget(m_zoomLabel
);
124 topLayout
->addWidget(m_zoomSlider
, 1);
125 topLayout
->addWidget(m_spaceInfo
, 1);
126 topLayout
->addWidget(m_stopButton
);
127 topLayout
->addWidget(m_progressTextLabel
);
128 topLayout
->addWidget(m_progressBar
);
130 setVisible(GeneralSettings::showStatusBar());
131 setExtensionsVisible(true);
132 setWhatsThis(xi18nc("@info:whatsthis Statusbar",
134 "the <emphasis>Statusbar</emphasis>. It contains three elements "
135 "by default (left to right):<list><item>A <emphasis>text field"
136 "</emphasis> that displays the size of selected items. If only "
137 "one item is selected the name and type is shown as well.</item>"
138 "<item>A <emphasis>zoom slider</emphasis> that allows you "
139 "to adjust the size of the icons in the view.</item>"
140 "<item><emphasis>Space information</emphasis> about the "
141 "current storage device.</item></list></para>"));
144 DolphinStatusBar::~DolphinStatusBar()
148 void DolphinStatusBar::setText(const QString
&text
)
150 if (m_text
== text
) {
154 m_textTimestamp
= QTime::currentTime();
157 // will update status bar text in 50ms
158 m_delayUpdateTimer
->start();
161 QString
DolphinStatusBar::text() const
166 void DolphinStatusBar::setProgressText(const QString
&text
)
168 m_progressTextLabel
->setText(text
);
171 QString
DolphinStatusBar::progressText() const
173 return m_progressTextLabel
->text();
176 void DolphinStatusBar::setProgress(int percent
)
178 // Show a busy indicator if a value < 0 is provided:
179 m_progressBar
->setMaximum((percent
< 0) ? 0 : 100);
181 percent
= qBound(0, percent
, 100);
182 const bool progressRestarted
= (percent
< 100) && (percent
< m_progress
);
183 m_progress
= percent
;
184 if (progressRestarted
&& !m_progressBar
->isVisible()) {
185 // Show the progress bar delayed: In the case if 100 % are reached within
186 // a short time, no progress bar will be shown at all.
187 m_showProgressBarTimer
->start();
190 m_progressBar
->setValue(m_progress
);
191 if (percent
== 100) {
192 // The end of the progress has been reached. Assure that the progress bar
193 // gets hidden and the extensions widgets get visible again.
194 m_showProgressBarTimer
->stop();
195 updateProgressInfo();
199 int DolphinStatusBar::progress() const
204 void DolphinStatusBar::resetToDefaultText()
209 if (currentTime
.msecsTo(m_textTimestamp
) < UpdateDelay
) {
210 m_delayUpdateTimer
->start();
216 void DolphinStatusBar::setDefaultText(const QString
&text
)
218 m_defaultText
= text
;
222 QString
DolphinStatusBar::defaultText() const
224 return m_defaultText
;
227 void DolphinStatusBar::setUrl(const QUrl
&url
)
229 if (GeneralSettings::showSpaceInfo()) {
230 m_spaceInfo
->setUrl(url
);
234 QUrl
DolphinStatusBar::url() const
236 return m_spaceInfo
->url();
239 void DolphinStatusBar::setZoomLevel(int zoomLevel
)
241 if (zoomLevel
!= m_zoomSlider
->value()) {
242 m_zoomSlider
->setValue(zoomLevel
);
246 int DolphinStatusBar::zoomLevel() const
248 return m_zoomSlider
->value();
251 void DolphinStatusBar::readSettings()
253 setVisible(GeneralSettings::showStatusBar());
254 setExtensionsVisible(true);
257 void DolphinStatusBar::updateSpaceInfo()
259 m_spaceInfo
->update();
262 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent
*event
)
268 QAction
*showZoomSliderAction
= menu
.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
269 showZoomSliderAction
->setCheckable(true);
270 showZoomSliderAction
->setChecked(GeneralSettings::showZoomSlider());
272 QAction
*showSpaceInfoAction
= menu
.addAction(i18nc("@action:inmenu", "Show Space Information"));
273 showSpaceInfoAction
->setCheckable(true);
274 showSpaceInfoAction
->setChecked(GeneralSettings::showSpaceInfo());
276 const QAction
*action
= menu
.exec(event
->reason() == QContextMenuEvent::Reason::Mouse
? QCursor::pos() : mapToGlobal(QPoint(width() / 2, height() / 2)));
277 if (action
== showZoomSliderAction
) {
278 const bool visible
= showZoomSliderAction
->isChecked();
279 GeneralSettings::setShowZoomSlider(visible
);
280 m_zoomSlider
->setVisible(visible
);
281 m_zoomLabel
->setVisible(visible
);
282 } else if (action
== showSpaceInfoAction
) {
283 const bool visible
= showSpaceInfoAction
->isChecked();
284 GeneralSettings::setShowSpaceInfo(visible
);
285 m_spaceInfo
->setVisible(visible
);
289 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel
)
291 updateZoomSliderToolTip(zoomLevel
);
293 QPoint global
= m_zoomSlider
->rect().topLeft();
294 global
.ry() += m_zoomSlider
->height() / 2;
295 QHelpEvent
toolTipEvent(QEvent::ToolTip
, QPoint(0, 0), m_zoomSlider
->mapToGlobal(global
));
296 QApplication::sendEvent(m_zoomSlider
, &toolTipEvent
);
299 void DolphinStatusBar::updateProgressInfo()
301 if (m_progress
< 100) {
302 // Show the progress information and hide the extensions
303 m_stopButton
->show();
304 m_progressTextLabel
->show();
305 m_progressBar
->show();
306 setExtensionsVisible(false);
308 // Hide the progress information and show the extensions
309 m_stopButton
->hide();
310 m_progressTextLabel
->hide();
311 m_progressBar
->hide();
312 setExtensionsVisible(true);
316 void DolphinStatusBar::updateLabelText()
318 const QString text
= m_text
.isEmpty() ? m_defaultText
: m_text
;
319 m_label
->setText(text
);
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();
337 m_spaceInfo
->setShown(showSpaceInfo
);
338 m_spaceInfo
->setVisible(showSpaceInfo
);
339 m_zoomSlider
->setVisible(showZoomSlider
);
340 m_zoomLabel
->setVisible(showZoomSlider
);
343 void DolphinStatusBar::paintEvent(QPaintEvent
*paintEvent
)
348 style()->drawPrimitive(QStyle::PE_PanelStatusBar
, &opt
, &p
, this);
351 #include "moc_dolphinstatusbar.cpp"