]>
cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/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>
32 #include <QToolButton>
35 StatusBarMessageLabel::StatusBarMessageLabel(QWidget
* parent
) :
37 m_type(DolphinStatusBar::Default
),
44 setMinimumHeight(KIconLoader::SizeSmall
);
46 m_timer
= new QTimer(this);
47 connect(m_timer
, SIGNAL(timeout()),
48 this, SLOT(timerDone()));
50 m_closeButton
= new QToolButton(this);
51 m_closeButton
->setAutoRaise(true);
52 m_closeButton
->setIcon(KIcon("dialog-close"));
53 m_closeButton
->setToolTip(i18nc("@info", "Close"));
54 m_closeButton
->hide();
55 connect(m_closeButton
, SIGNAL(clicked()),
56 this, SLOT(closeErrorMessage()));
59 StatusBarMessageLabel::~StatusBarMessageLabel()
63 void StatusBarMessageLabel::setMessage(const QString
& text
,
64 DolphinStatusBar::Type type
)
66 if ((text
== m_text
) && (type
== m_type
)) {
70 if (m_type
== DolphinStatusBar::Error
) {
71 if (type
== DolphinStatusBar::Error
) {
72 m_pendingMessages
.insert(0, m_text
);
73 } else if ((m_state
!= Default
) || !m_pendingMessages
.isEmpty()) {
74 // a non-error message should not be shown, as there
75 // are other pending error messages in the queue
87 const char* iconName
= 0;
90 case DolphinStatusBar::OperationCompleted
:
91 iconName
= "dialog-ok";
92 // "ok" icon should probably be "dialog-success", but we don't have that icon in KDE 4.0
93 m_closeButton
->hide();
96 case DolphinStatusBar::Information
:
97 iconName
= "dialog-information";
98 m_closeButton
->hide();
101 case DolphinStatusBar::Error
:
103 m_state
= Illuminate
;
105 updateCloseButtonPosition();
106 m_closeButton
->show();
109 case DolphinStatusBar::Default
:
111 m_closeButton
->hide();
115 m_pixmap
= (iconName
== 0) ? QPixmap() : SmallIcon(iconName
);
116 QTimer::singleShot(GeometryTimeout
, this, SLOT(assureVisibleText()));
120 DolphinStatusBar::Type
StatusBarMessageLabel::type() const
125 QString
StatusBarMessageLabel::text() const
130 void StatusBarMessageLabel::setDefaultText(const QString
& text
)
132 m_defaultText
= text
;
135 QString
StatusBarMessageLabel::defaultText() const
137 return m_defaultText
;
140 void StatusBarMessageLabel::setMinimumTextHeight(int min
)
142 if (min
!= m_minTextHeight
) {
143 m_minTextHeight
= min
;
144 setMinimumHeight(min
);
145 if (m_closeButton
->height() > min
) {
146 m_closeButton
->setFixedHeight(min
);
151 int StatusBarMessageLabel::minimumTextHeight() const
153 return m_minTextHeight
;
156 int StatusBarMessageLabel::widthGap() const
158 QFontMetrics
fontMetrics(font());
159 const int defaultGap
= 10;
160 return fontMetrics
.width(m_text
) - availableTextWidth() + defaultGap
;
163 void StatusBarMessageLabel::paintEvent(QPaintEvent
* /* event */)
165 QPainter
painter(this);
167 if (m_illumination
> 0) {
168 // at this point, a: we are a second label being drawn over the already
169 // painted status area, so we can be translucent, and b: our palette's
170 // window color (bg only) seems to be wrong (always black)
171 KColorScheme
scheme(palette().currentColorGroup(), KColorScheme::Window
);
172 QColor backgroundColor
= scheme
.background(KColorScheme::NegativeBackground
).color();
173 backgroundColor
.setAlpha(qMin(255, m_illumination
* 2));
174 painter
.setBrush(backgroundColor
);
175 painter
.setPen(Qt::NoPen
);
176 painter
.drawRect(QRect(0, 0, width(), height()));
181 const int y
= (m_minTextHeight
- m_pixmap
.height()) / 2;
183 if (!m_pixmap
.isNull()) {
184 painter
.drawPixmap(x
, y
, m_pixmap
);
185 x
+= m_pixmap
.width() + BorderGap
;
189 painter
.setPen(palette().windowText().color());
190 int flags
= Qt::AlignVCenter
;
191 if (height() > m_minTextHeight
) {
192 flags
= flags
| Qt::TextWordWrap
;
194 painter
.drawText(QRect(x
, 0, availableTextWidth(), height()), flags
, m_text
);
198 void StatusBarMessageLabel::resizeEvent(QResizeEvent
* event
)
200 QWidget::resizeEvent(event
);
201 updateCloseButtonPosition();
202 QTimer::singleShot(GeometryTimeout
, this, SLOT(assureVisibleText()));
205 void StatusBarMessageLabel::timerDone()
209 // increase the illumination
210 const int illumination_max
= 128;
211 if (m_illumination
< illumination_max
) {
212 m_illumination
+= 32;
213 if (m_illumination
> illumination_max
) {
214 m_illumination
= illumination_max
;
218 m_state
= Illuminated
;
219 m_timer
->start(5000);
225 // start desaturation
226 m_state
= Desaturate
;
233 if (m_illumination
> 0) {
248 void StatusBarMessageLabel::assureVisibleText()
250 if (m_text
.isEmpty()) {
254 int requiredHeight
= m_minTextHeight
;
255 if (m_type
!= DolphinStatusBar::Default
) {
256 // Calculate the required height of the widget thats
257 // needed for having a fully visible text. Note that for the default
258 // statusbar type (e. g. hover information) increasing the text height
259 // is not wanted, as this might rearrange the layout of items.
261 QFontMetrics
fontMetrics(font());
262 const QRect
bounds(fontMetrics
.boundingRect(0, 0, availableTextWidth(), height(),
263 Qt::AlignVCenter
| Qt::TextWordWrap
, m_text
));
264 requiredHeight
= bounds
.height();
265 if (requiredHeight
< m_minTextHeight
) {
266 requiredHeight
= m_minTextHeight
;
270 // Increase/decrease the current height of the widget to the
271 // required height. The increasing/decreasing is done in several
272 // steps to have an animation if the height is modified
273 // (see StatusBarMessageLabel::resizeEvent())
274 const int gap
= m_minTextHeight
/ 2;
275 int minHeight
= minimumHeight();
276 if (minHeight
< requiredHeight
) {
278 if (minHeight
> requiredHeight
) {
279 minHeight
= requiredHeight
;
281 setMinimumHeight(minHeight
);
283 } else if (minHeight
> requiredHeight
) {
285 if (minHeight
< requiredHeight
) {
286 minHeight
= requiredHeight
;
288 setMinimumHeight(minHeight
);
292 updateCloseButtonPosition();
295 int StatusBarMessageLabel::availableTextWidth() const
297 const int buttonWidth
= (m_type
== DolphinStatusBar::Error
) ?
298 m_closeButton
->width() + BorderGap
: 0;
299 return width() - m_pixmap
.width() - (BorderGap
* 4) - buttonWidth
;
302 void StatusBarMessageLabel::updateCloseButtonPosition()
304 const int x
= width() - m_closeButton
->width() - BorderGap
;
305 m_closeButton
->move(x
, 0);
308 void StatusBarMessageLabel::closeErrorMessage()
310 if (!showPendingMessage()) {
312 setMessage(m_defaultText
, DolphinStatusBar::Default
);
316 bool StatusBarMessageLabel::showPendingMessage()
318 if (!m_pendingMessages
.isEmpty()) {
320 setMessage(m_pendingMessages
.takeFirst(), DolphinStatusBar::Error
);
326 void StatusBarMessageLabel::reset()
329 m_type
= DolphinStatusBar::Default
;
332 #include "statusbarmessagelabel.moc"