]>
cloud.milkyroute.net Git - dolphin.git/blob - src/statusbarmessagelabel.cpp
c870406d3419258b66f90a51a27f9aba6c8c83cb
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 <QFontMetrics>
30 #include <QPaintEvent>
31 #include <QPushButton>
33 #include <QResizeEvent>
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()
61 void StatusBarMessageLabel::setMessage(const QString
& text
,
62 DolphinStatusBar::Type type
)
64 if ((text
== m_text
) && (type
== m_type
)) {
68 if (m_type
== DolphinStatusBar::Error
) {
69 if (type
== DolphinStatusBar::Error
) {
70 m_pendingMessages
.insert(0, m_text
);
72 else if ((m_state
!= Default
) || !m_pendingMessages
.isEmpty()) {
73 // a non-error message should not be shown, as there
74 // are other pending error messages in the queue
86 const char* iconName
= 0;
89 case DolphinStatusBar::OperationCompleted
:
91 m_closeButton
->hide();
94 case DolphinStatusBar::Information
:
95 iconName
= "document-properties";
96 m_closeButton
->hide();
99 case DolphinStatusBar::Error
:
102 m_state
= Illuminate
;
104 updateCloseButtonPosition();
105 m_closeButton
->show();
108 case DolphinStatusBar::Default
:
110 m_closeButton
->hide();
114 m_pixmap
= (iconName
== 0) ? QPixmap() : SmallIcon(iconName
);
115 QTimer::singleShot(GeometryTimeout
, this, SLOT(assureVisibleText()));
119 void StatusBarMessageLabel::setMinimumTextHeight(int min
)
121 if (min
!= m_minTextHeight
) {
122 m_minTextHeight
= min
;
123 setMinimumHeight(min
);
124 m_closeButton
->setFixedHeight(min
- borderGap() * 2);
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);
140 QColor
backgroundColor(palette().brush(QPalette::Background
).color());
141 QColor
foregroundColor(KGlobalSettings::textColor());
142 if (m_illumination
> 0) {
143 backgroundColor
= mixColors(backgroundColor
, QColor(255, 255, 128), m_illumination
);
144 foregroundColor
= mixColors(foregroundColor
, QColor(0, 0, 0), m_illumination
);
146 painter
.setBrush(backgroundColor
);
147 painter
.setPen(backgroundColor
);
148 painter
.drawRect(QRect(0, 0, width(), height()));
152 int y
= (m_minTextHeight
- m_pixmap
.height()) / 2;
154 if (!m_pixmap
.isNull()) {
155 painter
.drawPixmap(x
, y
, m_pixmap
);
156 x
+= m_pixmap
.width() + borderGap();
160 painter
.setPen(foregroundColor
);
161 int flags
= Qt::AlignVCenter
;
162 if (height() > m_minTextHeight
) {
163 flags
= flags
| Qt::TextWordWrap
;
165 painter
.drawText(QRect(x
, 0, availableTextWidth(), height()), flags
, m_text
);
169 void StatusBarMessageLabel::resizeEvent(QResizeEvent
* event
)
171 QWidget::resizeEvent(event
);
172 updateCloseButtonPosition();
173 QTimer::singleShot(GeometryTimeout
, this, SLOT(assureVisibleText()));
176 void StatusBarMessageLabel::timerDone()
180 // increase the illumination
181 if (m_illumination
< 100) {
182 m_illumination
+= 20;
186 m_state
= Illuminated
;
187 m_timer
->start(5000);
193 // start desaturation
194 m_state
= Desaturate
;
201 if (m_illumination
> 0) {
217 void StatusBarMessageLabel::assureVisibleText()
219 if (m_text
.isEmpty()) {
223 // calculate the required height of the widget thats
224 // needed for having a fully visible text
225 QFontMetrics
fontMetrics(font());
226 const QRect
bounds(fontMetrics
.boundingRect(0, 0, availableTextWidth(), height(),
227 Qt::AlignVCenter
| Qt::TextWordWrap
,
229 int requiredHeight
= bounds
.height();
230 if (requiredHeight
< m_minTextHeight
) {
231 requiredHeight
= m_minTextHeight
;
234 // Increase/decrease the current height of the widget to the
235 // required height. The increasing/decreasing is done in several
236 // steps to have an animation if the height is modified
237 // (see StatusBarMessageLabel::resizeEvent())
238 const int gap
= m_minTextHeight
/ 2;
239 int minHeight
= minimumHeight();
240 if (minHeight
< requiredHeight
) {
242 if (minHeight
> requiredHeight
) {
243 minHeight
= requiredHeight
;
245 setMinimumHeight(minHeight
);
248 else if (minHeight
> requiredHeight
) {
250 if (minHeight
< requiredHeight
) {
251 minHeight
= requiredHeight
;
253 setMinimumHeight(minHeight
);
257 updateCloseButtonPosition();
260 int StatusBarMessageLabel::availableTextWidth() const
262 const int buttonWidth
= (m_type
== DolphinStatusBar::Error
) ?
263 m_closeButton
->width() + borderGap() : 0;
264 return width() - m_pixmap
.width() - (borderGap() * 4) - buttonWidth
;
267 QColor
StatusBarMessageLabel::mixColors(const QColor
& c1
,
271 const int recip
= 100 - percent
;
272 const int red
= (c1
.red() * recip
+ c2
.red() * percent
) / 100;
273 const int green
= (c1
.green() * recip
+ c2
.green() * percent
) / 100;
274 const int blue
= (c1
.blue() * recip
+ c2
.blue() * percent
) / 100;
275 return QColor(red
, green
, blue
);
278 void StatusBarMessageLabel::updateCloseButtonPosition()
280 const int x
= width() - m_closeButton
->width() - borderGap();
281 const int y
= height() - m_closeButton
->height() - borderGap();
282 m_closeButton
->move(x
, y
);
285 void StatusBarMessageLabel::closeErrorMessage()
287 if (!showPendingMessage()) {
289 setMessage(m_defaultText
, DolphinStatusBar::Default
);
293 bool StatusBarMessageLabel::showPendingMessage()
295 if (!m_pendingMessages
.isEmpty()) {
297 setMessage(m_pendingMessages
.takeFirst(), DolphinStatusBar::Error
);
303 void StatusBarMessageLabel::reset()
306 m_type
= DolphinStatusBar::Default
;
309 #include "statusbarmessagelabel.moc"