MessageBox.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include "MessageBox.h"
  2. #include "BmpWidget.h"
  3. #include "UserInterfaceUtility.h"
  4. #include <QFile>
  5. #include <QPainter>
  6. #include <QTimer>
  7. #include <QFont>
  8. #include <QEvent>
  9. class MessageBoxPrivate
  10. {
  11. Q_DISABLE_COPY(MessageBoxPrivate)
  12. public:
  13. explicit MessageBoxPrivate(MessageBox *parent);
  14. ~MessageBoxPrivate();
  15. void initialize();
  16. void connectAllSlots();
  17. QString m_Text;
  18. QTimer* m_Timer;
  19. QScopedPointer<QPixmap> m_BackgroundPixmap;
  20. QString m_BackgroundPath;
  21. QFont m_Font;
  22. bool m_AutoHide;
  23. bool m_Reload;
  24. private:
  25. MessageBox *m_Parent;
  26. };
  27. MessageBox::MessageBox(QWidget *parent)
  28. : QWidget(parent)
  29. , m_Private(new MessageBoxPrivate(this))
  30. {
  31. }
  32. MessageBox::~MessageBox()
  33. {
  34. }
  35. void MessageBox::setText(const QString &text)
  36. {
  37. if (text != m_Private->m_Text) {
  38. m_Private->m_Text = text;
  39. if (isVisible()) {
  40. update();
  41. }
  42. }
  43. }
  44. void MessageBox::setAutoHide(const bool flag)
  45. {
  46. if (flag != m_Private->m_AutoHide) {
  47. m_Private->m_AutoHide = flag;
  48. }
  49. if (m_Private->m_AutoHide) {
  50. m_Private->m_Timer->start();
  51. }
  52. }
  53. void MessageBox::setBackgroundBmpPath(const QString &path)
  54. {
  55. if (QFile::exists(path)) {
  56. m_Private->m_Reload = false;
  57. m_Private->m_BackgroundPath = path;
  58. m_Private->m_BackgroundPixmap.reset(new QPixmap(path));
  59. update();
  60. }
  61. }
  62. void MessageBox::setFontPointSize(const int pointSize)
  63. {
  64. m_Private->m_Font.setPointSize(pointSize);
  65. if (isVisible()) {
  66. update();
  67. }
  68. }
  69. void MessageBox::styleChange()
  70. {
  71. m_Private->m_Reload = true;
  72. }
  73. void MessageBox::showEvent(QShowEvent *event)
  74. {
  75. m_Private->m_Timer->start();
  76. emit messageWidgetChange(MessageBox::T_Show);
  77. QWidget::showEvent(event);
  78. }
  79. void MessageBox::hideEvent(QHideEvent *event)
  80. {
  81. if (isVisible()) {
  82. if (m_Private->m_Timer->isActive()) {
  83. m_Private->m_Timer->stop();
  84. }
  85. emit messageWidgetChange(MessageBox::T_Hide);
  86. }
  87. QWidget::hideEvent(event);
  88. }
  89. void MessageBox::paintEvent(QPaintEvent *event)
  90. {
  91. QPainter painter(this);
  92. painter.fillRect(rect(), QColor(0, 0, 0, 80));
  93. if (!m_Private->m_BackgroundPixmap.isNull()) {
  94. if (m_Private->m_Reload) {
  95. m_Private->m_Reload = false;
  96. setBackgroundBmpPath(m_Private->m_BackgroundPath);
  97. }
  98. int x = (width() - m_Private->m_BackgroundPixmap->width()) >> 1;
  99. int y = (height() - m_Private->m_BackgroundPixmap->height()) >> 1;
  100. painter.drawPixmap(x, y, *m_Private->m_BackgroundPixmap);
  101. }
  102. if (!m_Private->m_Text.isEmpty()) {
  103. painter.setFont(m_Private->m_Font);
  104. painter.setPen(Qt::white);
  105. painter.drawText(rect(), Qt::AlignCenter, QObject::tr(m_Private->m_Text.toLocal8Bit().constData()));
  106. }
  107. QWidget::paintEvent(event);
  108. }
  109. void MessageBox::mouseReleaseEvent(QMouseEvent *event)
  110. {
  111. if (m_Private->m_AutoHide && isVisible()) {
  112. setVisible(false);
  113. }
  114. if (m_Private->m_Timer->isActive()) {
  115. m_Private->m_Timer->stop();
  116. }
  117. QWidget::mouseReleaseEvent(event);
  118. }
  119. MessageBoxPrivate::MessageBoxPrivate(MessageBox *parent)
  120. : m_Parent(parent)
  121. {
  122. m_Timer = NULL;
  123. m_AutoHide = true;
  124. m_Reload = false;
  125. initialize();
  126. connectAllSlots();
  127. }
  128. MessageBoxPrivate::~MessageBoxPrivate()
  129. {
  130. }
  131. void MessageBoxPrivate::initialize()
  132. {
  133. m_BackgroundPixmap.reset(NULL);
  134. m_Text.clear();
  135. m_Timer = new QTimer(m_Parent);
  136. m_Timer->setSingleShot(true);
  137. m_Timer->setInterval(3000);
  138. }
  139. void MessageBoxPrivate::connectAllSlots()
  140. {
  141. Qt::ConnectionType type = static_cast<Qt::ConnectionType>(Qt::UniqueConnection | Qt::AutoConnection);
  142. QObject::connect(m_Timer, SIGNAL(timeout()),
  143. m_Parent, SLOT(onTimeOut()),
  144. type);
  145. }
  146. void MessageBox::onTimeOut()
  147. {
  148. if (m_Private->m_AutoHide) {
  149. setVisible(false);
  150. }
  151. emit messageShowTimeout();
  152. }