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