]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbarmessagelabel.cpp
forwardport r757231
[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 <kcolorscheme.h>
24 #include <kiconloader.h>
25 #include <kicon.h>
26 #include <klocale.h>
27
28 #include <QFontMetrics>
29 #include <QPainter>
30 #include <QKeyEvent>
31 #include <QPushButton>
32 #include <QPixmap>
33 #include <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(KIconLoader::SizeSmall);
45
46 QPalette palette;
47 palette.setColor(QPalette::Background, Qt::transparent);
48 setPalette(palette);
49
50 m_timer = new QTimer(this);
51 connect(m_timer, SIGNAL(timeout()),
52 this, SLOT(timerDone()));
53
54 m_closeButton = new QPushButton(i18nc("@action:button", "Close"), this);
55 m_closeButton->hide();
56 connect(m_closeButton, SIGNAL(clicked()),
57 this, SLOT(closeErrorMessage()));
58 }
59
60 StatusBarMessageLabel::~StatusBarMessageLabel()
61 {
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 = "dialog-ok";
93 // "ok" icon should probably be "dialog-success", but we don't have that icon in KDE 4.0
94 m_closeButton->hide();
95 break;
96
97 case DolphinStatusBar::Information:
98 iconName = "dialog-information";
99 m_closeButton->hide();
100 break;
101
102 case DolphinStatusBar::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 if (m_closeButton->height() > min) {
127 m_closeButton->setFixedHeight(min);
128 }
129 }
130 }
131
132 int StatusBarMessageLabel::widthGap() const
133 {
134 QFontMetrics fontMetrics(font());
135 const int defaultGap = 10;
136 return fontMetrics.width(m_text) - availableTextWidth() + defaultGap;
137 }
138
139 void StatusBarMessageLabel::paintEvent(QPaintEvent* /* event */)
140 {
141 QPainter painter(this);
142
143 // draw background
144 QColor backgroundColor = palette().window().color();
145 if (m_illumination > 0) {
146 // at this point, a: we are a second label being drawn over the already
147 // painted status area, so we can be translucent, and b: our palette's
148 // window color (bg only) seems to be wrong (always black)
149 KColorScheme scheme(palette().currentColorGroup(), KColorScheme::Window);
150 backgroundColor = scheme.background(KColorScheme::NegativeBackground).color();
151 backgroundColor.setAlpha(qMin(255, m_illumination*2));
152 }
153 painter.setBrush(backgroundColor);
154 painter.setPen(Qt::NoPen);
155 painter.drawRect(QRect(0, 0, width(), height()));
156
157 // draw pixmap
158 int x = BorderGap;
159 int y = (m_minTextHeight - m_pixmap.height()) / 2;
160
161 if (!m_pixmap.isNull()) {
162 painter.drawPixmap(x, y, m_pixmap);
163 x += m_pixmap.width() + BorderGap;
164 }
165
166 // draw text
167 painter.setPen(palette().windowText().color());
168 int flags = Qt::AlignVCenter;
169 if (height() > m_minTextHeight) {
170 flags = flags | Qt::TextWordWrap;
171 }
172 painter.drawText(QRect(x, 0, availableTextWidth(), height()), flags, m_text);
173 painter.end();
174 }
175
176 void StatusBarMessageLabel::resizeEvent(QResizeEvent* event)
177 {
178 QWidget::resizeEvent(event);
179 updateCloseButtonPosition();
180 QTimer::singleShot(GeometryTimeout, this, SLOT(assureVisibleText()));
181 }
182
183 void StatusBarMessageLabel::timerDone()
184 {
185 switch (m_state) {
186 case Illuminate: {
187 // increase the illumination
188 const int illumination_max = 128;
189 if (m_illumination < illumination_max) {
190 m_illumination += 32;
191 if (m_illumination > illumination_max) {
192 m_illumination = illumination_max;
193 }
194 update();
195 } else {
196 m_state = Illuminated;
197 m_timer->start(5000);
198 }
199 break;
200 }
201
202 case Illuminated: {
203 // start desaturation
204 m_state = Desaturate;
205 m_timer->start(100);
206 break;
207 }
208
209 case Desaturate: {
210 // desaturate
211 if (m_illumination > 0) {
212 m_illumination -= 5;
213 update();
214 } else {
215 m_state = Default;
216 m_timer->stop();
217 }
218 break;
219 }
220
221 default:
222 break;
223 }
224 }
225
226 void StatusBarMessageLabel::assureVisibleText()
227 {
228 if (m_text.isEmpty()) {
229 return;
230 }
231
232 // calculate the required height of the widget thats
233 // needed for having a fully visible text
234 QFontMetrics fontMetrics(font());
235 const QRect bounds(fontMetrics.boundingRect(0, 0, availableTextWidth(), height(),
236 Qt::AlignVCenter | Qt::TextWordWrap,
237 m_text));
238 int requiredHeight = bounds.height();
239 if (requiredHeight < m_minTextHeight) {
240 requiredHeight = m_minTextHeight;
241 }
242
243 // Increase/decrease the current height of the widget to the
244 // required height. The increasing/decreasing is done in several
245 // steps to have an animation if the height is modified
246 // (see StatusBarMessageLabel::resizeEvent())
247 const int gap = m_minTextHeight / 2;
248 int minHeight = minimumHeight();
249 if (minHeight < requiredHeight) {
250 minHeight += gap;
251 if (minHeight > requiredHeight) {
252 minHeight = requiredHeight;
253 }
254 setMinimumHeight(minHeight);
255 updateGeometry();
256 } else if (minHeight > requiredHeight) {
257 minHeight -= gap;
258 if (minHeight < requiredHeight) {
259 minHeight = requiredHeight;
260 }
261 setMinimumHeight(minHeight);
262 updateGeometry();
263 }
264
265 updateCloseButtonPosition();
266 }
267
268 int StatusBarMessageLabel::availableTextWidth() const
269 {
270 const int buttonWidth = (m_type == DolphinStatusBar::Error) ?
271 m_closeButton->width() + BorderGap : 0;
272 return width() - m_pixmap.width() - (BorderGap * 4) - buttonWidth;
273 }
274
275 void StatusBarMessageLabel::updateCloseButtonPosition()
276 {
277 const int x = width() - m_closeButton->width() - BorderGap;
278 const int y = (height() - m_closeButton->height()) / 2;
279 m_closeButton->move(x, y);
280 }
281
282 void StatusBarMessageLabel::closeErrorMessage()
283 {
284 if (!showPendingMessage()) {
285 reset();
286 setMessage(m_defaultText, DolphinStatusBar::Default);
287 }
288 }
289
290 bool StatusBarMessageLabel::showPendingMessage()
291 {
292 if (!m_pendingMessages.isEmpty()) {
293 reset();
294 setMessage(m_pendingMessages.takeFirst(), DolphinStatusBar::Error);
295 return true;
296 }
297 return false;
298 }
299
300 void StatusBarMessageLabel::reset()
301 {
302 m_text.clear();
303 m_type = DolphinStatusBar::Default;
304 }
305
306 #include "statusbarmessagelabel.moc"