#include "ExpertInfoWidget.h" #include #include #include #include #include #include #include ExpertInfoWidget::ExpertInfoWidget(QWidget *parent) : QWidget(parent) { initWindow(); initialize(); initLayout(); connectSignalsAndSlots(); updateState(); } void ExpertInfoWidget::showEvent(QShowEvent *event) { QWidget::showEvent(event); clearFocus(); } void ExpertInfoWidget::hideEvent(QHideEvent *event) { QWidget::hideEvent(event); } void ExpertInfoWidget::setMode(ExpertInfoWidget::Mode mode) { m_mode = mode; updateState(); } void ExpertInfoWidget::clearInputs() { m_idLineEdit->clear(); m_passwordLineEdit->clear(); m_nameLineEdit->clear(); m_companyLineEdit->clear(); m_jobLineEdit->clear(); m_majorLineEdit->clear(); m_contactInfoLineEdit->clear(); m_timeLineEdit->clear(); m_noteTextEdit->clear(); } void ExpertInfoWidget::initWindow() { setMaximumWidth(400); setWindowFlags(Qt::Window); setWindowFlag(Qt::WindowMinMaxButtonsHint, false); } void ExpertInfoWidget::initialize() { int labelHeight = 35; int editWidth = 300; m_vBoxLayout = new QVBoxLayout(this); m_gridLayout = new QGridLayout(); m_idLabel = new QLabel(this); m_idLabel->setText("账号ID:"); m_idLabel->setFixedHeight(labelHeight); m_idLineEdit = new LineEdit(this); m_idLineEdit->setFixedWidth(editWidth); m_idLineEdit->setPlaceholderText("请输入a-z/A-Z/0-9数字字母组合"); m_passwordLabel = new QLabel(this); m_passwordLabel->setText("用户密码:"); m_passwordLabel->setFixedHeight(labelHeight); m_passwordLineEdit = new PasswordLineEdit(this); m_passwordLineEdit->setFixedWidth(editWidth); m_passwordLineEdit->setPlaceholderText("请输入密码"); m_nameLabel = new QLabel(this); m_nameLabel->setText("专家名:"); m_nameLabel->setFixedHeight(labelHeight); m_nameLineEdit = new LineEdit(this); m_nameLineEdit->setFixedWidth(editWidth); m_nameLineEdit->setPlaceholderText("请输入专家名"); m_companyLabel = new QLabel(this); m_companyLabel->setText("单位:"); m_companyLabel->setFixedHeight(labelHeight); m_companyLineEdit = new LineEdit(this); m_companyLineEdit->setFixedWidth(editWidth); m_jobLabel = new QLabel(this); m_jobLabel->setText("职务:"); m_jobLabel->setFixedHeight(labelHeight); m_jobLineEdit = new LineEdit(this); m_jobLineEdit->setFixedWidth(editWidth); m_majorLabel = new QLabel(this); m_majorLabel->setText("专业:"); m_majorLabel->setFixedHeight(labelHeight); m_majorLineEdit = new LineEdit(this); m_majorLineEdit->setFixedWidth(editWidth); m_contactInfoLabel = new QLabel(this); m_contactInfoLabel->setText("联系方式:"); m_contactInfoLabel->setFixedHeight(labelHeight); m_contactInfoLineEdit = new LineEdit(this); m_contactInfoLineEdit->setFixedWidth(editWidth); m_timeLabel = new QLabel(this); m_timeLabel->setText("填写时间:"); m_timeLabel->setFixedHeight(labelHeight); m_timeLineEdit = new DateTimeEdit(this); m_timeLineEdit->setFixedWidth(editWidth); m_noteLabel = new QLabel(this); m_noteLabel->setText("备注:"); m_noteLabel->setFixedHeight(labelHeight); m_noteTextEdit = new TextEdit(this); m_noteTextEdit->setFixedWidth(editWidth); m_noteTextEdit->setFixedHeight(100); m_hBoxLayout = new QHBoxLayout(); m_confirmButton = new PushButton("确认", this); m_cancelButton = new PushButton("取消", this); } void ExpertInfoWidget::initLayout() { m_vBoxLayout->setContentsMargins(20, 10, 20, 10); m_vBoxLayout->addLayout(m_gridLayout); m_gridLayout->addWidget(m_idLabel, 0, 0, 1, 1, Qt::AlignRight); m_gridLayout->addWidget(m_idLineEdit, 0, 1, 1, 2, Qt::AlignLeft); m_gridLayout->addWidget(m_passwordLabel, 1, 0, 1, 1, Qt::AlignRight); m_gridLayout->addWidget(m_passwordLineEdit, 1, 1, Qt::AlignLeft); m_gridLayout->addWidget(m_nameLabel, 2, 0, 1, 1, Qt::AlignRight); m_gridLayout->addWidget(m_nameLineEdit, 2, 1, 1, 1, Qt::AlignLeft); m_gridLayout->addWidget(m_companyLabel, 3, 0, 1, 1, Qt::AlignRight); m_gridLayout->addWidget(m_companyLineEdit, 3, 1, 1, 1, Qt::AlignLeft); m_gridLayout->addWidget(m_jobLabel, 4, 0, 1, 1, Qt::AlignRight); m_gridLayout->addWidget(m_jobLineEdit, 4, 1, 1, 1, Qt::AlignLeft); m_gridLayout->addWidget(m_majorLabel, 5, 0, 1, 1, Qt::AlignRight); m_gridLayout->addWidget(m_majorLineEdit, 5, 1, 1, 1, Qt::AlignLeft); m_gridLayout->addWidget(m_contactInfoLabel, 6, 0, 1, 1, Qt::AlignRight); m_gridLayout->addWidget(m_contactInfoLineEdit, 6, 1, 1, 1, Qt::AlignLeft); m_gridLayout->addWidget(m_timeLabel, 7, 0, 1, 1, Qt::AlignRight); m_gridLayout->addWidget(m_timeLineEdit, 7, 1, 1, 1, Qt::AlignLeft); m_gridLayout->addWidget(m_noteLabel, 8, 0, 1, 1, Qt::AlignRight | Qt::AlignTop); m_gridLayout->addWidget(m_noteTextEdit, 8, 1, 1, 1, Qt::AlignLeft); m_vBoxLayout->addStretch(); m_vBoxLayout->addLayout(m_hBoxLayout); m_hBoxLayout->addStretch(); m_hBoxLayout->addWidget(m_confirmButton); m_hBoxLayout->addWidget(m_cancelButton); } void ExpertInfoWidget::connectSignalsAndSlots() { connect(m_confirmButton, &PushButton::clicked, this, &ExpertInfoWidget::slotConfirm); connect(m_cancelButton, &PushButton::clicked, this, &ExpertInfoWidget::slotCancel); } void ExpertInfoWidget::updateState() { int h = height(); QString t = windowTitle(); switch (m_mode) { case Add: { t = "添加用户"; h = 300; break; } case Update: { t = "修改用户信息"; h = 300; break; } case Read: { t = "用户信息"; h = 450; break; } } setMaximumHeight(h); setWindowTitle(t); setEditable(m_mode != Read); setDetailsHideen(m_mode == Add); // update(); repaint(); } void ExpertInfoWidget::setDetailsHideen(bool hidden) { m_companyLabel->setHidden(hidden); m_companyLineEdit->setHidden(hidden); m_jobLabel->setHidden(hidden); m_jobLineEdit->setHidden(hidden); m_majorLabel->setHidden(hidden); m_majorLineEdit->setHidden(hidden); m_contactInfoLabel->setHidden(hidden); m_contactInfoLineEdit->setHidden(hidden); } void ExpertInfoWidget::setEditable(bool editable) { m_idLineEdit->setReadOnly(!editable); m_passwordLineEdit->setReadOnly(!editable); m_nameLineEdit->setReadOnly(!editable); m_companyLineEdit->setReadOnly(!editable); m_jobLineEdit->setReadOnly(!editable); m_majorLineEdit->setReadOnly(!editable); m_idLineEdit->setReadOnly(!editable); m_contactInfoLineEdit->setReadOnly(!editable); m_timeLineEdit->setReadOnly(!editable); m_noteTextEdit->setReadOnly(!editable); m_confirmButton->setHidden(!editable); m_cancelButton->setHidden(!editable); } void ExpertInfoWidget::slotConfirm() { close(); } void ExpertInfoWidget::slotCancel() { close(); }