HomeView.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 "QFDAlert.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. loadProjects();
  25. }
  26. void HomeView::hideEvent(QHideEvent *event)
  27. {
  28. QWidget::hideEvent(event);
  29. }
  30. void HomeView::initWidgets()
  31. {
  32. m_title = new QLabel(this);
  33. m_title->setText("项目列表");
  34. QFont ft("Microsoft YaHei", 12);
  35. m_title->setFont(ft);
  36. m_create = new PushButton("新建", NEWFLICON(FluentIcon, ADD), this);
  37. m_create->setToolTip("新建项目");
  38. m_search = new LineEdit(this);
  39. m_search->setIsClearButtonEnabled(true);
  40. m_search->setPlaceholderText("输入项目名搜索");
  41. m_search->setMinimumWidth(300);
  42. m_filter = new PushButton("筛选", NEWFLICON(QFDIcon, Filter), this);
  43. m_filter->setToolTip("筛选项目");
  44. m_projListWidget = new ProjectListWidget(this);
  45. m_createProjWidget = new CreateProjWidget(this);
  46. }
  47. void HomeView::initLayout()
  48. {
  49. // 总体布局
  50. m_layout = new QVBoxLayout(this);
  51. m_layout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
  52. m_layout->setContentsMargins(15, 10, 10, 15);
  53. m_topLayout = new QHBoxLayout();
  54. m_layout->addLayout(m_topLayout);
  55. m_layout->addWidget(m_projListWidget);
  56. // 顶部布局
  57. m_topLayout->addWidget(m_title);
  58. m_topLayout->addSpacing(15);
  59. m_topLayout->addWidget(m_create, 1, Qt::AlignLeft);
  60. m_topLayout->addWidget(m_search, 0, Qt::AlignLeft);
  61. m_topLayout->addStretch();
  62. m_topLayout->addWidget(m_filter);
  63. }
  64. void HomeView::connectSignalsAndSlots()
  65. {
  66. connect(m_create, &PushButton::clicked, this, &HomeView::slotCreateProjClicked);
  67. connect(m_search, &LineEdit::textChanged, this, &HomeView::slotSearchTextChanged);
  68. connect(m_createProjWidget, &CreateProjWidget::signalCreate, this, &HomeView::slotConfirmCreate);
  69. }
  70. void HomeView::loadProjects()
  71. {
  72. qDeleteAll(m_projList);
  73. m_projList.clear();
  74. int code = ProjectManager::queryProjects(&m_projList);
  75. if (code != QF_CODE_SUCCEEDED) {
  76. QFDAlert::showAlertWithCode(code, this);
  77. return;
  78. }
  79. m_projListWidget->showProjects(searchResult());
  80. }
  81. QList<ProjectInfo *> HomeView::searchResult() const
  82. {
  83. QList<ProjectInfo *> list;
  84. for (ProjectInfo *proj : m_projList) {
  85. if (proj->projectName.contains(m_search->text())) {
  86. list.append(proj);
  87. }
  88. }
  89. return list;
  90. }
  91. void HomeView::slotCreateProjClicked()
  92. {
  93. if (m_createProjWidget->isVisible() == false) {
  94. m_createProjWidget->resetInputs();
  95. m_createProjWidget->show();
  96. }
  97. }
  98. void HomeView::slotSearchTextChanged()
  99. {
  100. m_projListWidget->showProjects(searchResult());
  101. }
  102. void HomeView::slotConfirmCreate()
  103. {
  104. ProjectInfo info = m_createProjWidget->projectInfo();
  105. int code = ProjectManager::insertProject(info);
  106. QFDAlert::showAlertWithCode(code, m_createProjWidget);
  107. if (code == QF_CODE_SUCCEEDED) {
  108. m_createProjWidget->close();
  109. m_search->clear();
  110. loadProjects();
  111. }
  112. }