]> git.sesse.net Git - kdenlive/blob - src/statusbarmessagelabel.cpp
* Allow transcoding of several clips (selected in project tree):
[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 #include <KNotification>
30
31 #include <QFontMetrics>
32 #include <QPainter>
33 #include <QKeyEvent>
34 #include <QPushButton>
35 #include <QPixmap>
36
37
38 StatusBarMessageLabel::StatusBarMessageLabel(QWidget* parent) :
39         QWidget(parent),
40         m_type(DefaultMessage),
41         m_state(Default),
42         m_illumination(-64),
43         m_minTextHeight(-1),
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     connect(&m_timer, SIGNAL(timeout()), this, SLOT(timerDone()));
53
54     m_closeButton = new QPushButton(i18nc("@action:button", "Close"), this);
55     m_closeButton->hide();
56     connect(m_closeButton, SIGNAL(clicked()), this, SLOT(closeErrorMessage()));
57 }
58
59 StatusBarMessageLabel::~StatusBarMessageLabel()
60 {
61 }
62
63 void StatusBarMessageLabel::setMessage(const QString& text,
64                                        MessageType type)
65 {
66     if ((text == m_text) && (type == m_type)) {
67         if (type == ErrorMessage) KNotification::event("ErrorMessage", m_text);
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         KNotification::event("ErrorMessage", m_text);
108         break;
109
110     case MltError:
111         iconName = "dialog-close";
112         m_timer.start(100);
113         m_state = Illuminate;
114         updateCloseButtonPosition();
115         m_closeButton->show();
116         break;
117
118     case DefaultMessage:
119     default:
120         m_closeButton->hide();
121         break;
122     }
123
124     m_pixmap = (iconName == 0) ? QPixmap() : SmallIcon(iconName);
125
126     /*QFontMetrics fontMetrics(font());
127     setMaximumWidth(fontMetrics.boundingRect(m_text).width() + m_pixmap.width() + (BorderGap * 4));
128     updateGeometry();*/
129
130     //QTimer::singleShot(GeometryTimeout, this, SLOT(assureVisibleText()));
131     update();
132 }
133
134 void StatusBarMessageLabel::setMinimumTextHeight(int min)
135 {
136     if (min != m_minTextHeight) {
137         m_minTextHeight = min;
138         setMinimumHeight(min);
139         if (m_closeButton->height() > min) {
140             m_closeButton->setFixedHeight(min);
141         }
142     }
143 }
144
145 void StatusBarMessageLabel::paintEvent(QPaintEvent* /* event */)
146 {
147     QPainter painter(this);
148
149     // draw background
150     QColor backgroundColor;
151     if (m_state == Default || m_illumination < 0) backgroundColor = palette().window().color();
152     else {
153         KColorScheme scheme(palette().currentColorGroup(), KColorScheme::Window);
154         backgroundColor = scheme.background(KColorScheme::NegativeBackground).color();
155     }
156     if (m_state == Desaturate && m_illumination > 0) {
157         backgroundColor.setAlpha(m_illumination * 2);
158     }
159     painter.fillRect(0, 0, width(), height(), backgroundColor);
160
161     // draw pixmap
162     int x = BorderGap;
163     int y = (height() - m_pixmap.height()) / 2;
164
165     if (!m_pixmap.isNull()) {
166         painter.drawPixmap(x, y, m_pixmap);
167         x += m_pixmap.width() + BorderGap * 2;
168     }
169
170     // draw text
171     painter.setPen(palette().windowText().color());
172     int flags = Qt::AlignVCenter;
173     if (height() > m_minTextHeight) {
174         flags = flags | Qt::TextWordWrap;
175     }
176     painter.drawText(QRect(x, 0, availableTextWidth(), height()), flags, m_text);
177     painter.end();
178 }
179
180 void StatusBarMessageLabel::resizeEvent(QResizeEvent* event)
181 {
182     QWidget::resizeEvent(event);
183     updateCloseButtonPosition();
184     //QTimer::singleShot(GeometryTimeout, this, SLOT(assureVisibleText()));
185 }
186
187 void StatusBarMessageLabel::timerDone()
188 {
189     switch (m_state) {
190     case Illuminate: {
191         // increase the illumination
192         const int illumination_max = 128;
193         if (m_illumination < illumination_max) {
194             m_illumination += 32;
195             if (m_illumination > illumination_max) {
196                 m_illumination = illumination_max;
197             }
198             update();
199         } else {
200             m_state = Illuminated;
201             m_timer.start(1500);
202         }
203         break;
204     }
205
206     case Illuminated: {
207         // start desaturation
208         if (m_type != MltError) {
209             m_state = Desaturate;
210             m_timer.start(80);
211         }
212         break;
213     }
214
215     case Desaturate: {
216         // desaturate
217         if (m_illumination < -128) {
218             m_illumination = 0;
219             m_state = Default;
220             m_timer.stop();
221             setMessage(QString(), DefaultMessage);
222         } else {
223             m_illumination -= 5;
224             update();
225         }
226         break;
227     }
228
229     default:
230         break;
231     }
232 }
233
234 void StatusBarMessageLabel::assureVisibleText()
235 {
236     if (m_text.isEmpty()) {
237         return;
238     }
239
240     int requiredHeight = m_minTextHeight;
241     if (m_type != DefaultMessage) {
242         // Calculate the required height of the widget thats
243         // needed for having a fully visible text. Note that for the default
244         // statusbar type (e. g. hover information) increasing the text height
245         // is not wanted, as this might rearrange the layout of items.
246
247         QFontMetrics fontMetrics(font());
248         const QRect bounds(fontMetrics.boundingRect(0, 0, availableTextWidth(), height(),
249                            Qt::AlignVCenter | Qt::TextWordWrap, m_text));
250         requiredHeight = bounds.height();
251         if (requiredHeight < m_minTextHeight) {
252             requiredHeight = m_minTextHeight;
253         }
254     }
255
256     // Increase/decrease the current height of the widget to the
257     // required height. The increasing/decreasing is done in several
258     // steps to have an animation if the height is modified
259     // (see StatusBarMessageLabel::resizeEvent())
260     const int gap = m_minTextHeight / 2;
261     int minHeight = minimumHeight();
262     if (minHeight < requiredHeight) {
263         minHeight += gap;
264         if (minHeight > requiredHeight) {
265             minHeight = requiredHeight;
266         }
267         setMinimumHeight(minHeight);
268         updateGeometry();
269     } else if (minHeight > requiredHeight) {
270         minHeight -= gap;
271         if (minHeight < requiredHeight) {
272             minHeight = requiredHeight;
273         }
274         setMinimumHeight(minHeight);
275         updateGeometry();
276     }
277
278     updateCloseButtonPosition();
279 }
280
281 int StatusBarMessageLabel::availableTextWidth() const
282 {
283     const int buttonWidth = 0; /*(m_type == ErrorMessage) ?
284                             m_closeButton->width() + BorderGap : 0;*/
285     return width() - m_pixmap.width() - (BorderGap * 4) - buttonWidth;
286 }
287
288 void StatusBarMessageLabel::updateCloseButtonPosition()
289 {
290     const int x = width() - m_closeButton->width() - BorderGap;
291     const int y = (height() - m_closeButton->height()) / 2;
292     m_closeButton->move(x, y);
293 }
294
295 void StatusBarMessageLabel::closeErrorMessage()
296 {
297     if (!showPendingMessage()) {
298         setMessage(QString(), DefaultMessage);
299     }
300 }
301
302 bool StatusBarMessageLabel::showPendingMessage()
303 {
304     if (!m_pendingMessages.isEmpty()) {
305         setMessage(m_pendingMessages.takeFirst(), ErrorMessage);
306         return true;
307     }
308     return false;
309 }
310
311
312 #include "statusbarmessagelabel.moc"