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