HomeView.cpp 3.5 KB

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