]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinstatusbar.cpp
remove the asynchronous update of the zooming again, it decreases the "feeled" perfor...
[dolphin.git] / src / dolphinstatusbar.cpp
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 ***************************************************************************/
20
21 #include "dolphinstatusbar.h"
22 #include "dolphinsettings.h"
23 #include "dolphinview.h"
24 #include "dolphin_generalsettings.h"
25 #include "statusbarmessagelabel.h"
26 #include "statusbarspaceinfo.h"
27 #include "zoomlevelinfo.h"
28
29 #include <QtGui/QLabel>
30 #include <QtGui/QProgressBar>
31 #include <QtCore/QTimer>
32
33 #include <kiconloader.h>
34 #include <kvbox.h>
35
36 DolphinStatusBar::DolphinStatusBar(QWidget* parent, DolphinView* view) :
37 KHBox(parent),
38 m_view(view),
39 m_messageLabel(0),
40 m_spaceInfo(0),
41 m_zoomSlider(0),
42 m_progressBar(0),
43 m_progress(100)
44 {
45 setSpacing(4);
46
47 connect(m_view, SIGNAL(urlChanged(const KUrl&)),
48 this, SLOT(updateSpaceInfoContent(const KUrl&)));
49
50 // initialize message label
51 m_messageLabel = new StatusBarMessageLabel(this);
52 m_messageLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
53
54 // initialize space information
55 m_spaceInfo = new StatusBarSpaceInfo(this);
56 m_spaceInfo->setUrl(m_view->url());
57
58 // initialize zoom slider
59 m_zoomSlider = new QSlider(Qt::Horizontal, this);
60 m_zoomSlider->setPageStep(1);
61
62 const int min = ZoomLevelInfo::minimumLevel();
63 const int max = ZoomLevelInfo::maximumLevel();
64 m_zoomSlider->setRange(min, max);
65 m_zoomSlider->setValue(view->zoomLevel());
66
67 connect(m_zoomSlider, SIGNAL(sliderMoved(int)),
68 this, SLOT(setZoomLevel(int)));
69 connect(m_view, SIGNAL(zoomLevelChanged(int)),
70 m_zoomSlider, SLOT(setValue(int)));
71
72 // initialize progress informatino
73 m_progressText = new QLabel(this);
74 m_progressText->hide();
75
76 m_progressBar = new QProgressBar(this);
77 m_progressBar->hide();
78
79 // initialize sizes
80 const int contentHeight = QFontMetrics(m_messageLabel->font()).height() + 4;
81 const int barHeight = contentHeight + 4;
82
83 setMinimumHeight(barHeight);
84 m_messageLabel->setMinimumTextHeight(barHeight);
85 m_spaceInfo->setFixedHeight(contentHeight);
86 m_progressBar->setFixedHeight(contentHeight);
87 m_progressBar->setMaximumWidth(200);
88 m_zoomSlider->setMaximumWidth(100);
89
90 setExtensionsVisible(true);
91 }
92
93
94 DolphinStatusBar::~DolphinStatusBar()
95 {
96 }
97
98 void DolphinStatusBar::setMessage(const QString& msg,
99 Type type)
100 {
101 m_messageLabel->setMessage(msg, type);
102
103 const int widthGap = m_messageLabel->widthGap();
104 if (widthGap > 0) {
105 m_progressBar->hide();
106 m_progressText->hide();
107 }
108 assureVisibleText();
109 }
110
111 DolphinStatusBar::Type DolphinStatusBar::type() const
112 {
113 return m_messageLabel->type();
114 }
115
116 QString DolphinStatusBar::message() const
117 {
118 return m_messageLabel->text();
119 }
120
121 void DolphinStatusBar::setProgressText(const QString& text)
122 {
123 m_progressText->setText(text);
124 }
125
126 QString DolphinStatusBar::progressText() const
127 {
128 return m_progressText->text();
129 }
130
131 void DolphinStatusBar::setProgress(int percent)
132 {
133 if (percent < 0) {
134 percent = 0;
135 } else if (percent > 100) {
136 percent = 100;
137 }
138
139 m_progress = percent;
140 if (m_messageLabel->type() == Error) {
141 // don't update any widget or status bar text if an
142 // error message is shown
143 return;
144 }
145
146 m_progressBar->setValue(m_progress);
147 if (!m_progressBar->isVisible() || (percent == 100)) {
148 QTimer::singleShot(300, this, SLOT(updateProgressInfo()));
149 }
150
151 const QString& defaultText = m_messageLabel->defaultText();
152 const QString msg(m_messageLabel->text());
153 if ((percent == 0) && !msg.isEmpty()) {
154 setMessage(QString(), Default);
155 } else if ((percent == 100) && (msg != defaultText)) {
156 setMessage(defaultText, Default);
157 }
158 }
159
160 void DolphinStatusBar::clear()
161 {
162 setMessage(m_messageLabel->defaultText(), Default);
163 }
164
165 void DolphinStatusBar::setDefaultText(const QString& text)
166 {
167 m_messageLabel->setDefaultText(text);
168 }
169
170 const QString& DolphinStatusBar::defaultText() const
171 {
172 return m_messageLabel->defaultText();
173 }
174
175 void DolphinStatusBar::resizeEvent(QResizeEvent* event)
176 {
177 QWidget::resizeEvent(event);
178 QMetaObject::invokeMethod(this, "assureVisibleText", Qt::QueuedConnection);
179 }
180
181 void DolphinStatusBar::updateProgressInfo()
182 {
183 const bool isErrorShown = (m_messageLabel->type() == Error);
184 if (m_progress < 100) {
185 // show the progress information and hide the extensions
186 setExtensionsVisible(false);
187 if (!isErrorShown) {
188 m_progressText->show();
189 m_progressBar->show();
190 }
191 } else {
192 // hide the progress information and show the extensions
193 m_progressText->hide();
194 m_progressBar->hide();
195 assureVisibleText();
196 }
197 }
198
199 void DolphinStatusBar::updateSpaceInfoContent(const KUrl& url)
200 {
201 m_spaceInfo->setUrl(url);
202 assureVisibleText();
203 }
204
205 void DolphinStatusBar::setZoomLevel(int zoomLevel)
206 {
207 m_view->setZoomLevel(zoomLevel);
208 }
209
210 void DolphinStatusBar::assureVisibleText()
211 {
212 const int widthGap = m_messageLabel->widthGap();
213 const bool isProgressBarVisible = m_progressBar->isVisible();
214
215 const int spaceInfoWidth = m_spaceInfo->isVisible() ? m_spaceInfo->width() : 0;
216 const int zoomSliderWidth = m_zoomSlider->isVisible() ? m_zoomSlider->width() : 0;
217 const int widgetsWidth = spaceInfoWidth + zoomSliderWidth;
218
219 if (widgetsWidth > 0) {
220 // The space information and (or) the zoom slider are (is) shown.
221 // Hide them if the status bar text does not fit into the available width.
222 if (widthGap > 0) {
223 setExtensionsVisible(false);
224 }
225 } else if (!isProgressBarVisible && (widthGap + widgetsWidth <= 0)) {
226 setExtensionsVisible(true);
227 }
228 }
229
230 void DolphinStatusBar::setExtensionsVisible(bool visible)
231 {
232 bool spaceInfoVisible = visible;
233 bool zoomSliderVisible = visible;
234 if (visible) {
235 const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
236 spaceInfoVisible = settings->showSpaceInfo();
237 zoomSliderVisible = settings->showZoomSlider();
238 }
239
240 m_spaceInfo->setVisible(spaceInfoVisible);
241 m_zoomSlider->setVisible(zoomSliderVisible);
242 }
243
244 #include "dolphinstatusbar.moc"