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),
46 setMinimumHeight(KIconLoader::SizeSmall);
49 palette.setColor(QPalette::Background, Qt::transparent);
52 m_timer = new QTimer(this);
53 connect(m_timer, SIGNAL(timeout()),
54 this, SLOT(timerDone()));
56 m_closeButton = new QPushButton(i18nc("@action:button", "Close"), this);
57 m_closeButton->hide();
58 connect(m_closeButton, SIGNAL(clicked()),
59 this, SLOT(closeErrorMessage()));
62 StatusBarMessageLabel::~StatusBarMessageLabel()
66 void StatusBarMessageLabel::setMessage(const QString& text,
69 if ((text == m_text) && (type == m_type)) {
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();
99 case InformationMessage:
100 iconName = "dialog-information";
101 m_closeButton->hide();
105 iconName = "dialog-warning";
107 m_state = Illuminate;
109 //updateCloseButtonPosition();
110 //m_closeButton->show();
115 m_closeButton->hide();
119 m_pixmap = (iconName == 0) ? QPixmap() : SmallIcon(iconName);
120 //QTimer::singleShot(GeometryTimeout, this, SLOT(assureVisibleText()));
124 void StatusBarMessageLabel::setMinimumTextHeight(int min)
126 if (min != m_minTextHeight) {
127 m_minTextHeight = min;
128 setMinimumHeight(min);
129 /*if (m_closeButton->height() > min) {
130 m_closeButton->setFixedHeight(min);
135 int StatusBarMessageLabel::widthGap() const
137 QFontMetrics fontMetrics(font());
138 const int defaultGap = 10;
139 return fontMetrics.width(m_text) - availableTextWidth() + defaultGap;
142 void StatusBarMessageLabel::paintEvent(QPaintEvent* /* event */)
144 QPainter painter(this);
147 QColor backgroundColor = palette().window().color();
148 if (m_illumination > 0) {
149 // at this point, a: we are a second label being drawn over the already
150 // painted status area, so we can be translucent, and b: our palette's
151 // window color (bg only) seems to be wrong (always black)
152 KColorScheme scheme(palette().currentColorGroup(), KColorScheme::Window);
153 backgroundColor = scheme.background(KColorScheme::NegativeBackground).color();
154 backgroundColor.setAlpha(qMin(255, m_illumination*2));
156 painter.setBrush(backgroundColor);
157 painter.setPen(Qt::NoPen);
158 painter.drawRect(0, 0, width(), height());
162 int y = (height() - m_pixmap.height()) / 2;
164 if (!m_pixmap.isNull()) {
165 painter.drawPixmap(x, y, m_pixmap);
166 x += m_pixmap.width() + BorderGap * 2;
170 painter.setPen(palette().windowText().color());
171 int flags = Qt::AlignVCenter;
172 /*if (height() > m_minTextHeight) {
173 flags = flags | Qt::TextWordWrap;
175 painter.drawText(QRect(x, 0, availableTextWidth(), height()), flags, m_text);
179 void StatusBarMessageLabel::resizeEvent(QResizeEvent* event)
181 QWidget::resizeEvent(event);
182 //updateCloseButtonPosition();
183 //QTimer::singleShot(GeometryTimeout, this, SLOT(assureVisibleText()));
186 void StatusBarMessageLabel::timerDone()
190 // increase the illumination
191 const int illumination_max = 128;
192 if (m_illumination < illumination_max) {
193 m_illumination += 32;
194 if (m_illumination > illumination_max) {
195 m_illumination = illumination_max;
199 m_state = Illuminated;
200 m_timer->start(5000);
206 // start desaturation
207 m_state = Desaturate;
214 if (m_illumination > 0) {
230 void StatusBarMessageLabel::assureVisibleText()
232 if (m_text.isEmpty()) {
236 int requiredHeight = m_minTextHeight;
237 if (m_type != DefaultMessage) {
238 // Calculate the required height of the widget thats
239 // needed for having a fully visible text. Note that for the default
240 // statusbar type (e. g. hover information) increasing the text height
241 // is not wanted, as this might rearrange the layout of items.
243 QFontMetrics fontMetrics(font());
244 const QRect bounds(fontMetrics.boundingRect(0, 0, availableTextWidth(), height(),
245 Qt::AlignVCenter | Qt::TextWordWrap, m_text));
246 requiredHeight = bounds.height();
247 if (requiredHeight < m_minTextHeight) {
248 requiredHeight = m_minTextHeight;
252 // Increase/decrease the current height of the widget to the
253 // required height. The increasing/decreasing is done in several
254 // steps to have an animation if the height is modified
255 // (see StatusBarMessageLabel::resizeEvent())
256 const int gap = m_minTextHeight / 2;
257 int minHeight = minimumHeight();
258 if (minHeight < requiredHeight) {
260 if (minHeight > requiredHeight) {
261 minHeight = requiredHeight;
263 setMinimumHeight(minHeight);
265 } else if (minHeight > requiredHeight) {
267 if (minHeight < requiredHeight) {
268 minHeight = requiredHeight;
270 setMinimumHeight(minHeight);
274 //updateCloseButtonPosition();
277 int StatusBarMessageLabel::availableTextWidth() const
279 const int buttonWidth = 0; /*(m_type == ErrorMessage) ?
280 m_closeButton->width() + BorderGap : 0;*/
281 return width() - m_pixmap.width() - (BorderGap * 4) - buttonWidth;
284 void StatusBarMessageLabel::updateCloseButtonPosition()
286 const int x = width() - m_closeButton->width() - BorderGap;
287 const int y = (height() - m_closeButton->height()) / 2;
288 m_closeButton->move(x, y);
291 void StatusBarMessageLabel::closeErrorMessage()
293 if (!showPendingMessage()) {
295 setMessage(m_defaultText, DefaultMessage);
299 bool StatusBarMessageLabel::showPendingMessage()
301 if (!m_pendingMessages.isEmpty()) {
303 setMessage(m_pendingMessages.takeFirst(), ErrorMessage);
309 void StatusBarMessageLabel::reset()
312 m_pixmap = QPixmap();
313 m_type = DefaultMessage;
317 #include "statusbarmessagelabel.moc"