1
/***************************************************************************
2
* Copyright (C) 2006 by Peter Penz *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License as published by *
7
* the Free Software Foundation; either version 2 of the License, or *
8
* (at your option) any later version. *
10
* This program is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program; if not, write to the *
17
* Free Software Foundation, Inc., *
18
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19
***************************************************************************/
21
#include "dolphinstatusbar.h"
22
#include "settings/dolphinsettings.h"
23
#include "dolphinview.h"
24
#include "dolphin_generalsettings.h"
25
#include "statusbarmessagelabel.h"
26
#include "statusbarspaceinfo.h"
27
#include "zoomlevelinfo.h"
29
#include <QApplication>
30
#include <QHBoxLayout>
32
#include <QProgressBar>
33
#include <QToolButton>
36
#include <kiconloader.h>
41
DolphinStatusBar::DolphinStatusBar(QWidget* parent, DolphinView* view) :
56
connect(m_view, SIGNAL(urlChanged(const KUrl&)),
57
this, SLOT(updateSpaceInfoContent(const KUrl&)));
59
// initialize message label
60
m_messageLabel = new StatusBarMessageLabel(this);
61
m_messageLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
63
// initialize zoom slider
64
m_zoomWidget = new QWidget(this);
66
m_zoomOut = new QToolButton(m_zoomWidget);
67
m_zoomOut->setIcon(KIcon("zoom-out"));
68
m_zoomOut->setAutoRaise(true);
70
m_zoomSlider = new QSlider(Qt::Horizontal, m_zoomWidget);
71
m_zoomSlider->setPageStep(1);
73
const int min = ZoomLevelInfo::minimumLevel();
74
const int max = ZoomLevelInfo::maximumLevel();
75
m_zoomSlider->setRange(min, max);
76
m_zoomSlider->setValue(view->zoomLevel());
77
updateZoomSliderToolTip(view->zoomLevel());
79
m_zoomIn = new QToolButton(m_zoomWidget);
80
m_zoomIn->setIcon(KIcon("zoom-in"));
81
m_zoomIn->setAutoRaise(true);
83
QHBoxLayout* zoomWidgetLayout = new QHBoxLayout(m_zoomWidget);
84
zoomWidgetLayout->setSpacing(0);
85
zoomWidgetLayout->setMargin(0);
86
zoomWidgetLayout->addWidget(m_zoomOut);
87
zoomWidgetLayout->addWidget(m_zoomSlider);
88
zoomWidgetLayout->addWidget(m_zoomIn);
90
connect(m_zoomSlider, SIGNAL(valueChanged(int)), this, SLOT(setZoomLevel(int)));
91
connect(m_zoomSlider, SIGNAL(sliderMoved(int)), this, SLOT(showZoomSliderToolTip(int)));
92
connect(m_view, SIGNAL(zoomLevelChanged(int)), m_zoomSlider, SLOT(setValue(int)));
93
connect(m_zoomOut, SIGNAL(clicked()), this, SLOT(zoomOut()));
94
connect(m_zoomIn, SIGNAL(clicked()), this, SLOT(zoomIn()));
96
// initialize space information
97
m_spaceInfo = new StatusBarSpaceInfo(this);
98
m_spaceInfo->setUrl(m_view->url());
100
// initialize progress information
101
m_progressText = new QLabel(this);
102
m_progressText->hide();
104
m_progressBar = new QProgressBar(this);
105
m_progressBar->hide();
108
const int fontHeight = QFontMetrics(m_messageLabel->font()).height();
109
const int zoomWidgetHeight = m_zoomWidget->minimumSizeHint().height();
110
const int contentHeight = qMax(fontHeight, zoomWidgetHeight);
112
m_messageLabel->setMinimumTextHeight(contentHeight);
113
m_spaceInfo->setFixedHeight(contentHeight - 5);
114
m_progressBar->setFixedSize(200, contentHeight);
115
m_zoomWidget->setFixedSize(150, contentHeight);
117
setExtensionsVisible(true);
120
DolphinStatusBar::~DolphinStatusBar()
124
void DolphinStatusBar::setMessage(const QString& msg,
127
if ((msg == m_messageLabel->text()) && (type == m_messageLabel->type())) {
131
m_messageLabel->setMessage(msg, type);
133
const int widthGap = m_messageLabel->widthGap();
135
m_progressBar->hide();
136
m_progressText->hide();
141
DolphinStatusBar::Type DolphinStatusBar::type() const
143
return m_messageLabel->type();
146
QString DolphinStatusBar::message() const
148
return m_messageLabel->text();
151
void DolphinStatusBar::setProgressText(const QString& text)
153
m_progressText->setText(text);
156
QString DolphinStatusBar::progressText() const
158
return m_progressText->text();
161
void DolphinStatusBar::setProgress(int percent)
165
} else if (percent > 100) {
169
m_progress = percent;
170
if (m_messageLabel->type() == Error) {
171
// don't update any widget or status bar text if an
172
// error message is shown
176
m_progressBar->setValue(m_progress);
177
if (!m_progressBar->isVisible() || (percent == 100)) {
178
QTimer::singleShot(300, this, SLOT(updateProgressInfo()));
181
const QString& defaultText = m_messageLabel->defaultText();
182
const QString msg(m_messageLabel->text());
183
if ((percent == 0) && !msg.isEmpty()) {
184
setMessage(QString(), Default);
185
} else if ((percent == 100) && (msg != defaultText)) {
186
setMessage(defaultText, Default);
190
void DolphinStatusBar::clear()
192
setMessage(m_messageLabel->defaultText(), Default);
195
void DolphinStatusBar::setDefaultText(const QString& text)
197
m_messageLabel->setDefaultText(text);
200
const QString& DolphinStatusBar::defaultText() const
202
return m_messageLabel->defaultText();
205
void DolphinStatusBar::refresh()
207
setExtensionsVisible(true);
211
void DolphinStatusBar::resizeEvent(QResizeEvent* event)
213
// It is important to hide the widgets before invoking
214
// QWidget::resizeEvent(), otherwise it is possible that
215
// the dock widgets are shrinked when having a small
216
// statusbar width (bug #202147).
217
m_zoomWidget->hide();
220
QWidget::resizeEvent(event);
221
QMetaObject::invokeMethod(this, "assureVisibleText", Qt::QueuedConnection);
224
void DolphinStatusBar::updateProgressInfo()
226
const bool isErrorShown = (m_messageLabel->type() == Error);
227
if (m_progress < 100) {
228
// show the progress information and hide the extensions
229
setExtensionsVisible(false);
231
m_progressText->show();
232
m_progressBar->show();
235
// hide the progress information and show the extensions
236
m_progressText->hide();
237
m_progressBar->hide();
242
void DolphinStatusBar::updateSpaceInfoContent(const KUrl& url)
244
m_spaceInfo->setUrl(url);
248
void DolphinStatusBar::setZoomLevel(int zoomLevel)
250
m_zoomOut->setEnabled(zoomLevel > m_zoomSlider->minimum());
251
m_zoomIn->setEnabled(zoomLevel < m_zoomSlider->maximum());
252
m_view->setZoomLevel(zoomLevel);
253
updateZoomSliderToolTip(zoomLevel);
256
void DolphinStatusBar::assureVisibleText()
258
const int widthGap = m_messageLabel->widthGap();
259
if (m_spaceInfo->isVisible() || m_zoomWidget->isVisible()) {
260
// At least the space information or the zoom slider is shown.
261
// Hide them if the status bar text does not fit into the available width.
262
setExtensionsVisible(widthGap <= 0);
263
} else if (!m_progressBar->isVisible()) {
264
const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
265
const int spaceInfoWidth = settings->showSpaceInfo() ? m_spaceInfo->minimumWidth() : 0;
266
const int zoomWidgetWidth = settings->showZoomSlider() ? m_zoomWidget->minimumWidth() : 0;
267
const int widgetsWidth = spaceInfoWidth + zoomWidgetWidth;
268
setExtensionsVisible(widthGap + widgetsWidth <= 0);
272
void DolphinStatusBar::zoomOut()
274
const int value = m_zoomSlider->value();
275
m_zoomSlider->setValue(value - 1);
278
void DolphinStatusBar::zoomIn()
280
const int value = m_zoomSlider->value();
281
m_zoomSlider->setValue(value + 1);
284
void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel)
286
updateZoomSliderToolTip(zoomLevel);
288
QPoint global = m_zoomSlider->rect().topLeft();
289
global.ry() += m_zoomSlider->height() / 2;
290
QHelpEvent toolTipEvent(QEvent::ToolTip, QPoint(0, 0), m_zoomSlider->mapToGlobal(global));
291
QApplication::sendEvent(m_zoomSlider, &toolTipEvent);
294
void DolphinStatusBar::setExtensionsVisible(bool visible)
296
bool spaceInfoVisible = visible;
297
bool zoomSliderVisible = visible;
299
const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
300
spaceInfoVisible = settings->showSpaceInfo();
301
zoomSliderVisible = settings->showZoomSlider();
304
m_spaceInfo->setVisible(spaceInfoVisible);
305
m_zoomWidget->setVisible(zoomSliderVisible);
308
void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel)
310
const int size = ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel);
311
m_zoomSlider->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size));
314
#include "dolphinstatusbar.moc"