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>
29 #include <KNotification>
31 #include <QFontMetrics>
34 #include <QPushButton>
38 StatusBarMessageLabel::StatusBarMessageLabel(QWidget* parent) :
40 m_type(DefaultMessage),
46 setMinimumHeight(KIconLoader::SizeSmall);
49 palette.setColor(QPalette::Background, Qt::transparent);
52 connect(&m_timer, SIGNAL(timeout()), this, SLOT(timerDone()));
54 m_closeButton = new QPushButton(i18nc("@action:button", "Close"), this);
55 m_closeButton->hide();
56 connect(m_closeButton, SIGNAL(clicked()), this, SLOT(closeErrorMessage()));
59 StatusBarMessageLabel::~StatusBarMessageLabel()
63 void StatusBarMessageLabel::setMessage(const QString& text,
66 if ((text == m_text) && (type == m_type)) {
67 if (type == ErrorMessage) KNotification::event("ErrorMessage", m_text);
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();
107 KNotification::event("ErrorMessage", m_text);
111 iconName = "dialog-close";
113 m_state = Illuminate;
114 updateCloseButtonPosition();
115 m_closeButton->show();
120 m_closeButton->hide();
124 m_pixmap = (iconName == 0) ? QPixmap() : SmallIcon(iconName);
126 /*QFontMetrics fontMetrics(font());
127 setMaximumWidth(fontMetrics.boundingRect(m_text).width() + m_pixmap.width() + (BorderGap * 4));
130 //QTimer::singleShot(GeometryTimeout, this, SLOT(assureVisibleText()));
134 void StatusBarMessageLabel::setMinimumTextHeight(int min)
136 if (min != m_minTextHeight) {
137 m_minTextHeight = min;
138 setMinimumHeight(min);
139 if (m_closeButton->height() > min) {
140 m_closeButton->setFixedHeight(min);
145 void StatusBarMessageLabel::paintEvent(QPaintEvent* /* event */)
147 QPainter painter(this);
150 QColor backgroundColor;
151 if (m_state == Default || m_illumination < 0) backgroundColor = palette().window().color();
153 KColorScheme scheme(palette().currentColorGroup(), KColorScheme::Window);
154 backgroundColor = scheme.background(KColorScheme::NegativeBackground).color();
156 if (m_state == Desaturate && m_illumination > 0) {
157 backgroundColor.setAlpha(m_illumination * 2);
159 painter.fillRect(0, 0, width(), height(), backgroundColor);
163 int y = (height() - m_pixmap.height()) / 2;
165 if (!m_pixmap.isNull()) {
166 painter.drawPixmap(x, y, m_pixmap);
167 x += m_pixmap.width() + BorderGap * 2;
171 painter.setPen(palette().windowText().color());
172 int flags = Qt::AlignVCenter;
173 if (height() > m_minTextHeight) {
174 flags = flags | Qt::TextWordWrap;
176 painter.drawText(QRect(x, 0, availableTextWidth(), height()), flags, m_text);
180 void StatusBarMessageLabel::resizeEvent(QResizeEvent* event)
182 QWidget::resizeEvent(event);
183 updateCloseButtonPosition();
184 //QTimer::singleShot(GeometryTimeout, this, SLOT(assureVisibleText()));
187 void StatusBarMessageLabel::timerDone()
191 // increase the illumination
192 const int illumination_max = 128;
193 if (m_illumination < illumination_max) {
194 m_illumination += 32;
195 if (m_illumination > illumination_max) {
196 m_illumination = illumination_max;
200 m_state = Illuminated;
207 // start desaturation
208 if (m_type != MltError) {
209 m_state = Desaturate;
217 if (m_illumination < -128) {
221 setMessage(QString(), DefaultMessage);
234 void StatusBarMessageLabel::assureVisibleText()
236 if (m_text.isEmpty()) {
240 int requiredHeight = m_minTextHeight;
241 if (m_type != DefaultMessage) {
242 // Calculate the required height of the widget thats
243 // needed for having a fully visible text. Note that for the default
244 // statusbar type (e. g. hover information) increasing the text height
245 // is not wanted, as this might rearrange the layout of items.
247 QFontMetrics fontMetrics(font());
248 const QRect bounds(fontMetrics.boundingRect(0, 0, availableTextWidth(), height(),
249 Qt::AlignVCenter | Qt::TextWordWrap, m_text));
250 requiredHeight = bounds.height();
251 if (requiredHeight < m_minTextHeight) {
252 requiredHeight = m_minTextHeight;
256 // Increase/decrease the current height of the widget to the
257 // required height. The increasing/decreasing is done in several
258 // steps to have an animation if the height is modified
259 // (see StatusBarMessageLabel::resizeEvent())
260 const int gap = m_minTextHeight / 2;
261 int minHeight = minimumHeight();
262 if (minHeight < requiredHeight) {
264 if (minHeight > requiredHeight) {
265 minHeight = requiredHeight;
267 setMinimumHeight(minHeight);
269 } else if (minHeight > requiredHeight) {
271 if (minHeight < requiredHeight) {
272 minHeight = requiredHeight;
274 setMinimumHeight(minHeight);
278 updateCloseButtonPosition();
281 int StatusBarMessageLabel::availableTextWidth() const
283 const int buttonWidth = 0; /*(m_type == ErrorMessage) ?
284 m_closeButton->width() + BorderGap : 0;*/
285 return width() - m_pixmap.width() - (BorderGap * 4) - buttonWidth;
288 void StatusBarMessageLabel::updateCloseButtonPosition()
290 const int x = width() - m_closeButton->width() - BorderGap;
291 const int y = (height() - m_closeButton->height()) / 2;
292 m_closeButton->move(x, y);
295 void StatusBarMessageLabel::closeErrorMessage()
297 if (!showPendingMessage()) {
298 setMessage(QString(), DefaultMessage);
302 bool StatusBarMessageLabel::showPendingMessage()
304 if (!m_pendingMessages.isEmpty()) {
305 setMessage(m_pendingMessages.takeFirst(), ErrorMessage);
312 #include "statusbarmessagelabel.moc"