HomeView.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include "HomeView.h"
  2. #include "CreateProjWidget.h"
  3. #include "ProjectStateWidget.h"
  4. #include "ProjectManager.h"
  5. #include "QFDAlert.h"
  6. #include "QFDIcon.h"
  7. #include "ProjectListWidget.h"
  8. #include "QFDConfig.h"
  9. #include <Widgets/Button.h>
  10. #include <Widgets/LineEdit.h>
  11. #include <DialogBox/Dialog.h>
  12. #include <QBoxLayout>
  13. #include <QLabel>
  14. #include <QDebug>
  15. HomeView::HomeView(QWidget *parent) : QWidget(parent) { }
  16. void HomeView::showEvent(QShowEvent *event)
  17. {
  18. if (m_initilized == false) {
  19. initWidgets();
  20. initLayout();
  21. connectSignalsAndSlots();
  22. m_initilized = true;
  23. }
  24. QWidget::showEvent(event);
  25. if (qfReloadHomeProjectsAtShow) {
  26. loadProjects();
  27. qfReloadHomeProjectsAtShow = false;
  28. }
  29. }
  30. void HomeView::hideEvent(QHideEvent *event)
  31. {
  32. QWidget::hideEvent(event);
  33. }
  34. void HomeView::initWidgets()
  35. {
  36. m_title = new QLabel(this);
  37. m_title->setText("项目列表");
  38. QFont ft("Microsoft YaHei", 12);
  39. m_title->setFont(ft);
  40. m_create = new PushButton("新建", NEWFLICON(FluentIcon, ADD), this);
  41. m_create->setToolTip("新建项目");
  42. m_search = new LineEdit(this);
  43. m_search->setIsClearButtonEnabled(true);
  44. m_search->setPlaceholderText("输入项目名搜索");
  45. m_search->setMinimumWidth(300);
  46. m_filter = new PushButton("筛选", NEWFLICON(QFDIcon, Filter), this);
  47. m_filter->setToolTip("筛选项目");
  48. m_projListWidget = new ProjectListWidget(this);
  49. m_createProjWidget = new CreateProjWidget(this);
  50. }
  51. void HomeView::initLayout()
  52. {
  53. // 总体布局
  54. m_layout = new QVBoxLayout(this);
  55. m_layout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
  56. m_layout->setContentsMargins(15, 10, 10, 15);
  57. m_topLayout = new QHBoxLayout();
  58. m_layout->addLayout(m_topLayout);
  59. m_layout->addWidget(m_projListWidget);
  60. // 顶部布局
  61. m_topLayout->addWidget(m_title);
  62. m_topLayout->addSpacing(15);
  63. m_topLayout->addWidget(m_create, 1, Qt::AlignLeft);
  64. m_topLayout->addWidget(m_search, 0, Qt::AlignLeft);
  65. m_topLayout->addStretch();
  66. m_topLayout->addWidget(m_filter);
  67. }
  68. void HomeView::connectSignalsAndSlots()
  69. {
  70. connect(m_create, &PushButton::clicked, this, &HomeView::slotCreateProjClicked);
  71. connect(m_search, &LineEdit::textChanged, this, &HomeView::slotSearchTextChanged);
  72. connect(m_createProjWidget, &CreateProjWidget::signalConfirm, this, &HomeView::slotProjInfoConfirmed);
  73. connect(m_projListWidget, &ProjectListWidget::sigOpen, this, &HomeView::slotOpenProject);
  74. connect(m_projListWidget, &ProjectListWidget::sigInfo, this, &HomeView::slotProjectInfo);
  75. connect(m_projListWidget, &ProjectListWidget::sigDelete, this, &HomeView::slotDeleteProject);
  76. }
  77. void HomeView::loadProjects()
  78. {
  79. qDeleteAll(m_projList);
  80. m_projList.clear();
  81. int code = ProjectManager::queryProjects(&m_projList);
  82. if (code != QF_CODE_SUCCEEDED) {
  83. QFDAlert::showAlertWithCode(code, this);
  84. return;
  85. }
  86. m_projListWidget->setProjects(searchResult());
  87. }
  88. QList<ProjectInfo *> HomeView::searchResult() const
  89. {
  90. QList<ProjectInfo *> list;
  91. for (ProjectInfo *proj : m_projList) {
  92. if (proj->projectName.contains(m_search->text())) {
  93. list.append(proj);
  94. }
  95. }
  96. return list;
  97. }
  98. void HomeView::slotCreateProjClicked()
  99. {
  100. if (m_createProjWidget->isVisible() == false) {
  101. m_createProjWidget->setMode(CreateProjWidget::Create);
  102. m_createProjWidget->resetInputs();
  103. m_createProjWidget->show();
  104. }
  105. }
  106. void HomeView::slotSearchTextChanged()
  107. {
  108. m_projListWidget->setProjects(searchResult());
  109. }
  110. /// @todo 修改或者新建项目后, 列表移到该条目
  111. void HomeView::slotProjInfoConfirmed()
  112. {
  113. int code = -1;
  114. ProjectInfo *info = m_createProjWidget->projInfo();
  115. ProjectInfo editedInfo = m_createProjWidget->editedProjInfo();
  116. if (m_createProjWidget->mode() == CreateProjWidget::Create) {
  117. code = ProjectManager::insertProject(editedInfo);
  118. } else if (m_createProjWidget->mode() == CreateProjWidget::Update) {
  119. editedInfo.id = m_createProjWidget->projInfo()->id;
  120. code = ProjectManager::updateProject(editedInfo);
  121. } else {
  122. return;
  123. }
  124. QFDAlert::showAlertWithCode(code, m_createProjWidget);
  125. if (code == QF_CODE_SUCCEEDED) {
  126. m_createProjWidget->close();
  127. m_search->clear();
  128. loadProjects();
  129. }
  130. }
  131. void HomeView::slotProjectInfo(ProjectInfo *proj)
  132. {
  133. if (m_createProjWidget->isVisible() == false) {
  134. m_createProjWidget->setMode(CreateProjWidget::Info);
  135. m_createProjWidget->setProjectInfo(proj);
  136. m_createProjWidget->show();
  137. }
  138. }
  139. void HomeView::slotOpenProject(ProjectInfo *proj)
  140. {
  141. emit sigOpenProject(proj);
  142. }
  143. void HomeView::slotDeleteProject(ProjectInfo *proj)
  144. {
  145. QString title = "删除工程 “" + proj->projectName + "” ?";
  146. MessageBox *m = new MessageBox(title, "删除后无法恢复", this);
  147. if (m->exec()) {
  148. int code = ProjectManager::deleteProject(proj->id);
  149. QFDAlert::showAlertWithCode(code, this);
  150. if (code == QF_CODE_SUCCEEDED) {
  151. m_projList.removeOne(proj);
  152. m_projListWidget->removeProject(proj);
  153. }
  154. }
  155. }