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>
30 #include <QFontMetrics>
33 #include <QPushButton>
37 StatusBarMessageLabel::StatusBarMessageLabel(QWidget* parent) :
39 m_type(DefaultMessage),
45 setMinimumHeight(KIconLoader::SizeSmall);
48 palette.setColor(QPalette::Background, Qt::transparent);
51 connect(&m_timer, SIGNAL(timeout()), this, SLOT(timerDone()));
53 m_closeButton = new QPushButton(i18nc("@action:button", "Close"), this);
54 m_closeButton->hide();
55 connect(m_closeButton, SIGNAL(clicked()), this, SLOT(closeErrorMessage()));
58 StatusBarMessageLabel::~StatusBarMessageLabel()
62 void StatusBarMessageLabel::setMessage(const QString& text,
65 if ((text == m_text) && (type == m_type)) {
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
86 const char* iconName = 0;
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();
95 case InformationMessage:
96 iconName = "dialog-information";
97 m_closeButton->hide();
101 iconName = "dialog-warning";
103 m_state = Illuminate;
104 m_closeButton->hide();
108 iconName = "dialog-close";
110 m_state = Illuminate;
111 updateCloseButtonPosition();
112 m_closeButton->show();
117 m_closeButton->hide();
121 m_pixmap = (iconName == 0) ? QPixmap() : SmallIcon(iconName);
123 /*QFontMetrics fontMetrics(font());
124 setMaximumWidth(fontMetrics.boundingRect(m_text).width() + m_pixmap.width() + (BorderGap * 4));
127 //QTimer::singleShot(GeometryTimeout, this, SLOT(assureVisibleText()));
131 void StatusBarMessageLabel::setMinimumTextHeight(int min)
133 if (min != m_minTextHeight) {
134 m_minTextHeight = min;
135 setMinimumHeight(min);
136 if (m_closeButton->height() > min) {
137 m_closeButton->setFixedHeight(min);
142 void StatusBarMessageLabel::paintEvent(QPaintEvent* /* event */)
144 QPainter painter(this);
147 QColor backgroundColor;
148 if (m_state == Default || m_illumination < 0) backgroundColor = palette().window().color();
150 KColorScheme scheme(palette().currentColorGroup(), KColorScheme::Window);
151 backgroundColor = scheme.background(KColorScheme::NegativeBackground).color();
153 if (m_state == Desaturate && m_illumination > 0) {
154 backgroundColor.setAlpha(m_illumination * 2);
156 painter.fillRect(0, 0, width(), height(), backgroundColor);
160 int y = (height() - m_pixmap.height()) / 2;
162 if (!m_pixmap.isNull()) {
163 painter.drawPixmap(x, y, m_pixmap);
164 x += m_pixmap.width() + BorderGap * 2;
168 painter.setPen(palette().windowText().color());
169 int flags = Qt::AlignVCenter;
170 if (height() > m_minTextHeight) {
171 flags = flags | Qt::TextWordWrap;
173 painter.drawText(QRect(x, 0, availableTextWidth(), height()), flags, m_text);
177 void StatusBarMessageLabel::resizeEvent(QResizeEvent* event)
179 QWidget::resizeEvent(event);
180 updateCloseButtonPosition();
181 //QTimer::singleShot(GeometryTimeout, this, SLOT(assureVisibleText()));
184 void StatusBarMessageLabel::timerDone()
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;
197 m_state = Illuminated;
204 // start desaturation
205 if (m_type != MltError) {
206 m_state = Desaturate;
214 if (m_illumination < -128) {
218 setMessage(QString(), DefaultMessage);
231 void StatusBarMessageLabel::assureVisibleText()
233 if (m_text.isEmpty()) {
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.
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;
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) {
261 if (minHeight > requiredHeight) {
262 minHeight = requiredHeight;
264 setMinimumHeight(minHeight);
266 } else if (minHeight > requiredHeight) {
268 if (minHeight < requiredHeight) {
269 minHeight = requiredHeight;
271 setMinimumHeight(minHeight);
275 updateCloseButtonPosition();
278 int StatusBarMessageLabel::availableTextWidth() const
280 const int buttonWidth = 0; /*(m_type == ErrorMessage) ?
281 m_closeButton->width() + BorderGap : 0;*/
282 return width() - m_pixmap.width() - (BorderGap * 4) - buttonWidth;
285 void StatusBarMessageLabel::updateCloseButtonPosition()
287 const int x = width() - m_closeButton->width() - BorderGap;
288 const int y = (height() - m_closeButton->height()) / 2;
289 m_closeButton->move(x, y);
292 void StatusBarMessageLabel::closeErrorMessage()
294 if (!showPendingMessage()) {
295 setMessage(QString(), DefaultMessage);
299 bool StatusBarMessageLabel::showPendingMessage()
301 if (!m_pendingMessages.isEmpty()) {
302 setMessage(m_pendingMessages.takeFirst(), ErrorMessage);
309 #include "statusbarmessagelabel.moc"