]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbarmessagelabel.cpp
when the column view is used, then 'Split view' should take the root URL of the colum...
[dolphin.git] / src / statusbarmessagelabel.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 "statusbarmessagelabel.h"
22
23 #include <kglobalsettings.h>
24 #include <kiconloader.h>
25 #include <kicon.h>
26 #include <klocale.h>
27
28 #include <QtGui/QFontMetrics>
29 #include <QtGui/QPainter>
30 #include <QtGui/QKeyEvent>
31 #include <QtGui/QPushButton>
32 #include <QtGui/QPixmap>
33 #include <QtCore/QTimer>
34
35 StatusBarMessageLabel::StatusBarMessageLabel(QWidget* parent) :
36 QWidget(parent),
37 m_type(DolphinStatusBar::Default),
38 m_state(Default),
39 m_illumination(0),
40 m_minTextHeight(-1),
41 m_timer(0),
42 m_closeButton(0)
43 {
44 setMinimumHeight(K3Icon::SizeSmall);
45
46 m_timer = new QTimer(this);
47 connect(m_timer, SIGNAL(timeout()),
48 this, SLOT(timerDone()));
49
50 m_closeButton = new QPushButton(i18n("Close"), this);
51 m_closeButton->hide();
52 connect(m_closeButton, SIGNAL(clicked()),
53 this, SLOT(closeErrorMessage()));
54 }
55
56 StatusBarMessageLabel::~StatusBarMessageLabel()
57 {}
58
59 void StatusBarMessageLabel::setMessage(const QString& text,
60 DolphinStatusBar::Type type)
61 {
62 if ((text == m_text) && (type == m_type)) {
63 return;
64 }
65
66 if (m_type == DolphinStatusBar::Error) {
67 if (type == DolphinStatusBar::Error) {
68 m_pendingMessages.insert(0, m_text);
69 } else if ((m_state != Default) || !m_pendingMessages.isEmpty()) {
70 // a non-error message should not be shown, as there
71 // are other pending error messages in the queue
72 return;
73 }
74 }
75
76 m_text = text;
77 m_type = type;
78
79 m_timer->stop();
80 m_illumination = 0;
81 m_state = Default;
82
83 const char* iconName = 0;
84 QPixmap pixmap;
85 switch (type) {
86 case DolphinStatusBar::OperationCompleted:
87 iconName = "ok";
88 m_closeButton->hide();
89 break;
90
91 case DolphinStatusBar::Information:
92 iconName = "dialog-information";
93 m_closeButton->hide();
94 break;
95
96 case DolphinStatusBar::Error:
97 iconName = "dialog-error";
98 m_timer->start(100);
99 m_state = Illuminate;
100
101 updateCloseButtonPosition();
102 m_closeButton->show();
103 break;
104
105 case DolphinStatusBar::Default:
106 default:
107 m_closeButton->hide();
108 break;
109 }
110
111 m_pixmap = (iconName == 0) ? QPixmap() : SmallIcon(iconName);
112 QTimer::singleShot(GeometryTimeout, this, SLOT(assureVisibleText()));
113 update();
114 }
115
116 void StatusBarMessageLabel::setMinimumTextHeight(int min)
117 {
118 if (min != m_minTextHeight) {
119 m_minTextHeight = min;
120 setMinimumHeight(min);
121 m_closeButton->setFixedHeight(min - borderGap() * 2);
122 }
123 }
124
125 int StatusBarMessageLabel::widthGap() const
126 {
127 QFontMetrics fontMetrics(font());
128 const int defaultGap = 10;
129 return fontMetrics.width(m_text) - availableTextWidth() + defaultGap;
130 }
131
132 void StatusBarMessageLabel::paintEvent(QPaintEvent* /* event */)
133 {
134 QPainter painter(this);
135
136 // draw background
137 QColor backgroundColor(palette().brush(QPalette::Background).color());
138 QColor foregroundColor(KGlobalSettings::textColor());
139 if (m_illumination > 0) {
140 backgroundColor = mixColors(backgroundColor, QColor(255, 255, 128), m_illumination);
141 foregroundColor = mixColors(foregroundColor, QColor(0, 0, 0), m_illumination);
142 }
143 painter.setBrush(backgroundColor);
144 painter.setPen(backgroundColor);
145 painter.drawRect(QRect(0, 0, width(), height()));
146
147 // draw pixmap
148 int x = borderGap();
149 int y = (m_minTextHeight - m_pixmap.height()) / 2;
150
151 if (!m_pixmap.isNull()) {
152 painter.drawPixmap(x, y, m_pixmap);
153 x += m_pixmap.width() + borderGap();
154 }
155
156 // draw text
157 painter.setPen(foregroundColor);
158 int flags = Qt::AlignVCenter;
159 if (height() > m_minTextHeight) {
160 flags = flags | Qt::TextWordWrap;
161 }
162 painter.drawText(QRect(x, 0, availableTextWidth(), height()), flags, m_text);
163 painter.end();
164 }
165
166 void StatusBarMessageLabel::resizeEvent(QResizeEvent* event)
167 {
168 QWidget::resizeEvent(event);
169 updateCloseButtonPosition();
170 QTimer::singleShot(GeometryTimeout, this, SLOT(assureVisibleText()));
171 }
172
173 void StatusBarMessageLabel::timerDone()
174 {
175 switch (m_state) {
176 case Illuminate: {
177 // increase the illumination
178 if (m_illumination < 100) {
179 m_illumination += 20;
180 update();
181 } else {
182 m_state = Illuminated;
183 m_timer->start(5000);
184 }
185 break;
186 }
187
188 case Illuminated: {
189 // start desaturation
190 m_state = Desaturate;
191 m_timer->start(100);
192 break;
193 }
194
195 case Desaturate: {
196 // desaturate
197 if (m_illumination > 0) {
198 m_illumination -= 5;
199 update();
200 } else {
201 m_state = Default;
202 m_timer->stop();
203 }
204 break;
205 }
206
207 default:
208 break;
209 }
210 }
211
212 void StatusBarMessageLabel::assureVisibleText()
213 {
214 if (m_text.isEmpty()) {
215 return;
216 }
217
218 // calculate the required height of the widget thats
219 // needed for having a fully visible text
220 QFontMetrics fontMetrics(font());
221 const QRect bounds(fontMetrics.boundingRect(0, 0, availableTextWidth(), height(),
222 Qt::AlignVCenter | Qt::TextWordWrap,
223 m_text));
224 int requiredHeight = bounds.height();
225 if (requiredHeight < m_minTextHeight) {
226 requiredHeight = m_minTextHeight;
227 }
228
229 // Increase/decrease the current height of the widget to the
230 // required height. The increasing/decreasing is done in several
231 // steps to have an animation if the height is modified
232 // (see StatusBarMessageLabel::resizeEvent())
233 const int gap = m_minTextHeight / 2;
234 int minHeight = minimumHeight();
235 if (minHeight < requiredHeight) {
236 minHeight += gap;
237 if (minHeight > requiredHeight) {
238 minHeight = requiredHeight;
239 }
240 setMinimumHeight(minHeight);
241 updateGeometry();
242 } else if (minHeight > requiredHeight) {
243 minHeight -= gap;
244 if (minHeight < requiredHeight) {
245 minHeight = requiredHeight;
246 }
247 setMinimumHeight(minHeight);
248 updateGeometry();
249 }
250
251 updateCloseButtonPosition();
252 }
253
254 int StatusBarMessageLabel::availableTextWidth() const
255 {
256 const int buttonWidth = (m_type == DolphinStatusBar::Error) ?
257 m_closeButton->width() + borderGap() : 0;
258 return width() - m_pixmap.width() - (borderGap() * 4) - buttonWidth;
259 }
260
261 QColor StatusBarMessageLabel::mixColors(const QColor& c1,
262 const QColor& c2,
263 int percent) const
264 {
265 const int recip = 100 - percent;
266 const int red = (c1.red() * recip + c2.red() * percent) / 100;
267 const int green = (c1.green() * recip + c2.green() * percent) / 100;
268 const int blue = (c1.blue() * recip + c2.blue() * percent) / 100;
269 return QColor(red, green, blue);
270 }
271
272 void StatusBarMessageLabel::updateCloseButtonPosition()
273 {
274 const int x = width() - m_closeButton->width() - borderGap();
275 const int y = height() - m_closeButton->height() - borderGap();
276 m_closeButton->move(x, y);
277 }
278
279 void StatusBarMessageLabel::closeErrorMessage()
280 {
281 if (!showPendingMessage()) {
282 reset();
283 setMessage(m_defaultText, DolphinStatusBar::Default);
284 }
285 }
286
287 bool StatusBarMessageLabel::showPendingMessage()
288 {
289 if (!m_pendingMessages.isEmpty()) {
290 reset();
291 setMessage(m_pendingMessages.takeFirst(), DolphinStatusBar::Error);
292 return true;
293 }
294 return false;
295 }
296
297 void StatusBarMessageLabel::reset()
298 {
299 m_text.clear();
300 m_type = DolphinStatusBar::Default;
301 }
302
303 #include "statusbarmessagelabel.moc"