]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinstatusbar.cpp
Small fix to the zoom slider bar, the old sliderMoved(int) signal is just fired when...
[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(valueChanged(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 DolphinStatusBar::~DolphinStatusBar()
94 {
95 }
96
97 void DolphinStatusBar::setMessage(const QString& msg,
98 Type type)
99 {
100 m_messageLabel->setMessage(msg, type);
101
102 const int widthGap = m_messageLabel->widthGap();
103 if (widthGap > 0) {
104 m_progressBar->hide();
105 m_progressText->hide();
106 }
107 assureVisibleText();
108 }
109
110 DolphinStatusBar::Type DolphinStatusBar::type() const
111 {
112 return m_messageLabel->type();
113 }
114
115 QString DolphinStatusBar::message() const
116 {
117 return m_messageLabel->text();
118 }
119
120 void DolphinStatusBar::setProgressText(const QString& text)
121 {
122 m_progressText->setText(text);
123 }
124
125 QString DolphinStatusBar::progressText() const
126 {
127 return m_progressText->text();
128 }
129
130 void DolphinStatusBar::setProgress(int percent)
131 {
132 if (percent < 0) {
133 percent = 0;
134 } else if (percent > 100) {
135 percent = 100;
136 }
137
138 m_progress = percent;
139 if (m_messageLabel->type() == Error) {
140 // don't update any widget or status bar text if an
141 // error message is shown
142 return;
143 }
144
145 m_progressBar->setValue(m_progress);
146 if (!m_progressBar->isVisible() || (percent == 100)) {
147 QTimer::singleShot(300, this, SLOT(updateProgressInfo()));
148 }
149
150 const QString& defaultText = m_messageLabel->defaultText();
151 const QString msg(m_messageLabel->text());
152 if ((percent == 0) && !msg.isEmpty()) {
153 setMessage(QString(), Default);
154 } else if ((percent == 100) && (msg != defaultText)) {
155 setMessage(defaultText, Default);
156 }
157 }
158
159 void DolphinStatusBar::clear()
160 {
161 setMessage(m_messageLabel->defaultText(), Default);
162 }
163
164 void DolphinStatusBar::setDefaultText(const QString& text)
165 {
166 m_messageLabel->setDefaultText(text);
167 }
168
169 const QString& DolphinStatusBar::defaultText() const
170 {
171 return m_messageLabel->defaultText();
172 }
173
174 void DolphinStatusBar::refresh()
175 {
176 setExtensionsVisible(true);
177 assureVisibleText();
178 }
179
180 void DolphinStatusBar::resizeEvent(QResizeEvent* event)
181 {
182 QWidget::resizeEvent(event);
183 QMetaObject::invokeMethod(this, "assureVisibleText", Qt::QueuedConnection);
184 }
185
186 void DolphinStatusBar::updateProgressInfo()
187 {
188 const bool isErrorShown = (m_messageLabel->type() == Error);
189 if (m_progress < 100) {
190 // show the progress information and hide the extensions
191 setExtensionsVisible(false);
192 if (!isErrorShown) {
193 m_progressText->show();
194 m_progressBar->show();
195 }
196 } else {
197 // hide the progress information and show the extensions
198 m_progressText->hide();
199 m_progressBar->hide();
200 assureVisibleText();
201 }
202 }
203
204 void DolphinStatusBar::updateSpaceInfoContent(const KUrl& url)
205 {
206 m_spaceInfo->setUrl(url);
207 assureVisibleText();
208 }
209
210 void DolphinStatusBar::setZoomLevel(int zoomLevel)
211 {
212 m_view->setZoomLevel(zoomLevel);
213 }
214
215 void DolphinStatusBar::assureVisibleText()
216 {
217 const int widthGap = m_messageLabel->widthGap();
218 const bool isProgressBarVisible = m_progressBar->isVisible();
219
220 const int spaceInfoWidth = m_spaceInfo->isVisible() ? m_spaceInfo->width() : 0;
221 const int zoomSliderWidth = m_zoomSlider->isVisible() ? m_zoomSlider->width() : 0;
222 const int widgetsWidth = spaceInfoWidth + zoomSliderWidth;
223
224 if (widgetsWidth > 0) {
225 // The space information and (or) the zoom slider are (is) shown.
226 // Hide them if the status bar text does not fit into the available width.
227 if (widthGap > 0) {
228 setExtensionsVisible(false);
229 }
230 } else if (!isProgressBarVisible && (widthGap + widgetsWidth <= 0)) {
231 setExtensionsVisible(true);
232 }
233 }
234
235 void DolphinStatusBar::setExtensionsVisible(bool visible)
236 {
237 bool spaceInfoVisible = visible;
238 bool zoomSliderVisible = visible;
239 if (visible) {
240 const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
241 spaceInfoVisible = settings->showSpaceInfo();
242 zoomSliderVisible = settings->showZoomSlider();
243 }
244
245 m_spaceInfo->setVisible(spaceInfoVisible);
246 m_zoomSlider->setVisible(zoomSliderVisible);
247 }
248
249 #include "dolphinstatusbar.moc"