]>
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 <kcolorutils.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
);
48 palette
.setColor(QPalette::Background
, Qt::transparent
);
51 m_timer
= new QTimer(this);
52 connect(m_timer
, SIGNAL(timeout()),
53 this, SLOT(timerDone()));
55 m_closeButton
= new QPushButton(i18n("Close"), this);
56 m_closeButton
->hide();
57 connect(m_closeButton
, SIGNAL(clicked()),
58 this, SLOT(closeErrorMessage()));
61 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
:
93 m_closeButton
->hide();
96 case DolphinStatusBar::Information
:
97 iconName
= "dialog-information";
98 m_closeButton
->hide();
101 case DolphinStatusBar::Error
:
102 iconName
= "dialog-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 m_closeButton
->setFixedHeight(min
- borderGap() * 2);
130 int StatusBarMessageLabel::widthGap() const
132 QFontMetrics
fontMetrics(font());
133 const int defaultGap
= 10;
134 return fontMetrics
.width(m_text
) - availableTextWidth() + defaultGap
;
137 void StatusBarMessageLabel::paintEvent(QPaintEvent
* /* event */)
139 QPainter
painter(this);
142 QColor
backgroundColor(palette().brush(QPalette::Background
).color());
143 QColor
foregroundColor(KGlobalSettings::textColor());
144 if (m_illumination
> 0) {
145 // TODO: are there foreground and background colors available for
147 backgroundColor
.setRgb(255, 255, 0, m_illumination
);
148 QColor
mixColor(0, 0, 0, m_illumination
);
149 foregroundColor
= KColorUtils::overlayColors(foregroundColor
, mixColor
);
151 painter
.setBrush(backgroundColor
);
152 painter
.setPen(backgroundColor
);
153 painter
.drawRect(QRect(0, 0, width(), height()));
157 int y
= (m_minTextHeight
- m_pixmap
.height()) / 2;
159 if (!m_pixmap
.isNull()) {
160 painter
.drawPixmap(x
, y
, m_pixmap
);
161 x
+= m_pixmap
.width() + borderGap();
165 painter
.setPen(foregroundColor
);
166 int flags
= Qt::AlignVCenter
;
167 if (height() > m_minTextHeight
) {
168 flags
= flags
| Qt::TextWordWrap
;
170 painter
.drawText(QRect(x
, 0, availableTextWidth(), height()), flags
, m_text
);
174 void StatusBarMessageLabel::resizeEvent(QResizeEvent
* event
)
176 QWidget::resizeEvent(event
);
177 updateCloseButtonPosition();
178 QTimer::singleShot(GeometryTimeout
, this, SLOT(assureVisibleText()));
181 void StatusBarMessageLabel::timerDone()
185 // increase the illumination
186 const int illumination_max
= 128;
187 if (m_illumination
< illumination_max
) {
188 m_illumination
+= 32;
189 if (m_illumination
> illumination_max
) {
190 m_illumination
= illumination_max
;
194 m_state
= Illuminated
;
195 m_timer
->start(5000);
201 // start desaturation
202 m_state
= Desaturate
;
209 if (m_illumination
> 0) {
224 void StatusBarMessageLabel::assureVisibleText()
226 if (m_text
.isEmpty()) {
230 // calculate the required height of the widget thats
231 // needed for having a fully visible text
232 QFontMetrics
fontMetrics(font());
233 const QRect
bounds(fontMetrics
.boundingRect(0, 0, availableTextWidth(), height(),
234 Qt::AlignVCenter
| Qt::TextWordWrap
,
236 int requiredHeight
= bounds
.height();
237 if (requiredHeight
< m_minTextHeight
) {
238 requiredHeight
= m_minTextHeight
;
241 // Increase/decrease the current height of the widget to the
242 // required height. The increasing/decreasing is done in several
243 // steps to have an animation if the height is modified
244 // (see StatusBarMessageLabel::resizeEvent())
245 const int gap
= m_minTextHeight
/ 2;
246 int minHeight
= minimumHeight();
247 if (minHeight
< requiredHeight
) {
249 if (minHeight
> requiredHeight
) {
250 minHeight
= requiredHeight
;
252 setMinimumHeight(minHeight
);
254 } else if (minHeight
> requiredHeight
) {
256 if (minHeight
< requiredHeight
) {
257 minHeight
= requiredHeight
;
259 setMinimumHeight(minHeight
);
263 updateCloseButtonPosition();
266 int StatusBarMessageLabel::availableTextWidth() const
268 const int buttonWidth
= (m_type
== DolphinStatusBar::Error
) ?
269 m_closeButton
->width() + borderGap() : 0;
270 return width() - m_pixmap
.width() - (borderGap() * 4) - buttonWidth
;
273 void StatusBarMessageLabel::updateCloseButtonPosition()
275 const int x
= width() - m_closeButton
->width() - borderGap();
276 const int y
= height() - m_closeButton
->height() - borderGap();
277 m_closeButton
->move(x
, y
);
280 void StatusBarMessageLabel::closeErrorMessage()
282 if (!showPendingMessage()) {
284 setMessage(m_defaultText
, DolphinStatusBar::Default
);
288 bool StatusBarMessageLabel::showPendingMessage()
290 if (!m_pendingMessages
.isEmpty()) {
292 setMessage(m_pendingMessages
.takeFirst(), DolphinStatusBar::Error
);
298 void StatusBarMessageLabel::reset()
301 m_type
= DolphinStatusBar::Default
;
304 #include "statusbarmessagelabel.moc"