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