]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinstatusbar.cpp
${CMAKE_SOURCE_DIR}/libkonq doesn't exist now
[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 <QtGui/QLabel>
27 #include <QtGui/QProgressBar>
28 #include <QtCore/QTimer>
29
30 #include <kiconloader.h>
31 #include <kvbox.h>
32
33 DolphinStatusBar::DolphinStatusBar(QWidget* parent, const KUrl& url) :
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(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
63
64 DolphinStatusBar::~DolphinStatusBar()
65 {}
66
67 void DolphinStatusBar::setMessage(const QString& msg,
68 Type type)
69 {
70 m_messageLabel->setMessage(msg, type);
71
72 const int widthGap = m_messageLabel->widthGap();
73 if (widthGap > 0) {
74 m_progressBar->hide();
75 m_progressText->hide();
76 }
77 showSpaceInfo();
78 }
79
80 DolphinStatusBar::Type DolphinStatusBar::type() const
81 {
82 return m_messageLabel->type();
83 }
84
85 QString DolphinStatusBar::message() const
86 {
87 return m_messageLabel->text();
88 }
89
90 void DolphinStatusBar::setProgressText(const QString& text)
91 {
92 m_progressText->setText(text);
93 }
94
95 QString DolphinStatusBar::progressText() const
96 {
97 return m_progressText->text();
98 }
99
100 void DolphinStatusBar::setProgress(int percent)
101 {
102 if (percent < 0) {
103 percent = 0;
104 } else if (percent > 100) {
105 percent = 100;
106 }
107
108 m_progress = percent;
109 if (m_messageLabel->type() == Error) {
110 // don't update any widget or status bar text if an
111 // error message is shown
112 return;
113 }
114
115 m_progressBar->setValue(m_progress);
116 if (!m_progressBar->isVisible() || (percent == 100)) {
117 QTimer::singleShot(500, this, SLOT(updateProgressInfo()));
118 }
119
120 const QString& defaultText = m_messageLabel->defaultText();
121 const QString msg(m_messageLabel->text());
122 if ((percent == 0) && !msg.isEmpty()) {
123 setMessage(QString(), Default);
124 } else if ((percent == 100) && (msg != defaultText)) {
125 setMessage(defaultText, Default);
126 }
127 }
128
129 void DolphinStatusBar::clear()
130 {
131 setMessage(m_messageLabel->defaultText(), Default);
132 }
133
134 void DolphinStatusBar::setDefaultText(const QString& text)
135 {
136 m_messageLabel->setDefaultText(text);
137 }
138
139 const QString& DolphinStatusBar::defaultText() const
140 {
141 return m_messageLabel->defaultText();
142 }
143
144 void DolphinStatusBar::resizeEvent(QResizeEvent* event)
145 {
146 QWidget::resizeEvent(event);
147 QMetaObject::invokeMethod(this, "showSpaceInfo", Qt::QueuedConnection);
148 }
149
150 void DolphinStatusBar::updateProgressInfo()
151 {
152 const bool isErrorShown = (m_messageLabel->type() == Error);
153 if (m_progress < 100) {
154 // show the progress information and hide the space information
155 m_spaceInfo->hide();
156 if (!isErrorShown) {
157 m_progressText->show();
158 m_progressBar->show();
159 }
160 } else {
161 // hide the progress information and show the space information
162 m_progressText->hide();
163 m_progressBar->hide();
164 showSpaceInfo();
165 }
166 }
167
168 void DolphinStatusBar::updateSpaceInfoContent(const KUrl& url)
169 {
170 m_spaceInfo->setUrl(url);
171 showSpaceInfo();
172 }
173
174 void DolphinStatusBar::showSpaceInfo()
175 {
176 const int widthGap = m_messageLabel->widthGap();
177 const bool isProgressBarVisible = m_progressBar->isVisible();
178
179 if (m_spaceInfo->isVisible()) {
180 // The space information is shown currently. Hide it
181 // if the progress bar is visible or if the status bar
182 // text does not fit into the available width.
183 const QSize size(m_progressBar->sizeHint());
184 if (isProgressBarVisible || (widthGap > 0)) {
185 m_spaceInfo->hide();
186 }
187 } else if (widthGap + m_spaceInfo->width() <= 0) {
188 m_spaceInfo->show();
189 }
190 }
191
192 #include "dolphinstatusbar.moc"