]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/dolphinstatusbar.cpp
Remove unused #include
[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 ResetToDefaultTimeout = 1000;
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_resetToDefaultTextTimer(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 m_resetToDefaultTextTimer = new QTimer(this);
99 m_resetToDefaultTextTimer->setInterval(ResetToDefaultTimeout);
100 m_resetToDefaultTextTimer->setSingleShot(true);
101 connect(m_resetToDefaultTextTimer, &QTimer::timeout, this, &DolphinStatusBar::slotResetToDefaultText);
102
103 // Initialize top layout and size policies
104 const int fontHeight = QFontMetrics(m_label->font()).height();
105 const int zoomSliderHeight = m_zoomSlider->minimumSizeHint().height();
106 const int buttonHeight = m_stopButton->height();
107 const int contentHeight = qMax(qMax(fontHeight, zoomSliderHeight), buttonHeight);
108
109 QFontMetrics fontMetrics(m_label->font());
110
111 m_label->setFixedHeight(contentHeight);
112 m_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
113
114 m_zoomSlider->setMaximumWidth(fontMetrics.averageCharWidth() * 25);
115
116 m_spaceInfo->setFixedHeight(zoomSliderHeight);
117 m_spaceInfo->setMaximumWidth(fontMetrics.averageCharWidth() * 25);
118 m_spaceInfo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
119
120 m_progressBar->setFixedHeight(zoomSliderHeight);
121 m_progressBar->setMaximumWidth(fontMetrics.averageCharWidth() * 25);
122
123 QHBoxLayout* topLayout = new QHBoxLayout(this);
124 topLayout->setContentsMargins(2, 0, 2, 0);
125 topLayout->setSpacing(4);
126 topLayout->addWidget(m_label, 1);
127 topLayout->addWidget(m_zoomSlider, 1);
128 topLayout->addWidget(m_spaceInfo, 1);
129 topLayout->addWidget(m_stopButton);
130 topLayout->addWidget(m_progressTextLabel);
131 topLayout->addWidget(m_progressBar);
132
133 setExtensionsVisible(true);
134 }
135
136 DolphinStatusBar::~DolphinStatusBar()
137 {
138 }
139
140 void DolphinStatusBar::setText(const QString& text)
141 {
142 if (m_text == text) {
143 return;
144 }
145
146 m_textTimestamp = QTime::currentTime();
147
148 if (text.isEmpty()) {
149 // Assure that the previous set text won't get
150 // cleared immediatelly.
151 m_resetToDefaultTextTimer->start();
152 } else {
153 m_text = text;
154
155 if (m_resetToDefaultTextTimer->isActive()) {
156 m_resetToDefaultTextTimer->start();
157 }
158
159 updateLabelText();
160 }
161 }
162
163 QString DolphinStatusBar::text() const
164 {
165 return m_text;
166 }
167
168 void DolphinStatusBar::setProgressText(const QString& text)
169 {
170 m_progressTextLabel->setText(text);
171 }
172
173 QString DolphinStatusBar::progressText() const
174 {
175 return m_progressTextLabel->text();
176 }
177
178 void DolphinStatusBar::setProgress(int percent)
179 {
180 // Show a busy indicator if a value < 0 is provided:
181 m_progressBar->setMaximum((percent < 0) ? 0 : 100);
182
183 percent = qBound(0, percent, 100);
184 const bool progressRestarted = (percent < 100) && (percent < m_progress);
185 m_progress = percent;
186 if (progressRestarted && !m_progressBar->isVisible()) {
187 // Show the progress bar delayed: In the case if 100 % are reached within
188 // a short time, no progress bar will be shown at all.
189 m_showProgressBarTimer->start();
190 }
191
192 m_progressBar->setValue(m_progress);
193 if (percent == 100) {
194 // The end of the progress has been reached. Assure that the progress bar
195 // gets hidden and the extensions widgets get visible again.
196 m_showProgressBarTimer->stop();
197 updateProgressInfo();
198 }
199 }
200
201 int DolphinStatusBar::progress() const
202 {
203 return m_progress;
204 }
205
206 void DolphinStatusBar::resetToDefaultText()
207 {
208 QTime currentTime;
209 if (currentTime.msecsTo(m_textTimestamp) < ResetToDefaultTimeout) {
210 m_resetToDefaultTextTimer->start();
211 } else {
212 m_resetToDefaultTextTimer->stop();
213 slotResetToDefaultText();
214 }
215 }
216
217 void DolphinStatusBar::setDefaultText(const QString& text)
218 {
219 m_defaultText = text;
220 updateLabelText();
221 }
222
223 QString DolphinStatusBar::defaultText() const
224 {
225 return m_defaultText;
226 }
227
228 void DolphinStatusBar::setUrl(const QUrl& url)
229 {
230 m_spaceInfo->setUrl(url);
231 }
232
233 QUrl DolphinStatusBar::url() const
234 {
235 return m_spaceInfo->url();
236 }
237
238 void DolphinStatusBar::setZoomLevel(int zoomLevel)
239 {
240 if (zoomLevel != m_zoomSlider->value()) {
241 m_zoomSlider->setValue(zoomLevel);
242 }
243 }
244
245 int DolphinStatusBar::zoomLevel() const
246 {
247 return m_zoomSlider->value();
248 }
249
250 void DolphinStatusBar::readSettings()
251 {
252 setExtensionsVisible(true);
253 }
254
255 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent* event)
256 {
257 Q_UNUSED(event);
258
259 QMenu menu(this);
260
261 QAction* showZoomSliderAction = menu.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
262 showZoomSliderAction->setCheckable(true);
263 showZoomSliderAction->setChecked(GeneralSettings::showZoomSlider());
264
265 QAction* showSpaceInfoAction = menu.addAction(i18nc("@action:inmenu", "Show Space Information"));
266 showSpaceInfoAction->setCheckable(true);
267 showSpaceInfoAction->setChecked(GeneralSettings::showSpaceInfo());
268
269 const QAction* action = menu.exec(QCursor::pos());
270 if (action == showZoomSliderAction) {
271 const bool visible = showZoomSliderAction->isChecked();
272 GeneralSettings::setShowZoomSlider(visible);
273 m_zoomSlider->setVisible(visible);
274 } else if (action == showSpaceInfoAction) {
275 const bool visible = showSpaceInfoAction->isChecked();
276 GeneralSettings::setShowSpaceInfo(visible);
277 m_spaceInfo->setVisible(visible);
278 }
279 }
280
281 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel)
282 {
283 updateZoomSliderToolTip(zoomLevel);
284
285 QPoint global = m_zoomSlider->rect().topLeft();
286 global.ry() += m_zoomSlider->height() / 2;
287 QHelpEvent toolTipEvent(QEvent::ToolTip, QPoint(0, 0), m_zoomSlider->mapToGlobal(global));
288 QApplication::sendEvent(m_zoomSlider, &toolTipEvent);
289 }
290
291 void DolphinStatusBar::updateProgressInfo()
292 {
293 if (m_progress < 100) {
294 // Show the progress information and hide the extensions
295 m_stopButton->show();
296 m_progressTextLabel->show();
297 m_progressBar->show();
298 setExtensionsVisible(false);
299 } else {
300 // Hide the progress information and show the extensions
301 m_stopButton->hide();
302 m_progressTextLabel->hide();
303 m_progressBar->hide();
304 setExtensionsVisible(true);
305 }
306 }
307
308 void DolphinStatusBar::updateLabelText()
309 {
310 const QString text = m_text.isEmpty() ? m_defaultText : m_text;
311 m_label->setText(text);
312 }
313
314 void DolphinStatusBar::slotResetToDefaultText()
315 {
316 m_text.clear();
317 updateLabelText();
318 }
319
320 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel)
321 {
322 const int size = ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel);
323 m_zoomSlider->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size));
324 }
325
326 void DolphinStatusBar::setExtensionsVisible(bool visible)
327 {
328 bool showSpaceInfo = visible;
329 bool showZoomSlider = visible;
330 if (visible) {
331 showSpaceInfo = GeneralSettings::showSpaceInfo();
332 showZoomSlider = GeneralSettings::showZoomSlider();
333 }
334 m_spaceInfo->setVisible(showSpaceInfo);
335 m_zoomSlider->setVisible(showZoomSlider);
336 }
337