]>
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 <kglobalsettings.h>
24 #include <kgraphicsutils.h>
25 #include <kiconloader.h>
29 #include <QtGui/QFontMetrics>
30 #include <QtGui/QPainter>
31 #include <QtGui/QKeyEvent>
32 #include <QtGui/QPushButton>
33 #include <QtGui/QPixmap>
34 #include <QtCore/QTimer>
36 StatusBarMessageLabel::StatusBarMessageLabel(QWidget
* parent
) :
38 m_type(DolphinStatusBar::Default
),
45 setMinimumHeight(K3Icon::SizeSmall
);
47 m_timer
= new QTimer(this);
48 connect(m_timer
, SIGNAL(timeout()),
49 this, SLOT(timerDone()));
51 m_closeButton
= new QPushButton(i18n("Close"), this);
52 m_closeButton
->hide();
53 connect(m_closeButton
, SIGNAL(clicked()),
54 this, SLOT(closeErrorMessage()));
57 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
:
89 m_closeButton
->hide();
92 case DolphinStatusBar::Information
:
93 iconName
= "dialog-information";
94 m_closeButton
->hide();
97 case DolphinStatusBar::Error
:
98 iconName
= "dialog-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 m_closeButton
->setFixedHeight(min
- borderGap() * 2);
126 int StatusBarMessageLabel::widthGap() const
128 QFontMetrics
fontMetrics(font());
129 const int defaultGap
= 10;
130 return fontMetrics
.width(m_text
) - availableTextWidth() + defaultGap
;
133 void StatusBarMessageLabel::paintEvent(QPaintEvent
* /* event */)
135 QPainter
painter(this);
138 QColor
backgroundColor(palette().brush(QPalette::Background
).color());
139 QColor
foregroundColor(KGlobalSettings::textColor());
140 if (m_illumination
> 0) {
141 QColor
mixColor(255, 255, 128);
142 mixColor
.setAlpha(m_illumination
);
143 backgroundColor
= KGraphicsUtils::blendColor(backgroundColor
, mixColor
);
145 mixColor
.setRgb(0, 0, 0, m_illumination
);
146 foregroundColor
= KGraphicsUtils::blendColor(foregroundColor
, mixColor
);
148 painter
.setBrush(backgroundColor
);
149 painter
.setPen(backgroundColor
);
150 painter
.drawRect(QRect(0, 0, width(), height()));
154 int y
= (m_minTextHeight
- m_pixmap
.height()) / 2;
156 if (!m_pixmap
.isNull()) {
157 painter
.drawPixmap(x
, y
, m_pixmap
);
158 x
+= m_pixmap
.width() + borderGap();
162 painter
.setPen(foregroundColor
);
163 int flags
= Qt::AlignVCenter
;
164 if (height() > m_minTextHeight
) {
165 flags
= flags
| Qt::TextWordWrap
;
167 painter
.drawText(QRect(x
, 0, availableTextWidth(), height()), flags
, m_text
);
171 void StatusBarMessageLabel::resizeEvent(QResizeEvent
* event
)
173 QWidget::resizeEvent(event
);
174 updateCloseButtonPosition();
175 QTimer::singleShot(GeometryTimeout
, this, SLOT(assureVisibleText()));
178 void StatusBarMessageLabel::timerDone()
182 // increase the illumination
183 if (m_illumination
< 255) {
184 m_illumination
+= 32;
185 if (m_illumination
> 255) {
186 m_illumination
= 255;
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 // calculate the required height of the widget thats
227 // needed for having a fully visible text
228 QFontMetrics
fontMetrics(font());
229 const QRect
bounds(fontMetrics
.boundingRect(0, 0, availableTextWidth(), height(),
230 Qt::AlignVCenter
| Qt::TextWordWrap
,
232 int requiredHeight
= bounds
.height();
233 if (requiredHeight
< m_minTextHeight
) {
234 requiredHeight
= m_minTextHeight
;
237 // Increase/decrease the current height of the widget to the
238 // required height. The increasing/decreasing is done in several
239 // steps to have an animation if the height is modified
240 // (see StatusBarMessageLabel::resizeEvent())
241 const int gap
= m_minTextHeight
/ 2;
242 int minHeight
= minimumHeight();
243 if (minHeight
< requiredHeight
) {
245 if (minHeight
> requiredHeight
) {
246 minHeight
= requiredHeight
;
248 setMinimumHeight(minHeight
);
250 } else if (minHeight
> requiredHeight
) {
252 if (minHeight
< requiredHeight
) {
253 minHeight
= requiredHeight
;
255 setMinimumHeight(minHeight
);
259 updateCloseButtonPosition();
262 int StatusBarMessageLabel::availableTextWidth() const
264 const int buttonWidth
= (m_type
== DolphinStatusBar::Error
) ?
265 m_closeButton
->width() + borderGap() : 0;
266 return width() - m_pixmap
.width() - (borderGap() * 4) - buttonWidth
;
269 void StatusBarMessageLabel::updateCloseButtonPosition()
271 const int x
= width() - m_closeButton
->width() - borderGap();
272 const int y
= height() - m_closeButton
->height() - borderGap();
273 m_closeButton
->move(x
, y
);
276 void StatusBarMessageLabel::closeErrorMessage()
278 if (!showPendingMessage()) {
280 setMessage(m_defaultText
, DolphinStatusBar::Default
);
284 bool StatusBarMessageLabel::showPendingMessage()
286 if (!m_pendingMessages
.isEmpty()) {
288 setMessage(m_pendingMessages
.takeFirst(), DolphinStatusBar::Error
);
294 void StatusBarMessageLabel::reset()
297 m_type
= DolphinStatusBar::Default
;
300 #include "statusbarmessagelabel.moc"