23 #include <QTextDocument>
25 #include <kcolorscheme.h>
26 #include <kiconloader.h>
31 #include <QFontMetrics>
35 #include <QToolButton>
41 class KonqStatusBarMessageLabel::Private
46 m_state(DefaultState),
53 bool isRichText()
const {
return m_text.startsWith(
"<html>") || m_text.startsWith(
"<qt>"); }
56 KonqStatusBarMessageLabel::State m_state;
61 QString m_defaultText;
62 QTextDocument m_textDocument;
65 QToolButton* m_closeButton;
71 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum );
73 d->m_timer =
new QTimer(
this);
74 connect(d->m_timer, SIGNAL(timeout()),
75 this, SLOT(timerDone()));
77 d->m_closeButton =
new QToolButton(
this);
78 d->m_closeButton->setAutoRaise(
true);
79 d->m_closeButton->setIcon(KIcon(
"dialog-close"));
80 d->m_closeButton->setToolTip(i18nc(
"@info",
"Close"));
81 d->m_closeButton->setAccessibleName(i18n(
"Close"));
82 d->m_closeButton->hide();
83 connect(d->m_closeButton, SIGNAL(clicked()),
84 this, SLOT(closeErrorMessage()));
95 if ((text == d->m_text) && (type == d->m_type)) {
99 if (d->m_type ==
Error) {
101 d->m_pendingMessages.insert(0, d->m_text);
102 }
else if ((d->m_state != DefaultState) || !d->m_pendingMessages.isEmpty()) {
112 if (d->isRichText()) {
113 d->m_textDocument.setTextWidth(-1);
114 d->m_textDocument.setDefaultFont(font());
115 QString html =
"<html><font color=\"";
116 html += palette().windowText().color().name();
119 d->m_textDocument.setHtml(html);
123 d->m_illumination = 0;
124 d->m_state = DefaultState;
126 const char* iconName = 0;
130 iconName =
"dialog-ok";
132 d->m_closeButton->hide();
136 iconName =
"dialog-information";
137 d->m_closeButton->hide();
141 d->m_timer->start(100);
142 d->m_state = Illuminate;
144 updateCloseButtonPosition();
145 d->m_closeButton->show();
151 d->m_closeButton->hide();
156 d->m_pixmap = (iconName == 0) ? QPixmap() : SmallIcon(iconName);
160 setAccessibleName(i18n(
"Error: %1", text));
162 setAccessibleName(text);
180 d->m_defaultText =
text;
185 return d->m_defaultText;
190 if (min != d->m_minTextHeight) {
191 d->m_minTextHeight = min;
192 setMinimumHeight(min);
193 if (d->m_closeButton->height() > min) {
194 d->m_closeButton->setFixedHeight(min);
201 return d->m_minTextHeight;
206 QPainter painter(
this);
208 if (d->m_illumination > 0) {
212 KColorScheme scheme(palette().currentColorGroup(), KColorScheme::Window);
213 QColor backgroundColor = scheme.background(KColorScheme::NegativeBackground).color();
214 backgroundColor.setAlpha(qMin(255, d->m_illumination * 2));
215 painter.setBrush(backgroundColor);
216 painter.setPen(Qt::NoPen);
217 painter.drawRect(QRect(0, 0, width(), height()));
222 const int y = (d->m_minTextHeight - d->m_pixmap.height()) / 2;
224 if (!d->m_pixmap.isNull()) {
225 painter.drawPixmap(x, y, d->m_pixmap);
231 const QRect availTextRect(x, 0, availableTextWidth(), height());
233 if (d->isRichText()) {
234 const QSize sz = d->m_textDocument.size().toSize();
237 const QRect textRect = QStyle::alignedRect(Qt::LeftToRight, Qt::AlignLeft | Qt::AlignVCenter, sz, availTextRect);
242 painter.translate(textRect.left(), textRect.top());
243 d->m_textDocument.drawContents(&painter);
246 painter.setPen(palette().windowText().color());
247 int flags = Qt::AlignVCenter;
248 if (height() > d->m_minTextHeight) {
249 flags = flags | Qt::TextWordWrap;
251 painter.drawText(availTextRect, flags, d->m_text);
258 QWidget::resizeEvent(event);
259 updateCloseButtonPosition();
263 void KonqStatusBarMessageLabel::timerDone()
265 switch (d->m_state) {
268 const int illumination_max = 128;
269 if (d->m_illumination < illumination_max) {
270 d->m_illumination += 32;
271 if (d->m_illumination > illumination_max) {
272 d->m_illumination = illumination_max;
276 d->m_state = Illuminated;
277 d->m_timer->start(5000);
284 d->m_state = Desaturate;
285 d->m_timer->start(100);
291 if (d->m_illumination > 0) {
292 d->m_illumination -= 5;
295 d->m_state = DefaultState;
306 void KonqStatusBarMessageLabel::assureVisibleText()
308 if (d->m_text.isEmpty()) {
312 int requiredHeight = d->m_minTextHeight;
319 QFontMetrics fontMetrics(font());
320 const QRect bounds(fontMetrics.boundingRect(0, 0, availableTextWidth(), height(),
321 Qt::AlignVCenter | Qt::TextWordWrap, d->m_text));
322 requiredHeight = bounds.height();
323 if (requiredHeight < d->m_minTextHeight) {
324 requiredHeight = d->m_minTextHeight;
332 const int gap = d->m_minTextHeight / 2;
333 int minHeight = minimumHeight();
334 if (minHeight < requiredHeight) {
336 if (minHeight > requiredHeight) {
337 minHeight = requiredHeight;
339 setMinimumHeight(minHeight);
341 }
else if (minHeight > requiredHeight) {
343 if (minHeight < requiredHeight) {
344 minHeight = requiredHeight;
346 setMinimumHeight(minHeight);
350 updateCloseButtonPosition();
353 int KonqStatusBarMessageLabel::availableTextWidth()
const
355 const int buttonWidth = (d->m_type ==
Error) ?
356 d->m_closeButton->width() +
BorderGap : 0;
357 return width() - d->m_pixmap.width() - (
BorderGap * 4) - buttonWidth;
360 void KonqStatusBarMessageLabel::updateCloseButtonPosition()
362 const int x = width() - d->m_closeButton->width() -
BorderGap;
363 d->m_closeButton->move(x, 0);
366 void KonqStatusBarMessageLabel::closeErrorMessage()
368 if (!showPendingMessage()) {
369 d->m_state = DefaultState;
374 bool KonqStatusBarMessageLabel::showPendingMessage()
376 if (!d->m_pendingMessages.isEmpty()) {
384 void KonqStatusBarMessageLabel::reset()
397 const int fontHeight = fontMetrics().height();
398 QSize sz(100, fontHeight);
399 if (d->m_closeButton->isVisible()) {
400 const QSize toolButtonSize = d->m_closeButton->sizeHint();
401 sz = toolButtonSize.expandedTo(sz);
406 #include "konq_statusbarmessagelabel.moc"
KonqStatusBarMessageLabel(QWidget *parent)
void setMinimumTextHeight(int min)
virtual QSize sizeHint() const
virtual void paintEvent(QPaintEvent *event)
virtual ~KonqStatusBarMessageLabel()
int minimumTextHeight() const
void setDefaultText(const QString &text)
QString defaultText() const
void setMessage(const QString &text, Type type)
Type
Describes the type of the message text.
virtual QSize minimumSizeHint() const
virtual void resizeEvent(QResizeEvent *event)