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