HomeView.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 <QBoxLayout>
  12. #include <QLabel>
  13. #include <QDebug>
  14. HomeView::HomeView(QWidget *parent) : QWidget(parent) { }
  15. void HomeView::showEvent(QShowEvent *event)
  16. {
  17. if (m_initilized == false) {
  18. initWidgets();
  19. initLayout();
  20. connectSignalsAndSlots();
  21. m_initilized = true;
  22. }
  23. QWidget::showEvent(event);
  24. if (qfReloadHomeProjectsAtShow) {
  25. loadProjects();
  26. qfReloadHomeProjectsAtShow = false;
  27. }
  28. }
  29. void HomeView::hideEvent(QHideEvent *event)
  30. {
  31. QWidget::hideEvent(event);
  32. }
  33. void HomeView::initWidgets()
  34. {
  35. m_title = new QLabel(this);
  36. m_title->setText("项目列表");
  37. QFont ft("Microsoft YaHei", 12);
  38. m_title->setFont(ft);
  39. m_create = new PushButton("新建", NEWFLICON(FluentIcon, ADD), this);
  40. m_create->setToolTip("新建项目");
  41. m_search = new LineEdit(this);
  42. m_search->setIsClearButtonEnabled(true);
  43. m_search->setPlaceholderText("输入项目名搜索");
  44. m_search->setMinimumWidth(300);
  45. m_filter = new PushButton("筛选", NEWFLICON(QFDIcon, Filter), this);
  46. m_filter->setToolTip("筛选项目");
  47. m_projListWidget = new ProjectListWidget(this);
  48. m_createProjWidget = new CreateProjWidget(this);
  49. }
  50. void HomeView::initLayout()
  51. {
  52. // 总体布局
  53. m_layout = new QVBoxLayout(this);
  54. m_layout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
  55. m_layout->setContentsMargins(15, 10, 10, 15);
  56. m_topLayout = new QHBoxLayout();
  57. m_layout->addLayout(m_topLayout);
  58. m_layout->addWidget(m_projListWidget);
  59. // 顶部布局
  60. m_topLayout->addWidget(m_title);
  61. m_topLayout->addSpacing(15);
  62. m_topLayout->addWidget(m_create, 1, Qt::AlignLeft);
  63. m_topLayout->addWidget(m_search, 0, Qt::AlignLeft);
  64. m_topLayout->addStretch();
  65. m_topLayout->addWidget(m_filter);
  66. }
  67. void HomeView::connectSignalsAndSlots()
  68. {
  69. connect(m_create, &PushButton::clicked, this, &HomeView::slotCreateProjClicked);
  70. connect(m_search, &LineEdit::textChanged, this, &HomeView::slotSearchTextChanged);
  71. connect(m_createProjWidget, &CreateProjWidget::signalCreate, this, &HomeView::slotConfirmCreate);
  72. connect(m_projListWidget, &ProjectListWidget::sigOpen, this, &HomeView::slotOpenProject);
  73. connect(m_projListWidget, &ProjectListWidget::sigEdit, this, &HomeView::slotEditProject);
  74. connect(m_projListWidget, &ProjectListWidget::sigInfo, this, &HomeView::slotProjectInfo);
  75. }
  76. void HomeView::loadProjects()
  77. {
  78. qDeleteAll(m_projList);
  79. m_projList.clear();
  80. int code = ProjectManager::queryProjects(&m_projList);
  81. if (code != QF_CODE_SUCCEEDED) {
  82. QFDAlert::showAlertWithCode(code, this);
  83. return;
  84. }
  85. m_projListWidget->showProjects(searchResult());
  86. }
  87. QList<ProjectInfo *> HomeView::searchResult() const
  88. {
  89. QList<ProjectInfo *> list;
  90. for (ProjectInfo *proj : m_projList) {
  91. if (proj->projectName.contains(m_search->text())) {
  92. list.append(proj);
  93. }
  94. }
  95. return list;
  96. }
  97. void HomeView::slotCreateProjClicked()
  98. {
  99. if (m_createProjWidget->isVisible() == false) {
  100. m_createProjWidget->setMode(CreateProjWidget::Create);
  101. m_createProjWidget->resetInputs();
  102. m_createProjWidget->show();
  103. }
  104. }
  105. void HomeView::slotSearchTextChanged()
  106. {
  107. m_projListWidget->showProjects(searchResult());
  108. }
  109. void HomeView::slotConfirmCreate()
  110. {
  111. int code = -1;
  112. ProjectInfo info = m_createProjWidget->editedProjInfo();
  113. if (m_createProjWidget->mode() == CreateProjWidget::Create) {
  114. code = ProjectManager::insertProject(info);
  115. } else if (m_createProjWidget->mode() == CreateProjWidget::Update) {
  116. info.id = m_createProjWidget->projInfo()->id;
  117. code = ProjectManager::updateProject(info);
  118. } else {
  119. return;
  120. }
  121. QFDAlert::showAlertWithCode(code, m_createProjWidget);
  122. if (code == QF_CODE_SUCCEEDED) {
  123. m_createProjWidget->close();
  124. m_search->clear();
  125. loadProjects();
  126. }
  127. }
  128. void HomeView::slotProjectInfo(ProjectInfo *proj)
  129. {
  130. if (m_createProjWidget->isVisible() == false) {
  131. m_createProjWidget->setMode(CreateProjWidget::Info);
  132. m_createProjWidget->setProjectInfo(proj);
  133. m_createProjWidget->show();
  134. }
  135. }
  136. void HomeView::slotOpenProject(ProjectInfo *proj) { }
  137. void HomeView::slotEditProject(ProjectInfo *proj)
  138. {
  139. if (m_createProjWidget->isVisible() == false) {
  140. m_createProjWidget->setMode(CreateProjWidget::Update);
  141. m_createProjWidget->setProjectInfo(proj);
  142. m_createProjWidget->show();
  143. }
  144. }