#include "ExpertInfoWidget.h" #include #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->setDateTime(QDateTime::currentDateTime()); m_noteTextEdit->clear(); } void ExpertInfoWidget::setUser(QFUser *user) { m_user = user; if (m_user == nullptr) { clearInputs(); return; } m_idLineEdit->setText(user->userNo); m_nameLineEdit->setText(user->userName); m_companyLineEdit->setText(user->workPosition); m_jobLineEdit->setText(user->post); m_majorLineEdit->setText(user->major); m_contactInfoLineEdit->setText(user->phone); QDateTime dt = QDateTime::fromString(user->writeTime, "yyyy/M/d H:mm"); m_timeLineEdit->setDateTime(dt); m_noteTextEdit->setText(user->remark); qDebug() << __FUNCTION__ << __LINE__ << user->writeTime << dt << user->remark; } QFUser *ExpertInfoWidget::user() const { if (m_mode == Create) { QFUser *user = new QFUser(m_idLineEdit->text(), m_passwordLineEdit->text()); user->password = m_passwordLineEdit->text(); user->userName = m_nameLineEdit->text(); user->writeTime = m_timeLineEdit->text(); user->role = QFUser::Expert; user->remark = m_noteTextEdit->toPlainText(); return user; } else { return m_user; } } void ExpertInfoWidget::initWindow() { setWindowFlags(Qt::Window); setWindowFlag(Qt::WindowMinMaxButtonsHint, false); setFixedWidth(420); // setModal(true); // setWindowFlags(Qt::Dialog); // setWindowFlag(Qt::WindowContextHelpButtonHint, false); // resize(400, 250); } 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); } /// 根据 mode 更新页面 void ExpertInfoWidget::updateState() { int h = height(); QString t = windowTitle(); switch (m_mode) { case Create: { t = "添加专家"; h = 350; break; } case Update: { t = "修改专家信息"; h = 350; break; } case Info: { t = "专家信息"; h = 425; break; } } setFixedHeight(h); setWindowTitle(t); setEditable(m_mode != Info); setDetailsHideen(m_mode == Create); m_passwordLabel->setHidden(m_mode == Info); m_passwordLineEdit->setHidden(m_mode == Info); m_confirmButton->setHidden(m_mode == Info); m_cancelButton->setHidden(m_mode == Info); } /// 设置用户详情是否可见 /// 单位、职务、专业、联系方式 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); } /// 设置所有 Edit 的可编辑状态 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_timeLineEdit->upButton->setEnabled(editable); m_timeLineEdit->downButton->setEnabled(editable); m_noteTextEdit->setReadOnly(!editable); } void ExpertInfoWidget::slotConfirm() { emit sigConfirm(); } void ExpertInfoWidget::slotCancel() { close(); }