delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / apps / dolphin / src / dolphinstatusbar.cpp
blob7a9b5dc8a834fc88005504e28790f9319bcbf494
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz *
3 * peter.penz@gmx.at *
4 * *
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. *
9 * *
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. *
14 * *
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>
31 #include <QLabel>
32 #include <QProgressBar>
33 #include <QToolButton>
34 #include <QTimer>
36 #include <kiconloader.h>
37 #include <kicon.h>
38 #include <klocale.h>
39 #include <kvbox.h>
41 DolphinStatusBar::DolphinStatusBar(QWidget* parent, DolphinView* view) :
42 KHBox(parent),
43 m_view(view),
44 m_messageLabel(0),
45 m_spaceInfo(0),
46 m_zoomWidget(0),
47 m_zoomOut(0),
48 m_zoomSlider(0),
49 m_zoomIn(0),
50 m_progressBar(0),
51 m_progress(100)
53 setMargin(0);
54 setSpacing(4);
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();
107 // initialize sizes
108 const int fontHeight = QFontMetrics(m_messageLabel->font()).height();
109 const int zoomWidgetHeight = m_zoomWidget->minimumSizeHint().height();
110 const int contentHeight = (fontHeight < zoomWidgetHeight) ? zoomWidgetHeight : fontHeight;
112 m_messageLabel->setMinimumTextHeight(contentHeight);
113 m_spaceInfo->setFixedHeight(contentHeight);
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,
125 Type type)
127 if ((msg == m_messageLabel->text()) && (type == m_messageLabel->type())) {
128 return;
131 m_messageLabel->setMessage(msg, type);
133 const int widthGap = m_messageLabel->widthGap();
134 if (widthGap > 0) {
135 m_progressBar->hide();
136 m_progressText->hide();
138 assureVisibleText();
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)
163 if (percent < 0) {
164 percent = 0;
165 } else if (percent > 100) {
166 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
173 return;
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);
208 assureVisibleText();
211 void DolphinStatusBar::resizeEvent(QResizeEvent* event)
213 QWidget::resizeEvent(event);
214 QMetaObject::invokeMethod(this, "assureVisibleText", Qt::QueuedConnection);
217 void DolphinStatusBar::updateProgressInfo()
219 const bool isErrorShown = (m_messageLabel->type() == Error);
220 if (m_progress < 100) {
221 // show the progress information and hide the extensions
222 setExtensionsVisible(false);
223 if (!isErrorShown) {
224 m_progressText->show();
225 m_progressBar->show();
227 } else {
228 // hide the progress information and show the extensions
229 m_progressText->hide();
230 m_progressBar->hide();
231 assureVisibleText();
235 void DolphinStatusBar::updateSpaceInfoContent(const KUrl& url)
237 m_spaceInfo->setUrl(url);
238 assureVisibleText();
241 void DolphinStatusBar::setZoomLevel(int zoomLevel)
243 m_zoomOut->setEnabled(zoomLevel > m_zoomSlider->minimum());
244 m_zoomIn->setEnabled(zoomLevel < m_zoomSlider->maximum());
245 m_view->setZoomLevel(zoomLevel);
246 updateZoomSliderToolTip(zoomLevel);
249 void DolphinStatusBar::assureVisibleText()
251 const int widthGap = m_messageLabel->widthGap();
252 if (m_spaceInfo->isVisible() || m_zoomWidget->isVisible()) {
253 // At least the space information or the zoom slider is shown.
254 // Hide them if the status bar text does not fit into the available width.
255 if (widthGap > 0) {
256 setExtensionsVisible(false);
258 } else if (!m_progressBar->isVisible()) {
259 const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
260 const int spaceInfoWidth = settings->showSpaceInfo() ? m_spaceInfo->minimumWidth() : 0;
261 const int zoomWidgetWidth = settings->showZoomSlider() ? m_zoomWidget->minimumWidth() : 0;
262 const int widgetsWidth = spaceInfoWidth + zoomWidgetWidth;
264 if (widthGap + widgetsWidth <= 0) {
265 setExtensionsVisible(true);
270 void DolphinStatusBar::zoomOut()
272 const int value = m_zoomSlider->value();
273 m_zoomSlider->setValue(value - 1);
276 void DolphinStatusBar::zoomIn()
278 const int value = m_zoomSlider->value();
279 m_zoomSlider->setValue(value + 1);
282 void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel)
284 updateZoomSliderToolTip(zoomLevel);
286 QPoint global = m_zoomSlider->rect().topLeft();
287 global.ry() += m_zoomSlider->height() / 2;
288 QHelpEvent toolTipEvent(QEvent::ToolTip, QPoint(0, 0), m_zoomSlider->mapToGlobal(global));
289 QApplication::sendEvent(m_zoomSlider, &toolTipEvent);
292 void DolphinStatusBar::setExtensionsVisible(bool visible)
294 bool spaceInfoVisible = visible;
295 bool zoomSliderVisible = visible;
296 if (visible) {
297 const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
298 spaceInfoVisible = settings->showSpaceInfo();
299 zoomSliderVisible = settings->showZoomSlider();
302 m_spaceInfo->setVisible(spaceInfoVisible);
303 m_zoomWidget->setVisible(zoomSliderVisible);
306 void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel)
308 const int size = ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel);
309 m_zoomSlider->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size));
312 #include "dolphinstatusbar.moc"