Dialog.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include "Dialog.h"
  2. #include "Widgets/Button.h"
  3. #include "Common/StyleSheet.h"
  4. #include "Common/AutoWrap.h"
  5. #include <QDialog>
  6. #include <QDebug>
  7. #include <QEvent>
  8. void MessageBoxBase::setUpUi(const QString &title, const QString &content, QWidget *parent)
  9. {
  10. m_content = content;
  11. m_selfWidget = parent; //真正的窗口子类
  12. QString magic = title;
  13. for (int i = 0; i < title.count(); ++i) {
  14. magic.append(" ");
  15. }
  16. titleLabel = new QLabel(magic, parent);
  17. contentLabel = new QLabel(content, parent);
  18. contentLabel->setWordWrap(true);
  19. buttonGroup = new QFrame(parent);
  20. yesButton = new PrimaryPushButton(QObject::tr("OK"), buttonGroup);
  21. cancelButton = new QPushButton(QObject::tr("Cancel"), buttonGroup);
  22. vBoxLayout = new QVBoxLayout(parent);
  23. textLayout = new QVBoxLayout();
  24. buttonLayout = new QHBoxLayout(buttonGroup);
  25. initWidget();
  26. }
  27. void MessageBoxBase::adjustText()
  28. {
  29. int w = 0;
  30. int chars;
  31. if (!m_selfWidget) {
  32. return;
  33. }
  34. if (m_selfWidget->isWindow()) {
  35. if (m_selfWidget->parent()) {
  36. w = qMax(titleLabel->width(), ((QWidget *)m_selfWidget->parent())->width());
  37. chars = qMax(qMin(w / 9, 140), 30);
  38. } else {
  39. chars = 100;
  40. }
  41. } else {
  42. w = qMax(titleLabel->width(), m_selfWidget->window()->width());
  43. chars = qMax(qMin(w / 9, 100), 30);
  44. }
  45. contentLabel->setText(TextWrap::wrap(m_content, chars, false).first);
  46. }
  47. void MessageBoxBase::initWidget()
  48. {
  49. setQss();
  50. initLayout();
  51. // fixes https://github.com/zhiyiYo/PyQt-Fluent-Widgets/issues/19
  52. yesButton->setAttribute(Qt::WA_LayoutUsesWidgetRect);
  53. cancelButton->setAttribute(Qt::WA_LayoutUsesWidgetRect);
  54. yesButton->setFocus();
  55. buttonGroup->setFixedHeight(81);
  56. }
  57. void MessageBoxBase::setQss()
  58. {
  59. // 设置层叠样式
  60. titleLabel->setObjectName("titleLabel");
  61. contentLabel->setObjectName("contentLabel");
  62. buttonGroup->setObjectName("buttonGroup");
  63. cancelButton->setObjectName("cancelButton");
  64. FluentStyleSheet::apply("DIALOG", m_selfWidget);
  65. yesButton->adjustSize();
  66. cancelButton->adjustSize();
  67. }
  68. void MessageBoxBase::initLayout()
  69. {
  70. vBoxLayout->setSpacing(0);
  71. vBoxLayout->setContentsMargins(0, 0, 0, 0);
  72. vBoxLayout->addLayout(textLayout, 1);
  73. vBoxLayout->addWidget(buttonGroup, 0, Qt::AlignBottom);
  74. vBoxLayout->setSizeConstraint(QVBoxLayout::SetMinimumSize);
  75. textLayout->setSpacing(12);
  76. textLayout->setContentsMargins(24, 24, 24, 24);
  77. textLayout->addWidget(titleLabel, 0, Qt::AlignTop);
  78. textLayout->addWidget(contentLabel, 0, Qt::AlignTop);
  79. buttonLayout->setSpacing(12);
  80. buttonLayout->setContentsMargins(24, 24, 24, 24);
  81. buttonLayout->addWidget(yesButton, 1, Qt::AlignVCenter);
  82. buttonLayout->addWidget(cancelButton, 1, Qt::AlignVCenter);
  83. }
  84. Dialog::Dialog(const QString &title, const QString &content, QWidget *parent) : QDialog(parent), MessageBoxBase()
  85. {
  86. //补丁使用
  87. setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint);
  88. setFixedSize(240, 192);
  89. setUpUi(title, content, this);
  90. connect(yesButton, &PrimaryPushButton::clicked, this, &Dialog::onYesButtonClicked);
  91. connect(cancelButton, &QPushButton::clicked, this, &Dialog::onCancelButtonClicked);
  92. m_windowTitleLabel = new QLabel(title, this);
  93. vBoxLayout->insertWidget(0, m_windowTitleLabel, 0, Qt::AlignTop);
  94. m_windowTitleLabel->setObjectName("windowTitleLabel");
  95. FluentStyleSheet::apply("DIALOG", this);
  96. }
  97. void Dialog::setTitleBarVisible(bool isVisible)
  98. {
  99. m_windowTitleLabel->setVisible(isVisible);
  100. }
  101. void Dialog::onYesButtonClicked()
  102. {
  103. this->accept();
  104. emit yesSignal();
  105. }
  106. void Dialog::onCancelButtonClicked()
  107. {
  108. this->reject();
  109. emit cancelSignal();
  110. }
  111. MessageBox::MessageBox(const QString &title, const QString &content, QWidget *parent)
  112. : MaskDialogBase(parent), MessageBoxBase()
  113. {
  114. //补丁使用
  115. setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint);
  116. setUpUi(title, content, this->widget);
  117. connect(yesButton, &PrimaryPushButton::clicked, this, &MessageBox::onYesButtonClicked);
  118. connect(cancelButton, &QPushButton::clicked, this, &MessageBox::onCancelButtonClicked);
  119. setShadowEffect(60, QPointF(0, 10), QColor(0, 0, 0, 50));
  120. setMaskColor(QColor(0, 0, 0, 76));
  121. hBoxLayout()->removeWidget(widget);
  122. hBoxLayout()->addWidget(widget, 1, Qt::AlignCenter);
  123. buttonGroup->setMinimumWidth(280);
  124. widget->setFixedSize(qMax(contentLabel->width(), titleLabel->width()) + 48,
  125. contentLabel->y() + contentLabel->height() + 205);
  126. }
  127. bool MessageBox::eventFilter(QObject *watched, QEvent *event)
  128. {
  129. if (watched == this->window()) {
  130. if (event->type() == QEvent::Resize) {
  131. adjustText();
  132. }
  133. }
  134. return MaskDialogBase::eventFilter(watched, event);
  135. }
  136. void MessageBox::onYesButtonClicked()
  137. {
  138. this->accept();
  139. emit yesSignal();
  140. }
  141. void MessageBox::onCancelButtonClicked()
  142. {
  143. this->reject();
  144. emit cancelSignal();
  145. }