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