1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
21 #include "statusbarmessagelabel.h"
23 #include <kcolorscheme.h>
24 #include <kiconloader.h>
28 #include <QFontMetrics>
31 #include <QPushButton>
35 StatusBarMessageLabel::StatusBarMessageLabel(QWidget
* parent
) :
37 m_type(DolphinStatusBar::Default
),
44 setMinimumHeight(KIconLoader::SizeSmall
);
46 m_timer
= new QTimer(this);
47 connect(m_timer
, SIGNAL(timeout()),
48 this, SLOT(timerDone()));
50 m_closeButton
= new QPushButton(i18nc("@action:button", "Close"), this);
51 m_closeButton
->hide();
52 connect(m_closeButton
, SIGNAL(clicked()),
53 this, SLOT(closeErrorMessage()));
56 StatusBarMessageLabel::~StatusBarMessageLabel()
60 void StatusBarMessageLabel::setMessage(const QString
& text
,
61 DolphinStatusBar::Type type
)
63 if ((text
== m_text
) && (type
== m_type
)) {
67 if (m_type
== DolphinStatusBar::Error
) {
68 if (type
== DolphinStatusBar::Error
) {
69 m_pendingMessages
.insert(0, m_text
);
70 } else if ((m_state
!= Default
) || !m_pendingMessages
.isEmpty()) {
71 // a non-error message should not be shown, as there
72 // are other pending error messages in the queue
84 const char* iconName
= 0;
87 case DolphinStatusBar::OperationCompleted
:
88 iconName
= "dialog-ok";
89 // "ok" icon should probably be "dialog-success", but we don't have that icon in KDE 4.0
90 m_closeButton
->hide();
93 case DolphinStatusBar::Information
:
94 iconName
= "dialog-information";
95 m_closeButton
->hide();
98 case DolphinStatusBar::Error
:
100 m_state
= Illuminate
;
102 updateCloseButtonPosition();
103 m_closeButton
->show();
106 case DolphinStatusBar::Default
:
108 m_closeButton
->hide();
112 m_pixmap
= (iconName
== 0) ? QPixmap() : SmallIcon(iconName
);
113 QTimer::singleShot(GeometryTimeout
, this, SLOT(assureVisibleText()));
117 void StatusBarMessageLabel::setMinimumTextHeight(int min
)
119 if (min
!= m_minTextHeight
) {
120 m_minTextHeight
= min
;
121 setMinimumHeight(min
);
122 if (m_closeButton
->height() > min
) {
123 m_closeButton
->setFixedHeight(min
);
128 int StatusBarMessageLabel::widthGap() const
130 QFontMetrics
fontMetrics(font());
131 const int defaultGap
= 10;
132 return fontMetrics
.width(m_text
) - availableTextWidth() + defaultGap
;
135 void StatusBarMessageLabel::paintEvent(QPaintEvent
* /* event */)
137 QPainter
painter(this);
139 if (m_illumination
> 0) {
140 // at this point, a: we are a second label being drawn over the already
141 // painted status area, so we can be translucent, and b: our palette's
142 // window color (bg only) seems to be wrong (always black)
143 KColorScheme
scheme(palette().currentColorGroup(), KColorScheme::Window
);
144 QColor backgroundColor
= scheme
.background(KColorScheme::NegativeBackground
).color();
145 backgroundColor
.setAlpha(qMin(255, m_illumination
* 2));
146 painter
.setBrush(backgroundColor
);
147 painter
.setPen(Qt::NoPen
);
148 painter
.drawRect(QRect(0, 0, width(), height()));
153 const int y
= (m_minTextHeight
- m_pixmap
.height()) / 2;
155 if (!m_pixmap
.isNull()) {
156 painter
.drawPixmap(x
, y
, m_pixmap
);
157 x
+= m_pixmap
.width() + BorderGap
;
161 painter
.setPen(palette().windowText().color());
162 int flags
= Qt::AlignVCenter
;
163 if (height() > m_minTextHeight
) {
164 flags
= flags
| Qt::TextWordWrap
;
166 painter
.drawText(QRect(x
, 0, availableTextWidth(), height()), flags
, m_text
);
170 void StatusBarMessageLabel::resizeEvent(QResizeEvent
* event
)
172 QWidget::resizeEvent(event
);
173 updateCloseButtonPosition();
174 QTimer::singleShot(GeometryTimeout
, this, SLOT(assureVisibleText()));
177 void StatusBarMessageLabel::timerDone()
181 // increase the illumination
182 const int illumination_max
= 128;
183 if (m_illumination
< illumination_max
) {
184 m_illumination
+= 32;
185 if (m_illumination
> illumination_max
) {
186 m_illumination
= illumination_max
;
190 m_state
= Illuminated
;
191 m_timer
->start(5000);
197 // start desaturation
198 m_state
= Desaturate
;
205 if (m_illumination
> 0) {
220 void StatusBarMessageLabel::assureVisibleText()
222 if (m_text
.isEmpty()) {
226 int requiredHeight
= m_minTextHeight
;
227 if (m_type
!= DolphinStatusBar::Default
) {
228 // Calculate the required height of the widget thats
229 // needed for having a fully visible text. Note that for the default
230 // statusbar type (e. g. hover information) increasing the text height
231 // is not wanted, as this might rearrange the layout of items.
233 QFontMetrics
fontMetrics(font());
234 const QRect
bounds(fontMetrics
.boundingRect(0, 0, availableTextWidth(), height(),
235 Qt::AlignVCenter
| Qt::TextWordWrap
, m_text
));
236 requiredHeight
= bounds
.height();
237 if (requiredHeight
< m_minTextHeight
) {
238 requiredHeight
= m_minTextHeight
;
242 // Increase/decrease the current height of the widget to the
243 // required height. The increasing/decreasing is done in several
244 // steps to have an animation if the height is modified
245 // (see StatusBarMessageLabel::resizeEvent())
246 const int gap
= m_minTextHeight
/ 2;
247 int minHeight
= minimumHeight();
248 if (minHeight
< requiredHeight
) {
250 if (minHeight
> requiredHeight
) {
251 minHeight
= requiredHeight
;
253 setMinimumHeight(minHeight
);
255 } else if (minHeight
> requiredHeight
) {
257 if (minHeight
< requiredHeight
) {
258 minHeight
= requiredHeight
;
260 setMinimumHeight(minHeight
);
264 updateCloseButtonPosition();
267 int StatusBarMessageLabel::availableTextWidth() const
269 const int buttonWidth
= (m_type
== DolphinStatusBar::Error
) ?
270 m_closeButton
->width() + BorderGap
: 0;
271 return width() - m_pixmap
.width() - (BorderGap
* 4) - buttonWidth
;
274 void StatusBarMessageLabel::updateCloseButtonPosition()
276 const int x
= width() - m_closeButton
->width() - BorderGap
;
277 const int y
= (height() - m_closeButton
->height()) / 2;
278 m_closeButton
->move(x
, y
);
281 void StatusBarMessageLabel::closeErrorMessage()
283 if (!showPendingMessage()) {
285 setMessage(m_defaultText
, DolphinStatusBar::Default
);
289 bool StatusBarMessageLabel::showPendingMessage()
291 if (!m_pendingMessages
.isEmpty()) {
293 setMessage(m_pendingMessages
.takeFirst(), DolphinStatusBar::Error
);
299 void StatusBarMessageLabel::reset()
302 m_type
= DolphinStatusBar::Default
;
305 #include "statusbarmessagelabel.moc"