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