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