Message_box.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include <QLabel>
  2. #include <QPushButton>
  3. #include <QMessageBox>
  4. #include <QCheckBox>
  5. #include <QHBoxLayout>
  6. #include <QEvent>
  7. #include <QApplication>
  8. #include "Message_box.h"
  9. MessageBox::MessageBox(QWidget *parent, const QString &title, const QString &text,
  10. QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
  11. : CustomWindow(parent)
  12. {
  13. setWindowIcon(QIcon(":/Images/logo"));
  14. setWindowTitle(title);
  15. setMinimumSize(300, 130);
  16. setMinimizeVisible(false);
  17. setMaximizeVisible(false);
  18. setWidgetResizable(false);
  19. m_pButtonBox = new QDialogButtonBox(this);
  20. m_pButtonBox->setStandardButtons(QDialogButtonBox::StandardButtons(int(buttons)));
  21. setDefaultButton(defaultButton);
  22. QPushButton *pYesButton = m_pButtonBox->button(QDialogButtonBox::Yes);
  23. if (pYesButton != NULL)
  24. {
  25. pYesButton->setObjectName("blueButton");
  26. pYesButton->setStyle(QApplication::style());
  27. }
  28. m_pIconLabel = new QLabel(this);
  29. m_pLabel = new QLabel(this);
  30. QPixmap pixmap(":/Images/information");
  31. m_pIconLabel->setPixmap(pixmap);
  32. m_pIconLabel->setFixedSize(35, 35);
  33. m_pIconLabel->setScaledContents(true);
  34. m_pLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
  35. m_pLabel->setObjectName("whiteLabel");
  36. m_pLabel->setOpenExternalLinks(true);
  37. m_pLabel->setText(text);
  38. m_pGridLayout = new QGridLayout();
  39. m_pGridLayout->addWidget(m_pIconLabel, 0, 0, 2, 1, Qt::AlignTop);
  40. m_pGridLayout->addWidget(m_pLabel, 0, 1, 2, 1);
  41. m_pGridLayout->addWidget(m_pButtonBox, m_pGridLayout->rowCount(), 0, 1, m_pGridLayout->columnCount());
  42. m_pGridLayout->setSizeConstraint(QLayout::SetNoConstraint);
  43. m_pGridLayout->setHorizontalSpacing(10);
  44. m_pGridLayout->setVerticalSpacing(10);
  45. m_pGridLayout->setContentsMargins(10, 10, 10, 10);
  46. m_pLayout->addLayout(m_pGridLayout);
  47. translateUI();
  48. connect(m_pButtonBox, SIGNAL(clicked(QAbstractButton*)), this, SLOT(onButtonClicked(QAbstractButton*)));
  49. }
  50. MessageBox::~MessageBox()
  51. {
  52. }
  53. void MessageBox::changeEvent(QEvent *event)
  54. {
  55. switch (event->type())
  56. {
  57. case QEvent::LanguageChange:
  58. translateUI();
  59. break;
  60. default:
  61. CustomWindow::changeEvent(event);
  62. }
  63. }
  64. void MessageBox::translateUI()
  65. {
  66. QPushButton *pYesButton = m_pButtonBox->button(QDialogButtonBox::Yes);
  67. if (pYesButton != NULL)
  68. pYesButton->setText(tr("Yes"));
  69. QPushButton *pNoButton = m_pButtonBox->button(QDialogButtonBox::No);
  70. if (pNoButton != NULL)
  71. pNoButton->setText(tr("No"));
  72. QPushButton *pOkButton = m_pButtonBox->button(QDialogButtonBox::Ok);
  73. if (pOkButton != NULL)
  74. pOkButton->setText(tr("Ok"));
  75. QPushButton *pCancelButton = m_pButtonBox->button(QDialogButtonBox::Cancel);
  76. if (pCancelButton != NULL)
  77. pCancelButton->setText(tr("Cancel"));
  78. }
  79. QMessageBox::StandardButton MessageBox::standardButton(QAbstractButton *button) const
  80. {
  81. return (QMessageBox::StandardButton)m_pButtonBox->standardButton(button);
  82. }
  83. QAbstractButton *MessageBox::clickedButton() const
  84. {
  85. return m_pClickedButton;
  86. }
  87. int MessageBox::execReturnCode(QAbstractButton *button)
  88. {
  89. int nResult = m_pButtonBox->standardButton(button);
  90. return nResult;
  91. }
  92. void MessageBox::onButtonClicked(QAbstractButton *button)
  93. {
  94. m_pClickedButton = button;
  95. done(execReturnCode(button));
  96. }
  97. void MessageBox::setDefaultButton(QPushButton *button)
  98. {
  99. if (!m_pButtonBox->buttons().contains(button))
  100. return;
  101. m_pDefaultButton = button;
  102. button->setDefault(true);
  103. button->setFocus();
  104. }
  105. void MessageBox::setDefaultButton(QMessageBox::StandardButton button)
  106. {
  107. setDefaultButton(m_pButtonBox->button(QDialogButtonBox::StandardButton(button)));
  108. }
  109. void MessageBox::setTitle(const QString &title)
  110. {
  111. setWindowTitle(title);
  112. }
  113. void MessageBox::setText(const QString &text)
  114. {
  115. m_pLabel->setText(text);
  116. }
  117. void MessageBox::setIcon(const QString &icon)
  118. {
  119. m_pIconLabel->setPixmap(QPixmap(icon));
  120. }
  121. void MessageBox::addWidget(QWidget *pWidget)
  122. {
  123. m_pLabel->hide();
  124. m_pGridLayout->addWidget(pWidget, 0, 1, 2, 1);
  125. }