#include "HomeView.h" #include "CreateProjWidget.h" #include "ProjectStateWidget.h" #include "ProjectManager.h" #include "QFDAlert.h" #include "QFDIcon.h" #include "ProjectListWidget.h" #include "QFDConfig.h" #include #include #include #include #include #include HomeView::HomeView(QWidget *parent) : QWidget(parent) { } void HomeView::showEvent(QShowEvent *event) { if (m_initilized == false) { initWidgets(); initLayout(); connectSignalsAndSlots(); m_initilized = true; } QWidget::showEvent(event); if (qfReloadHomeProjectsAtShow) { loadProjects(); qfReloadHomeProjectsAtShow = false; } } void HomeView::hideEvent(QHideEvent *event) { QWidget::hideEvent(event); } void HomeView::initWidgets() { m_title = new QLabel(this); m_title->setText("项目列表"); QFont ft("Microsoft YaHei", 12); m_title->setFont(ft); m_create = new PushButton("新建", NEWFLICON(FluentIcon, ADD), this); m_create->setToolTip("新建项目"); m_search = new LineEdit(this); m_search->setIsClearButtonEnabled(true); m_search->setPlaceholderText("输入项目名搜索"); m_search->setMinimumWidth(300); m_filter = new PushButton("筛选", NEWFLICON(QFDIcon, Filter), this); m_filter->setToolTip("筛选项目"); m_projListWidget = new ProjectListWidget(this); m_createProjWidget = new CreateProjWidget(this); } void HomeView::initLayout() { // 总体布局 m_layout = new QVBoxLayout(this); m_layout->setAlignment(Qt::AlignTop | Qt::AlignLeft); m_layout->setContentsMargins(15, 10, 10, 15); m_topLayout = new QHBoxLayout(); m_layout->addLayout(m_topLayout); m_layout->addWidget(m_projListWidget); // 顶部布局 m_topLayout->addWidget(m_title); m_topLayout->addSpacing(15); m_topLayout->addWidget(m_create, 1, Qt::AlignLeft); m_topLayout->addWidget(m_search, 0, Qt::AlignLeft); m_topLayout->addStretch(); m_topLayout->addWidget(m_filter); } void HomeView::connectSignalsAndSlots() { connect(m_create, &PushButton::clicked, this, &HomeView::slotCreateProjClicked); connect(m_search, &LineEdit::textChanged, this, &HomeView::slotSearchTextChanged); connect(m_createProjWidget, &CreateProjWidget::signalConfirm, this, &HomeView::slotProjInfoConfirmed); connect(m_projListWidget, &ProjectListWidget::sigOpen, this, &HomeView::slotOpenProject); connect(m_projListWidget, &ProjectListWidget::sigInfo, this, &HomeView::slotProjectInfo); connect(m_projListWidget, &ProjectListWidget::sigDelete, this, &HomeView::slotDeleteProject); } void HomeView::loadProjects() { qDeleteAll(m_projList); m_projList.clear(); int code = ProjectManager::queryProjects(&m_projList); if (code != QF_CODE_SUCCEEDED) { QFDAlert::showAlertWithCode(code, this); return; } m_projListWidget->setProjects(searchResult()); } QList HomeView::searchResult() const { QList list; for (ProjectInfo *proj : m_projList) { if (proj->projectName.contains(m_search->text())) { list.append(proj); } } return list; } void HomeView::slotCreateProjClicked() { if (m_createProjWidget->isVisible() == false) { m_createProjWidget->setMode(CreateProjWidget::Create); m_createProjWidget->resetInputs(); m_createProjWidget->show(); } } void HomeView::slotSearchTextChanged() { m_projListWidget->setProjects(searchResult()); } /// @todo 修改或者新建项目后, 列表移到该条目 void HomeView::slotProjInfoConfirmed() { int code = -1; ProjectInfo *info = m_createProjWidget->projInfo(); ProjectInfo editedInfo = m_createProjWidget->editedProjInfo(); if (m_createProjWidget->mode() == CreateProjWidget::Create) { code = ProjectManager::insertProject(editedInfo); } else if (m_createProjWidget->mode() == CreateProjWidget::Update) { editedInfo.id = m_createProjWidget->projInfo()->id; code = ProjectManager::updateProject(editedInfo); } else { return; } QFDAlert::showAlertWithCode(code, m_createProjWidget); if (code == QF_CODE_SUCCEEDED) { m_createProjWidget->close(); m_search->clear(); loadProjects(); } } void HomeView::slotProjectInfo(ProjectInfo *proj) { if (m_createProjWidget->isVisible() == false) { m_createProjWidget->setMode(CreateProjWidget::Info); m_createProjWidget->setProjectInfo(proj); m_createProjWidget->show(); } } void HomeView::slotOpenProject(ProjectInfo *proj) { emit sigOpenProject(proj); } void HomeView::slotDeleteProject(ProjectInfo *proj) { QString title = "删除工程 “" + proj->projectName + "” ?"; MessageBox *m = new MessageBox(title, "删除后无法恢复", this); if (m->exec()) { int code = ProjectManager::deleteProject(proj->id); QFDAlert::showAlertWithCode(code, this); if (code == QF_CODE_SUCCEEDED) { m_projList.removeOne(proj); m_projListWidget->removeProject(proj); } } }