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
->installEventFilter(this);
67 // Initialize zoom widget
68 m_zoomSlider
= new QSlider(Qt::Horizontal
, this);
69 m_zoomSlider
->setAccessibleName(i18n("Zoom"));
70 m_zoomSlider
->setAccessibleDescription(i18nc("Description for zoom-slider (accessibility)", "Sets the size of the file icons."));
71 m_zoomSlider
->setPageStep(1);
72 m_zoomSlider
->setRange(ZoomLevelInfo::minimumLevel(), ZoomLevelInfo::maximumLevel());
74 connect(m_zoomSlider
, SIGNAL(valueChanged(int)), this, SIGNAL(zoomLevelChanged(int)));
75 connect(m_zoomSlider
, SIGNAL(sliderMoved(int)), this, SLOT(showZoomSliderToolTip(int)));
77 // Initialize space information
78 m_spaceInfo
= new StatusBarSpaceInfo(this);
80 // Initialize progress information
81 m_stopButton
= new QToolButton(this);
82 m_stopButton
->setIcon(KIcon("process-stop"));
83 m_stopButton
->setAccessibleName(i18n("Stop"));
84 m_stopButton
->setAutoRaise(true);
85 m_stopButton
->setToolTip(i18nc("@tooltip", "Stop loading"));
87 connect(m_stopButton
, SIGNAL(clicked()), this, SIGNAL(stopPressed()));
89 m_progressTextLabel
= new QLabel(this);
90 m_progressTextLabel
->hide();
92 m_progressBar
= new QProgressBar(this);
93 m_progressBar
->hide();
95 m_showProgressBarTimer
= new QTimer(this);
96 m_showProgressBarTimer
->setInterval(500);
97 m_showProgressBarTimer
->setSingleShot(true);
98 connect(m_showProgressBarTimer
, SIGNAL(timeout()), this, SLOT(updateProgressInfo()));
100 m_resetToDefaultTextTimer
= new QTimer(this);
101 m_resetToDefaultTextTimer
->setInterval(ResetToDefaultTimeout
);
102 m_resetToDefaultTextTimer
->setSingleShot(true);
103 connect(m_resetToDefaultTextTimer
, SIGNAL(timeout()), this, SLOT(slotResetToDefaultText()));
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 contentHeight
= qMax(fontHeight
, zoomSliderHeight
);
110 m_label
->setMinimumHeight(contentHeight
);
111 m_label
->setMaximumHeight(contentHeight
);
112 m_label
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
114 const QSize
size(150, contentHeight
);
115 applyFixedWidgetSize(m_spaceInfo
, size
);
116 applyFixedWidgetSize(m_progressBar
, size
);
117 applyFixedWidgetSize(m_zoomSlider
, size
);
119 QHBoxLayout
* topLayout
= new QHBoxLayout(this);
120 topLayout
->setMargin(0);
121 topLayout
->setSpacing(4);
122 topLayout
->addWidget(m_label
);
123 topLayout
->addWidget(m_zoomSlider
);
124 topLayout
->addWidget(m_spaceInfo
);
125 topLayout
->addWidget(m_stopButton
);
126 topLayout
->addWidget(m_progressTextLabel
);
127 topLayout
->addWidget(m_progressBar
);
129 setExtensionsVisible(true);
132 DolphinStatusBar::~DolphinStatusBar()
136 void DolphinStatusBar::setText(const QString
& text
)
138 if (m_text
== text
) {
142 m_textTimestamp
= QTime::currentTime();
144 if (text
.isEmpty()) {
145 // Assure that the previous set text won't get
146 // cleared immediatelly.
147 m_resetToDefaultTextTimer
->start();
151 if (m_resetToDefaultTextTimer
->isActive()) {
152 m_resetToDefaultTextTimer
->start();
159 QString
DolphinStatusBar::text() const
164 void DolphinStatusBar::setProgressText(const QString
& text
)
166 m_progressTextLabel
->setText(text
);
169 QString
DolphinStatusBar::progressText() const
171 return m_progressTextLabel
->text();
174 void DolphinStatusBar::setProgress(int percent
)
176 // Show a busy indicator if a value < 0 is provided:
177 m_progressBar
->setMaximum((percent
< 0) ? 0 : 100);
179 percent
= qBound(0, percent
, 100);
180 const bool progressRestarted
= (percent
< 100) && (percent
< m_progress
);
181 m_progress
= percent
;
182 if (progressRestarted
&& !m_progressBar
->isVisible()) {
183 // Show the progress bar delayed: In the case if 100 % are reached within
184 // a short time, no progress bar will be shown at all.
185 m_showProgressBarTimer
->start();
188 m_progressBar
->setValue(m_progress
);
189 if (percent
== 100) {
190 // The end of the progress has been reached. Assure that the progress bar
191 // gets hidden and the extensions widgets get visible again.
192 m_showProgressBarTimer
->stop();
193 updateProgressInfo();
197 int DolphinStatusBar::progress() const
202 void DolphinStatusBar::resetToDefaultText()
205 if (currentTime
.msecsTo(m_textTimestamp
) < ResetToDefaultTimeout
) {
206 m_resetToDefaultTextTimer
->start();
208 m_resetToDefaultTextTimer
->stop();
209 slotResetToDefaultText();
213 void DolphinStatusBar::setDefaultText(const QString
& text
)
215 m_defaultText
= text
;
219 QString
DolphinStatusBar::defaultText() const
221 return m_defaultText
;
224 void DolphinStatusBar::setUrl(const KUrl
& url
)
226 m_spaceInfo
->setUrl(url
);
229 KUrl
DolphinStatusBar::url() const
231 return m_spaceInfo
->url();
234 void DolphinStatusBar::setZoomLevel(int zoomLevel
)
236 if (zoomLevel
!= m_zoomSlider
->value()) {
237 m_zoomSlider
->setValue(zoomLevel
);
238 updateZoomSliderToolTip(zoomLevel
);
242 int DolphinStatusBar::zoomLevel() const
244 return m_zoomSlider
->value();
247 void DolphinStatusBar::readSettings()
249 setExtensionsVisible(true);
252 void DolphinStatusBar::contextMenuEvent(QContextMenuEvent
* event
)
258 QAction
* copyAction
= menu
.addAction(i18nc("@action:inmenu", "Copy Text"));
259 QAction
* showZoomSliderAction
= menu
.addAction(i18nc("@action:inmenu", "Show Zoom Slider"));
260 showZoomSliderAction
->setCheckable(true);
261 showZoomSliderAction
->setChecked(GeneralSettings::showZoomSlider());
263 QAction
* showSpaceInfoAction
= menu
.addAction(i18nc("@action:inmenu", "Show Space Information"));
264 showSpaceInfoAction
->setCheckable(true);
265 showSpaceInfoAction
->setChecked(GeneralSettings::showSpaceInfo());
267 const QAction
* action
= menu
.exec(QCursor::pos());
268 if (action
== copyAction
) {
269 QMimeData
* mimeData
= new QMimeData();
270 mimeData
->setText(text());
271 QApplication::clipboard()->setMimeData(mimeData
);
272 } else if (action
== showZoomSliderAction
) {
273 const bool visible
= showZoomSliderAction
->isChecked();
274 GeneralSettings::setShowZoomSlider(visible
);
275 m_zoomSlider
->setVisible(visible
);
276 } else if (action
== showSpaceInfoAction
) {
277 const bool visible
= showSpaceInfoAction
->isChecked();
278 GeneralSettings::setShowSpaceInfo(visible
);
279 m_spaceInfo
->setVisible(visible
);
283 bool DolphinStatusBar::eventFilter(QObject
* obj
, QEvent
* event
)
285 if (obj
== m_label
&& event
->type() == QEvent::Resize
) {
288 return QWidget::eventFilter(obj
, event
);
291 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel
)
293 updateZoomSliderToolTip(zoomLevel
);
295 QPoint global
= m_zoomSlider
->rect().topLeft();
296 global
.ry() += m_zoomSlider
->height() / 2;
297 QHelpEvent
toolTipEvent(QEvent::ToolTip
, QPoint(0, 0), m_zoomSlider
->mapToGlobal(global
));
298 QApplication::sendEvent(m_zoomSlider
, &toolTipEvent
);
301 void DolphinStatusBar::updateProgressInfo()
303 if (m_progress
< 100) {
304 // Show the progress information and hide the extensions
305 m_stopButton
->show();
306 m_progressTextLabel
->show();
307 m_progressBar
->show();
308 setExtensionsVisible(false);
310 // Hide the progress information and show the extensions
311 m_stopButton
->hide();
312 m_progressTextLabel
->hide();
313 m_progressBar
->hide();
314 setExtensionsVisible(true);
318 void DolphinStatusBar::updateLabelText()
320 const QString text
= m_text
.isEmpty() ? m_defaultText
: m_text
;
322 QFontMetrics
fontMetrics(m_label
->font());
323 const QString elidedText
= fontMetrics
.elidedText(text
, Qt::ElideRight
, m_label
->width());
324 m_label
->setText(elidedText
);
325 m_label
->setToolTip(text
== elidedText
? QString() : text
);
328 void DolphinStatusBar::slotResetToDefaultText()
334 void DolphinStatusBar::setExtensionsVisible(bool visible
)
336 bool showSpaceInfo
= visible
;
337 bool showZoomSlider
= visible
;
339 showSpaceInfo
= GeneralSettings::showSpaceInfo();
340 showZoomSlider
= GeneralSettings::showZoomSlider();
342 m_spaceInfo
->setVisible(showSpaceInfo
);
343 m_zoomSlider
->setVisible(showZoomSlider
);
346 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel
)
348 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel
);
349 m_zoomSlider
->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size
));
352 void DolphinStatusBar::applyFixedWidgetSize(QWidget
* widget
, const QSize
& size
)
354 widget
->setMinimumSize(size
);
355 widget
->setMaximumSize(size
);
356 widget
->setSizePolicy(QSizePolicy::Fixed
, QSizePolicy::Fixed
);
359 #include "dolphinstatusbar.moc"