HomeView.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. connect(m_createProjWidget, &CreateProjWidget::signalCreate, this, &HomeView::slotConfirmCreate);
  68. }
  69. void HomeView::loadProjects()
  70. {
  71. qDeleteAll(m_projList);
  72. m_projList.clear();
  73. int code = ProjectManager::queryProjects(&m_projList);
  74. if (code != QF_CODE_SUCCEEDED) {
  75. QFDAlert::showAlertWithCode(code, this);
  76. return;
  77. }
  78. m_projListWidget->showProjects(searchResult());
  79. }
  80. QList<ProjectInfo *> HomeView::searchResult() const
  81. {
  82. QList<ProjectInfo *> list;
  83. for (ProjectInfo *proj : m_projList) {
  84. if (proj->projectName.contains(m_search->text())) {
  85. list.append(proj);
  86. }
  87. }
  88. return list;
  89. }
  90. void HomeView::slotCreateProjClicked()
  91. {
  92. if (m_createProjWidget->isVisible() == false) {
  93. m_createProjWidget->resetInputs();
  94. m_createProjWidget->show();
  95. }
  96. }
  97. void HomeView::slotSearchTextChanged()
  98. {
  99. m_projListWidget->showProjects(searchResult());
  100. }
  101. void HomeView::slotConfirmCreate()
  102. {
  103. ProjectInfo info = m_createProjWidget->projectInfo();
  104. }