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