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