#include "ExpertManageView.h" #include "QFDIcon.h" #include "MainWindow.h" #include "ExpertListWidget.h" #include "ExpertManager.h" #include "QFDAlert.h" #include #include #include #include #include #include #include #include ExpertManageView::ExpertManageView(QWidget *parent) : QWidget(parent) { } void ExpertManageView::showEvent(QShowEvent *event) { QWidget::showEvent(event); init(); } void ExpertManageView::hideEvent(QHideEvent *event) { QWidget::hideEvent(event); hideExpertInfoWidget(); } void ExpertManageView::init() { if (m_initilized == false) { initialize(); initLayout(); connectSignalsAndSlots(); m_initilized = true; } refreshList(); } void ExpertManageView::initialize() { m_vBoxLayout = new QVBoxLayout(this); m_titleLabel = new QLabel(this); m_titleLabel->setText("专家列表"); QFont ft("Microsoft YaHei", 12); m_titleLabel->setFont(ft); m_hBoxLayout = new QHBoxLayout(); m_searchLineEdit = new LineEdit(this); m_searchLineEdit->setIsClearButtonEnabled(true); m_searchLineEdit->setPlaceholderText("搜索"); m_searchLineEdit->setMinimumWidth(300); m_addExpertButton = new PushButton("添加", NEWFLICON(FluentIcon, ADD), this); m_detailButton = new ToolButton(NEWFLICON(QFDIcon, Detail), this); m_detailButton->setToolTip("详细信息"); m_deleteButton = new ToolButton(NEWFLICON(FluentIcon, DELETE), this); m_deleteButton->setToolTip("删除用户"); m_expertListWidget = new ExpertListWidget(this); } void ExpertManageView::initLayout() { m_vBoxLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft); m_vBoxLayout->setContentsMargins(15, 10, 10, 15); m_vBoxLayout->addLayout(m_hBoxLayout); m_hBoxLayout->setContentsMargins(10, 0, 10, 0); m_hBoxLayout->addWidget(m_titleLabel); m_hBoxLayout->addSpacing(15); m_hBoxLayout->addWidget(m_searchLineEdit, 0, Qt::AlignLeft); m_hBoxLayout->addWidget(m_addExpertButton, 1, Qt::AlignLeft); m_hBoxLayout->addStretch(); m_hBoxLayout->addWidget(m_detailButton); m_hBoxLayout->addWidget(m_deleteButton); setListButtonHidden(true); m_vBoxLayout->addWidget(m_expertListWidget); } void ExpertManageView::connectSignalsAndSlots() { connect(m_addExpertButton, &PushButton::clicked, this, &ExpertManageView::slotAddExpertClicked); connect(m_detailButton, &PushButton::clicked, this, &ExpertManageView::slotDetailClicked); connect(m_deleteButton, &PushButton::clicked, this, &ExpertManageView::slotDeleteClicked); connect(m_expertListWidget, &ExpertListWidget::signalSelectionChanged, this, &ExpertManageView::slotListSelectionChanged); connect(m_expertListWidget, &ExpertListWidget::siganlItemDoubleClicked, this, &ExpertManageView::slotListItemDoubleClicked); connect(m_searchLineEdit, &LineEdit::textChanged, this, &ExpertManageView::slotSearchTextChanged); } void ExpertManageView::setListButtonHidden(bool hidden) { m_detailButton->setHidden(hidden); m_deleteButton->setHidden(hidden); } void ExpertManageView::showExpertInfoWidget(ExpertInfoWidget::Mode mode) { if (m_expertInfoWidget == nullptr) { m_expertInfoWidget = new ExpertInfoWidget(this); connect(m_expertInfoWidget, &ExpertInfoWidget::sigConfirm, this, &ExpertManageView::slotConfirmAddExpert); } if (m_expertInfoWidget->isVisible() == false) { m_expertInfoWidget->show(); m_expertInfoWidget->setFocus(Qt::NoFocusReason); } m_expertInfoWidget->setMode(mode); m_expertInfoWidget->clearInputs(); } void ExpertManageView::hideExpertInfoWidget() { if (m_expertInfoWidget && m_expertInfoWidget->isVisible()) { m_expertInfoWidget->close(); } } void ExpertManageView::showExpertInfo(QFUser *user) { showExpertInfoWidget(); m_expertInfoWidget->setUser(user); } void ExpertManageView::refreshList() { qDeleteAll(m_userList); m_userList.clear(); if (!UserService().QueryUserListNotAdmin(&m_userList)) { QFDAlert::showAlertWithCode(QF_CODE_DATA_ERROR, this); return; } m_expertListWidget->showUsers(searchResult()); } QList ExpertManageView::searchResult() const { QList list; for (QFUser *user : m_userList) { if (user->userName.contains(m_searchLineEdit->text())) { list.append(user); } } return list; } void ExpertManageView::slotAddExpertClicked() { showExpertInfoWidget(ExpertInfoWidget::Create); m_expertListWidget->clearSelection(); } void ExpertManageView::slotDetailClicked() { QList list = searchResult(); int row = m_expertListWidget->selectedRow(); if (row < 0 || row >= list.count()) { return; } QFUser *user = list[row]; showExpertInfo(user); } void ExpertManageView::slotDeleteClicked() { hideExpertInfoWidget(); MessageBox *m = new MessageBox("删除专家信息", "删除后不可恢复,确认删除?", topLevelWidget()); connect(m, &MessageBox::yesSignal, this, &ExpertManageView::slotConfirmDeleteExpert); m->exec(); } void ExpertManageView::slotListSelectionChanged() { setListButtonHidden(!m_expertListWidget->hasSelectedItem()); } void ExpertManageView::slotListItemDoubleClicked(int row) { QList list = searchResult(); if (row < 0 || row >= list.count()) { return; } QFUser *user = list[row]; showExpertInfo(user); } void ExpertManageView::slotSearchTextChanged() { m_expertListWidget->showUsers(searchResult()); } void ExpertManageView::slotConfirmAddExpert() { QFUser *user = m_expertInfoWidget->user(); int code = ExpertManager::addUser(*user); QFDAlert::showAlertWithCode(code, this); if (code == QF_CODE_ADD_USER_SUCCEEDED) { hideExpertInfoWidget(); refreshList(); } } void ExpertManageView::slotConfirmDeleteExpert() { QList list = searchResult(); int row = m_expertListWidget->selectedRow(); if (row < 0 || row >= list.count()) { return; } QFUser *user = list[row]; int code = ExpertManager::deleteUser(user->id); QFDAlert::showAlertWithCode(code, this); if (code == QF_CODE_DELETE_USER_SUCCEEDED) { refreshList(); } qDebug() << "---" << code << user->id; }