DataTableWidget.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include "DataTableWidget.h"
  2. #include "ProjectManager.h"
  3. #include <dbService/CNodeDataService.h>
  4. #include <dbService/ClassSet.h>
  5. #include <Widgets/Button.h>
  6. #include <QBoxLayout>
  7. #include <QTabWidget>
  8. #include <QLabel>
  9. #include <QTableView>
  10. #include <QTableWidget>
  11. #include <QAbstractItemModel>
  12. #include <QStandardItemModel>
  13. #include <QStandardItem>
  14. #include <QHeaderView>
  15. #include <QDebug>
  16. DataTableWidget::DataTableWidget(SchemePlanManager::SchemeProcessInfo process, QWidget *parent)
  17. : QWidget(parent), m_process(process)
  18. {
  19. m_mind1 = new CMind(this);
  20. m_mind2 = new CMind(this);
  21. initWidget();
  22. initLayout();
  23. connectSignalsAndSlots();
  24. }
  25. SchemePlanManager::SchemeProcessInfo DataTableWidget::process() const
  26. {
  27. return m_process;
  28. }
  29. void DataTableWidget::initWidget()
  30. {
  31. m_dataTab = new QTabWidget(this);
  32. m_dataTab->setTabPosition(QTabWidget::South);
  33. m_pageLab = new QLabel(this);
  34. m_previous = new PushButton("上一级指标", this);
  35. m_next = new PushButton("下一级指标", this);
  36. }
  37. void DataTableWidget::initLayout()
  38. {
  39. m_layout = new QVBoxLayout(this);
  40. m_layout->addWidget(m_dataTab);
  41. m_pageLayout = new QHBoxLayout();
  42. m_layout->addLayout(m_pageLayout);
  43. m_pageLayout->setSpacing(10);
  44. m_pageLayout->addStretch();
  45. m_pageLayout->addWidget(m_previous);
  46. m_pageLayout->addWidget(m_pageLab);
  47. m_pageLayout->addWidget(m_next);
  48. m_pageLayout->addStretch();
  49. }
  50. void DataTableWidget::connectSignalsAndSlots()
  51. {
  52. connect(m_previous, &PushButton::clicked, this, &DataTableWidget::slotPrevious);
  53. connect(m_next, &PushButton::clicked, this, &DataTableWidget::slotNext);
  54. connect(m_dataTab, &QTabWidget::currentChanged, this, &DataTableWidget::slotTabCurrentChanged);
  55. }
  56. void DataTableWidget::setupTabWidget()
  57. {
  58. /// 创建 tableView 并添加进 tabWidget
  59. /// 这个过程中会触发 tabWidget 的 currentChanged,
  60. /// 所以使用 m_isSettingTable 标记此过程, 以采取必要措施来规避一些异常操作
  61. m_isSettingTable = true;
  62. m_dataTab->clear();
  63. for (CNodeData n : m_mind1->nodesInLevel(m_currentPage)) {
  64. QTableView *t = new QTableView(m_dataTab);
  65. t->setAlternatingRowColors(m_mind2->nodeList().count() > 0);
  66. t->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
  67. t->horizontalHeader()->setStyleSheet("QHeaderView::section{background:rgb(244,244,244);color: black;}");
  68. t->verticalHeader()->setStyleSheet("QHeaderView::section{background:rgb(244,244,244);color: black;}");
  69. t->verticalHeader()->setDefaultAlignment(Qt::AlignCenter);
  70. t->setSelectionMode(QAbstractItemView::SingleSelection);
  71. m_dataTab->addTab(t, n.name);
  72. QStandardItemModel *model = new QStandardItemModel(t);
  73. t->setModel(model);
  74. }
  75. m_isSettingTable = false;
  76. }
  77. int DataTableWidget::currentPage() const
  78. {
  79. return m_currentPage;
  80. }
  81. void DataTableWidget::setCurrentPage(int p)
  82. {
  83. if (p < 1 || p >= m_mind1->levels()) {
  84. return;
  85. }
  86. m_currentPage = p;
  87. setupTabWidget();
  88. m_pageLab->setText(QString("共 %1 页, 当前第 %2 页").arg(m_mind1->levels() - 1).arg(p));
  89. updateCurrentTable();
  90. }
  91. void DataTableWidget::updateCurrentTable()
  92. {
  93. int c = m_dataTab->currentIndex();
  94. QTableView *table = (QTableView *)m_dataTab->widget(c);
  95. if (table == nullptr || table->model() == nullptr || m_isSettingTable) {
  96. return;
  97. }
  98. QStandardItemModel *model = (QStandardItemModel *)table->model();
  99. /// 设置顶部水平方向标题
  100. int hIndex = 0;
  101. // 导入评估数据时, 增加一列指标权重
  102. if (m_process.type == SchemePlanManager::ImportEvalData) {
  103. QStandardItem *item = new QStandardItem("指标权重");
  104. model->setHorizontalHeaderItem(hIndex++, item);
  105. }
  106. // 指标
  107. CNodeData n = m_mind1->nodesInLevel(m_currentPage)[c];
  108. QList<CNodeData> hList = m_mind1->subNodes(n);
  109. // 以下情况需要显示指标
  110. // 导入权重分析数据
  111. // 导入需求分析评估的评估数据
  112. if (m_process.type == SchemePlanManager::ImportWeightData || m_process.indexType == ProjectManager::TechIndex) {
  113. for (CNodeData node : hList) {
  114. QStandardItem *item = new QStandardItem(node.name);
  115. item->setToolTip(node.remark);
  116. model->setHorizontalHeaderItem(hIndex++, item);
  117. }
  118. }
  119. // 导入方案优选评估或效能评估的评估数据时, 需要显示量纲
  120. if (m_process.type == SchemePlanManager::ImportEvalData
  121. && (m_process.indexType == ProjectManager::OptimalIndex
  122. || m_process.indexType == ProjectManager::EfficiencyIndex)) {
  123. QStandardItem *item = new QStandardItem("指标量纲");
  124. model->setHorizontalHeaderItem(hIndex++, item);
  125. }
  126. // 导入方案优选评估的评估数据时, 需要显示指标类型
  127. if (m_process.type == SchemePlanManager::ImportEvalData && m_process.indexType == ProjectManager::OptimalIndex) {
  128. QStandardItem *item = new QStandardItem("指标类型");
  129. model->setHorizontalHeaderItem(hIndex++, item);
  130. }
  131. /// 设置左侧垂直方向标题
  132. QList<CNodeData> vList;
  133. // 导入权重分析的专家数据时, 显示指标
  134. if (m_process.type == SchemePlanManager::ImportWeightData && m_process.dSource == SchemePlanManager::FromExpert) {
  135. vList = hList;
  136. }
  137. // 导入评估数据时, 显示最后一级指标
  138. if (m_process.type == SchemePlanManager::ImportEvalData) {
  139. if (m_process.indexType == ProjectManager::TechIndex) {
  140. vList = m_mind2->leaves();
  141. } else {
  142. vList = m_mind1->leaves();
  143. }
  144. }
  145. for (int i = 0; i < vList.count(); i++) {
  146. CNodeData node = vList[i];
  147. QStandardItem *item = new QStandardItem(QString(" %1 ").arg(node.name));
  148. item->setToolTip(node.remark);
  149. model->setVerticalHeaderItem(i, item);
  150. table->setRowHeight(i, 35);
  151. }
  152. return;
  153. /// 填充单元格
  154. for (int i = 0; i < vList.count(); i++) {
  155. for (int j = 0; j < hList.count(); j++) {
  156. QStandardItem *item = new QStandardItem();
  157. item->setEditable(false);
  158. if (m_mind2->nodeList().count() <= 0) {
  159. if (i == j) {
  160. item->setText("1"); // 对角线
  161. }
  162. if (i >= j) {
  163. item->setBackground(QBrush(QColor("lightgray"))); // 左下方
  164. }
  165. }
  166. item->setData(Qt::AlignCenter, Qt::TextAlignmentRole); // 单元格文字居中
  167. model->setItem(i, j, item);
  168. }
  169. table->setRowHeight(i, 35);
  170. }
  171. }
  172. CMind *DataTableWidget::mind1() const
  173. {
  174. return m_mind1;
  175. }
  176. CMind *DataTableWidget::mind2() const
  177. {
  178. return m_mind2;
  179. }
  180. void DataTableWidget::slotPrevious()
  181. {
  182. setCurrentPage(m_currentPage - 1);
  183. }
  184. void DataTableWidget::slotNext()
  185. {
  186. setCurrentPage(m_currentPage + 1);
  187. }
  188. void DataTableWidget::slotTabCurrentChanged(int c)
  189. {
  190. Q_UNUSED(c)
  191. updateCurrentTable();
  192. }