]>
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 int requiredHeight
= m_minTextHeight
;
233 if (m_type
!= DolphinStatusBar::Default
) {
234 // Calculate the required height of the widget thats
235 // needed for having a fully visible text. Note that for the default
236 // statusbar type (e. g. hover information) increasing the text height
237 // is not wanted, as this might rearrange the layout of items.
239 QFontMetrics
fontMetrics(font());
240 const QRect
bounds(fontMetrics
.boundingRect(0, 0, availableTextWidth(), height(),
241 Qt::AlignVCenter
| Qt::TextWordWrap
, m_text
));
242 requiredHeight
= bounds
.height();
243 if (requiredHeight
< m_minTextHeight
) {
244 requiredHeight
= m_minTextHeight
;
248 // Increase/decrease the current height of the widget to the
249 // required height. The increasing/decreasing is done in several
250 // steps to have an animation if the height is modified
251 // (see StatusBarMessageLabel::resizeEvent())
252 const int gap
= m_minTextHeight
/ 2;
253 int minHeight
= minimumHeight();
254 if (minHeight
< requiredHeight
) {
256 if (minHeight
> requiredHeight
) {
257 minHeight
= requiredHeight
;
259 setMinimumHeight(minHeight
);
261 } else if (minHeight
> requiredHeight
) {
263 if (minHeight
< requiredHeight
) {
264 minHeight
= requiredHeight
;
266 setMinimumHeight(minHeight
);
270 updateCloseButtonPosition();
273 int StatusBarMessageLabel::availableTextWidth() const
275 const int buttonWidth
= (m_type
== DolphinStatusBar::Error
) ?
276 m_closeButton
->width() + BorderGap
: 0;
277 return width() - m_pixmap
.width() - (BorderGap
* 4) - buttonWidth
;
280 void StatusBarMessageLabel::updateCloseButtonPosition()
282 const int x
= width() - m_closeButton
->width() - BorderGap
;
283 const int y
= (height() - m_closeButton
->height()) / 2;
284 m_closeButton
->move(x
, y
);
287 void StatusBarMessageLabel::closeErrorMessage()
289 if (!showPendingMessage()) {
291 setMessage(m_defaultText
, DolphinStatusBar::Default
);
295 bool StatusBarMessageLabel::showPendingMessage()
297 if (!m_pendingMessages
.isEmpty()) {
299 setMessage(m_pendingMessages
.takeFirst(), DolphinStatusBar::Error
);
305 void StatusBarMessageLabel::reset()
308 m_type
= DolphinStatusBar::Default
;
311 #include "statusbarmessagelabel.moc"