]>
cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/dolphinstatusbar.cpp
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 UpdateDelay
= 50;
45 DolphinStatusBar::DolphinStatusBar(QWidget
* parent
) :
51 m_zoomSlider(nullptr),
52 m_progressBar(nullptr),
53 m_stopButton(nullptr),
55 m_showProgressBarTimer(nullptr),
56 m_delayUpdateTimer(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 // initialize text updater delay timer
99 m_delayUpdateTimer
= new QTimer(this);
100 m_delayUpdateTimer
->setInterval(UpdateDelay
);
101 m_delayUpdateTimer
->setSingleShot(true);
102 connect(m_delayUpdateTimer
, &QTimer::timeout
,
103 this, &DolphinStatusBar::updateLabelText
);
105 // Initialize top layout and size policies
106 const int fontHeight
= QFontMetrics(m_label
->font()).height();
107 const int zoomSliderHeight
= m_zoomSlider
->minimumSizeHint().height();
108 const int buttonHeight
= m_stopButton
->height();
109 const int contentHeight
= qMax(qMax(fontHeight
, zoomSliderHeight
), buttonHeight
);
111 QFontMetrics
fontMetrics(m_label
->font());
113 m_label
->setFixedHeight(contentHeight
);
114 m_label
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
116 m_zoomSlider
->setMaximumWidth(fontMetrics
.averageCharWidth() * 25);
118 m_spaceInfo
->setFixedHeight(contentHeight
);
119 m_spaceInfo
->setMaximumWidth(fontMetrics
.averageCharWidth() * 25);
120 m_spaceInfo
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
122 m_progressBar
->setFixedHeight(zoomSliderHeight
);
123 m_progressBar
->setMaximumWidth(fontMetrics
.averageCharWidth() * 25);
125 QHBoxLayout
* topLayout
= new QHBoxLayout(this);
126 topLayout
->setContentsMargins(2, 0, 2, 0);
127 topLayout
->setSpacing(4);
128 topLayout
->addWidget(m_label
, 1);
129 topLayout
->addWidget(m_zoomSlider
, 1);
130 topLayout
->addWidget(m_spaceInfo
, 1);
131 topLayout
->addWidget(m_stopButton
);
132 topLayout
->addWidget(m_progressTextLabel
);
133 topLayout
->addWidget(m_progressBar
);
135 setExtensionsVisible(true);
136 setWhatsThis(xi18nc("@info:whatsthis Statusbar", "<para>This is "
137 "the <emphasis>Statusbar</emphasis>. It contains three elements "
138 "by default (left to right):<list><item>A <emphasis>text field"
139 "</emphasis> that displays the size of selected items. If only "
140 "one item is selected the name and type is shown as well.</item>"
141 "<item>A <emphasis>zoom slider</emphasis> that allows you "
142 "to adjust the size of the icons in the view.</item>"
143 "<item><emphasis>Space information</emphasis> about the "
144 "current storage device.</item></list></para>"));
147 DolphinStatusBar::~DolphinStatusBar()
151 void DolphinStatusBar::setText(const QString
& text
)
153 if (m_text
== text
) {
157 m_textTimestamp
= QTime::currentTime();
160 // will update status bar text in 50ms
161 m_delayUpdateTimer
->start();
164 QString
DolphinStatusBar::text() const
169 void DolphinStatusBar::setProgressText(const QString
& text
)
171 m_progressTextLabel
->setText(text
);
174 QString
DolphinStatusBar::progressText() const
176 return m_progressTextLabel
->text();
179 void DolphinStatusBar::setProgress(int percent
)
181 // Show a busy indicator if a value < 0 is provided:
182 m_progressBar
->setMaximum((percent
< 0) ? 0 : 100);
184 percent
= qBound(0, percent
, 100);
185 const bool progressRestarted
= (percent
< 100) && (percent
< m_progress
);
186 m_progress
= percent
;
187 if (progressRestarted
&& !m_progressBar
->isVisible()) {
188 // Show the progress bar delayed: In the case if 100 % are reached within
189 // a short time, no progress bar will be shown at all.
190 m_showProgressBarTimer
->start();
193 m_progressBar
->setValue(m_progress
);
194 if (percent
== 100) {
195 // The end of the progress has been reached. Assure that the progress bar
196 // gets hidden and the extensions widgets get visible again.
197 m_showProgressBarTimer
->stop();
198 updateProgressInfo();
202 int DolphinStatusBar::progress() const
207 void DolphinStatusBar::resetToDefaultText()
212 if (currentTime
.msecsTo(m_textTimestamp
) < UpdateDelay
) {
213 m_delayUpdateTimer
->start();
219 void DolphinStatusBar::setDefaultText(const QString
& text
)
221 m_defaultText
= text
;
225 QString
DolphinStatusBar::defaultText() const
227 return m_defaultText
;
230 void DolphinStatusBar::setUrl(const QUrl
& url
)
232 m_spaceInfo
->setUrl(url
);
235 QUrl
DolphinStatusBar::url() const
237 return m_spaceInfo
->url();
240 void DolphinStatusBar::setZoomLevel(int zoomLevel
)
242 if (zoomLevel
!= m_zoomSlider
->value()) {
243 m_zoomSlider
->setValue(zoomLevel
);
247 int DolphinStatusBar::zoomLevel() const
249 return m_zoomSlider
->value();
252 void DolphinStatusBar::readSettings()
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(QCursor::pos());
277 if (action
== showZoomSliderAction
) {
278 const bool visible
= showZoomSliderAction
->isChecked();
279 GeneralSettings::setShowZoomSlider(visible
);
280 m_zoomSlider
->setVisible(visible
);
281 } else if (action
== showSpaceInfoAction
) {
282 const bool visible
= showSpaceInfoAction
->isChecked();
283 GeneralSettings::setShowSpaceInfo(visible
);
284 m_spaceInfo
->setVisible(visible
);
288 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel
)
290 updateZoomSliderToolTip(zoomLevel
);
292 QPoint global
= m_zoomSlider
->rect().topLeft();
293 global
.ry() += m_zoomSlider
->height() / 2;
294 QHelpEvent
toolTipEvent(QEvent::ToolTip
, QPoint(0, 0), m_zoomSlider
->mapToGlobal(global
));
295 QApplication::sendEvent(m_zoomSlider
, &toolTipEvent
);
298 void DolphinStatusBar::updateProgressInfo()
300 if (m_progress
< 100) {
301 // Show the progress information and hide the extensions
302 m_stopButton
->show();
303 m_progressTextLabel
->show();
304 m_progressBar
->show();
305 setExtensionsVisible(false);
307 // Hide the progress information and show the extensions
308 m_stopButton
->hide();
309 m_progressTextLabel
->hide();
310 m_progressBar
->hide();
311 setExtensionsVisible(true);
315 void DolphinStatusBar::updateLabelText()
317 const QString text
= m_text
.isEmpty() ? m_defaultText
: m_text
;
318 m_label
->setText(text
);
321 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel
)
323 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel
);
324 m_zoomSlider
->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size
));
327 void DolphinStatusBar::setExtensionsVisible(bool visible
)
329 bool showSpaceInfo
= visible
;
330 bool showZoomSlider
= visible
;
332 showSpaceInfo
= GeneralSettings::showSpaceInfo();
333 showZoomSlider
= GeneralSettings::showZoomSlider();
335 m_spaceInfo
->setVisible(showSpaceInfo
);
336 m_zoomSlider
->setVisible(showZoomSlider
);