]>
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>
25 #include <QToolButton>
29 const int UpdateDelay
= 50;
32 DolphinStatusBar::DolphinStatusBar(QWidget
*parent
)
37 , m_zoomLabel(nullptr)
38 , m_spaceInfo(nullptr)
39 , m_zoomSlider(nullptr)
40 , m_progressBar(nullptr)
41 , m_stopButton(nullptr)
43 , m_showProgressBarTimer(nullptr)
44 , m_delayUpdateTimer(nullptr)
47 // Initialize text label
48 m_label
= new KSqueezedTextLabel(m_text
, this);
49 m_label
->setWordWrap(true);
50 m_label
->setTextFormat(Qt::PlainText
);
52 // Initialize zoom slider's explanatory label
53 m_zoomLabel
= new QLabel(i18nc("Used as a noun, i.e. 'Here is the zoom level:'", "Zoom:"), this);
55 // Initialize zoom widget
56 m_zoomSlider
= new QSlider(Qt::Horizontal
, this);
57 m_zoomSlider
->setAccessibleName(i18n("Zoom"));
58 m_zoomSlider
->setAccessibleDescription(i18nc("Description for zoom-slider (accessibility)", "Sets the size of the file icons."));
59 m_zoomSlider
->setPageStep(1);
60 m_zoomSlider
->setRange(ZoomLevelInfo::minimumLevel(), ZoomLevelInfo::maximumLevel());
62 connect(m_zoomSlider
, &QSlider::valueChanged
, this, &DolphinStatusBar::zoomLevelChanged
);
63 connect(m_zoomSlider
, &QSlider::valueChanged
, this, &DolphinStatusBar::updateZoomSliderToolTip
);
64 connect(m_zoomSlider
, &QSlider::sliderMoved
, this, &DolphinStatusBar::showZoomSliderToolTip
);
66 // Initialize space information
67 m_spaceInfo
= new StatusBarSpaceInfo(this);
69 // Initialize progress information
70 m_stopButton
= new QToolButton(this);
71 m_stopButton
->setIcon(QIcon::fromTheme(QStringLiteral("process-stop")));
72 m_stopButton
->setAccessibleName(i18n("Stop"));
73 m_stopButton
->setAutoRaise(true);
74 m_stopButton
->setToolTip(i18nc("@tooltip", "Stop loading"));
76 connect(m_stopButton
, &QToolButton::clicked
, this, &DolphinStatusBar::stopPressed
);
78 m_progressTextLabel
= new QLabel(this);
79 m_progressTextLabel
->hide();
81 m_progressBar
= new QProgressBar(this);
82 m_progressBar
->hide();
84 m_showProgressBarTimer
= new QTimer(this);
85 m_showProgressBarTimer
->setInterval(500);
86 m_showProgressBarTimer
->setSingleShot(true);
87 connect(m_showProgressBarTimer
, &QTimer::timeout
, this, &DolphinStatusBar::updateProgressInfo
);
89 // initialize text updater delay timer
90 m_delayUpdateTimer
= new QTimer(this);
91 m_delayUpdateTimer
->setInterval(UpdateDelay
);
92 m_delayUpdateTimer
->setSingleShot(true);
93 connect(m_delayUpdateTimer
, &QTimer::timeout
, this, &DolphinStatusBar::updateLabelText
);
95 // Initialize top layout and size policies
96 const int fontHeight
= QFontMetrics(m_label
->font()).height();
97 const int zoomSliderHeight
= m_zoomSlider
->minimumSizeHint().height();
98 const int buttonHeight
= m_stopButton
->height();
99 const int contentHeight
= qMax(qMax(fontHeight
, zoomSliderHeight
), buttonHeight
);
101 QFontMetrics
fontMetrics(m_label
->font());
103 m_label
->setFixedHeight(contentHeight
);
104 m_label
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
106 m_zoomSlider
->setMaximumWidth(fontMetrics
.averageCharWidth() * 15);
108 m_spaceInfo
->setFixedHeight(contentHeight
);
109 m_spaceInfo
->setMaximumWidth(fontMetrics
.averageCharWidth() * 25);
110 m_spaceInfo
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
112 m_progressBar
->setFixedHeight(zoomSliderHeight
);
113 m_progressBar
->setMaximumWidth(fontMetrics
.averageCharWidth() * 20);
115 QHBoxLayout
*topLayout
= new QHBoxLayout(this);
116 topLayout
->setContentsMargins(2, 0, 2, 0);
117 topLayout
->setSpacing(4);
118 topLayout
->addWidget(m_label
, 1);
119 topLayout
->addWidget(m_zoomLabel
);
120 topLayout
->addWidget(m_zoomSlider
, 1);
121 topLayout
->addWidget(m_spaceInfo
, 1);
122 topLayout
->addWidget(m_stopButton
);
123 topLayout
->addWidget(m_progressTextLabel
);
124 topLayout
->addWidget(m_progressBar
);
126 setVisible(GeneralSettings::showStatusBar());
127 setExtensionsVisible(true);
128 setWhatsThis(xi18nc("@info:whatsthis Statusbar",
130 "the <emphasis>Statusbar</emphasis>. It contains three elements "
131 "by default (left to right):<list><item>A <emphasis>text field"
132 "</emphasis> that displays the size of selected items. If only "
133 "one item is selected the name and type is shown as well.</item>"
134 "<item>A <emphasis>zoom slider</emphasis> that allows you "
135 "to adjust the size of the icons in the view.</item>"
136 "<item><emphasis>Space information</emphasis> about the "
137 "current storage device.</item></list></para>"));
140 DolphinStatusBar::~DolphinStatusBar()
144 void DolphinStatusBar::setText(const QString
&text
)
146 if (m_text
== text
) {
150 m_textTimestamp
= QTime::currentTime();
153 // will update status bar text in 50ms
154 m_delayUpdateTimer
->start();
157 QString
DolphinStatusBar::text() const
162 void DolphinStatusBar::setProgressText(const QString
&text
)
164 m_progressTextLabel
->setText(text
);
167 QString
DolphinStatusBar::progressText() const
169 return m_progressTextLabel
->text();
172 void DolphinStatusBar::setProgress(int percent
)
174 // Show a busy indicator if a value < 0 is provided:
175 m_progressBar
->setMaximum((percent
< 0) ? 0 : 100);
177 percent
= qBound(0, percent
, 100);
178 const bool progressRestarted
= (percent
< 100) && (percent
< m_progress
);
179 m_progress
= percent
;
180 if (progressRestarted
&& !m_progressBar
->isVisible()) {
181 // Show the progress bar delayed: In the case if 100 % are reached within
182 // a short time, no progress bar will be shown at all.
183 m_showProgressBarTimer
->start();
186 m_progressBar
->setValue(m_progress
);
187 if (percent
== 100) {
188 // The end of the progress has been reached. Assure that the progress bar
189 // gets hidden and the extensions widgets get visible again.
190 m_showProgressBarTimer
->stop();
191 updateProgressInfo();
195 int DolphinStatusBar::progress() const
200 void DolphinStatusBar::resetToDefaultText()
205 if (currentTime
.msecsTo(m_textTimestamp
) < UpdateDelay
) {
206 m_delayUpdateTimer
->start();
212 void DolphinStatusBar::setDefaultText(const QString
&text
)
214 m_defaultText
= text
;
218 QString
DolphinStatusBar::defaultText() const
220 return m_defaultText
;
223 void DolphinStatusBar::setUrl(const QUrl
&url
)
225 if (GeneralSettings::showSpaceInfo()) {
226 m_spaceInfo
->setUrl(url
);
230 QUrl
DolphinStatusBar::url() const
232 return m_spaceInfo
->url();
235 void DolphinStatusBar::setZoomLevel(int zoomLevel
)
237 if (zoomLevel
!= m_zoomSlider
->value()) {
238 m_zoomSlider
->setValue(zoomLevel
);
242 int DolphinStatusBar::zoomLevel() const
244 return m_zoomSlider
->value();
247 void DolphinStatusBar::readSettings()
249 setVisible(GeneralSettings::showStatusBar());
250 setExtensionsVisible(true);
253 void DolphinStatusBar::updateSpaceInfo()
255 m_spaceInfo
->update();
258 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent
*event
)
264 QAction
*showZoomSliderAction
= menu
.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
265 showZoomSliderAction
->setCheckable(true);
266 showZoomSliderAction
->setChecked(GeneralSettings::showZoomSlider());
268 QAction
*showSpaceInfoAction
= menu
.addAction(i18nc("@action:inmenu", "Show Space Information"));
269 showSpaceInfoAction
->setCheckable(true);
270 showSpaceInfoAction
->setChecked(GeneralSettings::showSpaceInfo());
272 const QAction
*action
= menu
.exec(event
->reason() == QContextMenuEvent::Reason::Mouse
? QCursor::pos() : mapToGlobal(QPoint(width() / 2, height() / 2)));
273 if (action
== showZoomSliderAction
) {
274 const bool visible
= showZoomSliderAction
->isChecked();
275 GeneralSettings::setShowZoomSlider(visible
);
276 m_zoomSlider
->setVisible(visible
);
277 m_zoomLabel
->setVisible(visible
);
278 } else if (action
== showSpaceInfoAction
) {
279 const bool visible
= showSpaceInfoAction
->isChecked();
280 GeneralSettings::setShowSpaceInfo(visible
);
281 m_spaceInfo
->setVisible(visible
);
285 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel
)
287 updateZoomSliderToolTip(zoomLevel
);
289 QPoint global
= m_zoomSlider
->rect().topLeft();
290 global
.ry() += m_zoomSlider
->height() / 2;
291 QHelpEvent
toolTipEvent(QEvent::ToolTip
, QPoint(0, 0), m_zoomSlider
->mapToGlobal(global
));
292 QApplication::sendEvent(m_zoomSlider
, &toolTipEvent
);
295 void DolphinStatusBar::updateProgressInfo()
297 if (m_progress
< 100) {
298 // Show the progress information and hide the extensions
299 m_stopButton
->show();
300 m_progressTextLabel
->show();
301 m_progressBar
->show();
302 setExtensionsVisible(false);
304 // Hide the progress information and show the extensions
305 m_stopButton
->hide();
306 m_progressTextLabel
->hide();
307 m_progressBar
->hide();
308 setExtensionsVisible(true);
312 void DolphinStatusBar::updateLabelText()
314 const QString text
= m_text
.isEmpty() ? m_defaultText
: m_text
;
315 m_label
->setText(text
);
318 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel
)
320 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel
);
321 m_zoomSlider
->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size
));
324 void DolphinStatusBar::setExtensionsVisible(bool visible
)
326 bool showSpaceInfo
= visible
;
327 bool showZoomSlider
= visible
;
329 showSpaceInfo
= GeneralSettings::showSpaceInfo();
330 showZoomSlider
= GeneralSettings::showZoomSlider();
333 m_spaceInfo
->setShown(showSpaceInfo
);
334 m_spaceInfo
->setVisible(showSpaceInfo
);
335 m_zoomSlider
->setVisible(showZoomSlider
);
336 m_zoomLabel
->setVisible(showZoomSlider
);
339 #include "moc_dolphinstatusbar.cpp"