]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/dolphinstatusbar.cpp
dolphin: Escape text in statusbar tooltip
[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
24 #include <KIconLoader>
25 #include <KIcon>
26 #include <KLocale>
27 #include <KMenu>
28 #include <KVBox>
29
30 #include "statusbarspaceinfo.h"
31
32 #include <QApplication>
33 #include <QClipboard>
34 #include <QHBoxLayout>
35 #include <QLabel>
36 #include <QProgressBar>
37 #include <QTextDocument>
38 #include <QToolButton>
39 #include <QTime>
40 #include <QTimer>
41
42 #include <views/dolphinview.h>
43 #include <views/zoomlevelinfo.h>
44
45 namespace {
46 const int ResetToDefaultTimeout = 1000;
47 }
48
49 DolphinStatusBar::DolphinStatusBar(QWidget* parent) :
50 QWidget(parent),
51 m_text(),
52 m_defaultText(),
53 m_label(0),
54 m_spaceInfo(0),
55 m_zoomSlider(0),
56 m_progressBar(0),
57 m_stopButton(0),
58 m_progress(100),
59 m_showProgressBarTimer(0),
60 m_resetToDefaultTextTimer(0),
61 m_textTimestamp()
62 {
63 // Initialize text label
64 m_label = new QLabel(this);
65 m_label->setWordWrap(true);
66 m_label->setTextFormat(Qt::PlainText);
67 m_label->installEventFilter(this);
68
69 // Initialize zoom widget
70 m_zoomSlider = new QSlider(Qt::Horizontal, this);
71 m_zoomSlider->setAccessibleName(i18n("Zoom"));
72 m_zoomSlider->setAccessibleDescription(i18nc("Description for zoom-slider (accessibility)", "Sets the size of the file icons."));
73 m_zoomSlider->setPageStep(1);
74 m_zoomSlider->setRange(ZoomLevelInfo::minimumLevel(), ZoomLevelInfo::maximumLevel());
75
76 connect(m_zoomSlider, SIGNAL(valueChanged(int)), this, SIGNAL(zoomLevelChanged(int)));
77 connect(m_zoomSlider, SIGNAL(sliderMoved(int)), this, SLOT(showZoomSliderToolTip(int)));
78
79 // Initialize space information
80 m_spaceInfo = new StatusBarSpaceInfo(this);
81
82 // Initialize progress information
83 m_stopButton = new QToolButton(this);
84 m_stopButton->setIcon(KIcon("process-stop"));
85 m_stopButton->setAccessibleName(i18n("Stop"));
86 m_stopButton->setAutoRaise(true);
87 m_stopButton->setToolTip(i18nc("@tooltip", "Stop loading"));
88 m_stopButton->hide();
89 connect(m_stopButton, SIGNAL(clicked()), this, SIGNAL(stopPressed()));
90
91 m_progressTextLabel = new QLabel(this);
92 m_progressTextLabel->hide();
93
94 m_progressBar = new QProgressBar(this);
95 m_progressBar->hide();
96
97 m_showProgressBarTimer = new QTimer(this);
98 m_showProgressBarTimer->setInterval(500);
99 m_showProgressBarTimer->setSingleShot(true);
100 connect(m_showProgressBarTimer, SIGNAL(timeout()), this, SLOT(updateProgressInfo()));
101
102 m_resetToDefaultTextTimer = new QTimer(this);
103 m_resetToDefaultTextTimer->setInterval(ResetToDefaultTimeout);
104 m_resetToDefaultTextTimer->setSingleShot(true);
105 connect(m_resetToDefaultTextTimer, SIGNAL(timeout()), this, SLOT(slotResetToDefaultText()));
106
107 // Initialize top layout and size policies
108 const int fontHeight = QFontMetrics(m_label->font()).height();
109 const int zoomSliderHeight = m_zoomSlider->minimumSizeHint().height();
110 const int contentHeight = qMax(fontHeight, zoomSliderHeight);
111
112 m_label->setFixedHeight(contentHeight);
113 m_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
114
115 m_zoomSlider->setFixedHeight(contentHeight);
116 m_zoomSlider->setMaximumWidth(150);
117
118 m_spaceInfo->setFixedHeight(contentHeight);
119 m_spaceInfo->setMaximumWidth(150);
120 m_spaceInfo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
121
122 m_progressBar->setFixedHeight(contentHeight);
123 m_progressBar->setMaximumWidth(150);
124
125 QHBoxLayout* topLayout = new QHBoxLayout(this);
126 topLayout->setMargin(0);
127 topLayout->setSpacing(4);
128 topLayout->addWidget(m_label);
129 topLayout->addWidget(m_zoomSlider);
130 topLayout->addWidget(m_spaceInfo);
131 topLayout->addWidget(m_stopButton);
132 topLayout->addWidget(m_progressTextLabel);
133 topLayout->addWidget(m_progressBar);
134
135 setExtensionsVisible(true);
136 }
137
138 DolphinStatusBar::~DolphinStatusBar()
139 {
140 }
141
142 void DolphinStatusBar::setText(const QString& text)
143 {
144 if (m_text == text) {
145 return;
146 }
147
148 m_textTimestamp = QTime::currentTime();
149
150 if (text.isEmpty()) {
151 // Assure that the previous set text won't get
152 // cleared immediatelly.
153 m_resetToDefaultTextTimer->start();
154 } else {
155 m_text = text;
156
157 if (m_resetToDefaultTextTimer->isActive()) {
158 m_resetToDefaultTextTimer->start();
159 }
160
161 updateLabelText();
162 }
163 }
164
165 QString DolphinStatusBar::text() const
166 {
167 return m_text;
168 }
169
170 void DolphinStatusBar::setProgressText(const QString& text)
171 {
172 m_progressTextLabel->setText(text);
173 }
174
175 QString DolphinStatusBar::progressText() const
176 {
177 return m_progressTextLabel->text();
178 }
179
180 void DolphinStatusBar::setProgress(int percent)
181 {
182 // Show a busy indicator if a value < 0 is provided:
183 m_progressBar->setMaximum((percent < 0) ? 0 : 100);
184
185 percent = qBound(0, percent, 100);
186 const bool progressRestarted = (percent < 100) && (percent < m_progress);
187 m_progress = percent;
188 if (progressRestarted && !m_progressBar->isVisible()) {
189 // Show the progress bar delayed: In the case if 100 % are reached within
190 // a short time, no progress bar will be shown at all.
191 m_showProgressBarTimer->start();
192 }
193
194 m_progressBar->setValue(m_progress);
195 if (percent == 100) {
196 // The end of the progress has been reached. Assure that the progress bar
197 // gets hidden and the extensions widgets get visible again.
198 m_showProgressBarTimer->stop();
199 updateProgressInfo();
200 }
201 }
202
203 int DolphinStatusBar::progress() const
204 {
205 return m_progress;
206 }
207
208 void DolphinStatusBar::resetToDefaultText()
209 {
210 QTime currentTime;
211 if (currentTime.msecsTo(m_textTimestamp) < ResetToDefaultTimeout) {
212 m_resetToDefaultTextTimer->start();
213 } else {
214 m_resetToDefaultTextTimer->stop();
215 slotResetToDefaultText();
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 KUrl& url)
231 {
232 m_spaceInfo->setUrl(url);
233 }
234
235 KUrl 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 updateZoomSliderToolTip(zoomLevel);
245 }
246 }
247
248 int DolphinStatusBar::zoomLevel() const
249 {
250 return m_zoomSlider->value();
251 }
252
253 void DolphinStatusBar::readSettings()
254 {
255 setExtensionsVisible(true);
256 }
257
258 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent* event)
259 {
260 Q_UNUSED(event);
261
262 KMenu menu(this);
263
264 QAction* copyAction = menu.addAction(i18nc("@action:inmenu", "Copy Text"));
265 QAction* showZoomSliderAction = menu.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
266 showZoomSliderAction->setCheckable(true);
267 showZoomSliderAction->setChecked(GeneralSettings::showZoomSlider());
268
269 QAction* showSpaceInfoAction = menu.addAction(i18nc("@action:inmenu", "Show Space Information"));
270 showSpaceInfoAction->setCheckable(true);
271 showSpaceInfoAction->setChecked(GeneralSettings::showSpaceInfo());
272
273 const QAction* action = menu.exec(QCursor::pos());
274 if (action == copyAction) {
275 QMimeData* mimeData = new QMimeData();
276 mimeData->setText(text());
277 QApplication::clipboard()->setMimeData(mimeData);
278 } else if (action == showZoomSliderAction) {
279 const bool visible = showZoomSliderAction->isChecked();
280 GeneralSettings::setShowZoomSlider(visible);
281 m_zoomSlider->setVisible(visible);
282 } else if (action == showSpaceInfoAction) {
283 const bool visible = showSpaceInfoAction->isChecked();
284 GeneralSettings::setShowSpaceInfo(visible);
285 m_spaceInfo->setVisible(visible);
286 }
287 }
288
289 bool DolphinStatusBar::eventFilter(QObject* obj, QEvent* event)
290 {
291 if (obj == m_label && event->type() == QEvent::Resize) {
292 updateLabelText();
293 }
294 return QWidget::eventFilter(obj, event);
295 }
296
297 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel)
298 {
299 updateZoomSliderToolTip(zoomLevel);
300
301 QPoint global = m_zoomSlider->rect().topLeft();
302 global.ry() += m_zoomSlider->height() / 2;
303 QHelpEvent toolTipEvent(QEvent::ToolTip, QPoint(0, 0), m_zoomSlider->mapToGlobal(global));
304 QApplication::sendEvent(m_zoomSlider, &toolTipEvent);
305 }
306
307 void DolphinStatusBar::updateProgressInfo()
308 {
309 if (m_progress < 100) {
310 // Show the progress information and hide the extensions
311 m_stopButton->show();
312 m_progressTextLabel->show();
313 m_progressBar->show();
314 setExtensionsVisible(false);
315 } else {
316 // Hide the progress information and show the extensions
317 m_stopButton->hide();
318 m_progressTextLabel->hide();
319 m_progressBar->hide();
320 setExtensionsVisible(true);
321 }
322 }
323
324 void DolphinStatusBar::updateLabelText()
325 {
326 const QString text = m_text.isEmpty() ? m_defaultText : m_text;
327
328 // Set status bar text and elide it if too long
329 QFontMetrics fontMetrics(m_label->font());
330 const QString elidedText = fontMetrics.elidedText(text, Qt::ElideRight, m_label->width());
331 m_label->setText(elidedText);
332
333 // If the text has been elided, set the original text as tooltip
334 if (text != elidedText) {
335 m_label->setToolTip(Qt::convertFromPlainText(text));
336 } else {
337 m_label->setToolTip(QString());
338 }
339 }
340
341 void DolphinStatusBar::slotResetToDefaultText()
342 {
343 m_text.clear();
344 updateLabelText();
345 }
346
347 void DolphinStatusBar::setExtensionsVisible(bool visible)
348 {
349 bool showSpaceInfo = visible;
350 bool showZoomSlider = visible;
351 if (visible) {
352 showSpaceInfo = GeneralSettings::showSpaceInfo();
353 showZoomSlider = GeneralSettings::showZoomSlider();
354 }
355 m_spaceInfo->setVisible(showSpaceInfo);
356 m_zoomSlider->setVisible(showZoomSlider);
357 }
358
359 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel)
360 {
361 const int size = ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel);
362 m_zoomSlider->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size));
363 }
364
365 #include "dolphinstatusbar.moc"