]>
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 <kcolorutils.h>
25 #include <kiconloader.h>
29 #include <QFontMetrics>
32 #include <QPushButton>
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(i18nc("@action:button", "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
:
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 void StatusBarMessageLabel::setMinimumTextHeight(int min
)
122 if (min
!= m_minTextHeight
) {
123 m_minTextHeight
= min
;
124 setMinimumHeight(min
);
125 m_closeButton
->setFixedHeight(min
- borderGap() * 2);
129 int StatusBarMessageLabel::widthGap() const
131 QFontMetrics
fontMetrics(font());
132 const int defaultGap
= 10;
133 return fontMetrics
.width(m_text
) - availableTextWidth() + defaultGap
;
136 void StatusBarMessageLabel::paintEvent(QPaintEvent
* /* event */)
138 QPainter
painter(this);
141 QColor backgroundColor
= palette().brush(QPalette::Background
).color();
142 QColor foregroundColor
= KColorScheme(KColorScheme::View
).foreground();
143 if (m_illumination
> 0) {
144 // TODO: are there foreground and background colors available for
146 backgroundColor
.setRgb(255, 255, 0, m_illumination
);
147 QColor
mixColor(0, 0, 0, m_illumination
);
148 foregroundColor
= KColorUtils::overlayColors(foregroundColor
, mixColor
);
150 painter
.setBrush(backgroundColor
);
151 painter
.setPen(backgroundColor
);
152 painter
.drawRect(QRect(0, 0, width(), height()));
156 int y
= (m_minTextHeight
- m_pixmap
.height()) / 2;
158 if (!m_pixmap
.isNull()) {
159 painter
.drawPixmap(x
, y
, m_pixmap
);
160 x
+= m_pixmap
.width() + borderGap();
164 painter
.setPen(foregroundColor
);
165 int flags
= Qt::AlignVCenter
;
166 if (height() > m_minTextHeight
) {
167 flags
= flags
| Qt::TextWordWrap
;
169 painter
.drawText(QRect(x
, 0, availableTextWidth(), height()), flags
, m_text
);
173 void StatusBarMessageLabel::resizeEvent(QResizeEvent
* event
)
175 QWidget::resizeEvent(event
);
176 updateCloseButtonPosition();
177 QTimer::singleShot(GeometryTimeout
, this, SLOT(assureVisibleText()));
180 void StatusBarMessageLabel::timerDone()
184 // increase the illumination
185 const int illumination_max
= 128;
186 if (m_illumination
< illumination_max
) {
187 m_illumination
+= 32;
188 if (m_illumination
> illumination_max
) {
189 m_illumination
= illumination_max
;
193 m_state
= Illuminated
;
194 m_timer
->start(5000);
200 // start desaturation
201 m_state
= Desaturate
;
208 if (m_illumination
> 0) {
223 void StatusBarMessageLabel::assureVisibleText()
225 if (m_text
.isEmpty()) {
229 // calculate the required height of the widget thats
230 // needed for having a fully visible text
231 QFontMetrics
fontMetrics(font());
232 const QRect
bounds(fontMetrics
.boundingRect(0, 0, availableTextWidth(), height(),
233 Qt::AlignVCenter
| Qt::TextWordWrap
,
235 int requiredHeight
= bounds
.height();
236 if (requiredHeight
< m_minTextHeight
) {
237 requiredHeight
= m_minTextHeight
;
240 // Increase/decrease the current height of the widget to the
241 // required height. The increasing/decreasing is done in several
242 // steps to have an animation if the height is modified
243 // (see StatusBarMessageLabel::resizeEvent())
244 const int gap
= m_minTextHeight
/ 2;
245 int minHeight
= minimumHeight();
246 if (minHeight
< requiredHeight
) {
248 if (minHeight
> requiredHeight
) {
249 minHeight
= requiredHeight
;
251 setMinimumHeight(minHeight
);
253 } else if (minHeight
> requiredHeight
) {
255 if (minHeight
< requiredHeight
) {
256 minHeight
= requiredHeight
;
258 setMinimumHeight(minHeight
);
262 updateCloseButtonPosition();
265 int StatusBarMessageLabel::availableTextWidth() const
267 const int buttonWidth
= (m_type
== DolphinStatusBar::Error
) ?
268 m_closeButton
->width() + borderGap() : 0;
269 return width() - m_pixmap
.width() - (borderGap() * 4) - buttonWidth
;
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"