1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz *
4 * Code borrowed from Dolphin, adapted (2008) to Kdenlive by *
5 * Jean-Baptiste Mardelle, jb@kdenlive.org *
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. *
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. *
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 ***************************************************************************/
23 #include "statusbarmessagelabel.h"
25 #include <kcolorscheme.h>
26 #include <kiconloader.h>
29 #include <KNotification>
31 #include <QFontMetrics>
34 #include <QPushButton>
38 StatusBarMessageLabel::StatusBarMessageLabel(QWidget* parent) :
40 m_type(DefaultMessage),
46 setMinimumHeight(KIconLoader::SizeSmall);
49 palette.setColor(QPalette::Background, Qt::transparent);
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()));
56 m_closeButton = new QPushButton(i18nc("@action:button", "Close"), this);
57 m_closeButton->hide();
58 connect(m_closeButton, SIGNAL(clicked()), this, SLOT(closeErrorMessage()));
61 StatusBarMessageLabel::~StatusBarMessageLabel()
65 void StatusBarMessageLabel::setMessage(const QString& text,
68 if ((text == m_text) && (type == m_type)) {
69 if (type == ErrorMessage) KNotification::event("ErrorMessage", m_text);
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
90 const char* iconName = 0;
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();
100 case InformationMessage:
101 iconName = "dialog-information";
102 m_closeButton->hide();
107 iconName = "dialog-warning";
109 m_state = Illuminate;
110 m_closeButton->hide();
111 KNotification::event("ErrorMessage", m_text);
116 iconName = "dialog-close";
118 m_state = Illuminate;
119 updateCloseButtonPosition();
120 m_closeButton->show();
126 m_closeButton->hide();
131 m_pixmap = (iconName == 0) ? QPixmap() : SmallIcon(iconName);
133 /*QFontMetrics fontMetrics(font());
134 setMaximumWidth(fontMetrics.boundingRect(m_text).width() + m_pixmap.width() + (BorderGap * 4));
137 //QTimer::singleShot(GeometryTimeout, this, SLOT(assureVisibleText()));
141 void StatusBarMessageLabel::setMinimumTextHeight(int min)
143 if (min != m_minTextHeight) {
144 m_minTextHeight = min;
145 setMinimumHeight(min);
146 if (m_closeButton->height() > min) {
147 m_closeButton->setFixedHeight(min);
152 void StatusBarMessageLabel::paintEvent(QPaintEvent* /* event */)
154 QPainter painter(this);
157 QColor backgroundColor;
158 if (m_state == Default || m_illumination < 0) backgroundColor = palette().window().color();
160 KColorScheme scheme(palette().currentColorGroup(), KColorScheme::Window);
161 backgroundColor = scheme.background(KColorScheme::NegativeBackground).color();
163 if (m_state == Desaturate && m_illumination > 0) {
164 backgroundColor.setAlpha(m_illumination * 2);
166 painter.fillRect(0, 0, width(), height(), backgroundColor);
170 int y = (height() - m_pixmap.height()) / 2;
172 if (!m_pixmap.isNull()) {
173 painter.drawPixmap(x, y, m_pixmap);
174 x += m_pixmap.width() + BorderGap * 2;
178 painter.setPen(palette().windowText().color());
179 int flags = Qt::AlignVCenter;
180 if (height() > m_minTextHeight) {
181 flags = flags | Qt::TextWordWrap;
183 painter.drawText(QRect(x, 0, availableTextWidth(), height()), flags, m_text);
187 void StatusBarMessageLabel::resizeEvent(QResizeEvent* event)
189 QWidget::resizeEvent(event);
190 updateCloseButtonPosition();
191 //QTimer::singleShot(GeometryTimeout, this, SLOT(assureVisibleText()));
194 void StatusBarMessageLabel::timerDone()
198 // increase the illumination
199 const int illumination_max = 128;
200 if (m_illumination < illumination_max) {
201 m_illumination += 32;
202 if (m_illumination > illumination_max) {
203 m_illumination = illumination_max;
207 m_state = Illuminated;
214 // start desaturation
215 if (m_type != MltError) {
216 m_state = Desaturate;
224 if (m_illumination < -128) {
228 setMessage(QString(), DefaultMessage);
241 void StatusBarMessageLabel::assureVisibleText()
243 if (m_text.isEmpty()) {
247 int requiredHeight = m_minTextHeight;
248 if (m_type != DefaultMessage) {
249 // Calculate the required height of the widget thats
250 // needed for having a fully visible text. Note that for the default
251 // statusbar type (e. g. hover information) increasing the text height
252 // is not wanted, as this might rearrange the layout of items.
254 QFontMetrics fontMetrics(font());
255 const QRect bounds(fontMetrics.boundingRect(0, 0, availableTextWidth(), height(),
256 Qt::AlignVCenter | Qt::TextWordWrap, m_text));
257 requiredHeight = bounds.height();
258 if (requiredHeight < m_minTextHeight) {
259 requiredHeight = m_minTextHeight;
263 // Increase/decrease the current height of the widget to the
264 // required height. The increasing/decreasing is done in several
265 // steps to have an animation if the height is modified
266 // (see StatusBarMessageLabel::resizeEvent())
267 const int gap = m_minTextHeight / 2;
268 int minHeight = minimumHeight();
269 if (minHeight < requiredHeight) {
271 if (minHeight > requiredHeight) {
272 minHeight = requiredHeight;
274 setMinimumHeight(minHeight);
276 } else if (minHeight > requiredHeight) {
278 if (minHeight < requiredHeight) {
279 minHeight = requiredHeight;
281 setMinimumHeight(minHeight);
285 updateCloseButtonPosition();
288 int StatusBarMessageLabel::availableTextWidth() const
290 const int buttonWidth = 0; /*(m_type == ErrorMessage) ?
291 m_closeButton->width() + BorderGap : 0;*/
292 return width() - m_pixmap.width() - (BorderGap * 4) - buttonWidth;
295 void StatusBarMessageLabel::updateCloseButtonPosition()
297 const int x = width() - m_closeButton->width() - BorderGap;
298 const int y = (height() - m_closeButton->height()) / 2;
299 m_closeButton->move(x, y);
302 void StatusBarMessageLabel::closeErrorMessage()
304 if (!showPendingMessage()) {
305 setMessage(QString(), DefaultMessage);
309 bool StatusBarMessageLabel::showPendingMessage()
311 if (!m_pendingMessages.isEmpty()) {
312 setMessage(m_pendingMessages.takeFirst(), ErrorMessage);
319 #include "statusbarmessagelabel.moc"