Skip to content

Commit

Permalink
feat(toast): interface to set duration
Browse files Browse the repository at this point in the history
Change-Id: I3ef792c80adfb14f7c8adafd518030f2cfb74c61
  • Loading branch information
BLumia committed Aug 13, 2018
1 parent 115febe commit 5d0fc56
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/widgets/dtoast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class DToastPrivate: public DTK_CORE_NAMESPACE::DObjectPrivate
QLabel *iconLabel = Q_NULLPTR;
QLabel *textLabel = Q_NULLPTR;

int duration = 2000;

QPropertyAnimation *animation = Q_NULLPTR;
DGraphicsGlowEffect *effect = Q_NULLPTR;

Expand Down Expand Up @@ -76,6 +78,12 @@ QIcon DToast::icon() const
return d->icon;
}

int DToast::duration() const
{
D_DC(DToast);
return d->duration;
}

qreal DToast::opacity() const
{
D_DC(DToast);
Expand Down Expand Up @@ -105,6 +113,12 @@ void DToast::setIcon(QIcon icon, QSize defaultSize)
d->iconLabel->setPixmap(d->icon.pixmap(icon.actualSize(defaultSize)));
}

void DToast::setDuration(int duration)
{
D_D(DToast);
d->duration = duration;
}

void DToast::setOpacity(qreal opacity)
{
D_D(DToast);
Expand All @@ -123,8 +137,10 @@ void DToast::pop()
return;
}

int _duration = d->duration < 0 ? 2000 : d->duration;

d->animation = new QPropertyAnimation(this, "opacity");
d->animation->setDuration(2000);
d->animation->setDuration(_duration);
d->animation->setStartValue(0);
d->animation->setKeyValueAt(0.4, 1.0);
d->animation->setKeyValueAt(0.8, 1.0);
Expand All @@ -149,6 +165,18 @@ void DToast::pack()
}
}

void DToast::showEvent(QShowEvent *event)
{
Q_EMIT visibleChanged(true);
return QWidget::showEvent(event);
}

void DToast::hideEvent(QHideEvent *event)
{
Q_EMIT visibleChanged(false);
return QWidget::hideEvent(event);
}

DToastPrivate::DToastPrivate(DToast *qq)
: DObjectPrivate(qq)
{
Expand Down
8 changes: 8 additions & 0 deletions src/widgets/dtoast.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,28 @@ class LIBDTKWIDGETSHARED_EXPORT DToast : public QFrame, public DTK_CORE_NAMESPAC
Q_OBJECT

Q_PROPERTY(qreal opacity READ opacity WRITE setOpacity)
Q_PROPERTY(qreal duration READ duration WRITE setDuration)
public:
explicit DToast(QWidget *parent = 0);
~DToast();

QString text() const;
QIcon icon() const;
int duration() const;

Q_SIGNALS:
void visibleChanged(bool isVisible);

public Q_SLOTS:
void pop();
void pack();
void showEvent(QShowEvent *event) override;
void hideEvent(QHideEvent *event) override;

void setText(QString text);
void setIcon(QString icon);
void setIcon(QIcon icon, QSize defaultSize = QSize(20, 20));
void setDuration(int duration);

private:
qreal opacity() const;
Expand Down

0 comments on commit 5d0fc56

Please sign in to comment.