123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- #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 <Widgets/Button.h>
- #include <Widgets/LineEdit.h>
- #include <DialogBox/Dialog.h>
- #include <QBoxLayout>
- #include <QLabel>
- #include <QDebug>
- 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::signalCreate, this, &HomeView::slotConfirmCreate);
- connect(m_projListWidget, &ProjectListWidget::sigOpen, this, &HomeView::slotOpenProject);
- connect(m_projListWidget, &ProjectListWidget::sigEdit, this, &HomeView::slotEditProject);
- 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->showProjects(searchResult());
- }
- QList<ProjectInfo *> HomeView::searchResult() const
- {
- QList<ProjectInfo *> 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->showProjects(searchResult());
- }
- void HomeView::slotConfirmCreate()
- {
- int code = -1;
- ProjectInfo info = m_createProjWidget->editedProjInfo();
- if (m_createProjWidget->mode() == CreateProjWidget::Create) {
- code = ProjectManager::insertProject(info);
- } else if (m_createProjWidget->mode() == CreateProjWidget::Update) {
- info.id = m_createProjWidget->projInfo()->id;
- code = ProjectManager::updateProject(info);
- } 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) { }
- void HomeView::slotEditProject(ProjectInfo *proj)
- {
- if (m_createProjWidget->isVisible() == false) {
- m_createProjWidget->setMode(CreateProjWidget::Update);
- m_createProjWidget->setProjectInfo(proj);
- m_createProjWidget->show();
- }
- }
- 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) {
- loadProjects();
- }
- }
- }
|