HomeView.cpp 3.0 KB

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