Update KMessageWidget

This commit is contained in:
Janek Bevendorff 2017-01-15 01:10:46 +01:00
parent 84222e8078
commit b6ea06ba24
No known key found for this signature in database
GPG key ID: CFEC2F6850BFFA53
2 changed files with 579 additions and 421 deletions

View file

@ -1,6 +1,7 @@
/* This file is part of the KDE libraries /* This file is part of the KDE libraries
* *
* Copyright (c) 2011 Aurélien Gâteau <agateau@kde.org> * Copyright (c) 2011 Aurélien Gâteau <agateau@kde.org>
* Copyright (c) 2014 Dominik Haumann <dhaumann@kde.org>
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -19,407 +20,461 @@
*/ */
#include "kmessagewidget.h" #include "kmessagewidget.h"
#include <QAction>
#include <QEvent> #include <QEvent>
#include <QGridLayout> #include <QGridLayout>
#include <QDialogButtonBox> #include <QHBoxLayout>
#include <QLabel> #include <QLabel>
#include <QPainter> #include <QPainter>
#include <QShowEvent> #include <QShowEvent>
#include <QTimeLine> #include <QTimeLine>
#include <QToolButton> #include <QToolButton>
#include <QStyle> #include <QStyle>
#include <QAction>
//---------------------------------------------------------------------
// KMessageWidgetPrivate
//---------------------------------------------------------------------
class KMessageWidgetPrivate
{
public:
void init(KMessageWidget *);
KMessageWidget *q;
QFrame *content;
QLabel *iconLabel;
QLabel *textLabel;
QToolButton *closeButton;
QTimeLine *timeLine;
QIcon icon;
KMessageWidget::MessageType messageType;
bool wordWrap;
QList<QToolButton *> buttons;
QPixmap contentSnapShot;
void createLayout();
void updateSnapShot();
void updateLayout();
void slotTimeLineChanged(qreal);
void slotTimeLineFinished();
int bestContentHeight() const;
};
void KMessageWidgetPrivate::init(KMessageWidget *q_ptr) void KMessageWidgetPrivate::init(KMessageWidget *q_ptr)
{ {
q = q_ptr; q = q_ptr;
q->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed); q->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
timeLine = new QTimeLine(500, q); timeLine = new QTimeLine(500, q);
QObject::connect(timeLine, SIGNAL(valueChanged(qreal)), q, SLOT(slotTimeLineChanged(qreal))); QObject::connect(timeLine, SIGNAL(valueChanged(qreal)), q, SLOT(slotTimeLineChanged(qreal)));
QObject::connect(timeLine, SIGNAL(finished()), q, SLOT(slotTimeLineFinished())); QObject::connect(timeLine, SIGNAL(finished()), q, SLOT(slotTimeLineFinished()));
content = new QFrame(q); content = new QFrame(q);
content->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); content->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
wordWrap = false; wordWrap = false;
iconLabel = new QLabel(content); iconLabel = new QLabel(content);
iconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); iconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
iconLabel->hide(); iconLabel->hide();
textLabel = new QLabel(content); textLabel = new QLabel(content);
textLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); textLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
textLabel->setTextInteractionFlags(Qt::TextBrowserInteraction); textLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
QObject::connect(textLabel, SIGNAL(linkActivated(const QString &)), q, SIGNAL(linkActivated(const QString &))); QObject::connect(textLabel, SIGNAL(linkActivated(QString)), q, SIGNAL(linkActivated(QString)));
QObject::connect(textLabel, SIGNAL(linkHovered(const QString &)), q, SIGNAL(linkHovered(const QString &))); QObject::connect(textLabel, SIGNAL(linkHovered(QString)), q, SIGNAL(linkHovered(QString)));
QAction *closeAction = new QAction(QObject::tr("Close"), q); QAction *closeAction = new QAction(q);
q->connect(closeAction, SIGNAL(triggered(bool)), q, SLOT(animatedHide())); closeAction->setText(KMessageWidget::tr("&Close"));
closeAction->setToolTip(KMessageWidget::tr("Close message"));
closeButton = new QToolButton(content); closeAction->setIcon(q->style()->standardIcon(QStyle::SP_DialogCloseButton));
closeButton->setAutoRaise(true);
closeButton->setDefaultAction(closeAction); QObject::connect(closeAction, SIGNAL(triggered(bool)), q, SLOT(animatedHide()));
closeButton->setVisible(true);
q->setMessageType(KMessageWidget::Information); closeButton = new QToolButton(content);
closeButton->setAutoRaise(true);
closeButton->setDefaultAction(closeAction);
q->setMessageType(KMessageWidget::Information);
} }
void KMessageWidgetPrivate::createLayout() void KMessageWidgetPrivate::createLayout()
{ {
delete content->layout(); delete content->layout();
content->resize(q->size()); content->resize(q->size());
qDeleteAll(buttons); qDeleteAll(buttons);
buttons.clear(); buttons.clear();
Q_FOREACH (QAction *action, q->actions()) { Q_FOREACH (QAction *action, q->actions()) {
QToolButton *button = new QToolButton(content); QToolButton *button = new QToolButton(content);
button->setDefaultAction(action); button->setDefaultAction(action);
button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
buttons.append(button); buttons.append(button);
} }
// AutoRaise reduces visual clutter, but we don't want to turn it on if // AutoRaise reduces visual clutter, but we don't want to turn it on if
// there are other buttons, otherwise the close button will look different // there are other buttons, otherwise the close button will look different
// from the others. // from the others.
closeButton->setAutoRaise(buttons.isEmpty()); closeButton->setAutoRaise(buttons.isEmpty());
if (wordWrap) { if (wordWrap) {
QGridLayout *layout = new QGridLayout(content); QGridLayout *layout = new QGridLayout(content);
// Set alignment to make sure icon does not move down if text wraps // Set alignment to make sure icon does not move down if text wraps
layout->addWidget(iconLabel, 0, 0, 1, 1, Qt::AlignHCenter | Qt::AlignTop); layout->addWidget(iconLabel, 0, 0, 1, 1, Qt::AlignHCenter | Qt::AlignTop);
layout->addWidget(textLabel, 0, 1); layout->addWidget(textLabel, 0, 1);
QDialogButtonBox *buttonLayout = new QDialogButtonBox(); QHBoxLayout *buttonLayout = new QHBoxLayout;
//buttonLayout->addStretch(); buttonLayout->addStretch();
Q_FOREACH (QToolButton *button, buttons) { Q_FOREACH (QToolButton *button, buttons) {
// For some reason, calling show() is necessary if wordwrap is true, // For some reason, calling show() is necessary if wordwrap is true,
// otherwise the buttons do not show up. It is not needed if // otherwise the buttons do not show up. It is not needed if
// wordwrap is false. // wordwrap is false.
button->show(); button->show();
buttonLayout->addButton(button, QDialogButtonBox::QDialogButtonBox::AcceptRole); buttonLayout->addWidget(button);
} }
buttonLayout->addButton(closeButton, QDialogButtonBox::RejectRole); buttonLayout->addWidget(closeButton);
layout->addWidget(buttonLayout, 1, 0, 1, 2, Qt::AlignHCenter | Qt::AlignTop); layout->addItem(buttonLayout, 1, 0, 1, 2);
} else { } else {
bool closeButtonVisible = closeButton->isVisible(); QHBoxLayout *layout = new QHBoxLayout(content);
QHBoxLayout *layout = new QHBoxLayout(content); layout->addWidget(iconLabel);
layout->addWidget(iconLabel); layout->addWidget(textLabel);
layout->addWidget(textLabel);
Q_FOREACH (QToolButton *button, buttons) {
QDialogButtonBox *buttonLayout = new QDialogButtonBox(); layout->addWidget(button);
Q_FOREACH (QToolButton *button, buttons) { }
buttonLayout->addButton(button, QDialogButtonBox::QDialogButtonBox::AcceptRole);
} layout->addWidget(closeButton);
};
buttonLayout->addButton(closeButton, QDialogButtonBox::RejectRole);
// Something gets changed when added to the buttonLayout if (q->isVisible()) {
closeButton->setVisible(closeButtonVisible); q->setFixedHeight(content->sizeHint().height());
layout->addWidget(buttonLayout); }
}; q->updateGeometry();
if (q->isVisible()) {
q->setFixedHeight(content->sizeHint().height());
}
q->updateGeometry();
} }
void KMessageWidgetPrivate::updateLayout() void KMessageWidgetPrivate::updateLayout()
{ {
if (content->layout()) { if (content->layout()) {
createLayout(); createLayout();
} }
} }
void KMessageWidgetPrivate::updateSnapShot() void KMessageWidgetPrivate::updateSnapShot()
{ {
// Attention: updateSnapShot calls QWidget::render(), which causes the whole // Attention: updateSnapShot calls QWidget::render(), which causes the whole
// window layouts to be activated. Calling this method from resizeEvent() // window layouts to be activated. Calling this method from resizeEvent()
// can lead to infinite recursion, see: // can lead to infinite recursion, see:
// https://bugs.kde.org/show_bug.cgi?id=311336 // https://bugs.kde.org/show_bug.cgi?id=311336
contentSnapShot = QPixmap(content->size()); contentSnapShot = QPixmap(content->size() * q->devicePixelRatio());
contentSnapShot.fill(Qt::transparent); contentSnapShot.setDevicePixelRatio(q->devicePixelRatio());
content->render(&contentSnapShot, QPoint(), QRegion(), QWidget::DrawChildren); contentSnapShot.fill(Qt::transparent);
content->render(&contentSnapShot, QPoint(), QRegion(), QWidget::DrawChildren);
} }
void KMessageWidgetPrivate::slotTimeLineChanged(qreal value) void KMessageWidgetPrivate::slotTimeLineChanged(qreal value)
{ {
q->setFixedHeight(qMin(value * 2, qreal(1.0)) * content->height()); q->setFixedHeight(qMin(value * 2, qreal(1.0)) * content->height());
q->update(); q->update();
} }
void KMessageWidgetPrivate::slotTimeLineFinished() void KMessageWidgetPrivate::slotTimeLineFinished()
{ {
if (timeLine->direction() == QTimeLine::Forward) { if (timeLine->direction() == QTimeLine::Forward) {
// Show // Show
// We set the whole geometry here, because it may be wrong if a // We set the whole geometry here, because it may be wrong if a
// KMessageWidget is shown right when the toplevel window is created. // KMessageWidget is shown right when the toplevel window is created.
content->setGeometry(0, 0, q->width(), bestContentHeight()); content->setGeometry(0, 0, q->width(), bestContentHeight());
} else {
// Hide // notify about finished animation
q->hide(); emit q->showAnimationFinished();
} } else {
// hide and notify about finished animation
q->hide();
emit q->hideAnimationFinished();
}
} }
int KMessageWidgetPrivate::bestContentHeight() const int KMessageWidgetPrivate::bestContentHeight() const
{ {
int height = content->heightForWidth(q->width()); int height = content->heightForWidth(q->width());
if (height == -1) {
if (height == -1) { height = content->sizeHint().height();
height = content->sizeHint().height(); }
} return height;
return height;
} }
//--------------------------------------------------------------------- //---------------------------------------------------------------------
// KMessageWidget // KMessageWidget
//--------------------------------------------------------------------- //---------------------------------------------------------------------
KMessageWidget::KMessageWidget(QWidget *parent) : QFrame(parent), d(new KMessageWidgetPrivate) KMessageWidget::KMessageWidget(QWidget *parent)
: QFrame(parent)
, d(new KMessageWidgetPrivate)
{ {
d->init(this); d->init(this);
} }
KMessageWidget::KMessageWidget(const QString &text, QWidget *parent) : QFrame(parent), d(new KMessageWidgetPrivate) KMessageWidget::KMessageWidget(const QString &text, QWidget *parent)
: QFrame(parent)
, d(new KMessageWidgetPrivate)
{ {
d->init(this); d->init(this);
setText(text); setText(text);
} }
KMessageWidget::~KMessageWidget() KMessageWidget::~KMessageWidget()
{ {
delete d; delete d;
} }
QString KMessageWidget::text() const QString KMessageWidget::text() const
{ {
return d->textLabel->text(); return d->textLabel->text();
} }
void KMessageWidget::setText(const QString &text) void KMessageWidget::setText(const QString &text)
{ {
d->textLabel->setText(text); d->textLabel->setText(text);
updateGeometry(); updateGeometry();
} }
int KMessageWidget::bestContentHeight() const
{
return d->bestContentHeight();
}
KMessageWidget::MessageType KMessageWidget::messageType() const KMessageWidget::MessageType KMessageWidget::messageType() const
{ {
return d->messageType; return d->messageType;
}
static QColor darkShade(QColor c)
{
qreal contrast = 0.7; // taken from kcolorscheme for the dark shade
qreal darkAmount;
if (c.lightnessF() < 0.006) { /* too dark */
darkAmount = 0.02 + 0.40 * contrast;
} else if (c.lightnessF() > 0.93) { /* too bright */
darkAmount = -0.06 - 0.60 * contrast;
} else {
darkAmount = (-c.lightnessF()) * (0.55 + contrast * 0.35);
}
qreal v = c.lightnessF() + darkAmount;
v = v > 0.0 ? (v < 1.0 ? v : 1.0) : 0.0;
c.setHsvF(c.hslHueF(), c.hslSaturationF(), v);
return c;
} }
void KMessageWidget::setMessageType(KMessageWidget::MessageType type) void KMessageWidget::setMessageType(KMessageWidget::MessageType type)
{ {
d->messageType = type; d->messageType = type;
QColor bg0, bg1, bg2, border, fg; QColor bg0, bg1, bg2, border, fg;
switch (type) {
switch (type) { case Positive:
case Positive: bg1.setRgb(0, 110, 40); // values taken from kcolorscheme.cpp (Positive)
bg1 = QColor("#72D594"); // nice green break;
fg = QColor(Qt::white); case Information:
break; bg1 = palette().highlight().color();
break;
case Information: case Warning:
bg1 = QColor("#41A8E3"); // nice blue bg1.setRgb(176, 128, 0); // values taken from kcolorscheme.cpp (Neutral)
fg = QColor(Qt::black); break;
break; case Error:
bg1.setRgb(191, 3, 3); // values taken from kcolorscheme.cpp (Negative)
case Warning: break;
bg1 = QColor("#FFCD0F"); // nice yellow }
fg = QColor(Qt::black);
break; // Colors
fg = palette().highlightedText().color();
case Error: bg0 = bg1.lighter(110);
bg1 = QColor("#FF7D7D"); // nice red. bg2 = bg1.darker(110);
fg = QColor(Qt::black); border = darkShade(bg1);
break;
} d->content->setStyleSheet(
QString(QLatin1String(".QFrame {"
// Colors "background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,"
bg0 = bg1.lighter(110); " stop: 0 %1,"
bg2 = bg1.darker(110); " stop: 0.1 %2,"
border = bg2.darker(110); " stop: 1.0 %3);"
d->content->setStyleSheet( "border-radius: 5px;"
QString(".QFrame {" "border: 1px solid %4;"
"background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1," "margin: %5px;"
" stop: 0 %1," "}"
" stop: 0.1 %2," ".QLabel { color: %6; }"
" stop: 1.0 %3);" ))
"border-radius: 5px;" .arg(bg0.name())
"border: 1px solid %4;" .arg(bg1.name())
"margin: %5px;" .arg(bg2.name())
"}" .arg(border.name())
".QLabel { color: %6; }").arg(bg0.name()) // DefaultFrameWidth returns the size of the external margin + border width. We know our border is 1px, so we subtract this from the frame normal QStyle FrameWidth to get our margin
.arg(bg1.name()) .arg(style()->pixelMetric(QStyle::PM_DefaultFrameWidth, 0, this) - 1)
.arg(bg2.name()) .arg(fg.name())
.arg(border.name()) );
/*
DefaultFrameWidth returns the size of the external margin + border width.
We know our border is 1px, so we subtract this from the frame
normal QStyle FrameWidth to get our margin
*/
.arg(style()->pixelMetric(QStyle::PM_DefaultFrameWidth, 0, this) - 1)
.arg(fg.name()));
} }
QSize KMessageWidget::sizeHint() const QSize KMessageWidget::sizeHint() const
{ {
ensurePolished(); ensurePolished();
return d->content->sizeHint(); return d->content->sizeHint();
} }
QSize KMessageWidget::minimumSizeHint() const QSize KMessageWidget::minimumSizeHint() const
{ {
ensurePolished(); ensurePolished();
return d->content->minimumSizeHint(); return d->content->minimumSizeHint();
} }
bool KMessageWidget::event(QEvent *event) bool KMessageWidget::event(QEvent *event)
{ {
if (event->type() == QEvent::Polish && !d->content->layout()) { if (event->type() == QEvent::Polish && !d->content->layout()) {
d->createLayout(); d->createLayout();
} }
return QFrame::event(event);
return QFrame::event(event);
} }
void KMessageWidget::resizeEvent(QResizeEvent *event) void KMessageWidget::resizeEvent(QResizeEvent *event)
{ {
QFrame::resizeEvent(event); QFrame::resizeEvent(event);
if (d->timeLine->state() == QTimeLine::NotRunning) { if (d->timeLine->state() == QTimeLine::NotRunning) {
d->content->resize(width(), d->bestContentHeight()); d->content->resize(width(), d->bestContentHeight());
} }
} }
int KMessageWidget::heightForWidth(int width) const int KMessageWidget::heightForWidth(int width) const
{ {
ensurePolished(); ensurePolished();
return d->content->heightForWidth(width); return d->content->heightForWidth(width);
} }
void KMessageWidget::paintEvent(QPaintEvent *event) void KMessageWidget::paintEvent(QPaintEvent *event)
{ {
QFrame::paintEvent(event); QFrame::paintEvent(event);
if (d->timeLine->state() == QTimeLine::Running) {
if (d->timeLine->state() == QTimeLine::Running) { QPainter painter(this);
QPainter painter(this); painter.setOpacity(d->timeLine->currentValue() * d->timeLine->currentValue());
painter.setOpacity(d->timeLine->currentValue() * d->timeLine->currentValue()); painter.drawPixmap(0, 0, d->contentSnapShot);
painter.drawPixmap(0, 0, d->contentSnapShot); }
}
}
void KMessageWidget::showEvent(QShowEvent *event)
{
// Keep this method here to avoid breaking binary compatibility:
// QFrame::showEvent() used to be reimplemented.
QFrame::showEvent(event);
} }
bool KMessageWidget::wordWrap() const bool KMessageWidget::wordWrap() const
{ {
return d->wordWrap; return d->wordWrap;
} }
void KMessageWidget::setWordWrap(bool wordWrap) void KMessageWidget::setWordWrap(bool wordWrap)
{ {
d->wordWrap = wordWrap; d->wordWrap = wordWrap;
d->textLabel->setWordWrap(wordWrap); d->textLabel->setWordWrap(wordWrap);
QSizePolicy policy = sizePolicy(); QSizePolicy policy = sizePolicy();
policy.setHeightForWidth(wordWrap); policy.setHeightForWidth(wordWrap);
setSizePolicy(policy); setSizePolicy(policy);
d->updateLayout(); d->updateLayout();
// Without this, when user does wordWrap -> !wordWrap -> wordWrap, a minimum
// Without this, when user does wordWrap -> !wordWrap -> wordWrap, a minimum // height is set, causing the widget to be too high.
// height is set, causing the widget to be too high. // Mostly visible in test programs.
// Mostly visible in test programs. if (wordWrap) {
if (wordWrap) { setMinimumHeight(0);
setMinimumHeight(0); }
}
} }
bool KMessageWidget::isCloseButtonVisible() const bool KMessageWidget::isCloseButtonVisible() const
{ {
return d->closeButton->isVisible(); return d->closeButton->isVisible();
} }
void KMessageWidget::setCloseButtonVisible(bool show) void KMessageWidget::setCloseButtonVisible(bool show)
{ {
d->closeButton->setVisible(show); d->closeButton->setVisible(show);
updateGeometry(); updateGeometry();
} }
void KMessageWidget::addAction(QAction *action) void KMessageWidget::addAction(QAction *action)
{ {
QFrame::addAction(action); QFrame::addAction(action);
d->updateLayout(); d->updateLayout();
} }
void KMessageWidget::removeAction(QAction *action) void KMessageWidget::removeAction(QAction *action)
{ {
QFrame::removeAction(action); QFrame::removeAction(action);
d->updateLayout(); d->updateLayout();
} }
void KMessageWidget::animatedShow() void KMessageWidget::animatedShow()
{ {
if (isVisible()) { if (!style()->styleHint(QStyle::SH_Widget_Animate, 0, this)) {
return; show();
} emit showAnimationFinished();
return;
QFrame::show(); }
setFixedHeight(0);
int wantedHeight = d->bestContentHeight(); if (isVisible()) {
d->content->setGeometry(0, -wantedHeight, width(), wantedHeight); return;
}
d->updateSnapShot();
QFrame::show();
d->timeLine->setDirection(QTimeLine::Forward); setFixedHeight(0);
int wantedHeight = d->bestContentHeight();
if (d->timeLine->state() == QTimeLine::NotRunning) { d->content->setGeometry(0, -wantedHeight, width(), wantedHeight);
d->timeLine->start();
} d->updateSnapShot();
d->timeLine->setDirection(QTimeLine::Forward);
if (d->timeLine->state() == QTimeLine::NotRunning) {
d->timeLine->start();
}
} }
void KMessageWidget::animatedHide() void KMessageWidget::animatedHide()
{ {
if (!isVisible()) { if (!style()->styleHint(QStyle::SH_Widget_Animate, 0, this)) {
hide(); hide();
return; emit hideAnimationFinished();
} return;
}
if (!isVisible()) {
hide();
return;
}
d->content->move(0, -d->content->height());
d->updateSnapShot();
d->timeLine->setDirection(QTimeLine::Backward);
if (d->timeLine->state() == QTimeLine::NotRunning) {
d->timeLine->start();
}
}
d->content->move(0, -d->content->height()); bool KMessageWidget::isHideAnimationRunning() const
d->updateSnapShot(); {
return (d->timeLine->direction() == QTimeLine::Backward)
&& (d->timeLine->state() == QTimeLine::Running);
}
d->timeLine->setDirection(QTimeLine::Backward); bool KMessageWidget::isShowAnimationRunning() const
{
if (d->timeLine->state() == QTimeLine::NotRunning) { return (d->timeLine->direction() == QTimeLine::Forward)
d->timeLine->start(); && (d->timeLine->state() == QTimeLine::Running);
}
} }
QIcon KMessageWidget::icon() const QIcon KMessageWidget::icon() const
{ {
return d->icon; return d->icon;
} }
void KMessageWidget::setIcon(const QIcon &icon) void KMessageWidget::setIcon(const QIcon &icon)
{ {
d->icon = icon; d->icon = icon;
if (d->icon.isNull()) {
if (d->icon.isNull()) { d->iconLabel->hide();
d->iconLabel->hide(); } else {
} else { const int size = style()->pixelMetric(QStyle::PM_ToolBarIconSize);
d->iconLabel->setPixmap(d->icon.pixmap(QSize(16, 16))); d->iconLabel->setPixmap(d->icon.pixmap(size));
d->iconLabel->show(); d->iconLabel->show();
} }
} }
#include "moc_kmessagewidget.cpp"

View file

@ -1,6 +1,7 @@
/* This file is part of the KDE libraries /* This file is part of the KDE libraries
* *
* Copyright (c) 2011 Aurélien Gâteau <agateau@kde.org> * Copyright (c) 2011 Aurélien Gâteau <agateau@kde.org>
* Copyright (c) 2014 Dominik Haumann <dhaumann@kde.org>
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -31,9 +32,14 @@ class KMessageWidgetPrivate;
* feedback, or to implement opportunistic interactions. * feedback, or to implement opportunistic interactions.
* *
* As a feedback widget, KMessageWidget provides a less intrusive alternative * As a feedback widget, KMessageWidget provides a less intrusive alternative
* to "OK Only" message boxes. If you do not need the modalness of KMessageBox, * to "OK Only" message boxes. If you want to avoid a modal KMessageBox,
* consider using KMessageWidget instead. * consider using KMessageWidget instead.
* *
* Examples of KMessageWidget look as follows, all of them having an icon set
* with setIcon(), and the first three show a close button:
*
* \image html kmessagewidget.png "KMessageWidget with different message types"
*
* <b>Negative feedback</b> * <b>Negative feedback</b>
* *
* The KMessageWidget can be used as a secondary indicator of failure: the * The KMessageWidget can be used as a secondary indicator of failure: the
@ -67,7 +73,7 @@ class KMessageWidgetPrivate;
* @li Confirm success of "critical" transactions * @li Confirm success of "critical" transactions
* @li Indicate completion of background tasks * @li Indicate completion of background tasks
* *
* Example of inadapted uses: * Example of unadapted uses:
* *
* @li Indicate successful saving of a file * @li Indicate successful saving of a file
* @li Indicate a file has been successfully removed * @li Indicate a file has been successfully removed
@ -87,153 +93,250 @@ class KMessageWidgetPrivate;
* @author Aurélien Gâteau <agateau@kde.org> * @author Aurélien Gâteau <agateau@kde.org>
* @since 4.7 * @since 4.7
*/ */
class KMessageWidget : public QFrame { class KMessageWidget : public QFrame
Q_OBJECT {
Q_ENUMS(MessageType) Q_OBJECT
Q_ENUMS(MessageType)
Q_PROPERTY(QString text READ text WRITE setText)
Q_PROPERTY(bool wordWrap READ wordWrap WRITE setWordWrap) Q_PROPERTY(QString text READ text WRITE setText)
Q_PROPERTY(bool closeButtonVisible READ isCloseButtonVisible WRITE setCloseButtonVisible) Q_PROPERTY(bool wordWrap READ wordWrap WRITE setWordWrap)
Q_PROPERTY(MessageType messageType READ messageType WRITE setMessageType) Q_PROPERTY(bool closeButtonVisible READ isCloseButtonVisible WRITE setCloseButtonVisible)
Q_PROPERTY(QIcon icon READ icon WRITE setIcon) Q_PROPERTY(MessageType messageType READ messageType WRITE setMessageType)
Q_PROPERTY(QIcon icon READ icon WRITE setIcon)
public: public:
enum MessageType {
Positive, /**
Information, * Available message types.
Warning, * The background colors are chosen depending on the message type.
Error */
}; enum MessageType {
Positive,
/** Information,
* Constructs a KMessageWidget with the specified parent. Warning,
*/ Error
explicit KMessageWidget(QWidget *parent = 0); };
explicit KMessageWidget(const QString &text, QWidget *parent = 0); /**
* Constructs a KMessageWidget with the specified @p parent.
~KMessageWidget(); */
explicit KMessageWidget(QWidget *parent = 0);
QString text() const;
/**
bool wordWrap() const; * Constructs a KMessageWidget with the specified @p parent and
* contents @p text.
bool isCloseButtonVisible() const; */
explicit KMessageWidget(const QString &text, QWidget *parent = 0);
MessageType messageType() const;
/**
void addAction(QAction *action); * Destructor.
*/
void removeAction(QAction *action); ~KMessageWidget();
QSize sizeHint() const; /**
* Get the text of this message widget.
QSize minimumSizeHint() const; * @see setText()
*/
int heightForWidth(int width) const; QString text() const;
/** /**
* The icon shown on the left of the text. By default, no icon is shown. * Check whether word wrap is enabled.
* @since 4.11 *
*/ * If word wrap is enabled, the message widget wraps the displayed text
QIcon icon() const; * as required to the available width of the widget. This is useful to
* avoid breaking widget layouts.
public *
Q_SLOTS: * @see setWordWrap()
void setText(const QString &text); */
bool wordWrap() const;
void setWordWrap(bool wordWrap);
/**
void setCloseButtonVisible(bool visible); * Check whether the close button is visible.
*
void setMessageType(KMessageWidget::MessageType type); * @see setCloseButtonVisible()
*/
/** bool isCloseButtonVisible() const;
* Show the widget using an animation, unless
* KGlobalSettings::graphicsEffectLevel() does not allow simple effects. /**
*/ * Get the type of this message.
void animatedShow(); * By default, the type is set to KMessageWidget::Information.
*
/** * @see KMessageWidget::MessageType, setMessageType()
* Hide the widget using an animation, unless */
* KGlobalSettings::graphicsEffectLevel() does not allow simple effects. MessageType messageType() const;
*/
void animatedHide(); /**
* Add @p action to the message widget.
/** * For each action a button is added to the message widget in the
* Define an icon to be shown on the left of the text * order the actions were added.
* @since 4.11 *
*/ * @param action the action to add
void setIcon(const QIcon &icon); * @see removeAction(), QWidget::actions()
*/
int bestContentHeight() const; void addAction(QAction *action);
/**
* Remove @p action from the message widget.
*
* @param action the action to remove
* @see KMessageWidget::MessageType, addAction(), setMessageType()
*/
void removeAction(QAction *action);
/**
* Returns the preferred size of the message widget.
*/
QSize sizeHint() const Q_DECL_OVERRIDE;
/**
* Returns the minimum size of the message widget.
*/
QSize minimumSizeHint() const Q_DECL_OVERRIDE;
/**
* Returns the required height for @p width.
* @param width the width in pixels
*/
int heightForWidth(int width) const Q_DECL_OVERRIDE;
/**
* The icon shown on the left of the text. By default, no icon is shown.
* @since 4.11
*/
QIcon icon() const;
/**
* Check whether the hide animation started by calling animatedHide()
* is still running. If animations are disabled, this function always
* returns @e false.
*
* @see animatedHide(), hideAnimationFinished()
* @since 5.0
*/
bool isHideAnimationRunning() const;
/**
* Check whether the show animation started by calling animatedShow()
* is still running. If animations are disabled, this function always
* returns @e false.
*
* @see animatedShow(), showAnimationFinished()
* @since 5.0
*/
bool isShowAnimationRunning() const;
public Q_SLOTS:
/**
* Set the text of the message widget to @p text.
* If the message widget is already visible, the text changes on the fly.
*
* @param text the text to display, rich text is allowed
* @see text()
*/
void setText(const QString &text);
/**
* Set word wrap to @p wordWrap. If word wrap is enabled, the text()
* of the message widget is wrapped to fit the available width.
* If word wrap is disabled, the message widget's minimum size is
* such that the entire text fits.
*
* @param wordWrap disable/enable word wrap
* @see wordWrap()
*/
void setWordWrap(bool wordWrap);
/**
* Set the visibility of the close button. If @p visible is @e true,
* a close button is shown that calls animatedHide() if clicked.
*
* @see closeButtonVisible(), animatedHide()
*/
void setCloseButtonVisible(bool visible);
/**
* Set the message type to @p type.
* By default, the message type is set to KMessageWidget::Information.
*
* @see messageType(), KMessageWidget::MessageType
*/
void setMessageType(KMessageWidget::MessageType type);
/**
* Show the widget using an animation.
*/
void animatedShow();
/**
* Hide the widget using an animation.
*/
void animatedHide();
/**
* Define an icon to be shown on the left of the text
* @since 4.11
*/
void setIcon(const QIcon &icon);
Q_SIGNALS: Q_SIGNALS:
/** /**
* This signal is emitted when the user clicks a link in the text label. * This signal is emitted when the user clicks a link in the text label.
* The URL referred to by the href anchor is passed in contents. * The URL referred to by the href anchor is passed in contents.
* @param contents text of the href anchor * @param contents text of the href anchor
* @see QLabel::linkActivated() * @see QLabel::linkActivated()
* @since 4.10 * @since 4.10
*/ */
void linkActivated(const QString &contents); void linkActivated(const QString &contents);
/** /**
* This signal is emitted when the user hovers over a link in the text label. * This signal is emitted when the user hovers over a link in the text label.
* The URL referred to by the href anchor is passed in contents. * The URL referred to by the href anchor is passed in contents.
* @param contents text of the href anchor * @param contents text of the href anchor
* @see QLabel::linkHovered() * @see QLabel::linkHovered()
* @since 4.11 * @since 4.11
*/ */
void linkHovered(const QString &contents); void linkHovered(const QString &contents);
/**
* This signal is emitted when the hide animation is finished, started by
* calling animatedHide(). If animations are disabled, this signal is
* emitted immediately after the message widget got hidden.
*
* @note This signal is @e not emitted if the widget was hidden by
* calling hide(), so this signal is only useful in conjunction
* with animatedHide().
*
* @see animatedHide()
* @since 5.0
*/
void hideAnimationFinished();
/**
* This signal is emitted when the show animation is finished, started by
* calling animatedShow(). If animations are disabled, this signal is
* emitted immediately after the message widget got shown.
*
* @note This signal is @e not emitted if the widget was shown by
* calling show(), so this signal is only useful in conjunction
* with animatedShow().
*
* @see animatedShow()
* @since 5.0
*/
void showAnimationFinished();
protected: protected:
void paintEvent(QPaintEvent *event); void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
bool event(QEvent *event); bool event(QEvent *event) Q_DECL_OVERRIDE;
void resizeEvent(QResizeEvent *event); void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE;
void showEvent(QShowEvent *event);
private: private:
KMessageWidgetPrivate *const d; KMessageWidgetPrivate *const d;
friend class KMessageWidgetPrivate; friend class KMessageWidgetPrivate;
Q_PRIVATE_SLOT(d, void slotTimeLineChanged(qreal)) Q_PRIVATE_SLOT(d, void slotTimeLineChanged(qreal))
Q_PRIVATE_SLOT(d, void slotTimeLineFinished()) Q_PRIVATE_SLOT(d, void slotTimeLineFinished())
}; };
//--------------------------------------------------------------------- #endif /* KMESSAGEWIDGET_H */
// KMessageWidgetPrivate
//---------------------------------------------------------------------
class QLabel;
class QToolButton;
class QTimeLine;
#include <QIcon>
class KMessageWidgetPrivate {
public:
void init(KMessageWidget *);
KMessageWidget *q;
QFrame *content;
QLabel *iconLabel;
QLabel *textLabel;
QToolButton *closeButton;
QTimeLine *timeLine;
QIcon icon;
KMessageWidget::MessageType messageType;
bool wordWrap;
QList<QToolButton *> buttons;
QPixmap contentSnapShot;
void createLayout();
void updateSnapShot();
void updateLayout();
void slotTimeLineChanged(qreal);
void slotTimeLineFinished();
int bestContentHeight() const;
};
#endif // KMESSAGEWIDGET_H