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 m_timer = new QTimer(this);
52 connect(m_timer, SIGNAL(timeout()),
53 this, SLOT(timerDone()));
55 m_closeButton = new QPushButton(i18nc("@action:button", "Close"), this);
56 m_closeButton->hide();
57 connect(m_closeButton, SIGNAL(clicked()),
58 this, SLOT(closeErrorMessage()));
61 StatusBarMessageLabel::~StatusBarMessageLabel() {
64 void StatusBarMessageLabel::setMessage(const QString& text,
66 if ((text == m_text) && (type == m_type)) {
70 /*if (m_type == ErrorMessage) {
71 if (type == ErrorMessage) {
72 m_pendingMessages.insert(0, m_text);
73 } else if ((m_state != Default) || !m_pendingMessages.isEmpty()) {
74 // a non-error message should not be shown, as there
75 // are other pending error messages in the queue
87 const char* iconName = 0;
90 case OperationCompletedMessage:
91 iconName = "dialog-ok";
92 // "ok" icon should probably be "dialog-success", but we don't have that icon in KDE 4.0
93 m_closeButton->hide();
96 case InformationMessage:
97 iconName = "dialog-information";
98 m_closeButton->hide();
102 iconName = "dialog-warning";
104 m_state = Illuminate;
106 //updateCloseButtonPosition();
107 //m_closeButton->show();
112 m_closeButton->hide();
116 m_pixmap = (iconName == 0) ? QPixmap() : SmallIcon(iconName);
117 //QTimer::singleShot(GeometryTimeout, this, SLOT(assureVisibleText()));
121 void StatusBarMessageLabel::setMinimumTextHeight(int min) {
122 if (min != m_minTextHeight) {
123 m_minTextHeight = min;
124 setMinimumHeight(min);
125 /*if (m_closeButton->height() > min) {
126 m_closeButton->setFixedHeight(min);
131 int StatusBarMessageLabel::widthGap() const {
132 QFontMetrics fontMetrics(font());
133 const int defaultGap = 10;
134 return fontMetrics.width(m_text) - availableTextWidth() + defaultGap;
137 void StatusBarMessageLabel::paintEvent(QPaintEvent* /* event */) {
138 QPainter painter(this);
141 QColor backgroundColor = palette().window().color();
142 if (m_illumination > 0) {
143 // at this point, a: we are a second label being drawn over the already
144 // painted status area, so we can be translucent, and b: our palette's
145 // window color (bg only) seems to be wrong (always black)
146 KColorScheme scheme(palette().currentColorGroup(), KColorScheme::Window);
147 backgroundColor = scheme.background(KColorScheme::NegativeBackground).color();
148 backgroundColor.setAlpha(qMin(255, m_illumination*2));
150 painter.setBrush(backgroundColor);
151 painter.setPen(Qt::NoPen);
152 painter.drawRect(0, 0, width(), height());
156 int y = (height() - m_pixmap.height()) / 2;
158 if (!m_pixmap.isNull()) {
159 painter.drawPixmap(x, y, m_pixmap);
160 x += m_pixmap.width() + BorderGap * 2;
164 painter.setPen(palette().windowText().color());
165 int flags = Qt::AlignVCenter;
166 /*if (height() > m_minTextHeight) {
167 flags = flags | Qt::TextWordWrap;
169 painter.drawText(QRect(x, 0, availableTextWidth(), height()), flags, m_text);
173 void StatusBarMessageLabel::resizeEvent(QResizeEvent* event) {
174 QWidget::resizeEvent(event);
175 //updateCloseButtonPosition();
176 //QTimer::singleShot(GeometryTimeout, this, SLOT(assureVisibleText()));
179 void StatusBarMessageLabel::timerDone() {
182 // increase the illumination
183 const int illumination_max = 128;
184 if (m_illumination < illumination_max) {
185 m_illumination += 32;
186 if (m_illumination > illumination_max) {
187 m_illumination = illumination_max;
191 m_state = Illuminated;
192 m_timer->start(5000);
198 // start desaturation
199 m_state = Desaturate;
206 if (m_illumination > 0) {
222 void StatusBarMessageLabel::assureVisibleText() {
223 if (m_text.isEmpty()) {
227 int requiredHeight = m_minTextHeight;
228 if (m_type != DefaultMessage) {
229 // Calculate the required height of the widget thats
230 // needed for having a fully visible text. Note that for the default
231 // statusbar type (e. g. hover information) increasing the text height
232 // is not wanted, as this might rearrange the layout of items.
234 QFontMetrics fontMetrics(font());
235 const QRect bounds(fontMetrics.boundingRect(0, 0, availableTextWidth(), height(),
236 Qt::AlignVCenter | Qt::TextWordWrap, m_text));
237 requiredHeight = bounds.height();
238 if (requiredHeight < m_minTextHeight) {
239 requiredHeight = m_minTextHeight;
243 // Increase/decrease the current height of the widget to the
244 // required height. The increasing/decreasing is done in several
245 // steps to have an animation if the height is modified
246 // (see StatusBarMessageLabel::resizeEvent())
247 const int gap = m_minTextHeight / 2;
248 int minHeight = minimumHeight();
249 if (minHeight < requiredHeight) {
251 if (minHeight > requiredHeight) {
252 minHeight = requiredHeight;
254 setMinimumHeight(minHeight);
256 } else if (minHeight > requiredHeight) {
258 if (minHeight < requiredHeight) {
259 minHeight = requiredHeight;
261 setMinimumHeight(minHeight);
265 //updateCloseButtonPosition();
268 int StatusBarMessageLabel::availableTextWidth() const {
269 const int buttonWidth = 0; /*(m_type == ErrorMessage) ?
270 m_closeButton->width() + BorderGap : 0;*/
271 return width() - m_pixmap.width() - (BorderGap * 4) - buttonWidth;
274 void StatusBarMessageLabel::updateCloseButtonPosition() {
275 const int x = width() - m_closeButton->width() - BorderGap;
276 const int y = (height() - m_closeButton->height()) / 2;
277 m_closeButton->move(x, y);
280 void StatusBarMessageLabel::closeErrorMessage() {
281 if (!showPendingMessage()) {
283 setMessage(m_defaultText, DefaultMessage);
287 bool StatusBarMessageLabel::showPendingMessage() {
288 if (!m_pendingMessages.isEmpty()) {
290 setMessage(m_pendingMessages.takeFirst(), ErrorMessage);
296 void StatusBarMessageLabel::reset() {
298 m_pixmap = QPixmap();
299 m_type = DefaultMessage;
303 #include "statusbarmessagelabel.moc"