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"
24 #include "kdenlivesettings.h"
26 #include <kcolorscheme.h>
27 #include <kiconloader.h>
30 #include <KNotification>
32 #include <QFontMetrics>
35 #include <QPushButton>
39 StatusBarMessageLabel::StatusBarMessageLabel(QWidget* parent) :
41 m_type(DefaultMessage),
47 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 backgroundColor = KStatefulBrush(KColorScheme::Window, KColorScheme::NegativeBackground, KSharedConfig::openConfig(KdenliveSettings::colortheme())).brush(this).color();
162 if (m_state == Desaturate && m_illumination > 0) {
163 backgroundColor.setAlpha(m_illumination * 2);
165 painter.fillRect(0, 0, width(), height(), backgroundColor);
169 int y = (height() - m_pixmap.height()) / 2;
171 if (!m_pixmap.isNull()) {
172 painter.drawPixmap(x, y, m_pixmap);
173 x += m_pixmap.width() + BorderGap * 2;
177 painter.setPen(palette().windowText().color());
178 int flags = Qt::AlignVCenter;
179 if (height() > m_minTextHeight) {
180 flags = flags | Qt::TextWordWrap;
182 painter.drawText(QRect(x, 0, availableTextWidth(), height()), flags, m_text);
186 void StatusBarMessageLabel::resizeEvent(QResizeEvent* event)
188 QWidget::resizeEvent(event);
189 updateCloseButtonPosition();
190 //QTimer::singleShot(GeometryTimeout, this, SLOT(assureVisibleText()));
193 void StatusBarMessageLabel::timerDone()
197 // increase the illumination
198 const int illumination_max = 128;
199 if (m_illumination < illumination_max) {
200 m_illumination += 32;
201 if (m_illumination > illumination_max) {
202 m_illumination = illumination_max;
206 m_state = Illuminated;
213 // start desaturation
214 if (m_type != MltError) {
215 m_state = Desaturate;
223 if (m_illumination < -128) {
227 setMessage(QString(), DefaultMessage);
240 void StatusBarMessageLabel::assureVisibleText()
242 if (m_text.isEmpty()) {
246 int requiredHeight = m_minTextHeight;
247 if (m_type != DefaultMessage) {
248 // Calculate the required height of the widget thats
249 // needed for having a fully visible text. Note that for the default
250 // statusbar type (e. g. hover information) increasing the text height
251 // is not wanted, as this might rearrange the layout of items.
253 QFontMetrics fontMetrics(font());
254 const QRect bounds(fontMetrics.boundingRect(0, 0, availableTextWidth(), height(),
255 Qt::AlignVCenter | Qt::TextWordWrap, m_text));
256 requiredHeight = bounds.height();
257 if (requiredHeight < m_minTextHeight) {
258 requiredHeight = m_minTextHeight;
262 // Increase/decrease the current height of the widget to the
263 // required height. The increasing/decreasing is done in several
264 // steps to have an animation if the height is modified
265 // (see StatusBarMessageLabel::resizeEvent())
266 const int gap = m_minTextHeight / 2;
267 int minHeight = minimumHeight();
268 if (minHeight < requiredHeight) {
270 if (minHeight > requiredHeight) {
271 minHeight = requiredHeight;
273 setMinimumHeight(minHeight);
275 } else if (minHeight > requiredHeight) {
277 if (minHeight < requiredHeight) {
278 minHeight = requiredHeight;
280 setMinimumHeight(minHeight);
284 updateCloseButtonPosition();
287 int StatusBarMessageLabel::availableTextWidth() const
289 const int buttonWidth = 0; /*(m_type == ErrorMessage) ?
290 m_closeButton->width() + BorderGap : 0;*/
291 return width() - m_pixmap.width() - (BorderGap * 4) - buttonWidth;
294 void StatusBarMessageLabel::updateCloseButtonPosition()
296 const int x = width() - m_closeButton->width() - BorderGap;
297 const int y = (height() - m_closeButton->height()) / 2;
298 m_closeButton->move(x, y);
301 void StatusBarMessageLabel::closeErrorMessage()
303 if (!showPendingMessage()) {
304 setMessage(QString(), DefaultMessage);
308 bool StatusBarMessageLabel::showPendingMessage()
310 if (!m_pendingMessages.isEmpty()) {
311 setMessage(m_pendingMessages.takeFirst(), ErrorMessage);
318 #include "statusbarmessagelabel.moc"