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 <QToolButton>
41 #include <views/dolphinview.h>
42 #include <views/zoomlevelinfo.h>
45 const int ResetToDefaultTimeout
= 1000;
48 DolphinStatusBar::DolphinStatusBar(QWidget
* parent
) :
58 m_showProgressBarTimer(0),
59 m_resetToDefaultTextTimer(0),
62 // Initialize text label
63 m_label
= new QLabel(this);
64 m_label
->setWordWrap(true);
65 m_label
->setTextFormat(Qt::PlainText
);
66 m_label
->installEventFilter(this);
68 // Initialize zoom widget
69 m_zoomSlider
= new QSlider(Qt::Horizontal
, this);
70 m_zoomSlider
->setAccessibleName(i18n("Zoom"));
71 m_zoomSlider
->setAccessibleDescription(i18nc("Description for zoom-slider (accessibility)", "Sets the size of the file icons."));
72 m_zoomSlider
->setPageStep(1);
73 m_zoomSlider
->setRange(ZoomLevelInfo::minimumLevel(), ZoomLevelInfo::maximumLevel());
75 connect(m_zoomSlider
, SIGNAL(valueChanged(int)), this, SIGNAL(zoomLevelChanged(int)));
76 connect(m_zoomSlider
, SIGNAL(sliderMoved(int)), this, SLOT(showZoomSliderToolTip(int)));
78 // Initialize space information
79 m_spaceInfo
= new StatusBarSpaceInfo(this);
81 // Initialize progress information
82 m_stopButton
= new QToolButton(this);
83 m_stopButton
->setIcon(KIcon("process-stop"));
84 m_stopButton
->setAccessibleName(i18n("Stop"));
85 m_stopButton
->setAutoRaise(true);
86 m_stopButton
->setToolTip(i18nc("@tooltip", "Stop loading"));
88 connect(m_stopButton
, SIGNAL(clicked()), this, SIGNAL(stopPressed()));
90 m_progressTextLabel
= new QLabel(this);
91 m_progressTextLabel
->hide();
93 m_progressBar
= new QProgressBar(this);
94 m_progressBar
->hide();
96 m_showProgressBarTimer
= new QTimer(this);
97 m_showProgressBarTimer
->setInterval(500);
98 m_showProgressBarTimer
->setSingleShot(true);
99 connect(m_showProgressBarTimer
, SIGNAL(timeout()), this, SLOT(updateProgressInfo()));
101 m_resetToDefaultTextTimer
= new QTimer(this);
102 m_resetToDefaultTextTimer
->setInterval(ResetToDefaultTimeout
);
103 m_resetToDefaultTextTimer
->setSingleShot(true);
104 connect(m_resetToDefaultTextTimer
, SIGNAL(timeout()), this, SLOT(slotResetToDefaultText()));
106 // Initialize top layout and size policies
107 const int fontHeight
= QFontMetrics(m_label
->font()).height();
108 const int zoomSliderHeight
= m_zoomSlider
->minimumSizeHint().height();
109 const int contentHeight
= qMax(fontHeight
, zoomSliderHeight
);
111 m_label
->setFixedHeight(contentHeight
);
112 m_label
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
114 m_zoomSlider
->setFixedHeight(contentHeight
);
115 m_zoomSlider
->setMaximumWidth(150);
117 m_spaceInfo
->setFixedHeight(contentHeight
);
118 m_spaceInfo
->setMaximumWidth(150);
119 m_spaceInfo
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
121 m_progressBar
->setFixedHeight(contentHeight
);
122 m_progressBar
->setMaximumWidth(150);
124 QHBoxLayout
* topLayout
= new QHBoxLayout(this);
125 topLayout
->setMargin(0);
126 topLayout
->setSpacing(4);
127 topLayout
->addWidget(m_label
);
128 topLayout
->addWidget(m_zoomSlider
);
129 topLayout
->addWidget(m_spaceInfo
);
130 topLayout
->addWidget(m_stopButton
);
131 topLayout
->addWidget(m_progressTextLabel
);
132 topLayout
->addWidget(m_progressBar
);
134 setExtensionsVisible(true);
137 DolphinStatusBar::~DolphinStatusBar()
141 void DolphinStatusBar::setText(const QString
& text
)
143 if (m_text
== text
) {
147 m_textTimestamp
= QTime::currentTime();
149 if (text
.isEmpty()) {
150 // Assure that the previous set text won't get
151 // cleared immediatelly.
152 m_resetToDefaultTextTimer
->start();
156 if (m_resetToDefaultTextTimer
->isActive()) {
157 m_resetToDefaultTextTimer
->start();
164 QString
DolphinStatusBar::text() const
169 void DolphinStatusBar::setProgressText(const QString
& text
)
171 m_progressTextLabel
->setText(text
);
174 QString
DolphinStatusBar::progressText() const
176 return m_progressTextLabel
->text();
179 void DolphinStatusBar::setProgress(int percent
)
181 // Show a busy indicator if a value < 0 is provided:
182 m_progressBar
->setMaximum((percent
< 0) ? 0 : 100);
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();
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();
202 int DolphinStatusBar::progress() const
207 void DolphinStatusBar::resetToDefaultText()
210 if (currentTime
.msecsTo(m_textTimestamp
) < ResetToDefaultTimeout
) {
211 m_resetToDefaultTextTimer
->start();
213 m_resetToDefaultTextTimer
->stop();
214 slotResetToDefaultText();
218 void DolphinStatusBar::setDefaultText(const QString
& text
)
220 m_defaultText
= text
;
224 QString
DolphinStatusBar::defaultText() const
226 return m_defaultText
;
229 void DolphinStatusBar::setUrl(const KUrl
& url
)
231 m_spaceInfo
->setUrl(url
);
234 KUrl
DolphinStatusBar::url() const
236 return m_spaceInfo
->url();
239 void DolphinStatusBar::setZoomLevel(int zoomLevel
)
241 if (zoomLevel
!= m_zoomSlider
->value()) {
242 m_zoomSlider
->setValue(zoomLevel
);
243 updateZoomSliderToolTip(zoomLevel
);
247 int DolphinStatusBar::zoomLevel() const
249 return m_zoomSlider
->value();
252 void DolphinStatusBar::readSettings()
254 setExtensionsVisible(true);
257 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent
* event
)
263 QAction
* copyAction
= menu
.addAction(i18nc("@action:inmenu", "Copy Text"));
264 QAction
* showZoomSliderAction
= menu
.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
265 showZoomSliderAction
->setCheckable(true);
266 showZoomSliderAction
->setChecked(GeneralSettings::showZoomSlider());
268 QAction
* showSpaceInfoAction
= menu
.addAction(i18nc("@action:inmenu", "Show Space Information"));
269 showSpaceInfoAction
->setCheckable(true);
270 showSpaceInfoAction
->setChecked(GeneralSettings::showSpaceInfo());
272 const QAction
* action
= menu
.exec(QCursor::pos());
273 if (action
== copyAction
) {
274 QMimeData
* mimeData
= new QMimeData();
275 mimeData
->setText(text());
276 QApplication::clipboard()->setMimeData(mimeData
);
277 } else 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
);
288 bool DolphinStatusBar::eventFilter(QObject
* obj
, QEvent
* event
)
290 if (obj
== m_label
&& event
->type() == QEvent::Resize
) {
293 return QWidget::eventFilter(obj
, event
);
296 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel
)
298 updateZoomSliderToolTip(zoomLevel
);
300 QPoint global
= m_zoomSlider
->rect().topLeft();
301 global
.ry() += m_zoomSlider
->height() / 2;
302 QHelpEvent
toolTipEvent(QEvent::ToolTip
, QPoint(0, 0), m_zoomSlider
->mapToGlobal(global
));
303 QApplication::sendEvent(m_zoomSlider
, &toolTipEvent
);
306 void DolphinStatusBar::updateProgressInfo()
308 if (m_progress
< 100) {
309 // Show the progress information and hide the extensions
310 m_stopButton
->show();
311 m_progressTextLabel
->show();
312 m_progressBar
->show();
313 setExtensionsVisible(false);
315 // Hide the progress information and show the extensions
316 m_stopButton
->hide();
317 m_progressTextLabel
->hide();
318 m_progressBar
->hide();
319 setExtensionsVisible(true);
323 void DolphinStatusBar::updateLabelText()
325 const QString text
= m_text
.isEmpty() ? m_defaultText
: m_text
;
327 QFontMetrics
fontMetrics(m_label
->font());
328 const QString elidedText
= fontMetrics
.elidedText(text
, Qt::ElideRight
, m_label
->width());
329 m_label
->setText(elidedText
);
330 m_label
->setToolTip(text
== elidedText
? QString() : text
);
333 void DolphinStatusBar::slotResetToDefaultText()
339 void DolphinStatusBar::setExtensionsVisible(bool visible
)
341 bool showSpaceInfo
= visible
;
342 bool showZoomSlider
= visible
;
344 showSpaceInfo
= GeneralSettings::showSpaceInfo();
345 showZoomSlider
= GeneralSettings::showZoomSlider();
347 m_spaceInfo
->setVisible(showSpaceInfo
);
348 m_zoomSlider
->setVisible(showZoomSlider
);
351 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel
)
353 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel
);
354 m_zoomSlider
->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size
));
357 #include "dolphinstatusbar.moc"