]>
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 <kiconloader.h>
28 #include <QtGui/QFontMetrics>
29 #include <QtGui/QPainter>
30 #include <QtGui/QKeyEvent>
31 #include <QtGui/QPushButton>
32 #include <QtGui/QPixmap>
33 #include <QtCore/QTimer>
35 StatusBarMessageLabel::StatusBarMessageLabel(QWidget
* parent
) :
37 m_type(DolphinStatusBar::Default
),
44 setMinimumHeight(K3Icon::SizeSmall
);
46 m_timer
= new QTimer(this);
47 connect(m_timer
, SIGNAL(timeout()),
48 this, SLOT(timerDone()));
50 m_closeButton
= new QPushButton(i18n("Close"), this);
51 m_closeButton
->hide();
52 connect(m_closeButton
, SIGNAL(clicked()),
53 this, SLOT(closeErrorMessage()));
56 StatusBarMessageLabel::~StatusBarMessageLabel()
59 void StatusBarMessageLabel::setMessage(const QString
& text
,
60 DolphinStatusBar::Type type
)
62 if ((text
== m_text
) && (type
== m_type
)) {
66 if (m_type
== DolphinStatusBar::Error
) {
67 if (type
== DolphinStatusBar::Error
) {
68 m_pendingMessages
.insert(0, m_text
);
69 } else if ((m_state
!= Default
) || !m_pendingMessages
.isEmpty()) {
70 // a non-error message should not be shown, as there
71 // are other pending error messages in the queue
83 const char* iconName
= 0;
86 case DolphinStatusBar::OperationCompleted
:
88 m_closeButton
->hide();
91 case DolphinStatusBar::Information
:
92 iconName
= "dialog-information";
93 m_closeButton
->hide();
96 case DolphinStatusBar::Error
:
97 iconName
= "dialog-error";
101 updateCloseButtonPosition();
102 m_closeButton
->show();
105 case DolphinStatusBar::Default
:
107 m_closeButton
->hide();
111 m_pixmap
= (iconName
== 0) ? QPixmap() : SmallIcon(iconName
);
112 QTimer::singleShot(GeometryTimeout
, this, SLOT(assureVisibleText()));
116 void StatusBarMessageLabel::setMinimumTextHeight(int min
)
118 if (min
!= m_minTextHeight
) {
119 m_minTextHeight
= min
;
120 setMinimumHeight(min
);
121 m_closeButton
->setFixedHeight(min
- borderGap() * 2);
125 int StatusBarMessageLabel::widthGap() const
127 QFontMetrics
fontMetrics(font());
128 const int defaultGap
= 10;
129 return fontMetrics
.width(m_text
) - availableTextWidth() + defaultGap
;
132 void StatusBarMessageLabel::paintEvent(QPaintEvent
* /* event */)
134 QPainter
painter(this);
137 QColor
backgroundColor(palette().brush(QPalette::Background
).color());
138 QColor
foregroundColor(KGlobalSettings::textColor());
139 if (m_illumination
> 0) {
140 backgroundColor
= mixColors(backgroundColor
, QColor(255, 255, 128), m_illumination
);
141 foregroundColor
= mixColors(foregroundColor
, QColor(0, 0, 0), m_illumination
);
143 painter
.setBrush(backgroundColor
);
144 painter
.setPen(backgroundColor
);
145 painter
.drawRect(QRect(0, 0, width(), height()));
149 int y
= (m_minTextHeight
- m_pixmap
.height()) / 2;
151 if (!m_pixmap
.isNull()) {
152 painter
.drawPixmap(x
, y
, m_pixmap
);
153 x
+= m_pixmap
.width() + borderGap();
157 painter
.setPen(foregroundColor
);
158 int flags
= Qt::AlignVCenter
;
159 if (height() > m_minTextHeight
) {
160 flags
= flags
| Qt::TextWordWrap
;
162 painter
.drawText(QRect(x
, 0, availableTextWidth(), height()), flags
, m_text
);
166 void StatusBarMessageLabel::resizeEvent(QResizeEvent
* event
)
168 QWidget::resizeEvent(event
);
169 updateCloseButtonPosition();
170 QTimer::singleShot(GeometryTimeout
, this, SLOT(assureVisibleText()));
173 void StatusBarMessageLabel::timerDone()
177 // increase the illumination
178 if (m_illumination
< 100) {
179 m_illumination
+= 20;
182 m_state
= Illuminated
;
183 m_timer
->start(5000);
189 // start desaturation
190 m_state
= Desaturate
;
197 if (m_illumination
> 0) {
212 void StatusBarMessageLabel::assureVisibleText()
214 if (m_text
.isEmpty()) {
218 // calculate the required height of the widget thats
219 // needed for having a fully visible text
220 QFontMetrics
fontMetrics(font());
221 const QRect
bounds(fontMetrics
.boundingRect(0, 0, availableTextWidth(), height(),
222 Qt::AlignVCenter
| Qt::TextWordWrap
,
224 int requiredHeight
= bounds
.height();
225 if (requiredHeight
< m_minTextHeight
) {
226 requiredHeight
= m_minTextHeight
;
229 // Increase/decrease the current height of the widget to the
230 // required height. The increasing/decreasing is done in several
231 // steps to have an animation if the height is modified
232 // (see StatusBarMessageLabel::resizeEvent())
233 const int gap
= m_minTextHeight
/ 2;
234 int minHeight
= minimumHeight();
235 if (minHeight
< requiredHeight
) {
237 if (minHeight
> requiredHeight
) {
238 minHeight
= requiredHeight
;
240 setMinimumHeight(minHeight
);
242 } else if (minHeight
> requiredHeight
) {
244 if (minHeight
< requiredHeight
) {
245 minHeight
= requiredHeight
;
247 setMinimumHeight(minHeight
);
251 updateCloseButtonPosition();
254 int StatusBarMessageLabel::availableTextWidth() const
256 const int buttonWidth
= (m_type
== DolphinStatusBar::Error
) ?
257 m_closeButton
->width() + borderGap() : 0;
258 return width() - m_pixmap
.width() - (borderGap() * 4) - buttonWidth
;
261 QColor
StatusBarMessageLabel::mixColors(const QColor
& c1
,
265 const int recip
= 100 - percent
;
266 const int red
= (c1
.red() * recip
+ c2
.red() * percent
) / 100;
267 const int green
= (c1
.green() * recip
+ c2
.green() * percent
) / 100;
268 const int blue
= (c1
.blue() * recip
+ c2
.blue() * percent
) / 100;
269 return QColor(red
, green
, blue
);
272 void StatusBarMessageLabel::updateCloseButtonPosition()
274 const int x
= width() - m_closeButton
->width() - borderGap();
275 const int y
= height() - m_closeButton
->height() - borderGap();
276 m_closeButton
->move(x
, y
);
279 void StatusBarMessageLabel::closeErrorMessage()
281 if (!showPendingMessage()) {
283 setMessage(m_defaultText
, DolphinStatusBar::Default
);
287 bool StatusBarMessageLabel::showPendingMessage()
289 if (!m_pendingMessages
.isEmpty()) {
291 setMessage(m_pendingMessages
.takeFirst(), DolphinStatusBar::Error
);
297 void StatusBarMessageLabel::reset()
300 m_type
= DolphinStatusBar::Default
;
303 #include "statusbarmessagelabel.moc"