Files
GUI/src/dialogs/MultyMessageDialog.cpp

113 lines
2.9 KiB
C++
Raw Normal View History

2022-07-28 16:27:51 +08:00
#include "MultyMessageDialog.h"
#include <QHBoxLayout>
namespace
{
const int AUTO_DISAPPEAR_TIME = 5000;
const int DIALOG_WIDTH = 240;
const int DIALOG_HEIGHT = 50;
}
MultyMessageDialog::MultyMessageDialog(const QString& aMessage,MessageLevel aMessageLevel,QWidget* aParent)
: QDialog(aParent)
, mAutoDisappearTime(AUTO_DISAPPEAR_TIME)
, mTimer(new QTimer(this))
, mDisappearAnimation(new QPropertyAnimation(this,"pos",this))
, mShowAnimation(new QPropertyAnimation(this,"pos",this))
, mMessage(new QLabel(aMessage,this))
, mIcon(new QLabel(this))
, mLevel(aMessageLevel)
{
setWindowFlags(Qt::FramelessWindowHint| Qt::WindowStaysOnTopHint| Qt::BypassWindowManagerHint);
resize(DIALOG_WIDTH,DIALOG_HEIGHT);
mTimer->setSingleShot(true);
connect(mTimer,&QTimer::timeout,this,&MultyMessageDialog::startHideAnimation);
initializeAnimation();
initializeIcon();
QHBoxLayout* layout = new QHBoxLayout(this);
layout->addWidget(mIcon);
layout->addWidget(mMessage);
}
MultyMessageDialog::~MultyMessageDialog()
{
emit dialogDestroyed();
}
void MultyMessageDialog::initializeAnimation()
{
mDisappearAnimation->setDuration(100);
mShowAnimation->setDuration(100);
connect(mDisappearAnimation, &QPropertyAnimation::finished, this, &MultyMessageDialog::hideDialog);
}
void MultyMessageDialog::initializeIcon()
{
switch (mLevel)
{
case Info:
mIcon->setObjectName("MultyMessageDialogInfo");
break;
case Warning:
mIcon->setObjectName("MultyMessageDialogWarning");
break;
case Error:
mIcon->setObjectName("MultyMessageDialogError");
break;
case Sucess:
mIcon->setObjectName("MultyMessageDialogSucess");
break;
default:
mIcon->setObjectName("MultyMessageDialogInfo");
break;
}
}
void MultyMessageDialog::setMessage(const QString& aMessage)
{
mMessage->setText(aMessage);
}
void MultyMessageDialog::startHideAnimation()
{
if (mTimer->isActive())
{
mTimer->stop();
}
mDisappearAnimation->setStartValue(pos());
mDisappearAnimation->setEndValue(QPoint(pos().x(),pos().y()-30-height()));
mDisappearAnimation->start();
}
void MultyMessageDialog::hideDialog()
{
hide();
deleteLater();
}
void MultyMessageDialog::setAutoDisappearTime(int aAutoDisappearTime)
{
mAutoDisappearTime = aAutoDisappearTime;
}
void MultyMessageDialog::showEvent(QShowEvent* aEvent)
{
mTimer->start(mAutoDisappearTime);
mShowAnimation->setStartValue(QPoint(pos().x(),pos().y()-30-height()));
mShowAnimation->setEndValue(pos());
mShowAnimation->start();
QDialog::showEvent(aEvent);
}
void MultyMessageDialog::stopShowAnimation()
{
if (QAbstractAnimation::Running == mShowAnimation->state())
{
mShowAnimation->stop();
move(mShowAnimation->endValue().toPoint());
}
}