HomeView.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. connect(m_previous, &PushButton::clicked, this, &HomeView::slotPreviousClicked);
  94. connect(m_next, &PushButton::clicked, this, &HomeView::slotNextClicked);
  95. connect(m_first, &PushButton::clicked, this, &HomeView::slotFirstClicked);
  96. connect(m_final, &PushButton::clicked, this, &HomeView::slotFinalClicked);
  97. }
  98. void HomeView::loadProjects()
  99. {
  100. qDeleteAll(m_projList);
  101. m_projList.clear();
  102. int code = ProjectManager::queryProjects(&m_projList, m_total, m_page, m_pageSize, m_search->text());
  103. if (code != QF_CODE_SUCCEEDED) {
  104. QFDAlert::showAlertWithCode(code, this);
  105. return;
  106. }
  107. m_projListWidget->setProjects(filterResult());
  108. }
  109. QList<ProjectInfo *> HomeView::filterResult() const
  110. {
  111. QList<ProjectInfo *> list;
  112. for (ProjectInfo *proj : m_projList) {
  113. bool search = proj->projectName.contains(m_search->text());
  114. QList<ProjectManager::EvalType> types = ProjectManager::evalTypeList(*proj);
  115. bool typeValid = types.contains(ProjectManager::Requirements)
  116. | types.contains(ProjectManager::SchemeOptimization)
  117. | types.contains(ProjectManager::OverallEfficiency);
  118. if (search && typeValid) {
  119. list.append(proj);
  120. }
  121. }
  122. return list;
  123. }
  124. void HomeView::slotCreateProjClicked()
  125. {
  126. if (m_createProjWidget->isVisible() == false) {
  127. m_createProjWidget->setMode(CreateProjWidget::Create);
  128. m_createProjWidget->resetInputs();
  129. m_createProjWidget->show();
  130. }
  131. }
  132. void HomeView::slotSearchTextChanged()
  133. {
  134. m_projListWidget->setProjects(filterResult());
  135. }
  136. void HomeView::slotFilterClicked()
  137. {
  138. qDebug() << __FUNCTION__ << __LINE__ << endl;
  139. }
  140. /// @todo 修改或者新建项目后, 列表移到该条目
  141. void HomeView::slotProjInfoConfirmed()
  142. {
  143. int code = -1;
  144. ProjectInfo *info = m_createProjWidget->projInfo();
  145. ProjectInfo editedInfo = m_createProjWidget->editedProjInfo();
  146. if (m_createProjWidget->mode() == CreateProjWidget::Create) {
  147. code = ProjectManager::insertProject(editedInfo);
  148. } else if (m_createProjWidget->mode() == CreateProjWidget::Update) {
  149. editedInfo.id = m_createProjWidget->projInfo()->id;
  150. code = ProjectManager::updateProject(editedInfo);
  151. } else {
  152. return;
  153. }
  154. QFDAlert::showAlertWithCode(code, m_createProjWidget);
  155. if (code == QF_CODE_SUCCEEDED) {
  156. m_createProjWidget->close();
  157. m_search->clear();
  158. loadProjects();
  159. }
  160. }
  161. void HomeView::slotProjectInfo(ProjectInfo *proj)
  162. {
  163. if (m_createProjWidget->isVisible() == false) {
  164. m_createProjWidget->setMode(CreateProjWidget::Info);
  165. m_createProjWidget->setProjectInfo(proj);
  166. m_createProjWidget->show();
  167. }
  168. }
  169. void HomeView::slotOpenProject(ProjectInfo *proj)
  170. {
  171. emit sigOpenProject(proj);
  172. }
  173. void HomeView::slotDeleteProject(ProjectInfo *proj)
  174. {
  175. QString title = "删除工程 “" + proj->projectName + "” ?";
  176. MessageBox *m = new MessageBox(title, "删除后无法恢复", this);
  177. if (m->exec()) {
  178. int code = ProjectManager::deleteProject(proj->id);
  179. QFDAlert::showAlertWithCode(code, this);
  180. if (code == QF_CODE_SUCCEEDED) {
  181. m_projList.removeOne(proj);
  182. m_projListWidget->removeProject(proj);
  183. }
  184. }
  185. }
  186. void HomeView::slotPreviousClicked()
  187. {
  188. qDebug() << __FUNCTION__ << __LINE__ << endl;
  189. loadProjects();
  190. }
  191. void HomeView::slotNextClicked()
  192. {
  193. qDebug() << __FUNCTION__ << __LINE__ << endl;
  194. loadProjects();
  195. }
  196. void HomeView::slotFirstClicked()
  197. {
  198. qDebug() << __FUNCTION__ << __LINE__ << endl;
  199. loadProjects();
  200. }
  201. void HomeView::slotFinalClicked()
  202. {
  203. qDebug() << __FUNCTION__ << __LINE__ << endl;
  204. loadProjects();
  205. }