]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinstatusbar.cpp
adapt Dolphin to kdelibs coding style (http://techbase.kde.org/Policies/Kdelibs_Codin...
[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 "dolphinview.h"
23 #include "statusbarmessagelabel.h"
24 #include "statusbarspaceinfo.h"
25
26 #include <QLabel>
27 #include <QProgressBar>
28 #include <QTimer>
29
30 #include <kiconloader.h>
31 #include <kvbox.h>
32
33 DolphinStatusBar::DolphinStatusBar(DolphinView* parent) :
34 KHBox(parent),
35 m_messageLabel(0),
36 m_spaceInfo(0),
37 m_progressBar(0),
38 m_progress(100)
39 {
40 setSpacing(4);
41
42 m_messageLabel = new StatusBarMessageLabel(this);
43 m_messageLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
44
45 m_spaceInfo = new StatusBarSpaceInfo(this);
46 m_spaceInfo->setUrl(parent->url());
47
48 m_progressText = new QLabel(this);
49 m_progressText->hide();
50
51 m_progressBar = new QProgressBar(this);
52 m_progressBar->hide();
53
54 const QSize size(m_progressBar->sizeHint());
55 const int barHeight = size.height();
56
57 m_progressBar->setMaximumWidth(200);
58 setMinimumHeight(barHeight);
59 m_messageLabel->setMinimumTextHeight(barHeight);
60 m_spaceInfo->setFixedHeight(barHeight);
61
62 connect(parent, SIGNAL(urlChanged(const KUrl&)),
63 this, SLOT(updateSpaceInfoContent(const KUrl&)));
64 }
65
66
67 DolphinStatusBar::~DolphinStatusBar()
68 {}
69
70 void DolphinStatusBar::setMessage(const QString& msg,
71 Type type)
72 {
73 m_messageLabel->setMessage(msg, type);
74
75 const int widthGap = m_messageLabel->widthGap();
76 if (widthGap > 0) {
77 m_progressBar->hide();
78 m_progressText->hide();
79 }
80 showSpaceInfo();
81 }
82
83 DolphinStatusBar::Type DolphinStatusBar::type() const
84 {
85 return m_messageLabel->type();
86 }
87
88 QString DolphinStatusBar::message() const
89 {
90 return m_messageLabel->text();
91 }
92
93 void DolphinStatusBar::setProgressText(const QString& text)
94 {
95 m_progressText->setText(text);
96 }
97
98 QString DolphinStatusBar::progressText() const
99 {
100 return m_progressText->text();
101 }
102
103 void DolphinStatusBar::setProgress(int percent)
104 {
105 if (percent < 0) {
106 percent = 0;
107 } else if (percent > 100) {
108 percent = 100;
109 }
110
111 m_progress = percent;
112 if (m_messageLabel->type() == Error) {
113 // don't update any widget or status bar text if an
114 // error message is shown
115 return;
116 }
117
118 m_progressBar->setValue(m_progress);
119 if (!m_progressBar->isVisible() || (percent == 100)) {
120 QTimer::singleShot(500, this, SLOT(updateProgressInfo()));
121 }
122
123 const QString& defaultText = m_messageLabel->defaultText();
124 const QString msg(m_messageLabel->text());
125 if ((percent == 0) && !msg.isEmpty()) {
126 setMessage(QString(), Default);
127 } else if ((percent == 100) && (msg != defaultText)) {
128 setMessage(defaultText, Default);
129 }
130 }
131
132 void DolphinStatusBar::clear()
133 {
134 setMessage(m_messageLabel->defaultText(), Default);
135 }
136
137 void DolphinStatusBar::setDefaultText(const QString& text)
138 {
139 m_messageLabel->setDefaultText(text);
140 }
141
142 const QString& DolphinStatusBar::defaultText() const
143 {
144 return m_messageLabel->defaultText();
145 }
146
147 void DolphinStatusBar::resizeEvent(QResizeEvent* event)
148 {
149 QWidget::resizeEvent(event);
150 QTimer::singleShot(0, this, SLOT(showSpaceInfo()));
151 }
152
153 void DolphinStatusBar::updateProgressInfo()
154 {
155 const bool isErrorShown = (m_messageLabel->type() == Error);
156 if (m_progress < 100) {
157 // show the progress information and hide the space information
158 m_spaceInfo->hide();
159 if (!isErrorShown) {
160 m_progressText->show();
161 m_progressBar->show();
162 }
163 } else {
164 // hide the progress information and show the space information
165 m_progressText->hide();
166 m_progressBar->hide();
167 showSpaceInfo();
168 }
169 }
170
171 void DolphinStatusBar::updateSpaceInfoContent(const KUrl& url)
172 {
173 m_spaceInfo->setUrl(url);
174 showSpaceInfo();
175 }
176
177 void DolphinStatusBar::showSpaceInfo()
178 {
179 const int widthGap = m_messageLabel->widthGap();
180 const bool isProgressBarVisible = m_progressBar->isVisible();
181
182 if (m_spaceInfo->isVisible()) {
183 // The space information is shown currently. Hide it
184 // if the progress bar is visible or if the status bar
185 // text does not fit into the available width.
186 const QSize size(m_progressBar->sizeHint());
187 if (isProgressBarVisible || (widthGap > 0)) {
188 m_spaceInfo->hide();
189 }
190 } else if (widthGap + m_spaceInfo->width() <= 0) {
191 m_spaceInfo->show();
192 }
193 }
194
195 #include "dolphinstatusbar.moc"