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);
122 QTimer::singleShot(GeometryTimeout, this, SLOT(assureVisibleText()));
126 void StatusBarMessageLabel::setMinimumTextHeight(int min)
128 if (min != m_minTextHeight) {
129 m_minTextHeight = min;
130 setMinimumHeight(min);
131 if (m_closeButton->height() > min) {
132 m_closeButton->setFixedHeight(min);
137 void StatusBarMessageLabel::paintEvent(QPaintEvent* /* event */)
139 QPainter painter(this);
142 QColor backgroundColor;
143 if (m_state == Default || m_illumination < 0) backgroundColor = palette().window().color();
145 KColorScheme scheme(palette().currentColorGroup(), KColorScheme::Window);
146 backgroundColor = scheme.background(KColorScheme::NegativeBackground).color();
148 if (m_state == Desaturate && m_illumination > 0) {
149 backgroundColor.setAlpha(m_illumination * 2);
151 painter.fillRect(0, 0, width(), height(), backgroundColor);
155 int y = (height() - m_pixmap.height()) / 2;
157 if (!m_pixmap.isNull()) {
158 painter.drawPixmap(x, y, m_pixmap);
159 x += m_pixmap.width() + BorderGap * 2;
163 painter.setPen(palette().windowText().color());
164 int flags = Qt::AlignVCenter;
165 if (height() > m_minTextHeight) {
166 flags = flags | Qt::TextWordWrap;
168 painter.drawText(QRect(x, 0, availableTextWidth(), height()), flags, m_text);
172 void StatusBarMessageLabel::resizeEvent(QResizeEvent* event)
174 QWidget::resizeEvent(event);
175 updateCloseButtonPosition();
176 QTimer::singleShot(GeometryTimeout, this, SLOT(assureVisibleText()));
179 void StatusBarMessageLabel::timerDone()
183 // increase the illumination
184 const int illumination_max = 128;
185 if (m_illumination < illumination_max) {
186 m_illumination += 32;
187 if (m_illumination > illumination_max) {
188 m_illumination = illumination_max;
192 m_state = Illuminated;
199 // start desaturation
200 if (m_type != MltError) {
201 m_state = Desaturate;
209 if (m_illumination < -128) {
213 setMessage(QString(), DefaultMessage);
226 void StatusBarMessageLabel::assureVisibleText()
228 if (m_text.isEmpty()) {
232 int requiredHeight = m_minTextHeight;
233 if (m_type != DefaultMessage) {
234 // Calculate the required height of the widget thats
235 // needed for having a fully visible text. Note that for the default
236 // statusbar type (e. g. hover information) increasing the text height
237 // is not wanted, as this might rearrange the layout of items.
239 QFontMetrics fontMetrics(font());
240 const QRect bounds(fontMetrics.boundingRect(0, 0, availableTextWidth(), height(),
241 Qt::AlignVCenter | Qt::TextWordWrap, m_text));
242 requiredHeight = bounds.height();
243 if (requiredHeight < m_minTextHeight) {
244 requiredHeight = m_minTextHeight;
248 // Increase/decrease the current height of the widget to the
249 // required height. The increasing/decreasing is done in several
250 // steps to have an animation if the height is modified
251 // (see StatusBarMessageLabel::resizeEvent())
252 const int gap = m_minTextHeight / 2;
253 int minHeight = minimumHeight();
254 if (minHeight < requiredHeight) {
256 if (minHeight > requiredHeight) {
257 minHeight = requiredHeight;
259 setMinimumHeight(minHeight);
261 } else if (minHeight > requiredHeight) {
263 if (minHeight < requiredHeight) {
264 minHeight = requiredHeight;
266 setMinimumHeight(minHeight);
270 updateCloseButtonPosition();
273 int StatusBarMessageLabel::availableTextWidth() const
275 const int buttonWidth = 0; /*(m_type == ErrorMessage) ?
276 m_closeButton->width() + BorderGap : 0;*/
277 return width() - m_pixmap.width() - (BorderGap * 4) - buttonWidth;
280 void StatusBarMessageLabel::updateCloseButtonPosition()
282 const int x = width() - m_closeButton->width() - BorderGap;
283 const int y = (height() - m_closeButton->height()) / 2;
284 m_closeButton->move(x, y);
287 void StatusBarMessageLabel::closeErrorMessage()
289 if (!showPendingMessage()) {
290 setMessage(QString(), DefaultMessage);
294 bool StatusBarMessageLabel::showPendingMessage()
296 if (!m_pendingMessages.isEmpty()) {
297 setMessage(m_pendingMessages.takeFirst(), ErrorMessage);
304 #include "statusbarmessagelabel.moc"