]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinstatusbar.cpp
Further optimizations: do a delayed update of the geometry. This leads to a reduced...
[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 m_progressBar->setMaximumWidth(200);
56 setMinimumHeight(size.height());
57 m_messageLabel->setMinimumTextHeight(size.height());
58
59 connect(parent, SIGNAL(urlChanged(const KUrl&)),
60 this, SLOT(updateSpaceInfoContent(const KUrl&)));
61 }
62
63
64 DolphinStatusBar::~DolphinStatusBar()
65 {
66 }
67
68 void DolphinStatusBar::setMessage(const QString& msg,
69 Type type)
70 {
71 m_messageLabel->setText(msg);
72 m_messageLabel->setType(type);
73
74 const int widthGap = m_messageLabel->widthGap();
75 if (widthGap > 0) {
76 m_progressBar->hide();
77 m_progressText->hide();
78 }
79 showSpaceInfo();
80 }
81
82 DolphinStatusBar::Type DolphinStatusBar::type() const
83 {
84 return m_messageLabel->type();
85 }
86
87 QString DolphinStatusBar::message() const
88 {
89 return m_messageLabel->text();
90 }
91
92 void DolphinStatusBar::setProgressText(const QString& text)
93 {
94 m_progressText->setText(text);
95 }
96
97 QString DolphinStatusBar::progressText() const
98 {
99 return m_progressText->text();
100 }
101
102 void DolphinStatusBar::setProgress(int percent)
103 {
104 if (percent < 0) {
105 percent = 0;
106 }
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 msg(m_messageLabel->text());
124 if ((percent == 0) && !msg.isEmpty()) {
125 setMessage(QString::null, Default);
126 }
127 else if ((percent == 100) && (msg != m_defaultText)) {
128 setMessage(m_defaultText, Default);
129 }
130 }
131
132 void DolphinStatusBar::clear()
133 {
134 // TODO: check for timeout, so that it's prevented that
135 // a message is cleared too early.
136 setMessage(m_defaultText, Default);
137 }
138
139 void DolphinStatusBar::setDefaultText(const QString& text)
140 {
141 m_defaultText = text;
142 }
143
144 void DolphinStatusBar::resizeEvent(QResizeEvent* event)
145 {
146 QWidget::resizeEvent(event);
147 QTimer::singleShot(0, this, SLOT(showSpaceInfo()));
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 }
161 else {
162 // hide the progress information and show the space information
163 m_progressText->hide();
164 m_progressBar->hide();
165 showSpaceInfo();
166 }
167 }
168
169 void DolphinStatusBar::updateSpaceInfoContent(const KUrl& url)
170 {
171 m_spaceInfo->setUrl(url);
172 showSpaceInfo();
173 }
174
175 void DolphinStatusBar::showSpaceInfo()
176 {
177 const int widthGap = m_messageLabel->widthGap();
178 const bool isProgressBarVisible = m_progressBar->isVisible();
179
180 if (m_spaceInfo->isVisible()) {
181 // The space information is shown currently. Hide it
182 // if the progress bar is visible or if the status bar
183 // text does not fit into the available width.
184 const QSize size(m_progressBar->sizeHint());
185 if (isProgressBarVisible || (widthGap > 0)) {
186 m_spaceInfo->hide();
187 }
188 }
189 else if (widthGap + m_spaceInfo->width() <= 0) {
190 m_spaceInfo->show();
191 }
192 }
193
194 #include "dolphinstatusbar.moc"