awMsgBox.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "awMsgBox.h"
  2. #include "ui_awMsgBox.h"
  3. #include "CAppInfo.h"
  4. #include <QDesktopWidget>
  5. #include <QGraphicsDropShadowEffect>
  6. std::list<std::shared_ptr<awMsgBox>> awMsgBox::vMsgList;
  7. std::mutex awMsgBox::msgMutex;
  8. awMsgBox::awMsgBox(QWidget *parent) :
  9. QDialog(parent),
  10. ui(new Ui::awMsgBox)
  11. {
  12. ui->setupUi(this);
  13. setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
  14. setAttribute(Qt::WA_TranslucentBackground);
  15. setStyleSheet(g_appInfoPtr->m_sQssStr);
  16. if(m_pTimer == nullptr)
  17. {
  18. m_pTimer = std::make_shared<QTimer>();
  19. m_pTimer->setInterval(3 * 1000);
  20. }
  21. connect(m_pTimer.get(), &QTimer::timeout, this, [&]() {
  22. m_pTimer->stop();
  23. if (vMsgList.size() > 0)
  24. {
  25. std::scoped_lock lock(msgMutex);
  26. vMsgList.pop_front();
  27. }
  28. size_t i = 0;
  29. for (std::list<std::shared_ptr<awMsgBox>>::iterator it = vMsgList.begin(); it != vMsgList.end(); ++it)
  30. {
  31. (*it)->setGeometry((*it)->x(),
  32. g_appInfoPtr->m_fRate * 84 + i * g_appInfoPtr->m_fRate * 90,
  33. (*it)->width(), (*it)->height());
  34. ++i;
  35. }
  36. });
  37. m_pTimer->start();
  38. }
  39. awMsgBox::~awMsgBox()
  40. {
  41. m_pTimer->stop();
  42. delete ui;
  43. }
  44. void awMsgBox::clear(QWidget *parent)
  45. {
  46. std::scoped_lock lock(msgMutex);
  47. for (std::list<std::shared_ptr<awMsgBox>>::iterator it = vMsgList.begin(); it != vMsgList.end(); )
  48. {
  49. if((*it)->parent() == parent || parent == nullptr)
  50. {
  51. it = vMsgList.erase(it);
  52. }
  53. else
  54. {
  55. ++it;
  56. }
  57. }
  58. }
  59. void awMsgBox::initUI(MSG_ICON_TYPE type)
  60. {
  61. QString sStyle = "";
  62. if(type == MSG_ICON_TYPE::mit_information)
  63. {
  64. sStyle = "border-image:url(:/images/icon-um-info.png);";
  65. }
  66. else if(type == MSG_ICON_TYPE::mit_succeed)
  67. {
  68. sStyle = "border-image:url(:/images/icon-env-test-pass.png);";
  69. }
  70. else if(type == MSG_ICON_TYPE::mit_error)
  71. {
  72. sStyle = "border-image:url(:/images/icon-um.png);";
  73. }
  74. ui->label_awmb_icon->setStyleSheet(sStyle);
  75. ui->label_awmb_content->adjustSize();
  76. int nCW = g_appInfoPtr->m_fRate*40 + g_appInfoPtr->m_fRate*40 + g_appInfoPtr->m_fRate*(28 + 10) + ui->label_awmb_content->width();
  77. QDesktopWidget *dekwiget = QApplication::desktop();
  78. setGeometry((dekwiget->width() - nCW - g_appInfoPtr->m_fRate*10)/2,
  79. g_appInfoPtr->m_fRate*84 + vMsgList.size()*g_appInfoPtr->m_fRate * 90,
  80. nCW + g_appInfoPtr->m_fRate*10, g_appInfoPtr->m_fRate*90);
  81. ui->widget_awmb_BG->setGeometry(5, 5 , nCW, g_appInfoPtr->m_fRate*80);
  82. ui->label_awmb_icon->setGeometry(g_appInfoPtr->m_fRate*40, g_appInfoPtr->m_fRate*26,
  83. g_appInfoPtr->m_fRate*28, g_appInfoPtr->m_fRate*28);
  84. ui->label_awmb_content->setGeometry(ui->label_awmb_icon->x() + ui->label_awmb_icon->width() + g_appInfoPtr->m_fRate*10,
  85. (height() - ui->label_awmb_content->height())/2,
  86. ui->label_awmb_content->width(), ui->label_awmb_content->height());
  87. ui->btn_awmb_close->setGeometry(ui->widget_awmb_BG->width() - g_appInfoPtr->m_fRate*(10 + 16),
  88. g_appInfoPtr->m_fRate*10, g_appInfoPtr->m_fRate*16, g_appInfoPtr->m_fRate*16);
  89. QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
  90. effect->setOffset(g_appInfoPtr->m_fRate*4,g_appInfoPtr->m_fRate*4);
  91. effect->setColor(QColor(0,0,0,30));
  92. effect->setBlurRadius(g_appInfoPtr->m_fRate*16);
  93. ui->widget_awmb_BG->setGraphicsEffect(effect);
  94. }
  95. void ShowMsg(QString sMsg, QWidget *parent, MSG_ICON_TYPE type)
  96. {
  97. std::shared_ptr<awMsgBox> msg = std::make_shared<awMsgBox>(parent);
  98. msg->setMsg(sMsg);
  99. msg->initUI(type);
  100. msg->show();
  101. std::scoped_lock lock(awMsgBox::msgMutex);
  102. awMsgBox::vMsgList.push_back(msg);
  103. }
  104. void awMsgBox::setMsg(QString sMsg)
  105. {
  106. ui->label_awmb_content->setText(sMsg);
  107. }
  108. void awMsgBox::on_btn_awmb_close_clicked()
  109. {
  110. reject();
  111. }