HomeView.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. m_totalLab = new QLabel("共123条, 每页20条", this);
  51. m_pageLab = new QLabel("第1/12页", this);
  52. m_previous = new PushButton("上一页", this);
  53. m_next = new PushButton("下一页", this);
  54. m_first = new PushButton("首页", this);
  55. m_final = new PushButton("末页", this);
  56. }
  57. void HomeView::initLayout()
  58. {
  59. // 总体布局
  60. m_layout = new QVBoxLayout(this);
  61. m_layout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
  62. m_layout->setContentsMargins(15, 10, 10, 15);
  63. m_topLayout = new QHBoxLayout();
  64. m_layout->addLayout(m_topLayout);
  65. m_layout->addWidget(m_projListWidget);
  66. m_pageLayout = new QHBoxLayout();
  67. m_layout->addLayout(m_pageLayout);
  68. // 顶部布局
  69. m_topLayout->addWidget(m_title);
  70. m_topLayout->addSpacing(15);
  71. m_topLayout->addWidget(m_create, 1, Qt::AlignLeft);
  72. m_topLayout->addWidget(m_search, 0, Qt::AlignLeft);
  73. m_topLayout->addStretch();
  74. m_topLayout->addWidget(m_filter);
  75. m_pageLayout->addWidget(m_totalLab);
  76. m_pageLayout->addStretch();
  77. m_pageLayout->addWidget(m_first);
  78. m_pageLayout->addWidget(m_previous);
  79. m_pageLayout->addWidget(m_pageLab);
  80. m_pageLayout->addWidget(m_next);
  81. m_pageLayout->addWidget(m_final);
  82. m_pageLayout->addStretch();
  83. }
  84. void HomeView::connectSignalsAndSlots()
  85. {
  86. connect(m_create, &PushButton::clicked, this, &HomeView::slotCreateProjClicked);
  87. connect(m_search, &LineEdit::textChanged, this, &HomeView::slotSearchTextChanged);
  88. connect(m_filter, &PushButton::clicked, this, &HomeView::slotFilterClicked);
  89. connect(m_createProjWidget, &CreateProjWidget::signalConfirm, this, &HomeView::slotProjInfoConfirmed);
  90. connect(m_projListWidget, &ProjectListWidget::sigOpen, this, &HomeView::slotOpenProject);
  91. connect(m_projListWidget, &ProjectListWidget::sigInfo, this, &HomeView::slotProjectInfo);
  92. connect(m_projListWidget, &ProjectListWidget::sigDelete, this, &HomeView::slotDeleteProject);
  93. }
  94. void HomeView::loadProjects()
  95. {
  96. qDeleteAll(m_projList);
  97. m_projList.clear();
  98. int code = ProjectManager::queryProjects(&m_projList);
  99. if (code != QF_CODE_SUCCEEDED) {
  100. QFDAlert::showAlertWithCode(code, this);
  101. return;
  102. }
  103. m_projListWidget->setProjects(filterResult());
  104. }
  105. QList<ProjectInfo *> HomeView::filterResult() const
  106. {
  107. QList<ProjectInfo *> list;
  108. for (ProjectInfo *proj : m_projList) {
  109. bool search = proj->projectName.contains(m_search->text());
  110. QList<ProjectManager::EvalType> types = ProjectManager::evalTypeList(*proj);
  111. bool typeValid = types.contains(ProjectManager::Requirements)
  112. | types.contains(ProjectManager::SchemeOptimization)
  113. | types.contains(ProjectManager::OverallEfficiency);
  114. if (search && typeValid) {
  115. list.append(proj);
  116. }
  117. }
  118. return list;
  119. }
  120. void HomeView::slotCreateProjClicked()
  121. {
  122. if (m_createProjWidget->isVisible() == false) {
  123. m_createProjWidget->setMode(CreateProjWidget::Create);
  124. m_createProjWidget->resetInputs();
  125. m_createProjWidget->show();
  126. }
  127. }
  128. void HomeView::slotSearchTextChanged()
  129. {
  130. m_projListWidget->setProjects(filterResult());
  131. }
  132. void HomeView::slotFilterClicked()
  133. {
  134. qDebug() << __FUNCTION__ << __LINE__ << endl;
  135. }
  136. /// @todo 修改或者新建项目后, 列表移到该条目
  137. void HomeView::slotProjInfoConfirmed()
  138. {
  139. int code = -1;
  140. ProjectInfo *info = m_createProjWidget->projInfo();
  141. ProjectInfo editedInfo = m_createProjWidget->editedProjInfo();
  142. if (m_createProjWidget->mode() == CreateProjWidget::Create) {
  143. code = ProjectManager::insertProject(editedInfo);
  144. } else if (m_createProjWidget->mode() == CreateProjWidget::Update) {
  145. editedInfo.id = m_createProjWidget->projInfo()->id;
  146. code = ProjectManager::updateProject(editedInfo);
  147. } else {
  148. return;
  149. }
  150. QFDAlert::showAlertWithCode(code, m_createProjWidget);
  151. if (code == QF_CODE_SUCCEEDED) {
  152. m_createProjWidget->close();
  153. m_search->clear();
  154. loadProjects();
  155. }
  156. }
  157. void HomeView::slotProjectInfo(ProjectInfo *proj)
  158. {
  159. if (m_createProjWidget->isVisible() == false) {
  160. m_createProjWidget->setMode(CreateProjWidget::Info);
  161. m_createProjWidget->setProjectInfo(proj);
  162. m_createProjWidget->show();
  163. }
  164. }
  165. void HomeView::slotOpenProject(ProjectInfo *proj)
  166. {
  167. emit sigOpenProject(proj);
  168. }
  169. void HomeView::slotDeleteProject(ProjectInfo *proj)
  170. {
  171. QString title = "删除工程 “" + proj->projectName + "” ?";
  172. MessageBox *m = new MessageBox(title, "删除后无法恢复", this);
  173. if (m->exec()) {
  174. int code = ProjectManager::deleteProject(proj->id);
  175. QFDAlert::showAlertWithCode(code, this);
  176. if (code == QF_CODE_SUCCEEDED) {
  177. m_projList.removeOne(proj);
  178. m_projListWidget->removeProject(proj);
  179. }
  180. }
  181. }