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