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()), this, SLOT(timerDone()));
55 m_closeButton = new QPushButton(i18nc("@action:button", "Close"), this);
56 m_closeButton->hide();
57 connect(m_closeButton, SIGNAL(clicked()), this, SLOT(closeErrorMessage()));
60 StatusBarMessageLabel::~StatusBarMessageLabel()
64 void StatusBarMessageLabel::setMessage(const QString& text,
67 if ((text == m_text) && (type == m_type)) {
71 /*if (m_type == ErrorMessage) {
72 if (type == ErrorMessage) {
73 m_pendingMessages.insert(0, m_text);
74 } else if ((m_state != Default) || !m_pendingMessages.isEmpty()) {
75 // a non-error message should not be shown, as there
76 // are other pending error messages in the queue
88 const char* iconName = 0;
91 case OperationCompletedMessage:
92 iconName = "dialog-ok";
93 // "ok" icon should probably be "dialog-success", but we don't have that icon in KDE 4.0
94 m_closeButton->hide();
97 case InformationMessage:
98 iconName = "dialog-information";
99 m_closeButton->hide();
103 iconName = "dialog-warning";
105 m_state = Illuminate;
106 m_closeButton->hide();
110 iconName = "dialog-close";
112 m_state = Illuminate;
113 updateCloseButtonPosition();
114 m_closeButton->show();
119 m_closeButton->hide();
123 m_pixmap = (iconName == 0) ? QPixmap() : SmallIcon(iconName);
124 QTimer::singleShot(GeometryTimeout, this, SLOT(assureVisibleText()));
128 void StatusBarMessageLabel::setMinimumTextHeight(int min)
130 if (min != m_minTextHeight) {
131 m_minTextHeight = min;
132 setMinimumHeight(min);
133 if (m_closeButton->height() > min) {
134 m_closeButton->setFixedHeight(min);
139 void StatusBarMessageLabel::paintEvent(QPaintEvent* /* event */)
141 QPainter painter(this);
144 QColor backgroundColor;
145 if (m_state == Default || m_illumination < 0) backgroundColor = palette().window().color();
147 KColorScheme scheme(palette().currentColorGroup(), KColorScheme::Window);
148 backgroundColor = scheme.background(KColorScheme::NegativeBackground).color();
150 if (m_state == Desaturate && m_illumination > 0) {
151 backgroundColor.setAlpha(m_illumination * 2);
153 painter.fillRect(0, 0, width(), height(), backgroundColor);
157 int y = (height() - m_pixmap.height()) / 2;
159 if (!m_pixmap.isNull()) {
160 painter.drawPixmap(x, y, m_pixmap);
161 x += m_pixmap.width() + BorderGap * 2;
165 painter.setPen(palette().windowText().color());
166 int flags = Qt::AlignVCenter;
167 if (height() > m_minTextHeight) {
168 flags = flags | Qt::TextWordWrap;
170 painter.drawText(QRect(x, 0, availableTextWidth(), height()), flags, m_text);
174 void StatusBarMessageLabel::resizeEvent(QResizeEvent* event)
176 QWidget::resizeEvent(event);
177 updateCloseButtonPosition();
178 QTimer::singleShot(GeometryTimeout, this, SLOT(assureVisibleText()));
181 void StatusBarMessageLabel::timerDone()
185 // increase the illumination
186 const int illumination_max = 128;
187 if (m_illumination < illumination_max) {
188 m_illumination += 32;
189 if (m_illumination > illumination_max) {
190 m_illumination = illumination_max;
194 m_state = Illuminated;
195 m_timer->start(1500);
201 // start desaturation
202 if (m_type != MltError) {
203 m_state = Desaturate;
211 if (m_illumination < -128) {
215 setMessage(QString(), DefaultMessage);
228 void StatusBarMessageLabel::assureVisibleText()
230 if (m_text.isEmpty()) {
234 int requiredHeight = m_minTextHeight;
235 if (m_type != DefaultMessage) {
236 // Calculate the required height of the widget thats
237 // needed for having a fully visible text. Note that for the default
238 // statusbar type (e. g. hover information) increasing the text height
239 // is not wanted, as this might rearrange the layout of items.
241 QFontMetrics fontMetrics(font());
242 const QRect bounds(fontMetrics.boundingRect(0, 0, availableTextWidth(), height(),
243 Qt::AlignVCenter | Qt::TextWordWrap, m_text));
244 requiredHeight = bounds.height();
245 if (requiredHeight < m_minTextHeight) {
246 requiredHeight = m_minTextHeight;
250 // Increase/decrease the current height of the widget to the
251 // required height. The increasing/decreasing is done in several
252 // steps to have an animation if the height is modified
253 // (see StatusBarMessageLabel::resizeEvent())
254 const int gap = m_minTextHeight / 2;
255 int minHeight = minimumHeight();
256 if (minHeight < requiredHeight) {
258 if (minHeight > requiredHeight) {
259 minHeight = requiredHeight;
261 setMinimumHeight(minHeight);
263 } else if (minHeight > requiredHeight) {
265 if (minHeight < requiredHeight) {
266 minHeight = requiredHeight;
268 setMinimumHeight(minHeight);
272 updateCloseButtonPosition();
275 int StatusBarMessageLabel::availableTextWidth() const
277 const int buttonWidth = 0; /*(m_type == ErrorMessage) ?
278 m_closeButton->width() + BorderGap : 0;*/
279 return width() - m_pixmap.width() - (BorderGap * 4) - buttonWidth;
282 void StatusBarMessageLabel::updateCloseButtonPosition()
284 const int x = width() - m_closeButton->width() - BorderGap;
285 const int y = (height() - m_closeButton->height()) / 2;
286 m_closeButton->move(x, y);
289 void StatusBarMessageLabel::closeErrorMessage()
291 if (!showPendingMessage()) {
292 setMessage(QString(), DefaultMessage);
296 bool StatusBarMessageLabel::showPendingMessage()
298 if (!m_pendingMessages.isEmpty()) {
299 setMessage(m_pendingMessages.takeFirst(), ErrorMessage);
306 #include "statusbarmessagelabel.moc"