]> git.sesse.net Git - kdenlive/blob - src/statusbarmessagelabel.cpp
3fcf28fc84d676a8b753cf0e3b4a49c0c5188860
[kdenlive] / src / statusbarmessagelabel.cpp
1 /***************************************************************************
2  *   Copyright (C) 2006 by Peter Penz                                      *
3  *   peter.penz@gmx.at                                                     *
4  *   Code borrowed from Dolphin, adapted (2008) to Kdenlive by             *
5  *   Jean-Baptiste Mardelle, jb@kdenlive.org                               *
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or modify  *
8  *   it under the terms of the GNU General Public License as published by  *
9  *   the Free Software Foundation; either version 2 of the License, or     *
10  *   (at your option) any later version.                                   *
11  *                                                                         *
12  *   This program is distributed in the hope that it will be useful,       *
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
15  *   GNU General Public License for more details.                          *
16  *                                                                         *
17  *   You should have received a copy of the GNU General Public License     *
18  *   along with this program; if not, write to the                         *
19  *   Free Software Foundation, Inc.,                                       *
20  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
21  ***************************************************************************/
22
23 #include "statusbarmessagelabel.h"
24
25 #include <kcolorscheme.h>
26 #include <kiconloader.h>
27 #include <kicon.h>
28 #include <klocale.h>
29
30 #include <QFontMetrics>
31 #include <QPainter>
32 #include <QKeyEvent>
33 #include <QPushButton>
34 #include <QPixmap>
35 #include <QTimer>
36
37 StatusBarMessageLabel::StatusBarMessageLabel(QWidget* parent) :
38         QWidget(parent),
39         m_type(DefaultMessage),
40         m_state(Default),
41         m_illumination(-64),
42         m_minTextHeight(-1),
43         m_timer(0),
44         m_closeButton(0)
45 {
46     setMinimumHeight(KIconLoader::SizeSmall);
47
48     QPalette palette;
49     palette.setColor(QPalette::Background, Qt::transparent);
50     setPalette(palette);
51
52     m_timer = new QTimer(this);
53     connect(m_timer, SIGNAL(timeout()), this, SLOT(timerDone()));
54
55     m_closeButton = new QPushButton(i18nc("@action:button", "Close"), this);
56     m_closeButton->hide();
57     connect(m_closeButton, SIGNAL(clicked()), this, SLOT(closeErrorMessage()));
58 }
59
60 StatusBarMessageLabel::~StatusBarMessageLabel()
61 {
62 }
63
64 void StatusBarMessageLabel::setMessage(const QString& text,
65                                        MessageType type)
66 {
67     if ((text == m_text) && (type == m_type)) {
68         return;
69     }
70
71     /*if (m_type == ErrorMessage) {
72         if (type == ErrorMessage) {
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_illumination = -64;
85     m_state = Default;
86     m_timer->stop();
87
88     const char* iconName = 0;
89     QPixmap pixmap;
90     switch (type) {
91     case OperationCompletedMessage:
92         iconName = "dialog-ok";
93         // "ok" icon should probably be "dialog-success", but we don't have that icon in KDE 4.0
94         m_closeButton->hide();
95         break;
96
97     case InformationMessage:
98         iconName = "dialog-information";
99         m_closeButton->hide();
100         break;
101
102     case ErrorMessage:
103         iconName = "dialog-warning";
104         m_timer->start(100);
105         m_state = Illuminate;
106         m_closeButton->hide();
107         break;
108
109     case MltError:
110         iconName = "dialog-close";
111         m_timer->start(100);
112         m_state = Illuminate;
113         updateCloseButtonPosition();
114         m_closeButton->show();
115         break;
116
117     case DefaultMessage:
118     default:
119         m_closeButton->hide();
120         break;
121     }
122
123     m_pixmap = (iconName == 0) ? QPixmap() : SmallIcon(iconName);
124     QTimer::singleShot(GeometryTimeout, this, SLOT(assureVisibleText()));
125     update();
126 }
127
128 void StatusBarMessageLabel::setMinimumTextHeight(int min)
129 {
130     if (min != m_minTextHeight) {
131         m_minTextHeight = min;
132         setMinimumHeight(min);
133         if (m_closeButton->height() > min) {
134             m_closeButton->setFixedHeight(min);
135         }
136     }
137 }
138
139 void StatusBarMessageLabel::paintEvent(QPaintEvent* /* event */)
140 {
141     QPainter painter(this);
142
143     // draw background
144     QColor backgroundColor;
145     if (m_state == Default || m_illumination < 0) backgroundColor = palette().window().color();
146     else {
147         KColorScheme scheme(palette().currentColorGroup(), KColorScheme::Window);
148         backgroundColor = scheme.background(KColorScheme::NegativeBackground).color();
149     }
150     if (m_state == Desaturate && m_illumination > 0) {
151         backgroundColor.setAlpha(m_illumination * 2);
152     }
153     painter.fillRect(0, 0, width(), height(), backgroundColor);
154
155     // draw pixmap
156     int x = BorderGap;
157     int y = (height() - m_pixmap.height()) / 2;
158
159     if (!m_pixmap.isNull()) {
160         painter.drawPixmap(x, y, m_pixmap);
161         x += m_pixmap.width() + BorderGap * 2;
162     }
163
164     // draw text
165     painter.setPen(palette().windowText().color());
166     int flags = Qt::AlignVCenter;
167     if (height() > m_minTextHeight) {
168         flags = flags | Qt::TextWordWrap;
169     }
170     painter.drawText(QRect(x, 0, availableTextWidth(), height()), flags, m_text);
171     painter.end();
172 }
173
174 void StatusBarMessageLabel::resizeEvent(QResizeEvent* event)
175 {
176     QWidget::resizeEvent(event);
177     updateCloseButtonPosition();
178     QTimer::singleShot(GeometryTimeout, this, SLOT(assureVisibleText()));
179 }
180
181 void StatusBarMessageLabel::timerDone()
182 {
183     switch (m_state) {
184     case Illuminate: {
185         // increase the illumination
186         const int illumination_max = 128;
187         if (m_illumination < illumination_max) {
188             m_illumination += 32;
189             if (m_illumination > illumination_max) {
190                 m_illumination = illumination_max;
191             }
192             update();
193         } else {
194             m_state = Illuminated;
195             m_timer->start(1500);
196         }
197         break;
198     }
199
200     case Illuminated: {
201         // start desaturation
202         if (m_type != MltError) {
203             m_state = Desaturate;
204             m_timer->start(80);
205         }
206         break;
207     }
208
209     case Desaturate: {
210         // desaturate
211         if (m_illumination < -128) {
212             m_illumination = 0;
213             m_state = Default;
214             m_timer->stop();
215             setMessage(QString(), DefaultMessage);
216         } else {
217             m_illumination -= 5;
218             update();
219         }
220         break;
221     }
222
223     default:
224         break;
225     }
226 }
227
228 void StatusBarMessageLabel::assureVisibleText()
229 {
230     if (m_text.isEmpty()) {
231         return;
232     }
233
234     int requiredHeight = m_minTextHeight;
235     if (m_type != DefaultMessage) {
236         // Calculate the required height of the widget thats
237         // needed for having a fully visible text. Note that for the default
238         // statusbar type (e. g. hover information) increasing the text height
239         // is not wanted, as this might rearrange the layout of items.
240
241         QFontMetrics fontMetrics(font());
242         const QRect bounds(fontMetrics.boundingRect(0, 0, availableTextWidth(), height(),
243                            Qt::AlignVCenter | Qt::TextWordWrap, m_text));
244         requiredHeight = bounds.height();
245         if (requiredHeight < m_minTextHeight) {
246             requiredHeight = m_minTextHeight;
247         }
248     }
249
250     // Increase/decrease the current height of the widget to the
251     // required height. The increasing/decreasing is done in several
252     // steps to have an animation if the height is modified
253     // (see StatusBarMessageLabel::resizeEvent())
254     const int gap = m_minTextHeight / 2;
255     int minHeight = minimumHeight();
256     if (minHeight < requiredHeight) {
257         minHeight += gap;
258         if (minHeight > requiredHeight) {
259             minHeight = requiredHeight;
260         }
261         setMinimumHeight(minHeight);
262         updateGeometry();
263     } else if (minHeight > requiredHeight) {
264         minHeight -= gap;
265         if (minHeight < requiredHeight) {
266             minHeight = requiredHeight;
267         }
268         setMinimumHeight(minHeight);
269         updateGeometry();
270     }
271
272     updateCloseButtonPosition();
273 }
274
275 int StatusBarMessageLabel::availableTextWidth() const
276 {
277     const int buttonWidth = 0; /*(m_type == ErrorMessage) ?
278                             m_closeButton->width() + BorderGap : 0;*/
279     return width() - m_pixmap.width() - (BorderGap * 4) - buttonWidth;
280 }
281
282 void StatusBarMessageLabel::updateCloseButtonPosition()
283 {
284     const int x = width() - m_closeButton->width() - BorderGap;
285     const int y = (height() - m_closeButton->height()) / 2;
286     m_closeButton->move(x, y);
287 }
288
289 void StatusBarMessageLabel::closeErrorMessage()
290 {
291     if (!showPendingMessage()) {
292         setMessage(QString(), DefaultMessage);
293     }
294 }
295
296 bool StatusBarMessageLabel::showPendingMessage()
297 {
298     if (!m_pendingMessages.isEmpty()) {
299         setMessage(m_pendingMessages.takeFirst(), ErrorMessage);
300         return true;
301     }
302     return false;
303 }
304
305
306 #include "statusbarmessagelabel.moc"