124 lines
3.1 KiB
C++
Executable File
124 lines
3.1 KiB
C++
Executable File
#include "Message.h"
|
|
#include <QSpacerItem>
|
|
#include <QTimer>
|
|
#include <QVBoxLayout>
|
|
|
|
Message::Message(QWidget* parent)
|
|
: QWidget(parent)
|
|
{
|
|
btnStyle = "QPushButton { \
|
|
background: rgba(0,0,0,0.4); \
|
|
color: rgba(255,255,255); \
|
|
border: none; \
|
|
border-radius: 10px; \
|
|
min-height: 40px; \
|
|
}";
|
|
|
|
QVBoxLayout* layout = new QVBoxLayout();
|
|
layout->setContentsMargins(QMargins(10, 10, 10, 10));
|
|
layout->setSpacing(10);
|
|
this->setLayout(layout);
|
|
this->setFixedHeight(260);
|
|
QFont font;
|
|
// font.setFamily("Ubuntu Regular");
|
|
font.setPixelSize(16);
|
|
this->setFont(font);
|
|
|
|
QSpacerItem* verticalSpacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
|
|
layout->addSpacerItem(verticalSpacer);
|
|
}
|
|
|
|
Message::~Message()
|
|
{
|
|
}
|
|
|
|
void Message::success(QString content, int duration)
|
|
{
|
|
showMessage(content, duration, Success);
|
|
}
|
|
|
|
void Message::info(QString content, int duration)
|
|
{
|
|
showMessage(content, duration, Info);
|
|
}
|
|
|
|
void Message::error(QString content, int duration)
|
|
{
|
|
showMessage(content, duration, Error);
|
|
}
|
|
|
|
void Message::warning(QString content, int duration)
|
|
{
|
|
showMessage(content, duration, Warning);
|
|
}
|
|
|
|
void Message::showMessage(QString content, int duration, Level level)
|
|
{
|
|
QVBoxLayout* layout = static_cast<QVBoxLayout*>(this->layout());
|
|
if (count >= maxCount) {
|
|
// remove first
|
|
QPair<QPushButton*, QTimer*> pair = contentList.first();
|
|
contentList.removeFirst();
|
|
layout->removeWidget(pair.first);
|
|
delete pair.first;
|
|
delete pair.second;
|
|
count -= 1;
|
|
}
|
|
|
|
// apeend new
|
|
QPushButton* btn = generateButton(content, level);
|
|
layout->insertWidget(count, btn);
|
|
|
|
QTimer* timer = new QTimer();
|
|
timer->setInterval(duration);
|
|
timer->start();
|
|
QPair<QPushButton*, QTimer*> pair(btn, timer);
|
|
contentList.append(pair);
|
|
count += 1;
|
|
|
|
connect(timer, &QTimer::timeout, [=] {
|
|
for (int i = 0; i < contentList.length(); i++) {
|
|
QPair<QPushButton*, QTimer*> pair = contentList.at(i);
|
|
if (pair.first == btn) {
|
|
contentList.removeAt(i);
|
|
}
|
|
layout->removeWidget(pair.first);
|
|
delete pair.first;
|
|
delete pair.second;
|
|
}
|
|
});
|
|
}
|
|
|
|
QPushButton* Message::generateButton(QString content, Level level)
|
|
{
|
|
QPushButton* btn = new QPushButton(this);
|
|
btn->setText(content);
|
|
QString iconPath;
|
|
switch (level) {
|
|
case Info:
|
|
iconPath = ":/icons/info.png";
|
|
break;
|
|
case Success:
|
|
iconPath = ":/icons/success.png";
|
|
break;
|
|
case Error:
|
|
iconPath = ":/icons/error.png";
|
|
case Warning:
|
|
iconPath = ":/icons/waring.png";
|
|
break;
|
|
default:
|
|
iconPath = ":/icons/info.png";
|
|
break;
|
|
}
|
|
btn->setIcon(QIcon(iconPath));
|
|
btn->setIconSize(QSize(16, 16));
|
|
btn->setStyleSheet(btnStyle);
|
|
|
|
QFont font = btn->font();
|
|
QFontMetrics metrics(font);
|
|
int width = metrics.boundingRect(content).size().width();
|
|
btn->setFixedWidth(width);
|
|
|
|
return btn;
|
|
}
|