]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/dolphinstatusbar.cpp
Change group spacing from 18px to 12px
[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 if (GeneralSettings::showSpaceInfo()) {
233 m_spaceInfo->setUrl(url);
234 }
235 }
236
237 QUrl DolphinStatusBar::url() const
238 {
239 return m_spaceInfo->url();
240 }
241
242 void DolphinStatusBar::setZoomLevel(int zoomLevel)
243 {
244 if (zoomLevel != m_zoomSlider->value()) {
245 m_zoomSlider->setValue(zoomLevel);
246 }
247 }
248
249 int DolphinStatusBar::zoomLevel() const
250 {
251 return m_zoomSlider->value();
252 }
253
254 void DolphinStatusBar::readSettings()
255 {
256 setExtensionsVisible(true);
257 }
258
259 void DolphinStatusBar::updateSpaceInfo()
260 {
261 m_spaceInfo->update();
262 }
263
264 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent* event)
265 {
266 Q_UNUSED(event)
267
268 QMenu menu(this);
269
270 QAction* showZoomSliderAction = menu.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
271 showZoomSliderAction->setCheckable(true);
272 showZoomSliderAction->setChecked(GeneralSettings::showZoomSlider());
273
274 QAction* showSpaceInfoAction = menu.addAction(i18nc("@action:inmenu", "Show Space Information"));
275 showSpaceInfoAction->setCheckable(true);
276 showSpaceInfoAction->setChecked(GeneralSettings::showSpaceInfo());
277
278 const QAction* action = menu.exec(QCursor::pos());
279 if (action == showZoomSliderAction) {
280 const bool visible = showZoomSliderAction->isChecked();
281 GeneralSettings::setShowZoomSlider(visible);
282 m_zoomSlider->setVisible(visible);
283 } else if (action == showSpaceInfoAction) {
284 const bool visible = showSpaceInfoAction->isChecked();
285 GeneralSettings::setShowSpaceInfo(visible);
286 m_spaceInfo->setVisible(visible);
287 }
288 }
289
290 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel)
291 {
292 updateZoomSliderToolTip(zoomLevel);
293
294 QPoint global = m_zoomSlider->rect().topLeft();
295 global.ry() += m_zoomSlider->height() / 2;
296 QHelpEvent toolTipEvent(QEvent::ToolTip, QPoint(0, 0), m_zoomSlider->mapToGlobal(global));
297 QApplication::sendEvent(m_zoomSlider, &toolTipEvent);
298 }
299
300 void DolphinStatusBar::updateProgressInfo()
301 {
302 if (m_progress < 100) {
303 // Show the progress information and hide the extensions
304 m_stopButton->show();
305 m_progressTextLabel->show();
306 m_progressBar->show();
307 setExtensionsVisible(false);
308 } else {
309 // Hide the progress information and show the extensions
310 m_stopButton->hide();
311 m_progressTextLabel->hide();
312 m_progressBar->hide();
313 setExtensionsVisible(true);
314 }
315 }
316
317 void DolphinStatusBar::updateLabelText()
318 {
319 const QString text = m_text.isEmpty() ? m_defaultText : m_text;
320 m_label->setText(text);
321 }
322
323 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel)
324 {
325 const int size = ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel);
326 m_zoomSlider->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size));
327 }
328
329 void DolphinStatusBar::setExtensionsVisible(bool visible)
330 {
331 bool showSpaceInfo = visible;
332 bool showZoomSlider = visible;
333 if (visible) {
334 showSpaceInfo = GeneralSettings::showSpaceInfo();
335 showZoomSlider = GeneralSettings::showZoomSlider();
336 }
337 m_spaceInfo->setShown(showSpaceInfo);
338 m_spaceInfo->setVisible(showSpaceInfo);
339 m_zoomSlider->setVisible(showZoomSlider);
340 }
341