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"
24 #include <KIconLoader>
30 #include "statusbarspaceinfo.h"
32 #include <QApplication>
34 #include <QHBoxLayout>
36 #include <QProgressBar>
37 #include <QTextDocument>
38 #include <QToolButton>
42 #include <views/dolphinview.h>
43 #include <views/zoomlevelinfo.h>
46 const int ResetToDefaultTimeout
= 1000;
49 DolphinStatusBar::DolphinStatusBar(QWidget
* parent
) :
59 m_showProgressBarTimer(0),
60 m_resetToDefaultTextTimer(0),
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);
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());
76 connect(m_zoomSlider
, SIGNAL(valueChanged(int)), this, SIGNAL(zoomLevelChanged(int)));
77 connect(m_zoomSlider
, SIGNAL(sliderMoved(int)), this, SLOT(showZoomSliderToolTip(int)));
79 // Initialize space information
80 m_spaceInfo
= new StatusBarSpaceInfo(this);
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"));
89 connect(m_stopButton
, SIGNAL(clicked()), this, SIGNAL(stopPressed()));
91 m_progressTextLabel
= new QLabel(this);
92 m_progressTextLabel
->hide();
94 m_progressBar
= new QProgressBar(this);
95 m_progressBar
->hide();
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()));
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()));
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
);
112 m_label
->setFixedHeight(contentHeight
);
113 m_label
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
115 m_zoomSlider
->setFixedHeight(contentHeight
);
116 m_zoomSlider
->setMaximumWidth(150);
118 m_spaceInfo
->setFixedHeight(contentHeight
);
119 m_spaceInfo
->setMaximumWidth(150);
120 m_spaceInfo
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
122 m_progressBar
->setFixedHeight(contentHeight
);
123 m_progressBar
->setMaximumWidth(150);
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
);
135 setExtensionsVisible(true);
138 DolphinStatusBar::~DolphinStatusBar()
142 void DolphinStatusBar::setText(const QString
& text
)
144 if (m_text
== text
) {
148 m_textTimestamp
= QTime::currentTime();
150 if (text
.isEmpty()) {
151 // Assure that the previous set text won't get
152 // cleared immediatelly.
153 m_resetToDefaultTextTimer
->start();
157 if (m_resetToDefaultTextTimer
->isActive()) {
158 m_resetToDefaultTextTimer
->start();
165 QString
DolphinStatusBar::text() const
170 void DolphinStatusBar::setProgressText(const QString
& text
)
172 m_progressTextLabel
->setText(text
);
175 QString
DolphinStatusBar::progressText() const
177 return m_progressTextLabel
->text();
180 void DolphinStatusBar::setProgress(int percent
)
182 // Show a busy indicator if a value < 0 is provided:
183 m_progressBar
->setMaximum((percent
< 0) ? 0 : 100);
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();
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();
203 int DolphinStatusBar::progress() const
208 void DolphinStatusBar::resetToDefaultText()
211 if (currentTime
.msecsTo(m_textTimestamp
) < ResetToDefaultTimeout
) {
212 m_resetToDefaultTextTimer
->start();
214 m_resetToDefaultTextTimer
->stop();
215 slotResetToDefaultText();
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 KUrl
& url
)
232 m_spaceInfo
->setUrl(url
);
235 KUrl
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
);
244 updateZoomSliderToolTip(zoomLevel
);
248 int DolphinStatusBar::zoomLevel() const
250 return m_zoomSlider
->value();
253 void DolphinStatusBar::readSettings()
255 setExtensionsVisible(true);
258 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent
* event
)
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());
269 QAction
* showSpaceInfoAction
= menu
.addAction(i18nc("@action:inmenu", "Show Space Information"));
270 showSpaceInfoAction
->setCheckable(true);
271 showSpaceInfoAction
->setChecked(GeneralSettings::showSpaceInfo());
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
);
289 bool DolphinStatusBar::eventFilter(QObject
* obj
, QEvent
* event
)
291 if (obj
== m_label
&& event
->type() == QEvent::Resize
) {
294 return QWidget::eventFilter(obj
, event
);
297 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel
)
299 updateZoomSliderToolTip(zoomLevel
);
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
);
307 void DolphinStatusBar::updateProgressInfo()
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);
316 // Hide the progress information and show the extensions
317 m_stopButton
->hide();
318 m_progressTextLabel
->hide();
319 m_progressBar
->hide();
320 setExtensionsVisible(true);
324 void DolphinStatusBar::updateLabelText()
326 const QString text
= m_text
.isEmpty() ? m_defaultText
: m_text
;
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
);
333 // If the text has been elided, set the original text as tooltip
334 if (text
!= elidedText
) {
335 m_label
->setToolTip(Qt::convertFromPlainText(text
));
337 m_label
->setToolTip(QString());
341 void DolphinStatusBar::slotResetToDefaultText()
347 void DolphinStatusBar::setExtensionsVisible(bool visible
)
349 bool showSpaceInfo
= visible
;
350 bool showZoomSlider
= visible
;
352 showSpaceInfo
= GeneralSettings::showSpaceInfo();
353 showZoomSlider
= GeneralSettings::showZoomSlider();
355 m_spaceInfo
->setVisible(showSpaceInfo
);
356 m_zoomSlider
->setVisible(showZoomSlider
);
359 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel
)
361 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel
);
362 m_zoomSlider
->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size
));
365 #include "dolphinstatusbar.moc"