Update KMessageWidget to KF5

The KMessageWidget that we used was from an ancient version of KDE4, KF5
is out for quite a while, so it's good that we also update our software
that uses parts of KDE.

Signed-off-by: Tomaz Canabrava <tomaz.canabrava@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Tomaz Canabrava 2015-06-03 22:08:57 -03:00 committed by Dirk Hohndel
parent be0a708db0
commit ef10490e03
3 changed files with 534 additions and 370 deletions

View file

@ -1,6 +1,7 @@
/* This file is part of the KDE libraries
*
* 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
* modify it under the terms of the GNU Lesser General Public
@ -19,15 +20,46 @@
*/
#include "kmessagewidget.h"
#include <QAction>
#include <QEvent>
#include <QGridLayout>
#include <QDialogButtonBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QPainter>
#include <QShowEvent>
#include <QTimeLine>
#include <QToolButton>
#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)
{
@ -51,16 +83,20 @@ void KMessageWidgetPrivate::init(KMessageWidget *q_ptr)
textLabel = new QLabel(content);
textLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
textLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
QObject::connect(textLabel, SIGNAL(linkActivated(const QString &)), q, SIGNAL(linkActivated(const QString &)));
QObject::connect(textLabel, SIGNAL(linkHovered(const QString &)), q, SIGNAL(linkHovered(const QString &)));
QObject::connect(textLabel, SIGNAL(linkActivated(QString)), q, SIGNAL(linkActivated(QString)));
QObject::connect(textLabel, SIGNAL(linkHovered(QString)), q, SIGNAL(linkHovered(QString)));
QAction *closeAction = new QAction(QObject::tr("Close"), q);
q->connect(closeAction, SIGNAL(triggered(bool)), q, SLOT(animatedHide()));
QAction *closeAction = new QAction(q);
closeAction->setText(KMessageWidget::tr("&Close"));
closeAction->setToolTip(KMessageWidget::tr("Close message"));
closeAction->setIcon(q->style()->standardIcon(QStyle::SP_DialogCloseButton));
QObject::connect(closeAction, SIGNAL(triggered(bool)), q, SLOT(animatedHide()));
closeButton = new QToolButton(content);
closeButton->setAutoRaise(true);
closeButton->setDefaultAction(closeAction);
closeButton->setVisible(false);
q->setMessageType(KMessageWidget::Information);
}
@ -91,38 +127,32 @@ void KMessageWidgetPrivate::createLayout()
layout->addWidget(iconLabel, 0, 0, 1, 1, Qt::AlignHCenter | Qt::AlignTop);
layout->addWidget(textLabel, 0, 1);
QDialogButtonBox *buttonLayout = new QDialogButtonBox();
//buttonLayout->addStretch();
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addStretch();
Q_FOREACH (QToolButton *button, buttons) {
// For some reason, calling show() is necessary if wordwrap is true,
// otherwise the buttons do not show up. It is not needed if
// wordwrap is false.
button->show();
buttonLayout->addButton(button, QDialogButtonBox::QDialogButtonBox::AcceptRole);
buttonLayout->addWidget(button);
}
buttonLayout->addButton(closeButton, QDialogButtonBox::RejectRole);
layout->addWidget(buttonLayout, 1, 0, 1, 2, Qt::AlignHCenter | Qt::AlignTop);
buttonLayout->addWidget(closeButton);
layout->addItem(buttonLayout, 1, 0, 1, 2);
} else {
bool closeButtonVisible = closeButton->isVisible();
QHBoxLayout *layout = new QHBoxLayout(content);
layout->addWidget(iconLabel);
layout->addWidget(textLabel);
QDialogButtonBox *buttonLayout = new QDialogButtonBox();
Q_FOREACH (QToolButton *button, buttons) {
buttonLayout->addButton(button, QDialogButtonBox::QDialogButtonBox::AcceptRole);
layout->addWidget(button);
}
buttonLayout->addButton(closeButton, QDialogButtonBox::RejectRole);
// Something gets changed when added to the buttonLayout
closeButton->setVisible(closeButtonVisible);
layout->addWidget(buttonLayout);
layout->addWidget(closeButton);
};
if (q->isVisible()) {
q->setFixedHeight(content->sizeHint().height());
}
q->updateGeometry();
}
@ -139,7 +169,8 @@ void KMessageWidgetPrivate::updateSnapShot()
// window layouts to be activated. Calling this method from resizeEvent()
// can lead to infinite recursion, see:
// https://bugs.kde.org/show_bug.cgi?id=311336
contentSnapShot = QPixmap(content->size());
contentSnapShot = QPixmap(content->size() * q->devicePixelRatio());
contentSnapShot.setDevicePixelRatio(q->devicePixelRatio());
contentSnapShot.fill(Qt::transparent);
content->render(&contentSnapShot, QPoint(), QRegion(), QWidget::DrawChildren);
}
@ -157,33 +188,38 @@ void KMessageWidgetPrivate::slotTimeLineFinished()
// We set the whole geometry here, because it may be wrong if a
// KMessageWidget is shown right when the toplevel window is created.
content->setGeometry(0, 0, q->width(), bestContentHeight());
// notify about finished animation
emit q->showAnimationFinished();
} else {
// Hide
// hide and notify about finished animation
q->hide();
emit q->hideAnimationFinished();
}
}
int KMessageWidgetPrivate::bestContentHeight() const
{
int height = content->heightForWidth(q->width());
if (height == -1) {
height = content->sizeHint().height();
}
return height;
}
//---------------------------------------------------------------------
// KMessageWidget
//---------------------------------------------------------------------
KMessageWidget::KMessageWidget(QWidget *parent) : QFrame(parent), d(new KMessageWidgetPrivate)
KMessageWidget::KMessageWidget(QWidget *parent)
: QFrame(parent)
, d(new KMessageWidgetPrivate)
{
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);
setText(text);
@ -205,50 +241,57 @@ void KMessageWidget::setText(const QString &text)
updateGeometry();
}
int KMessageWidget::bestContentHeight() const
{
return d->bestContentHeight();
}
KMessageWidget::MessageType KMessageWidget::messageType() const
{
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)
{
d->messageType = type;
QColor bg0, bg1, bg2, border, fg;
switch (type) {
case Positive:
bg1 = QColor("#72D594"); // nice green
fg = QColor(Qt::white);
bg1.setRgb(0, 110, 40); // values taken from kcolorscheme.cpp (Positive)
break;
case Information:
bg1 = QColor("#41A8E3"); // nice blue
fg = QColor(Qt::black);
bg1 = palette().highlight().color();
break;
case Warning:
bg1 = QColor("#72D594"); // nice yellow
fg = QColor(Qt::black);
bg1.setRgb(176, 128, 0); // values taken from kcolorscheme.cpp (Neutral)
break;
case Error:
bg1 = QColor("#E4B7B7"); // nice red.
fg = QColor(Qt::black);
bg1.setRgb(191, 3, 3); // values taken from kcolorscheme.cpp (Negative)
break;
}
// Colors
fg = palette().highlightedText().color();
bg0 = bg1.lighter(110);
bg2 = bg1.darker(110);
border = bg2.darker(110);
border = darkShade(bg1);
d->content->setStyleSheet(
QString(".QFrame {"
QString(QLatin1String(".QFrame {"
"background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,"
" stop: 0 %1,"
" stop: 0.1 %2,"
@ -257,17 +300,16 @@ void KMessageWidget::setMessageType(KMessageWidget::MessageType type)
"border: 1px solid %4;"
"margin: %5px;"
"}"
".QLabel { color: %6; }").arg(bg0.name())
".QLabel { color: %6; }"
))
.arg(bg0.name())
.arg(bg1.name())
.arg(bg2.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
*/
// 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()));
.arg(fg.name())
);
}
QSize KMessageWidget::sizeHint() const
@ -287,7 +329,6 @@ bool KMessageWidget::event(QEvent *event)
if (event->type() == QEvent::Polish && !d->content->layout()) {
d->createLayout();
}
return QFrame::event(event);
}
@ -309,7 +350,6 @@ int KMessageWidget::heightForWidth(int width) const
void KMessageWidget::paintEvent(QPaintEvent *event)
{
QFrame::paintEvent(event);
if (d->timeLine->state() == QTimeLine::Running) {
QPainter painter(this);
painter.setOpacity(d->timeLine->currentValue() * d->timeLine->currentValue());
@ -317,13 +357,6 @@ void KMessageWidget::paintEvent(QPaintEvent *event)
}
}
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
{
return d->wordWrap;
@ -337,7 +370,6 @@ void KMessageWidget::setWordWrap(bool wordWrap)
policy.setHeightForWidth(wordWrap);
setSizePolicy(policy);
d->updateLayout();
// Without this, when user does wordWrap -> !wordWrap -> wordWrap, a minimum
// height is set, causing the widget to be too high.
// Mostly visible in test programs.
@ -371,6 +403,12 @@ void KMessageWidget::removeAction(QAction *action)
void KMessageWidget::animatedShow()
{
if (!style()->styleHint(QStyle::SH_Widget_Animate, 0, this)) {
show();
emit showAnimationFinished();
return;
}
if (isVisible()) {
return;
}
@ -383,7 +421,6 @@ void KMessageWidget::animatedShow()
d->updateSnapShot();
d->timeLine->setDirection(QTimeLine::Forward);
if (d->timeLine->state() == QTimeLine::NotRunning) {
d->timeLine->start();
}
@ -391,8 +428,13 @@ void KMessageWidget::animatedShow()
void KMessageWidget::animatedHide()
{
if (!isVisible()) {
if (!style()->styleHint(QStyle::SH_Widget_Animate, 0, this)) {
hide();
emit hideAnimationFinished();
return;
}
if (!isVisible()) {
return;
}
@ -400,12 +442,23 @@ void KMessageWidget::animatedHide()
d->updateSnapShot();
d->timeLine->setDirection(QTimeLine::Backward);
if (d->timeLine->state() == QTimeLine::NotRunning) {
d->timeLine->start();
}
}
bool KMessageWidget::isHideAnimationRunning() const
{
return (d->timeLine->direction() == QTimeLine::Backward)
&& (d->timeLine->state() == QTimeLine::Running);
}
bool KMessageWidget::isShowAnimationRunning() const
{
return (d->timeLine->direction() == QTimeLine::Forward)
&& (d->timeLine->state() == QTimeLine::Running);
}
QIcon KMessageWidget::icon() const
{
return d->icon;
@ -414,11 +467,13 @@ QIcon KMessageWidget::icon() const
void KMessageWidget::setIcon(const QIcon &icon)
{
d->icon = icon;
if (d->icon.isNull()) {
d->iconLabel->hide();
} else {
d->iconLabel->setPixmap(d->icon.pixmap(QSize(16, 16)));
const int size = style()->pixelMetric(QStyle::PM_ToolBarIconSize);
d->iconLabel->setPixmap(d->icon.pixmap(size));
d->iconLabel->show();
}
}
#include "moc_kmessagewidget.cpp"

View file

@ -1,6 +1,7 @@
/* This file is part of the KDE libraries
*
* 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
* modify it under the terms of the GNU Lesser General Public
@ -31,9 +32,14 @@ class KMessageWidgetPrivate;
* feedback, or to implement opportunistic interactions.
*
* 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.
*
* 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>
*
* 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 Indicate completion of background tasks
*
* Example of inadapted uses:
* Example of unadapted uses:
*
* @li Indicate successful saving of a file
* @li Indicate a file has been successfully removed
@ -87,7 +93,8 @@ class KMessageWidgetPrivate;
* @author Aurélien Gâteau <agateau@kde.org>
* @since 4.7
*/
class KMessageWidget : public QFrame {
class KMessageWidget : public QFrame
{
Q_OBJECT
Q_ENUMS(MessageType)
@ -97,6 +104,11 @@ class KMessageWidget : public QFrame {
Q_PROPERTY(MessageType messageType READ messageType WRITE setMessageType)
Q_PROPERTY(QIcon icon READ icon WRITE setIcon)
public:
/**
* Available message types.
* The background colors are chosen depending on the message type.
*/
enum MessageType {
Positive,
Information,
@ -105,31 +117,86 @@ public:
};
/**
* Constructs a KMessageWidget with the specified parent.
* Constructs a KMessageWidget with the specified @p parent.
*/
explicit KMessageWidget(QWidget *parent = 0);
/**
* Constructs a KMessageWidget with the specified @p parent and
* contents @p text.
*/
explicit KMessageWidget(const QString &text, QWidget *parent = 0);
/**
* Destructor.
*/
~KMessageWidget();
/**
* Get the text of this message widget.
* @see setText()
*/
QString text() const;
/**
* Check whether word wrap is enabled.
*
* If word wrap is enabled, the message widget wraps the displayed text
* as required to the available width of the widget. This is useful to
* avoid breaking widget layouts.
*
* @see setWordWrap()
*/
bool wordWrap() const;
/**
* Check whether the close button is visible.
*
* @see setCloseButtonVisible()
*/
bool isCloseButtonVisible() const;
/**
* Get the type of this message.
* By default, the type is set to KMessageWidget::Information.
*
* @see KMessageWidget::MessageType, setMessageType()
*/
MessageType messageType() const;
/**
* Add @p action to the message widget.
* For each action a button is added to the message widget in the
* order the actions were added.
*
* @param action the action to add
* @see removeAction(), QWidget::actions()
*/
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);
QSize sizeHint() const;
/**
* Returns the preferred size of the message widget.
*/
QSize sizeHint() const Q_DECL_OVERRIDE;
QSize minimumSizeHint() const;
/**
* Returns the minimum size of the message widget.
*/
QSize minimumSizeHint() const Q_DECL_OVERRIDE;
int heightForWidth(int width) const;
/**
* 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.
@ -137,25 +204,70 @@ public:
*/
QIcon icon() const;
public
Q_SLOTS:
/**
* 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, unless
* KGlobalSettings::graphicsEffectLevel() does not allow simple effects.
* Show the widget using an animation.
*/
void animatedShow();
/**
* Hide the widget using an animation, unless
* KGlobalSettings::graphicsEffectLevel() does not allow simple effects.
* Hide the widget using an animation.
*/
void animatedHide();
@ -165,7 +277,6 @@ Q_SLOTS:
*/
void setIcon(const QIcon &icon);
int bestContentHeight() const;
Q_SIGNALS:
/**
* This signal is emitted when the user clicks a link in the text label.
@ -185,14 +296,40 @@ Q_SIGNALS:
*/
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:
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 showEvent(QShowEvent *event);
void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE;
private:
KMessageWidgetPrivate *const d;
@ -202,38 +339,4 @@ private:
Q_PRIVATE_SLOT(d, void slotTimeLineFinished())
};
//---------------------------------------------------------------------
// 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
#endif /* KMESSAGEWIDGET_H */

View file

@ -116,7 +116,10 @@ MainTab::MainTab(QWidget *parent) : QTabWidget(parent),
ui.divemaster->setCompleter(completers.divemaster);
ui.suit->setCompleter(completers.suit);
ui.tagWidget->setCompleter(completers.tags);
ui.diveNotesMessage->hide();
ui.diveEquipmentMessage->hide();
ui.diveInfoMessage->hide();
ui.diveStatisticsMessage->hide();
setMinimumHeight(0);
setMinimumWidth(0);
@ -312,9 +315,12 @@ void MainTab::displayMessage(QString str)
ui.diveStatisticsMessage->setText(str);
ui.diveStatisticsMessage->animatedShow();
updateTextLabels();
ui.tagWidget->fixPopupPosition(ui.diveNotesMessage->bestContentHeight());
ui.buddy->fixPopupPosition(ui.diveNotesMessage->bestContentHeight());
ui.divemaster->fixPopupPosition(ui.diveNotesMessage->bestContentHeight());
// TODO: this doesn't exists anymore. Find out why it was removed from
// the KMessageWidget and try to see if this is still needed.
// ui.tagWidget->fixPopupPosition(ui.diveNotesMessage->bestContentHeight());
// ui.buddy->fixPopupPosition(ui.diveNotesMessage->bestContentHeight());
// ui.divemaster->fixPopupPosition(ui.diveNotesMessage->bestContentHeight());
}
void MainTab::updateTextLabels(bool showUnits)