]> git.sesse.net Git - kdenlive/blob - src/statusbarmessagelabel.cpp
Fix error message color
[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 #include "kdenlivesettings.h"
25
26 #include <kcolorscheme.h>
27 #include <kiconloader.h>
28 #include <kicon.h>
29 #include <klocale.h>
30 #include <KNotification>
31
32 #include <QFontMetrics>
33 #include <QPainter>
34 #include <QKeyEvent>
35 #include <QPushButton>
36 #include <QPixmap>
37
38
39 StatusBarMessageLabel::StatusBarMessageLabel(QWidget* parent) :
40         QWidget(parent),
41         m_type(DefaultMessage),
42         m_state(Default),
43         m_illumination(-64),
44         m_minTextHeight(-1),
45         m_closeButton(0)
46 {
47     setMinimumHeight(KIconLoader::SizeSmall);
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         backgroundColor = KStatefulBrush(KColorScheme::Window, KColorScheme::NegativeBackground, KSharedConfig::openConfig(KdenliveSettings::colortheme())).brush(this).color();
161     }
162     if (m_state == Desaturate && m_illumination > 0) {
163         backgroundColor.setAlpha(m_illumination * 2);
164     }
165     painter.fillRect(0, 0, width(), height(), backgroundColor);
166
167     // draw pixmap
168     int x = BorderGap;
169     int y = (height() - m_pixmap.height()) / 2;
170
171     if (!m_pixmap.isNull()) {
172         painter.drawPixmap(x, y, m_pixmap);
173         x += m_pixmap.width() + BorderGap * 2;
174     }
175
176     // draw text
177     painter.setPen(palette().windowText().color());
178     int flags = Qt::AlignVCenter;
179     if (height() > m_minTextHeight) {
180         flags = flags | Qt::TextWordWrap;
181     }
182     painter.drawText(QRect(x, 0, availableTextWidth(), height()), flags, m_text);
183     painter.end();
184 }
185
186 void StatusBarMessageLabel::resizeEvent(QResizeEvent* event)
187 {
188     QWidget::resizeEvent(event);
189     updateCloseButtonPosition();
190     //QTimer::singleShot(GeometryTimeout, this, SLOT(assureVisibleText()));
191 }
192
193 void StatusBarMessageLabel::timerDone()
194 {
195     switch (m_state) {
196     case Illuminate: {
197         // increase the illumination
198         const int illumination_max = 128;
199         if (m_illumination < illumination_max) {
200             m_illumination += 32;
201             if (m_illumination > illumination_max) {
202                 m_illumination = illumination_max;
203             }
204             update();
205         } else {
206             m_state = Illuminated;
207             m_timer.start(1500);
208         }
209         break;
210     }
211
212     case Illuminated: {
213         // start desaturation
214         if (m_type != MltError) {
215             m_state = Desaturate;
216             m_timer.start(80);
217         }
218         break;
219     }
220
221     case Desaturate: {
222         // desaturate
223         if (m_illumination < -128) {
224             m_illumination = 0;
225             m_state = Default;
226             m_timer.stop();
227             setMessage(QString(), DefaultMessage);
228         } else {
229             m_illumination -= 5;
230             update();
231         }
232         break;
233     }
234
235     default:
236         break;
237     }
238 }
239
240 void StatusBarMessageLabel::assureVisibleText()
241 {
242     if (m_text.isEmpty()) {
243         return;
244     }
245
246     int requiredHeight = m_minTextHeight;
247     if (m_type != DefaultMessage) {
248         // Calculate the required height of the widget thats
249         // needed for having a fully visible text. Note that for the default
250         // statusbar type (e. g. hover information) increasing the text height
251         // is not wanted, as this might rearrange the layout of items.
252
253         QFontMetrics fontMetrics(font());
254         const QRect bounds(fontMetrics.boundingRect(0, 0, availableTextWidth(), height(),
255                            Qt::AlignVCenter | Qt::TextWordWrap, m_text));
256         requiredHeight = bounds.height();
257         if (requiredHeight < m_minTextHeight) {
258             requiredHeight = m_minTextHeight;
259         }
260     }
261
262     // Increase/decrease the current height of the widget to the
263     // required height. The increasing/decreasing is done in several
264     // steps to have an animation if the height is modified
265     // (see StatusBarMessageLabel::resizeEvent())
266     const int gap = m_minTextHeight / 2;
267     int minHeight = minimumHeight();
268     if (minHeight < requiredHeight) {
269         minHeight += gap;
270         if (minHeight > requiredHeight) {
271             minHeight = requiredHeight;
272         }
273         setMinimumHeight(minHeight);
274         updateGeometry();
275     } else if (minHeight > requiredHeight) {
276         minHeight -= gap;
277         if (minHeight < requiredHeight) {
278             minHeight = requiredHeight;
279         }
280         setMinimumHeight(minHeight);
281         updateGeometry();
282     }
283
284     updateCloseButtonPosition();
285 }
286
287 int StatusBarMessageLabel::availableTextWidth() const
288 {
289     const int buttonWidth = 0; /*(m_type == ErrorMessage) ?
290                             m_closeButton->width() + BorderGap : 0;*/
291     return width() - m_pixmap.width() - (BorderGap * 4) - buttonWidth;
292 }
293
294 void StatusBarMessageLabel::updateCloseButtonPosition()
295 {
296     const int x = width() - m_closeButton->width() - BorderGap;
297     const int y = (height() - m_closeButton->height()) / 2;
298     m_closeButton->move(x, y);
299 }
300
301 void StatusBarMessageLabel::closeErrorMessage()
302 {
303     if (!showPendingMessage()) {
304         setMessage(QString(), DefaultMessage);
305     }
306 }
307
308 bool StatusBarMessageLabel::showPendingMessage()
309 {
310     if (!m_pendingMessages.isEmpty()) {
311         setMessage(m_pendingMessages.takeFirst(), ErrorMessage);
312         return true;
313     }
314     return false;
315 }
316
317
318 #include "statusbarmessagelabel.moc"