ProjectStateWidget.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "ProjectStateWidget.h"
  2. #include <dbService/ClassSet.h>
  3. #include <QTableWidget>
  4. #include <QHeaderView>
  5. #include <QBoxLayout>
  6. #include <QDateTime>
  7. #include <QDebug>
  8. int ProjectStateWidget::rowCount(EngineerInfo *proj)
  9. {
  10. int expertCount = proj->configs.count();
  11. int rowProj = std::max(proj->indexList().count(), proj->indexList().count() * expertCount);
  12. return rowProj;
  13. }
  14. ProjectStateWidget::ProjectStateWidget(QWidget *parent) : QWidget(parent)
  15. {
  16. initialize();
  17. initLayout();
  18. }
  19. void ProjectStateWidget::showProjects(QList<EngineerInfo *> proList)
  20. {
  21. m_projTableWidget->clearContents();
  22. m_projTableWidget->clearSpans();
  23. setRowCount(proList);
  24. int row = 0;
  25. for (int i = 0; i < proList.count(); i++) {
  26. QColor color = i % 2 ? QColor(238, 238, 255) : QColor(255, 255, 255);
  27. // 工程名,第0列
  28. EngineerInfo *proj = proList[i];
  29. QTableWidgetItem *item = new QTableWidgetItem(proj->engineerName);
  30. setItem(row, 0, item, color);
  31. int rowProj = rowCount(proj);
  32. if (rowProj > 1) {
  33. m_projTableWidget->setSpan(row, 0, rowProj, 1);
  34. }
  35. // 指标体系,第1列
  36. for (int j = 0; j < proj->indexList().count(); j++) {
  37. EngineerInfo::IndexType indexType = proj->indexList()[j];
  38. QString indexName = EngineerInfo::nameOFIndexType(indexType);
  39. QTableWidgetItem *item = new QTableWidgetItem(indexName);
  40. setItem(row, 1, item, color);
  41. if (proj->configs.count() > 1) {
  42. m_projTableWidget->setSpan(row, 1, proj->configs.count(), 1);
  43. }
  44. // 专家数据,第2、3列
  45. // 未配置专家时,添加一个空的专家配置来填充表格
  46. QList<UserConfig *> configs = proj->configs;
  47. if (configs.count() <= 0) {
  48. configs.append(new UserConfig());
  49. }
  50. for (int k = 0; k < configs.count(); k++) {
  51. UserConfig *config = configs[k];
  52. QTableWidgetItem *expItem = new QTableWidgetItem(config->id >= 0 ? config->userName : "无");
  53. setItem(row + k, 2, expItem, color);
  54. unsigned seed = QDateTime::currentDateTime().toTime_t() + pow(row + k, 4);
  55. srand(seed);
  56. bool loaded = (rand() % 4) == 0;
  57. QTableWidgetItem *loadItem = new QTableWidgetItem(loaded && config->id >= 0 ? "已录入" : "未录入");
  58. setItem(row + k, 3, loadItem, color);
  59. }
  60. row += configs.count();
  61. }
  62. }
  63. }
  64. void ProjectStateWidget::initialize()
  65. {
  66. m_vBoxLayout = new QVBoxLayout(this);
  67. m_projTableWidget = new QTableWidget(this);
  68. m_projTableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
  69. m_projTableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
  70. m_projTableWidget->setSelectionMode(QAbstractItemView::NoSelection);
  71. m_projTableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
  72. m_projTableWidget->verticalHeader()->setVisible(false);
  73. const QStringList headers = { "工程名", "工程类型", "专家信息", "评估状态" };
  74. m_projTableWidget->setColumnCount(headers.count());
  75. m_projTableWidget->setHorizontalHeaderLabels(headers);
  76. // m_projTableWidget->setAlternatingRowColors(true);
  77. m_projTableWidget->setStyleSheet(
  78. "QTableWidget {border: 1px solid rgba(0, 0, 0, 0.073);background: rgb(244, 244, "
  79. "255);alternate-background-color: rgb(255, 255, 255);}"
  80. // "QTableWidget::item{border:1px outset red;}"
  81. // "QHeaderView::section{font-size: 14px;font:bold;font-family: 'Microsoft YaHei UI';}"
  82. "QHeaderView::section {"
  83. "background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,"
  84. "stop:0 #616161, stop: 0.5 #505050,"
  85. "stop: 0.6 #434343, stop:1 #656565);"
  86. "color: white;"
  87. "padding-left: 4px;"
  88. "border: 1px solid #6c6c6c;"
  89. "}");
  90. }
  91. void ProjectStateWidget::initLayout()
  92. {
  93. layout()->addWidget(m_projTableWidget);
  94. }
  95. void ProjectStateWidget::setRowCount(QList<EngineerInfo *> list)
  96. {
  97. int count = 0;
  98. for (EngineerInfo *proj : list) {
  99. int rowProj = rowCount(proj);
  100. count += rowProj;
  101. }
  102. m_projTableWidget->setRowCount(count);
  103. }
  104. void ProjectStateWidget::setItem(int row, int column, QTableWidgetItem *item, QColor bgColor)
  105. {
  106. item->setBackgroundColor(bgColor);
  107. item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
  108. item->setTextAlignment(Qt::AlignCenter);
  109. m_projTableWidget->setItem(row, column, item);
  110. m_projTableWidget->setRowHeight(row, 50);
  111. }