]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbarmessagelabel.cpp
Use a KIO Job for applying the view properties recursively to sub directories.
[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(K3Icon::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 QPainter painter(this);
112
113 // draw background
114 QColor backgroundColor(palette().brush(QPalette::Background).color());
115 QColor foregroundColor(KGlobalSettings::textColor());
116 if (m_illumination > 0) {
117 backgroundColor = mixColors(backgroundColor, QColor(255, 255, 64), m_illumination);
118 foregroundColor = mixColors(foregroundColor, QColor(0, 0, 0), m_illumination);
119 }
120 painter.setBrush(backgroundColor);
121 painter.setPen(backgroundColor);
122 painter.drawRect(QRect(0, 0, width(), height()));
123
124 // draw pixmap
125 int x = pixmapGap();
126 int y = (height() - m_pixmap.height()) / 2;
127
128 if (!m_pixmap.isNull()) {
129 painter.drawPixmap(x, y, m_pixmap);
130 x += m_pixmap.width() + pixmapGap();
131 }
132
133 // draw text
134 painter.setPen(foregroundColor);
135 painter.drawText(QRect(x, 0, width() - x, height()), Qt::AlignVCenter | Qt::TextWordWrap, m_text);
136 painter.end();
137 }
138
139 void StatusBarMessageLabel::resizeEvent(QResizeEvent* event)
140 {
141 QWidget::resizeEvent(event);
142 QTimer::singleShot(0, this, SLOT(assureVisibleText()));
143 }
144
145 void StatusBarMessageLabel::timerDone()
146 {
147 switch (m_state) {
148 case Illuminate: {
149 // increase the illumination
150 if (m_illumination < 100) {
151 m_illumination += 20;
152 update();
153 }
154 else {
155 m_state = Illuminated;
156 m_timer->start(1000);
157 }
158 break;
159 }
160
161 case Illuminated: {
162 // start desaturation
163 m_state = Desaturate;
164 m_timer->start(100);
165 break;
166 }
167
168 case Desaturate: {
169 // desaturate
170 if (m_illumination > 0) {
171 m_illumination -= 5;
172 update();
173 }
174 else {
175 m_state = Default;
176 m_timer->stop();
177 }
178 break;
179 }
180
181 default:
182 break;
183 }
184 }
185
186 void StatusBarMessageLabel::assureVisibleText()
187 {
188 if (m_text.isEmpty()) {
189 return;
190 }
191
192
193 int availableWidth = width() - m_pixmap.width() - pixmapGap() * 2;
194
195 QFontMetrics fontMetrics(font());
196 QRect bounds(fontMetrics.boundingRect(0, 0, availableWidth, height(),
197 Qt::AlignVCenter | Qt::TextWordWrap,
198 m_text));
199 int requiredHeight = bounds.height();
200 if (requiredHeight < m_minTextHeight) {
201 requiredHeight = m_minTextHeight;
202 }
203 setMinimumHeight(requiredHeight);
204 updateGeometry();
205 }
206
207 QColor StatusBarMessageLabel::mixColors(const QColor& c1,
208 const QColor& c2,
209 int percent) const
210 {
211 const int recip = 100 - percent;
212 const int red = (c1.red() * recip + c2.red() * percent) / 100;
213 const int green = (c1.green() * recip + c2.green() * percent) / 100;
214 const int blue = (c1.blue() * recip + c2.blue() * percent) / 100;
215 return QColor(red, green, blue);
216 }
217
218 #include "statusbarmessagelabel.moc"