GroupTableWidget.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include "GroupTableWidget.h"
  2. #include "RequirementImportance.h"
  3. #include "algorithm/ConsistencyCheck.h"
  4. #include <QDebug>
  5. #include <QHeaderView>
  6. #include <QTime>
  7. #include <QtMath>
  8. #include "dbService/ClassSet.h"
  9. GroupTableWidget::GroupTableWidget(QWidget *parent) : QTableView(parent)
  10. {
  11. qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
  12. model = new QStandardItemModel();
  13. this->setModel(model);
  14. }
  15. GroupTableWidget::~GroupTableWidget() { }
  16. void GroupTableWidget::addRowNode(QString node, QString name, QString remark)
  17. {
  18. matrixRows << GroupTableWidget::MatrixNode(node, name, remark);
  19. }
  20. void GroupTableWidget::addColNode(QString node, QString name, QString remark)
  21. {
  22. matrixCols << GroupTableWidget::MatrixNode(node, name, remark);
  23. }
  24. void GroupTableWidget::setTableType(int type)
  25. {
  26. tableType = type;
  27. }
  28. QList<AnalysisDataSource> GroupTableWidget::getSource() const
  29. {
  30. return dataSource;
  31. }
  32. void GroupTableWidget::paintDemandTable(QList<QList<DemandWeight *>> nodeValueInfoList, QString arithmeticMethod)
  33. {
  34. int row = matrixRows.count();
  35. int col = matrixCols.count();
  36. this->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
  37. this->setSelectionMode(QAbstractItemView::SingleSelection);
  38. this->setSelectionBehavior(QAbstractItemView::SelectItems);
  39. model->setHorizontalHeaderItem(0, new QStandardItem(""));
  40. for (int c = 0; c < col; ++c) {
  41. model->setHorizontalHeaderItem(c + 1, new QStandardItem(matrixCols[c].name));
  42. model->horizontalHeaderItem(c + 1)->setToolTip(matrixCols[c].name);
  43. }
  44. model->setHorizontalHeaderItem(col + 1, new QStandardItem("群策结果"));
  45. model->horizontalHeaderItem(col + 1)->setToolTip("群策结果");
  46. // 第一行权重渲染
  47. for (int c = 0; c < col + 1; c++) {
  48. if (c == 0) {
  49. model->setItem(0, c, new QStandardItem("专家权重"));
  50. model->item(0, c)->setToolTip("专家权重");
  51. model->item(0, c)->setTextAlignment(Qt::AlignCenter);
  52. model->item(0, c)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
  53. } else {
  54. model->setItem(0, c, new QStandardItem(matrixCols[c - 1].remark));
  55. model->item(0, c)->setTextAlignment(Qt::AlignCenter);
  56. model->item(0, c)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
  57. }
  58. }
  59. for (int r = 1; r < row + 1; ++r) {
  60. model->setItem(r, 0, new QStandardItem(matrixRows[r - 1].name));
  61. // qDebug() << "test" << matrixRows[r - 1].name;
  62. model->item(r, 0)->setToolTip(matrixRows[r - 1].remark);
  63. model->item(r, 0)->setTextAlignment(Qt::AlignCenter);
  64. model->item(r, 0)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
  65. }
  66. for (int c = 1; c < col + 1; ++c) {
  67. QList<DemandWeight *> list = nodeValueInfoList.at(c - 1);
  68. for (int r = 1; r < row + 1; ++r) {
  69. AnalysisDataSource data;
  70. data.row = r;
  71. data.col = c;
  72. data.changed = true;
  73. // data.nodeValue = "";
  74. for (DemandWeight *demandWeight : list) {
  75. if (demandWeight->nodeName == matrixRows[r - 1].name) {
  76. if (tableType == IndexType::SchemaEval) {
  77. double tempValue = demandWeight->nodeWeight;
  78. if (QString::number(tempValue) != "nan") {
  79. data.nodeValue = QString::number(tempValue);
  80. } else {
  81. // data.nodeValue = "0";
  82. }
  83. } else {
  84. double tempValue = demandWeight->nodeValue;
  85. if (QString::number(tempValue) != "nan") {
  86. data.nodeValue = QString::number(tempValue);
  87. } else {
  88. // data.nodeValue = "0";
  89. }
  90. }
  91. break;
  92. }
  93. }
  94. model->setItem(r, c, new QStandardItem(data.nodeValue));
  95. model->item(r, c)->setTextAlignment(Qt::AlignCenter);
  96. model->item(r, c)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
  97. data.node = matrixRows[r - 1].node;
  98. data.abscissa = matrixRows[r - 1].name;
  99. data.ordinate = matrixCols[c - 1].name;
  100. dataSource << data;
  101. }
  102. }
  103. // 最后一列进行计算
  104. bool valid = false;
  105. if (arithmeticMethod == "arithmetic") {
  106. for (int r = 1; r < row + 1; r++) {
  107. qreal value = 0;
  108. for (int c = 1; c < col + 1; c++) {
  109. QStandardItem *item0 = model->item(0, c);
  110. QStandardItem *item = model->item(r, c);
  111. QString text0 = item0->text();
  112. QString text = item->text();
  113. if (text != "") {
  114. valid = true;
  115. }
  116. value += text0.toDouble() * text.toDouble();
  117. }
  118. model->setItem(r, col + 1, new QStandardItem(valid ? QString::number(value) : ""));
  119. model->item(r, col + 1)->setTextAlignment(Qt::AlignCenter);
  120. model->item(r, col + 1)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
  121. }
  122. } else {
  123. QList<double> valueList;
  124. double sum = 0;
  125. for (int r = 1; r < row + 1; r++) {
  126. qreal value = 1;
  127. for (int c = 1; c < col + 1; c++) {
  128. QStandardItem *item0 = model->item(0, c);
  129. QStandardItem *item = model->item(r, c);
  130. if (item->text() != "" && item0->text() != "") {
  131. valid = true;
  132. }
  133. double temp = qPow(item->text().toDouble(), item0->text().toDouble());
  134. value *= temp;
  135. }
  136. value = sqrt(value);
  137. valueList.append(value);
  138. sum += value;
  139. }
  140. for (int r = 1; r < row + 1; r++) {
  141. model->setItem(r, col + 1, new QStandardItem(valid ? QString::number(valueList.at(r - 1) / sum) : ""));
  142. model->item(r, col + 1)->setTextAlignment(Qt::AlignCenter);
  143. model->item(r, col + 1)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
  144. }
  145. }
  146. emit returnModel(model);
  147. }
  148. void GroupTableWidget::paintTechnicalTable(QList<QList<TechnicalImport *>> nodeValueInfoList, QString arithmeticMethod)
  149. {
  150. if (nodeValueInfoList.isEmpty()) {
  151. return;
  152. }
  153. int row = matrixRows.count();
  154. int col = matrixCols.count();
  155. this->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
  156. this->setSelectionMode(QAbstractItemView::SingleSelection);
  157. this->setSelectionBehavior(QAbstractItemView::SelectItems);
  158. model->setHorizontalHeaderItem(0, new QStandardItem(""));
  159. for (int c = 0; c < col; ++c) {
  160. model->setHorizontalHeaderItem(c + 1, new QStandardItem(matrixCols[c].name));
  161. model->horizontalHeaderItem(c + 1)->setToolTip(matrixCols[c].name);
  162. }
  163. model->setHorizontalHeaderItem(col + 1, new QStandardItem("技术重要度"));
  164. model->horizontalHeaderItem(col + 1)->setToolTip("技术重要度");
  165. // 第一行权重渲染
  166. for (int c = 0; c < col + 1; c++) {
  167. if (c == 0) {
  168. model->setItem(0, c, new QStandardItem("专家权重"));
  169. model->item(0, c)->setToolTip("专家权重");
  170. model->item(0, c)->setTextAlignment(Qt::AlignCenter);
  171. model->item(0, c)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
  172. } else {
  173. model->setItem(0, c, new QStandardItem(matrixCols[c - 1].remark));
  174. model->item(0, c)->setTextAlignment(Qt::AlignCenter);
  175. model->item(0, c)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
  176. }
  177. }
  178. for (int r = 1; r < row + 1; ++r) {
  179. model->setItem(r, 0, new QStandardItem(matrixRows[r - 1].name));
  180. model->item(r, 0)->setToolTip(matrixRows[r - 1].remark);
  181. model->item(r, 0)->setTextAlignment(Qt::AlignCenter);
  182. model->item(r, 0)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
  183. }
  184. for (int c = 1; c < col + 1; ++c) {
  185. QList<TechnicalImport *> list = nodeValueInfoList.at(c - 1);
  186. for (int r = 1; r < row + 1; ++r) {
  187. AnalysisDataSource data;
  188. data.row = r;
  189. data.col = c;
  190. data.changed = true;
  191. for (TechnicalImport *demandWeight : list) {
  192. if (demandWeight->nodeName == matrixRows[r - 1].name) {
  193. data.nodeValue = QString::number(demandWeight->nodeValue);
  194. break;
  195. } else {
  196. data.nodeValue = "0";
  197. // data.nodeValue = "";
  198. }
  199. }
  200. model->setItem(r, c, new QStandardItem(data.nodeValue));
  201. model->item(r, c)->setTextAlignment(Qt::AlignCenter);
  202. model->item(r, c)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
  203. data.node = matrixRows[r - 1].node;
  204. data.abscissa = matrixRows[r - 1].name;
  205. data.ordinate = matrixCols[c - 1].name;
  206. dataSource << data;
  207. }
  208. }
  209. // 最后一列进行计算
  210. if (arithmeticMethod == "arithmetic") {
  211. for (int r = 1; r < row + 1; r++) {
  212. qreal value = 0;
  213. for (int c = 1; c < col + 1; c++) {
  214. for (int c = 1; c < col + 1; c++) {
  215. QStandardItem *item0 = model->item(0, c);
  216. QStandardItem *item = model->item(r, c);
  217. value += item0->text().toDouble() * item->text().toDouble();
  218. }
  219. model->setItem(r, col + 1, new QStandardItem(QString::number(value)));
  220. model->item(r, col + 1)->setTextAlignment(Qt::AlignCenter);
  221. model->item(r, col + 1)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
  222. }
  223. }
  224. } else {
  225. double sum = 0;
  226. QList<double> valueList;
  227. for (int r = 1; r < row + 1; r++) {
  228. qreal value = 1;
  229. for (int c = 1; c < col + 1; c++) {
  230. QStandardItem *item0 = model->item(0, c);
  231. QStandardItem *item = model->item(r, c);
  232. double temp = qPow(item->text().toDouble(), item0->text().toDouble());
  233. value *= temp;
  234. }
  235. value = sqrt(value);
  236. valueList.append(value);
  237. sum += value;
  238. model->setItem(r, col + 1, new QStandardItem(QString::number(value)));
  239. model->item(r, col + 1)->setTextAlignment(Qt::AlignCenter);
  240. model->item(r, col + 1)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
  241. }
  242. }
  243. emit returnModel(model);
  244. }