]>
cloud.milkyroute.net Git - dolphin.git/blob - src/statusbarmessagelabel.cpp
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
);
47 palette
.setColor(QPalette::Background
, Qt::transparent
);
50 m_timer
= new QTimer(this);
51 connect(m_timer
, SIGNAL(timeout()),
52 this, SLOT(timerDone()));
54 m_closeButton
= new QPushButton(i18nc("@action:button", "Close"), this);
55 m_closeButton
->hide();
56 connect(m_closeButton
, SIGNAL(clicked()),
57 this, SLOT(closeErrorMessage()));
60 StatusBarMessageLabel::~StatusBarMessageLabel()
64 void StatusBarMessageLabel::setMessage(const QString
& text
,
65 DolphinStatusBar::Type type
)
67 if ((text
== m_text
) && (type
== m_type
)) {
71 if (m_type
== DolphinStatusBar::Error
) {
72 if (type
== DolphinStatusBar::Error
) {
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 DolphinStatusBar::OperationCompleted
:
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 DolphinStatusBar::Information
:
98 iconName
= "dialog-information";
99 m_closeButton
->hide();
102 case DolphinStatusBar::Error
:
104 m_state
= Illuminate
;
106 updateCloseButtonPosition();
107 m_closeButton
->show();
110 case DolphinStatusBar::Default
:
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
)
123 if (min
!= m_minTextHeight
) {
124 m_minTextHeight
= min
;
125 setMinimumHeight(min
);
126 if (m_closeButton
->height() > min
) {
127 m_closeButton
->setFixedHeight(min
);
132 int StatusBarMessageLabel::widthGap() const
134 QFontMetrics
fontMetrics(font());
135 const int defaultGap
= 10;
136 return fontMetrics
.width(m_text
) - availableTextWidth() + defaultGap
;
139 void StatusBarMessageLabel::paintEvent(QPaintEvent
* /* event */)
141 QPainter
painter(this);
144 QColor backgroundColor
= palette().window().color();
145 if (m_illumination
> 0) {
146 // at this point, a: we are a second label being drawn over the already
147 // painted status area, so we can be translucent, and b: our palette's
148 // window color (bg only) seems to be wrong (always black)
149 KColorScheme
scheme(palette().currentColorGroup(), KColorScheme::Window
);
150 backgroundColor
= scheme
.background(KColorScheme::NegativeBackground
).color();
151 backgroundColor
.setAlpha(qMin(255, m_illumination
*2));
153 painter
.setBrush(backgroundColor
);
154 painter
.setPen(Qt::NoPen
);
155 painter
.drawRect(QRect(0, 0, width(), height()));
159 int y
= (m_minTextHeight
- m_pixmap
.height()) / 2;
161 if (!m_pixmap
.isNull()) {
162 painter
.drawPixmap(x
, y
, m_pixmap
);
163 x
+= m_pixmap
.width() + BorderGap
;
167 painter
.setPen(palette().windowText().color());
168 int flags
= Qt::AlignVCenter
;
169 if (height() > m_minTextHeight
) {
170 flags
= flags
| Qt::TextWordWrap
;
172 painter
.drawText(QRect(x
, 0, availableTextWidth(), height()), flags
, m_text
);
176 void StatusBarMessageLabel::resizeEvent(QResizeEvent
* event
)
178 QWidget::resizeEvent(event
);
179 updateCloseButtonPosition();
180 QTimer::singleShot(GeometryTimeout
, this, SLOT(assureVisibleText()));
183 void StatusBarMessageLabel::timerDone()
187 // increase the illumination
188 const int illumination_max
= 128;
189 if (m_illumination
< illumination_max
) {
190 m_illumination
+= 32;
191 if (m_illumination
> illumination_max
) {
192 m_illumination
= illumination_max
;
196 m_state
= Illuminated
;
197 m_timer
->start(5000);
203 // start desaturation
204 m_state
= Desaturate
;
211 if (m_illumination
> 0) {
226 void StatusBarMessageLabel::assureVisibleText()
228 if (m_text
.isEmpty()) {
232 // calculate the required height of the widget thats
233 // needed for having a fully visible text
234 QFontMetrics
fontMetrics(font());
235 const QRect
bounds(fontMetrics
.boundingRect(0, 0, availableTextWidth(), height(),
236 Qt::AlignVCenter
| Qt::TextWordWrap
,
238 int requiredHeight
= bounds
.height();
239 if (requiredHeight
< m_minTextHeight
) {
240 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
270 const int buttonWidth
= (m_type
== DolphinStatusBar::Error
) ?
271 m_closeButton
->width() + BorderGap
: 0;
272 return width() - m_pixmap
.width() - (BorderGap
* 4) - buttonWidth
;
275 void StatusBarMessageLabel::updateCloseButtonPosition()
277 const int x
= width() - m_closeButton
->width() - BorderGap
;
278 const int y
= (height() - m_closeButton
->height()) / 2;
279 m_closeButton
->move(x
, y
);
282 void StatusBarMessageLabel::closeErrorMessage()
284 if (!showPendingMessage()) {
286 setMessage(m_defaultText
, DolphinStatusBar::Default
);
290 bool StatusBarMessageLabel::showPendingMessage()
292 if (!m_pendingMessages
.isEmpty()) {
294 setMessage(m_pendingMessages
.takeFirst(), DolphinStatusBar::Error
);
300 void StatusBarMessageLabel::reset()
303 m_type
= DolphinStatusBar::Default
;
306 #include "statusbarmessagelabel.moc"