ProjectStateWidget.cpp 4.7 KB

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