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