]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/dolphinstatusbar.cpp
a7585bed023632e3dadaa39c1e96e0369f0f2c7b
[dolphin.git] / src / statusbar / dolphinstatusbar.cpp
1 /***************************************************************************
2 * Copyright (C) 2006-2012 by Peter Penz <peter.penz19@gmail.com> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "dolphinstatusbar.h"
21
22 #include "dolphin_generalsettings.h"
23 #include "statusbarspaceinfo.h"
24 #include "views/dolphinview.h"
25 #include "views/zoomlevelinfo.h"
26
27 #include <KLocalizedString>
28 #include <KSqueezedTextLabel>
29
30 #include <QApplication>
31 #include <QHBoxLayout>
32 #include <QHelpEvent>
33 #include <QIcon>
34 #include <QMenu>
35 #include <QProgressBar>
36 #include <QSlider>
37 #include <QTextDocument>
38 #include <QTimer>
39 #include <QToolButton>
40
41 namespace {
42 const int UpdateDelay = 50;
43 }
44
45 DolphinStatusBar::DolphinStatusBar(QWidget* parent) :
46 QWidget(parent),
47 m_text(),
48 m_defaultText(),
49 m_label(nullptr),
50 m_spaceInfo(nullptr),
51 m_zoomSlider(nullptr),
52 m_progressBar(nullptr),
53 m_stopButton(nullptr),
54 m_progress(100),
55 m_showProgressBarTimer(nullptr),
56 m_delayUpdateTimer(nullptr),
57 m_textTimestamp()
58 {
59 // Initialize text label
60 m_label = new KSqueezedTextLabel(m_text, this);
61 m_label->setWordWrap(true);
62 m_label->setTextFormat(Qt::PlainText);
63
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());
70
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);
74
75 // Initialize space information
76 m_spaceInfo = new StatusBarSpaceInfo(this);
77
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"));
84 m_stopButton->hide();
85 connect(m_stopButton, &QToolButton::clicked, this, &DolphinStatusBar::stopPressed);
86
87 m_progressTextLabel = new QLabel(this);
88 m_progressTextLabel->hide();
89
90 m_progressBar = new QProgressBar(this);
91 m_progressBar->hide();
92
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);
97
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);
104
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);
110
111 QFontMetrics fontMetrics(m_label->font());
112
113 m_label->setFixedHeight(contentHeight);
114 m_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
115
116 m_zoomSlider->setMaximumWidth(fontMetrics.averageCharWidth() * 25);
117
118 m_spaceInfo->setFixedHeight(contentHeight);
119 m_spaceInfo->setMaximumWidth(fontMetrics.averageCharWidth() * 25);
120 m_spaceInfo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
121
122 m_progressBar->setFixedHeight(zoomSliderHeight);
123 m_progressBar->setMaximumWidth(fontMetrics.averageCharWidth() * 25);
124
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);
134
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>"));
145 }
146
147 DolphinStatusBar::~DolphinStatusBar()
148 {
149 }
150
151 void DolphinStatusBar::setText(const QString& text)
152 {
153 if (m_text == text) {
154 return;
155 }
156
157 m_textTimestamp = QTime::currentTime();
158
159 m_text = text;
160 // will update status bar text in 50ms
161 m_delayUpdateTimer->start();
162 }
163
164 QString DolphinStatusBar::text() const
165 {
166 return m_text;
167 }
168
169 void DolphinStatusBar::setProgressText(const QString& text)
170 {
171 m_progressTextLabel->setText(text);
172 }
173
174 QString DolphinStatusBar::progressText() const
175 {
176 return m_progressTextLabel->text();
177 }
178
179 void DolphinStatusBar::setProgress(int percent)
180 {
181 // Show a busy indicator if a value < 0 is provided:
182 m_progressBar->setMaximum((percent < 0) ? 0 : 100);
183
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();
191 }
192
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();
199 }
200 }
201
202 int DolphinStatusBar::progress() const
203 {
204 return m_progress;
205 }
206
207 void DolphinStatusBar::resetToDefaultText()
208 {
209 m_text.clear();
210
211 QTime currentTime;
212 if (currentTime.msecsTo(m_textTimestamp) < UpdateDelay) {
213 m_delayUpdateTimer->start();
214 } else {
215 updateLabelText();
216 }
217 }
218
219 void DolphinStatusBar::setDefaultText(const QString& text)
220 {
221 m_defaultText = text;
222 updateLabelText();
223 }
224
225 QString DolphinStatusBar::defaultText() const
226 {
227 return m_defaultText;
228 }
229
230 void DolphinStatusBar::setUrl(const QUrl& url)
231 {
232 m_spaceInfo->setUrl(url);
233 }
234
235 QUrl DolphinStatusBar::url() const
236 {
237 return m_spaceInfo->url();
238 }
239
240 void DolphinStatusBar::setZoomLevel(int zoomLevel)
241 {
242 if (zoomLevel != m_zoomSlider->value()) {
243 m_zoomSlider->setValue(zoomLevel);
244 }
245 }
246
247 int DolphinStatusBar::zoomLevel() const
248 {
249 return m_zoomSlider->value();
250 }
251
252 void DolphinStatusBar::readSettings()
253 {
254 setExtensionsVisible(true);
255 }
256
257 void DolphinStatusBar::updateSpaceInfo()
258 {
259 m_spaceInfo->update();
260 }
261
262 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent* event)
263 {
264 Q_UNUSED(event)
265
266 QMenu menu(this);
267
268 QAction* showZoomSliderAction = menu.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
269 showZoomSliderAction->setCheckable(true);
270 showZoomSliderAction->setChecked(GeneralSettings::showZoomSlider());
271
272 QAction* showSpaceInfoAction = menu.addAction(i18nc("@action:inmenu", "Show Space Information"));
273 showSpaceInfoAction->setCheckable(true);
274 showSpaceInfoAction->setChecked(GeneralSettings::showSpaceInfo());
275
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);
285 }
286 }
287
288 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel)
289 {
290 updateZoomSliderToolTip(zoomLevel);
291
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);
296 }
297
298 void DolphinStatusBar::updateProgressInfo()
299 {
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);
306 } else {
307 // Hide the progress information and show the extensions
308 m_stopButton->hide();
309 m_progressTextLabel->hide();
310 m_progressBar->hide();
311 setExtensionsVisible(true);
312 }
313 }
314
315 void DolphinStatusBar::updateLabelText()
316 {
317 const QString text = m_text.isEmpty() ? m_defaultText : m_text;
318 m_label->setText(text);
319 }
320
321 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel)
322 {
323 const int size = ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel);
324 m_zoomSlider->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size));
325 }
326
327 void DolphinStatusBar::setExtensionsVisible(bool visible)
328 {
329 bool showSpaceInfo = visible;
330 bool showZoomSlider = visible;
331 if (visible) {
332 showSpaceInfo = GeneralSettings::showSpaceInfo();
333 showZoomSlider = GeneralSettings::showZoomSlider();
334 }
335 m_spaceInfo->setVisible(showSpaceInfo);
336 m_zoomSlider->setVisible(showZoomSlider);
337 }
338