HomeView.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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::signalCreate, this, &HomeView::slotConfirmCreate);
  73. connect(m_projListWidget, &ProjectListWidget::sigOpen, this, &HomeView::slotOpenProject);
  74. connect(m_projListWidget, &ProjectListWidget::sigEdit, this, &HomeView::slotEditProject);
  75. connect(m_projListWidget, &ProjectListWidget::sigInfo, this, &HomeView::slotProjectInfo);
  76. connect(m_projListWidget, &ProjectListWidget::sigDelete, this, &HomeView::slotDeleteProject);
  77. }
  78. void HomeView::loadProjects()
  79. {
  80. qDeleteAll(m_projList);
  81. m_projList.clear();
  82. int code = ProjectManager::queryProjects(&m_projList);
  83. if (code != QF_CODE_SUCCEEDED) {
  84. QFDAlert::showAlertWithCode(code, this);
  85. return;
  86. }
  87. m_projListWidget->showProjects(searchResult());
  88. }
  89. QList<ProjectInfo *> HomeView::searchResult() const
  90. {
  91. QList<ProjectInfo *> list;
  92. for (ProjectInfo *proj : m_projList) {
  93. if (proj->projectName.contains(m_search->text())) {
  94. list.append(proj);
  95. }
  96. }
  97. return list;
  98. }
  99. void HomeView::slotCreateProjClicked()
  100. {
  101. if (m_createProjWidget->isVisible() == false) {
  102. m_createProjWidget->setMode(CreateProjWidget::Create);
  103. m_createProjWidget->resetInputs();
  104. m_createProjWidget->show();
  105. }
  106. }
  107. void HomeView::slotSearchTextChanged()
  108. {
  109. m_projListWidget->showProjects(searchResult());
  110. }
  111. void HomeView::slotConfirmCreate()
  112. {
  113. int code = -1;
  114. ProjectInfo info = m_createProjWidget->editedProjInfo();
  115. if (m_createProjWidget->mode() == CreateProjWidget::Create) {
  116. code = ProjectManager::insertProject(info);
  117. } else if (m_createProjWidget->mode() == CreateProjWidget::Update) {
  118. info.id = m_createProjWidget->projInfo()->id;
  119. code = ProjectManager::updateProject(info);
  120. } else {
  121. return;
  122. }
  123. QFDAlert::showAlertWithCode(code, m_createProjWidget);
  124. if (code == QF_CODE_SUCCEEDED) {
  125. m_createProjWidget->close();
  126. m_search->clear();
  127. loadProjects();
  128. }
  129. }
  130. void HomeView::slotProjectInfo(ProjectInfo *proj)
  131. {
  132. if (m_createProjWidget->isVisible() == false) {
  133. m_createProjWidget->setMode(CreateProjWidget::Info);
  134. m_createProjWidget->setProjectInfo(proj);
  135. m_createProjWidget->show();
  136. }
  137. }
  138. void HomeView::slotOpenProject(ProjectInfo *proj) { }
  139. void HomeView::slotEditProject(ProjectInfo *proj)
  140. {
  141. if (m_createProjWidget->isVisible() == false) {
  142. m_createProjWidget->setMode(CreateProjWidget::Update);
  143. m_createProjWidget->setProjectInfo(proj);
  144. m_createProjWidget->show();
  145. }
  146. }
  147. void HomeView::slotDeleteProject(ProjectInfo *proj)
  148. {
  149. QString title = "确认删除工程 “" + proj->projectName + "” ?";
  150. MessageBox *m = new MessageBox(title, "删除后无法恢复", this);
  151. if (m->exec()) {
  152. int code = ProjectManager::deleteProject(proj->id);
  153. QFDAlert::showAlertWithCode(code, this);
  154. if (code == QF_CODE_SUCCEEDED) {
  155. loadProjects();
  156. }
  157. }
  158. }