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