#include "CreateSchemeWidget.h" #include #include #include #include #include #include static const QString kNullImageTag = "无效图片"; CreateSchemeWidget::CreateSchemeWidget(QWidget *parent) : QDialog(parent) { initWindow(); initialize(); initLayout(); connectSignalsAndSlots(); } void CreateSchemeWidget::clearInputs() { m_nameEdit->clear(); m_schemeEdit->clear(); m_picLine->clear(); } void CreateSchemeWidget::showEvent(QShowEvent *e) { QDialog::showEvent(e); clearInputs(); } void CreateSchemeWidget::initWindow() { setWindowTitle("新建方案"); setModal(true); setWindowFlags(Qt::Dialog); setWindowFlag(Qt::WindowContextHelpButtonHint, false); resize(370, 350); } void CreateSchemeWidget::initialize() { m_vBoxLayout = new QVBoxLayout(this); m_gridLayout = new QGridLayout(); m_nameLabel = new QLabel("方案名称:", this); m_nameLabel->setFixedHeight(44); m_nameEdit = new LineEdit(this); m_nameEdit->setFixedWidth(250); m_schemeLabel = new QLabel("方案描述:", this); m_schemeLabel->setFixedHeight(35); m_schemeEdit = new TextEdit(this); m_schemeEdit->setObjectName("textEdit"); m_schemeEdit->setFixedWidth(250); m_schemeEdit->setFixedHeight(150); m_picButton = new PushButton("方案图片", this); m_picLine = new LineEdit(this); m_picLine->setFixedWidth(250); m_picLine->setEnabled(false); m_hBoxLayout = new QHBoxLayout(); m_confirButton = new PushButton("生成", this); m_confirButton->setEnabled(false); } void CreateSchemeWidget::initLayout() { m_vBoxLayout->setMargin(15); m_vBoxLayout->addLayout(m_gridLayout); m_gridLayout->addWidget(m_nameLabel, 0, 0, 1, 1, Qt::AlignRight); m_gridLayout->addWidget(m_nameEdit, 0, 1, 1, 2, Qt::AlignLeft); m_gridLayout->addWidget(m_schemeLabel, 1, 0, 1, 1, Qt::AlignRight | Qt::AlignTop); m_gridLayout->addWidget(m_schemeEdit, 1, 1, 1, 2, Qt::AlignLeft); m_gridLayout->addWidget(m_picButton, 2, 0, 1, 1, Qt::AlignRight); m_gridLayout->addWidget(m_picLine, 2, 1, 1, 2, Qt::AlignLeft); m_vBoxLayout->addSpacing(20); m_vBoxLayout->addStretch(); m_vBoxLayout->addLayout(m_hBoxLayout); m_hBoxLayout->addStretch(); m_hBoxLayout->addWidget(m_confirButton); } void CreateSchemeWidget::connectSignalsAndSlots() { connect(m_picButton, &QPushButton::clicked, this, &CreateSchemeWidget::slotSelectPic); connect(m_nameEdit, &LineEdit::textChanged, this, &CreateSchemeWidget::slotContentChanged); connect(m_schemeEdit, &TextEdit::textChanged, this, &CreateSchemeWidget::slotContentChanged); connect(m_picLine, &LineEdit::textChanged, this, &CreateSchemeWidget::slotContentChanged); connect(m_confirButton, &PushButton::clicked, this, &CreateSchemeWidget::slotConfirmed); } void CreateSchemeWidget::slotSelectPic() { QString fileName = QFileDialog::getOpenFileName(nullptr, "Open Image", "", "images(*.bmp *.png *.jpg *.webp)"); QFile file(fileName); file.open(QIODevice::ReadOnly); QByteArray data = file.readAll(); QPixmap pixmap; pixmap.loadFromData(data); if (pixmap.isNull()) { m_picLine->setText(kNullImageTag); } else { m_picLine->setText(fileName); } } void CreateSchemeWidget::slotContentChanged() { bool valid = m_nameEdit->text().length() > 0 && m_schemeEdit->toPlainText().length() > 0 && m_picLine->text().length() > 0 && m_picLine->text() != kNullImageTag; m_confirButton->setEnabled(valid); } void CreateSchemeWidget::slotConfirmed() { emit sigSchemeInfoConfirmed(m_nameEdit->text(), m_schemeEdit->toPlainText(), m_picLine->text()); close(); }