ProjectListWidget.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #include "ProjectListWidget.h"
  2. #include "QFDIcon.h"
  3. #include "ProjectManager.h"
  4. #include "dbService/ClassSet.h"
  5. #include <Widgets/LineEdit.h>
  6. #include <Widgets/Button.h>
  7. #include <Widgets/TreeView.h>
  8. #include <QBoxLayout>
  9. #include <QListWidget>
  10. #include <QLabel>
  11. #include <QDateTime>
  12. #include <QDebug>
  13. ProjectListItemWidget::ProjectListItemWidget(QWidget *parent) : QWidget(parent)
  14. {
  15. initWidgets();
  16. initLayout();
  17. connectSignalsAndSlots();
  18. }
  19. void ProjectListItemWidget::initWidgets()
  20. {
  21. m_name = new QLabel(this);
  22. m_name->setObjectName("name");
  23. m_time = new QLabel(this);
  24. m_time->setObjectName("time");
  25. m_taskLabel = new QLabel(this);
  26. m_taskLabel->setObjectName("taskLabel");
  27. m_taskLabel->setText("任务名称:");
  28. m_task = new QLabel(this);
  29. m_task->setObjectName("task");
  30. m_typeLabel = new QLabel(this);
  31. m_typeLabel->setObjectName("typeLabel");
  32. m_typeLabel->setText("评估类型:");
  33. m_type = new QLabel(this);
  34. m_type->setObjectName("type");
  35. m_info = new PushButton("详情", NEWFLICON(FluentIcon, INFO), this);
  36. m_info->setObjectName("info");
  37. m_open = new PushButton("打开", NEWFLICON(QFDIcon, Open), this);
  38. m_open->setObjectName("open");
  39. m_delete = new PushButton("删除", NEWFLICON(FluentIcon, DELETE), this);
  40. m_delete->setObjectName("delete");
  41. m_export = new PushButton("导出资源包", NEWFLICON(FluentIcon, SHARE), this);
  42. m_export->setObjectName("export");
  43. setStyleSheet("#name {color:#1196db; font-size:17px; font:bold}"
  44. "#time {color:gray; font-size:12px;}"
  45. "#taskLabel {color: gray; font-size:14px;}"
  46. "#task {color: gray; font-size:14px;}"
  47. "#typeLabel {color: gray; font-size:14px;}"
  48. "#type {color: gray; font-size:14px;}");
  49. }
  50. void ProjectListItemWidget::initLayout()
  51. {
  52. // 总体布局
  53. m_layout = new QVBoxLayout(this);
  54. m_layout->setMargin(10);
  55. m_nameLayout = new QHBoxLayout();
  56. m_layout->addLayout(m_nameLayout);
  57. m_layout->addSpacing(5);
  58. m_taskLayout = new QHBoxLayout();
  59. m_layout->addLayout(m_taskLayout);
  60. m_typeLayout = new QHBoxLayout();
  61. m_layout->addLayout(m_typeLayout);
  62. m_buttonLayout = new QHBoxLayout();
  63. m_layout->addLayout(m_buttonLayout);
  64. // 名称
  65. m_nameLayout->addWidget(m_name);
  66. m_nameLayout->addStretch();
  67. m_nameLayout->addWidget(m_time);
  68. // 任务
  69. m_taskLayout->setContentsMargins(10, 0, 10, 0);
  70. m_taskLayout->addWidget(m_taskLabel);
  71. m_taskLayout->addSpacing(5);
  72. m_taskLayout->addWidget(m_task);
  73. m_taskLayout->addStretch();
  74. // 类型
  75. m_typeLayout->setContentsMargins(10, 0, 10, 0);
  76. m_typeLayout->addWidget(m_typeLabel);
  77. m_typeLayout->addSpacing(5);
  78. m_typeLayout->addWidget(m_type);
  79. m_typeLayout->addStretch();
  80. // 按钮
  81. m_buttonLayout->addStretch();
  82. m_buttonLayout->addWidget(m_info);
  83. m_buttonLayout->addWidget(m_open);
  84. m_buttonLayout->addWidget(m_delete);
  85. m_buttonLayout->addWidget(m_export);
  86. }
  87. void ProjectListItemWidget::connectSignalsAndSlots()
  88. {
  89. connect(m_info, &PushButton::clicked, this, &ProjectListItemWidget::sigInfo);
  90. connect(m_open, &PushButton::clicked, this, &ProjectListItemWidget::sigOpen);
  91. connect(m_delete, &PushButton::clicked, this, &ProjectListItemWidget::sigDelete);
  92. connect(m_export, &PushButton::clicked, this, &ProjectListItemWidget::sigExport);
  93. }
  94. void ProjectListItemWidget::setProj(ProjectInfo *info)
  95. {
  96. m_proj = info;
  97. if (info == nullptr) {
  98. return;
  99. }
  100. m_name->setText(info->projectName);
  101. QDateTime t = QDateTime::fromTime_t(info->estimateTime.toUInt());
  102. m_time->setText(t.toString("yyyy-M-d H:mm"));
  103. m_task->setText(info->taskName);
  104. QString tStr;
  105. QList<ProjectManager::EvalType> types = ProjectManager::evalTypeList(*info);
  106. for (int i = 0; i < types.count(); i++) {
  107. tStr += ProjectManager::nameOfEvalType(types[i]);
  108. if (i < types.count() - 1) {
  109. tStr += "、";
  110. }
  111. }
  112. m_type->setText(tStr);
  113. }
  114. ProjectInfo *ProjectListItemWidget::proj() const
  115. {
  116. return m_proj;
  117. }
  118. ProjectListWidget::ProjectListWidget(QWidget *parent) : QWidget(parent)
  119. {
  120. initWidgets();
  121. initLayout();
  122. connectSiganlsAndSlots();
  123. }
  124. void ProjectListWidget::setProjects(QList<ProjectInfo *> list)
  125. {
  126. m_projList.clear();
  127. m_listWidget->clear();
  128. for (auto proj : list) {
  129. addProject(proj);
  130. }
  131. }
  132. void ProjectListWidget::showLast() const
  133. {
  134. if (m_projList.count() > 0) {
  135. m_listWidget->setCurrentRow(m_projList.count() - 1);
  136. }
  137. }
  138. void ProjectListWidget::addProject(ProjectInfo *proj)
  139. {
  140. m_projList.append(proj);
  141. ProjectListItemWidget *w = new ProjectListItemWidget(m_listWidget);
  142. w->setProj(proj);
  143. connect(w, &ProjectListItemWidget::sigInfo, this, &ProjectListWidget::slotInfo);
  144. connect(w, &ProjectListItemWidget::sigOpen, this, &ProjectListWidget::slotOpen);
  145. connect(w, &ProjectListItemWidget::sigDelete, this, &ProjectListWidget::slotDelete);
  146. connect(w, &ProjectListItemWidget::sigExport, this, &ProjectListWidget::slotExport);
  147. QListWidgetItem *item = new QListWidgetItem(m_listWidget);
  148. item->setSizeHint(QSize(300, 135));
  149. m_listWidget->setItemWidget(item, w);
  150. }
  151. void ProjectListWidget::removeProject(ProjectInfo *proj)
  152. {
  153. int index = m_projList.indexOf(proj);
  154. qDebug() << __FUNCTION__ << __LINE__ << "index:" << index << endl;
  155. m_listWidget->takeItem(index);
  156. }
  157. ProjectListItemWidget *ProjectListWidget::widgetOfItem(QListWidgetItem *item) const
  158. {
  159. QWidget *w = m_listWidget->itemWidget(item);
  160. return dynamic_cast<ProjectListItemWidget *>(w);
  161. }
  162. QListWidgetItem *ProjectListWidget::itemOfWidget(ProjectListItemWidget *widget) const
  163. {
  164. for (int i = 0; i < m_listWidget->count(); i++) {
  165. QListWidgetItem *item = m_listWidget->item(i);
  166. if (m_listWidget->itemWidget(item) == widget) {
  167. return item;
  168. }
  169. }
  170. return nullptr;
  171. }
  172. void ProjectListWidget::initWidgets()
  173. {
  174. QPalette pal(palette());
  175. pal.setColor(QPalette::Background, QColor("#eeeeee"));
  176. setAutoFillBackground(true);
  177. setPalette(pal);
  178. m_vBoxLayout = new QVBoxLayout(this);
  179. m_listWidget = new QListWidget(this);
  180. m_listWidget->setAlternatingRowColors(true);
  181. m_listWidget->setStyleSheet("QListWidget {border: 1px solid rgba(0, 0, 0, 0.073);background: rgb(255, 255, "
  182. "255);alternate-background-color: rgb(233, 233, 244);}");
  183. }
  184. void ProjectListWidget::initLayout()
  185. {
  186. m_vBoxLayout->setMargin(0);
  187. m_vBoxLayout->addWidget(m_listWidget);
  188. }
  189. void ProjectListWidget::connectSiganlsAndSlots()
  190. {
  191. connect(m_listWidget, &QListWidget::itemDoubleClicked, this, &ProjectListWidget::slotItemDoubleClicked);
  192. connect(m_listWidget, &QListWidget::itemClicked, this, &ProjectListWidget::slotItemClicked);
  193. connect(m_listWidget, &QListWidget::currentItemChanged, this, &ProjectListWidget::slotCurrentItemChanged);
  194. connect(m_listWidget, &QListWidget::currentRowChanged, this, &ProjectListWidget::slotCurrentRowChanged);
  195. connect(m_listWidget, &QListWidget::itemSelectionChanged, this, &ProjectListWidget::slotItemSelectionChanged);
  196. }
  197. void ProjectListWidget::slotItemDoubleClicked(QListWidgetItem *item)
  198. {
  199. ProjectListItemWidget *w = dynamic_cast<ProjectListItemWidget *>(m_listWidget->itemWidget(item));
  200. emit sigInfo(w->proj());
  201. }
  202. void ProjectListWidget::slotItemClicked(QListWidgetItem *item) { }
  203. void ProjectListWidget::slotCurrentItemChanged(QListWidgetItem *current, QListWidgetItem *previous) { }
  204. void ProjectListWidget::slotCurrentRowChanged(int currentRow) { }
  205. void ProjectListWidget::slotItemSelectionChanged() { }
  206. void ProjectListWidget::slotInfo()
  207. {
  208. ProjectListItemWidget *w = dynamic_cast<ProjectListItemWidget *>(sender());
  209. itemOfWidget(w)->setSelected(true);
  210. emit sigInfo(w->proj());
  211. }
  212. void ProjectListWidget::slotOpen()
  213. {
  214. ProjectListItemWidget *w = dynamic_cast<ProjectListItemWidget *>(sender());
  215. itemOfWidget(w)->setSelected(true);
  216. emit sigOpen(w->proj());
  217. }
  218. void ProjectListWidget::slotDelete()
  219. {
  220. ProjectListItemWidget *w = dynamic_cast<ProjectListItemWidget *>(sender());
  221. itemOfWidget(w)->setSelected(true);
  222. emit sigDelete(w->proj());
  223. }
  224. void ProjectListWidget::slotExport()
  225. {
  226. ProjectListItemWidget *w = dynamic_cast<ProjectListItemWidget *>(sender());
  227. itemOfWidget(w)->setSelected(true);
  228. emit sigExport(w->proj());
  229. }