]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbarmessagelabel.cpp
Reverts commit 726516. We have to report the URL changed to the outside
[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 <kcolorutils.h>
25 #include <kiconloader.h>
26 #include <kicon.h>
27 #include <klocale.h>
28
29 #include <QFontMetrics>
30 #include <QPainter>
31 #include <QKeyEvent>
32 #include <QPushButton>
33 #include <QPixmap>
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(KIconLoader::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(i18nc("@action:button", "Close"), this);
56 m_closeButton->hide();
57 connect(m_closeButton, SIGNAL(clicked()),
58 this, SLOT(closeErrorMessage()));
59 }
60
61 StatusBarMessageLabel::~StatusBarMessageLabel()
62 {
63 }
64
65 void StatusBarMessageLabel::setMessage(const QString& text,
66 DolphinStatusBar::Type type)
67 {
68 if ((text == m_text) && (type == m_type)) {
69 return;
70 }
71
72 if (m_type == DolphinStatusBar::Error) {
73 if (type == DolphinStatusBar::Error) {
74 m_pendingMessages.insert(0, m_text);
75 } else if ((m_state != Default) || !m_pendingMessages.isEmpty()) {
76 // a non-error message should not be shown, as there
77 // are other pending error messages in the queue
78 return;
79 }
80 }
81
82 m_text = text;
83 m_type = type;
84
85 m_timer->stop();
86 m_illumination = 0;
87 m_state = Default;
88
89 const char* iconName = 0;
90 QPixmap pixmap;
91 switch (type) {
92 case DolphinStatusBar::OperationCompleted:
93 iconName = "ok";
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().brush(QPalette::Background).color();
145 QColor foregroundColor = KColorScheme(QPalette::Active, KColorScheme::View).foreground().color();
146 if (m_illumination > 0) {
147 // TODO: are there foreground and background colors available for
148 // "error messages"?
149 backgroundColor.setRgb(255, 255, 0, m_illumination);
150 QColor mixColor(0, 0, 0, m_illumination);
151 foregroundColor = KColorUtils::overlayColors(foregroundColor, mixColor);
152 }
153 painter.setBrush(backgroundColor);
154 painter.setPen(backgroundColor);
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(foregroundColor);
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"