]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbarmessagelabel.cpp
commited initial version of Dolphin
[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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 #include "statusbarmessagelabel.h"
22 #include <qpainter.h>
23 #include <qtimer.h>
24 #include <qfontmetrics.h>
25 //Added by qt3to4:
26 #include <QPixmap>
27 #include <QResizeEvent>
28 #include <QPaintEvent>
29 #include <kiconloader.h>
30 #include <kglobalsettings.h>
31
32 StatusBarMessageLabel::StatusBarMessageLabel(QWidget* parent) :
33 QWidget(parent),
34 m_type(DolphinStatusBar::Default),
35 m_state(Default),
36 m_illumination(0),
37 m_minTextHeight(-1),
38 m_timer(0)
39 {
40 setMinimumHeight(KIcon::SizeSmall);
41
42 m_timer = new QTimer(this);
43 connect(m_timer, SIGNAL(timeout()),
44 this, SLOT(timerDone()));
45 }
46
47 StatusBarMessageLabel::~StatusBarMessageLabel()
48 {
49 }
50
51 void StatusBarMessageLabel::setType(DolphinStatusBar::Type type)
52 {
53 if (type != m_type) {
54 m_type = type;
55
56 m_timer->stop();
57 m_illumination = 0;
58 m_state = Default;
59
60 const char* iconName = 0;
61 QPixmap pixmap;
62 switch (type) {
63 case DolphinStatusBar::OperationCompleted:
64 iconName = "ok";
65 break;
66
67 case DolphinStatusBar::Information:
68 iconName = "info";
69 break;
70
71 case DolphinStatusBar::Error:
72 iconName = "error";
73 m_timer->start(100);
74 m_state = Illuminate;
75 break;
76
77 case DolphinStatusBar::Default:
78 default: break;
79 }
80
81 m_pixmap = (iconName == 0) ? QPixmap() : SmallIcon(iconName);
82 assureVisibleText();
83 update();
84 }
85 }
86
87 void StatusBarMessageLabel::setText(const QString& text)
88 {
89 if (text != m_text) {
90 if (m_type == DolphinStatusBar::Error) {
91 m_timer->start(100);
92 m_illumination = 0;
93 m_state = Illuminate;
94 }
95 m_text = text;
96 assureVisibleText();
97 update();
98 }
99 }
100
101 void StatusBarMessageLabel::setMinimumTextHeight(int min)
102 {
103 if (min != m_minTextHeight) {
104 m_minTextHeight = min;
105 setMinimumHeight(min);
106 }
107 }
108
109 void StatusBarMessageLabel::paintEvent(QPaintEvent* /* event */)
110 {
111 QPixmap buffer(size());
112 QPainter painter(&buffer);
113
114 // draw background
115 QColor backgroundColor(colorGroup().background());
116 QColor foregroundColor(KGlobalSettings::textColor());
117 if (m_illumination > 0) {
118 backgroundColor = mixColors(backgroundColor, QColor(255, 255, 64), m_illumination);
119 foregroundColor = mixColors(foregroundColor, QColor(0, 0, 0), m_illumination);
120 }
121 painter.setBrush(backgroundColor);
122 painter.setPen(backgroundColor);
123 painter.drawRect(QRect(0, 0, width(), height()));
124
125 // draw pixmap
126 int x = pixmapGap();
127 int y = (height() - m_pixmap.height()) / 2;
128
129 if (!m_pixmap.isNull()) {
130 painter.drawPixmap(x, y, m_pixmap);
131 x += m_pixmap.width() + pixmapGap();
132 }
133
134 // draw text
135 painter.setPen(foregroundColor);
136 painter.drawText(QRect(x, 0, width() - x, height()), Qt::AlignVCenter | Qt::WordBreak, m_text);
137 painter.end();
138
139 bitBlt(this, 0, 0, &buffer);
140 }
141
142 void StatusBarMessageLabel::resizeEvent(QResizeEvent* event)
143 {
144 QWidget::resizeEvent(event);
145 QTimer::singleShot(0, this, SLOT(assureVisibleText()));
146 }
147
148 void StatusBarMessageLabel::timerDone()
149 {
150 switch (m_state) {
151 case Illuminate: {
152 // increase the illumination
153 if (m_illumination < 100) {
154 m_illumination += 20;
155 update();
156 }
157 else {
158 m_state = Illuminated;
159 m_timer->start(1000);
160 }
161 break;
162 }
163
164 case Illuminated: {
165 // start desaturation
166 m_state = Desaturate;
167 m_timer->start(100);
168 break;
169 }
170
171 case Desaturate: {
172 // desaturate
173 if (m_illumination > 0) {
174 m_illumination -= 5;
175 update();
176 }
177 else {
178 m_state = Default;
179 m_timer->stop();
180 }
181 break;
182 }
183
184 default:
185 break;
186 }
187 }
188
189 void StatusBarMessageLabel::assureVisibleText()
190 {
191 if (m_text.isEmpty()) {
192 return;
193 }
194
195
196 int availableWidth = width() - m_pixmap.width() - pixmapGap() * 2;
197
198 QFontMetrics fontMetrics(font());
199 QRect bounds(fontMetrics.boundingRect(0, 0, availableWidth, height(),
200 Qt::AlignVCenter | Qt::TextWordWrap,
201 m_text));
202 int requiredHeight = bounds.height();
203 if (requiredHeight < m_minTextHeight) {
204 requiredHeight = m_minTextHeight;
205 }
206 setMinimumHeight(requiredHeight);
207 updateGeometry();
208 }
209
210 QColor StatusBarMessageLabel::mixColors(const QColor& c1,
211 const QColor& c2,
212 int percent) const
213 {
214 const int recip = 100 - percent;
215 const int red = (c1.red() * recip + c2.red() * percent) / 100;
216 const int green = (c1.green() * recip + c2.green() * percent) / 100;
217 const int blue = (c1.blue() * recip + c2.blue() * percent) / 100;
218 return QColor(red, green, blue);
219 }