CreateSchemeWidget.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "CreateSchemeWidget.h"
  2. #include <Widgets/Button.h>
  3. #include <Widgets/LineEdit.h>
  4. #include <QBoxLayout>
  5. #include <QLabel>
  6. #include <QDebug>
  7. #include <QFileDialog>
  8. static const QString kNullImageTag = "无效图片";
  9. CreateSchemeWidget::CreateSchemeWidget(QWidget *parent) : QDialog(parent)
  10. {
  11. initWindow();
  12. initialize();
  13. initLayout();
  14. connectSignalsAndSlots();
  15. }
  16. void CreateSchemeWidget::clearInputs()
  17. {
  18. m_nameEdit->clear();
  19. m_schemeEdit->clear();
  20. m_picLine->clear();
  21. }
  22. void CreateSchemeWidget::showEvent(QShowEvent *e)
  23. {
  24. QDialog::showEvent(e);
  25. clearInputs();
  26. }
  27. void CreateSchemeWidget::initWindow()
  28. {
  29. setWindowTitle("新建方案");
  30. setModal(true);
  31. setWindowFlags(Qt::Dialog);
  32. setWindowFlag(Qt::WindowContextHelpButtonHint, false);
  33. resize(370, 350);
  34. }
  35. void CreateSchemeWidget::initialize()
  36. {
  37. m_vBoxLayout = new QVBoxLayout(this);
  38. m_gridLayout = new QGridLayout();
  39. m_nameLabel = new QLabel("方案名称:", this);
  40. m_nameLabel->setFixedHeight(44);
  41. m_nameEdit = new LineEdit(this);
  42. m_nameEdit->setFixedWidth(250);
  43. m_schemeLabel = new QLabel("方案描述:", this);
  44. m_schemeLabel->setFixedHeight(35);
  45. m_schemeEdit = new TextEdit(this);
  46. m_schemeEdit->setObjectName("textEdit");
  47. m_schemeEdit->setFixedWidth(250);
  48. m_schemeEdit->setFixedHeight(150);
  49. m_picButton = new PushButton("方案图片", this);
  50. m_picLine = new LineEdit(this);
  51. m_picLine->setFixedWidth(250);
  52. m_picLine->setEnabled(false);
  53. m_hBoxLayout = new QHBoxLayout();
  54. m_confirButton = new PushButton("生成", this);
  55. m_confirButton->setEnabled(false);
  56. }
  57. void CreateSchemeWidget::initLayout()
  58. {
  59. m_vBoxLayout->setMargin(15);
  60. m_vBoxLayout->addLayout(m_gridLayout);
  61. m_gridLayout->addWidget(m_nameLabel, 0, 0, 1, 1, Qt::AlignRight);
  62. m_gridLayout->addWidget(m_nameEdit, 0, 1, 1, 2, Qt::AlignLeft);
  63. m_gridLayout->addWidget(m_schemeLabel, 1, 0, 1, 1, Qt::AlignRight | Qt::AlignTop);
  64. m_gridLayout->addWidget(m_schemeEdit, 1, 1, 1, 2, Qt::AlignLeft);
  65. m_gridLayout->addWidget(m_picButton, 2, 0, 1, 1, Qt::AlignRight);
  66. m_gridLayout->addWidget(m_picLine, 2, 1, 1, 2, Qt::AlignLeft);
  67. m_vBoxLayout->addSpacing(20);
  68. m_vBoxLayout->addStretch();
  69. m_vBoxLayout->addLayout(m_hBoxLayout);
  70. m_hBoxLayout->addStretch();
  71. m_hBoxLayout->addWidget(m_confirButton);
  72. }
  73. void CreateSchemeWidget::connectSignalsAndSlots()
  74. {
  75. connect(m_picButton, &QPushButton::clicked, this, &CreateSchemeWidget::slotSelectPic);
  76. connect(m_nameEdit, &LineEdit::textChanged, this, &CreateSchemeWidget::slotContentChanged);
  77. connect(m_schemeEdit, &TextEdit::textChanged, this, &CreateSchemeWidget::slotContentChanged);
  78. connect(m_picLine, &LineEdit::textChanged, this, &CreateSchemeWidget::slotContentChanged);
  79. connect(m_confirButton, &PushButton::clicked, this, &CreateSchemeWidget::slotConfirmed);
  80. }
  81. void CreateSchemeWidget::slotSelectPic()
  82. {
  83. QString fileName = QFileDialog::getOpenFileName(nullptr, "Open Image", "", "images(*.bmp *.png *.jpg *.webp)");
  84. QFile file(fileName);
  85. file.open(QIODevice::ReadOnly);
  86. QByteArray data = file.readAll();
  87. QPixmap pixmap;
  88. pixmap.loadFromData(data);
  89. if (pixmap.isNull()) {
  90. m_picLine->setText(kNullImageTag);
  91. } else {
  92. m_picLine->setText(fileName);
  93. }
  94. }
  95. void CreateSchemeWidget::slotContentChanged()
  96. {
  97. bool valid = m_nameEdit->text().length() > 0 && m_schemeEdit->toPlainText().length() > 0
  98. && m_picLine->text().length() > 0 && m_picLine->text() != kNullImageTag;
  99. m_confirButton->setEnabled(valid);
  100. }
  101. void CreateSchemeWidget::slotConfirmed()
  102. {
  103. emit sigSchemeInfoConfirmed(m_nameEdit->text(), m_schemeEdit->toPlainText(), m_picLine->text());
  104. close();
  105. }