#include "AnalysisTableWidget.h" #include "RequirementImportance.h" #include "algorithm/ConsistencyCheck.h" #include #include #include #include #include "dbService/ClassSet.h" #include #include #include #include AnalysisTableWidget::AnalysisTableWidget(QWidget *parent) : QTableView(parent) { qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime())); // qDebug() << "AnalysisTableWidget---init"; model = new QStandardItemModel(); this->setModel(model); paintDone = false; connect(model, &QStandardItemModel::itemChanged, this, &AnalysisTableWidget::nodeValueChanged); } AnalysisTableWidget::~AnalysisTableWidget() { } void AnalysisTableWidget::addRowNode(QString node, QString name, QString remark) { matrixRows << AnalysisTableWidget::MatrixNode(node, name, remark); } void AnalysisTableWidget::addColNode(QString node, QString name, QString remark) { matrixCols << AnalysisTableWidget::MatrixNode(node, name, remark); } QList AnalysisTableWidget::getSource() const { return dataSource; } void AnalysisTableWidget::setTableWeight(QList firstWeight, QList secondWeight) { m_firstTableWeight = firstWeight; m_secondTableWeight = secondWeight; } void AnalysisTableWidget::setTableIndexAndTableMsg(int index, int engineerId, QString expertId, QString tableMsg) { m_tableIndex = index; m_engineerId = engineerId; m_expertId = expertId; m_table_msg = tableMsg; } void AnalysisTableWidget::setTableMethod(QString method) { m_resultMethod = method; } void AnalysisTableWidget::paintMatrixTable(QList nodeValueInfoList, int type) { int row = matrixRows.count(); int col = matrixCols.count(); this->horizontalHeader()->setStretchLastSection(true); this->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); this->setSelectionMode(QAbstractItemView::SingleSelection); this->setSelectionBehavior(QAbstractItemView::SelectItems); model->setHorizontalHeaderItem(0, new QStandardItem("")); if (type == IndexType::IndexTech || type == IndexType::SchemaEval) { for (int c = 0; c < col; ++c) { model->setHorizontalHeaderItem(c + 1, new QStandardItem(matrixCols[c].name)); model->horizontalHeaderItem(c + 1)->setToolTip(matrixCols[c].remark); model->horizontalHeaderItem(c + 1)->setTextAlignment(Qt::AlignCenter); } model->setHorizontalHeaderItem(col + 1, new QStandardItem("权重")); model->horizontalHeaderItem(col + 1)->setToolTip("权重"); QStringList nodes; QVector nxn((int)qPow(row, 2), 0); for (int r = 0; r < row; ++r) { model->setItem(r, 0, new QStandardItem(matrixRows[r].name)); nodes << model->item(r, 0)->text().trimmed(); model->item(r, 0)->setToolTip(matrixRows[r].remark); model->item(r, 0)->setTextAlignment(Qt::AlignCenter); model->item(r, 0)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); for (int c = 1; c < col + 1; ++c) { AnalysisDataSource data; data.row = r; data.col = c; data.changed = true; if (nodeValueInfoList.size() > 0) { for (NodeMatrixInfo *nodeMatrixInfo : nodeValueInfoList) { if (nodeMatrixInfo->abscissa == matrixRows[r].name && nodeMatrixInfo->ordinate == matrixCols[c - 1].name) { data.nodeValue = nodeMatrixInfo->nodeValue; } } } else { // data.nodeValue = "0"; data.nodeValue = ""; } model->setItem(r, c, new QStandardItem(data.nodeValue)); QStringList content = model->item(r, c)->text().trimmed().split("/"); if (content.size() == 1) { nxn[r * col + c - 1] = content[0].toDouble(); } else { nxn[r * col + c - 1] = content[0].toDouble() / content[1].toDouble(); } model->item(r, c)->setTextAlignment(Qt::AlignCenter); model->item(r, c)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); data.node = matrixRows[r].node; data.abscissa = matrixRows[r].name; data.ordinate = matrixCols[c - 1].name; dataSource << data; } } ConsistencyCheck cc(nodes, nxn); for (int r = 0; r < row; ++r) { model->setItem(r, col + 1, new QStandardItem(QString::number(cc.getWeights().at(r)) == "nan" ? "" : QString::number(cc.getWeights().at(r)))); model->item(r, col + 1)->setTextAlignment(Qt::AlignCenter); model->item(r, col + 1)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); } QStringList nodeList = cc.getNodes(); QVector weights = cc.getWeights(); // qDebug() << weights; QList list; for (int i = 0; i < weights.length(); i++) { DemandWeight *demandWeight = new DemandWeight(); demandWeight->nodeName = nodeList.at(i); demandWeight->nodeWeight = weights.at(i); demandWeight->nodeValue = weights.at(i); demandWeight->engineerId = m_engineerId; demandWeight->expertId = m_expertId; demandWeight->isValid = 1; demandWeight->tableIndex = m_tableIndex; demandWeight->tableMsg = m_table_msg; demandWeight->pageIndex = 0; list.append(demandWeight); } if (DemandWeightService().QueryByTableIndexAndTableMsg(m_expertId, m_engineerId, m_tableIndex, m_table_msg)) { DemandWeightService().UpdateNodeValueList(list); } else { DemandWeightService().AddNodeWeightInfoList(list); } } else { // 获取到指标体系需求重要度 model->setHorizontalHeaderItem(1, new QStandardItem("权重")); model->horizontalHeaderItem(1)->setToolTip("权重"); for (int c = 0; c < col; ++c) { model->setHorizontalHeaderItem(c + 2, new QStandardItem(matrixCols[c].name)); model->horizontalHeaderItem(c + 2)->setToolTip(matrixCols[c].remark); } for (int r = 0; r < row; ++r) { model->setItem(r, 0, new QStandardItem(matrixRows[r].name)); model->item(r, 0)->setToolTip(matrixRows[r].remark); model->item(r, 0)->setTextAlignment(Qt::AlignCenter); model->item(r, 0)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); if (r >= m_secondTableWeight.count()) { model->setItem(r, 1, new QStandardItem("")); } else { model->setItem(r, 1, new QStandardItem(QString::number(m_secondTableWeight.at(r)->nodeValue) == "nan" ? "" : QString::number(m_secondTableWeight.at(r)->nodeValue))); } model->item(r, 1)->setTextAlignment(Qt::AlignCenter); model->item(r, 1)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); for (int c = 2; c < col + 2; ++c) { AnalysisDataSource data; data.row = r; data.col = c; data.changed = true; if (nodeValueInfoList.size() > 0) { for (NodeMatrixInfo *nodeMatrixInfo : nodeValueInfoList) { if (nodeMatrixInfo->abscissa == matrixRows[r].name && nodeMatrixInfo->ordinate == matrixCols[c - 2].name) { data.nodeValue = nodeMatrixInfo->nodeValue; } } } else { data.nodeValue = ""; } model->setItem(r, c, new QStandardItem(data.nodeValue)); model->item(r, c)->setTextAlignment(Qt::AlignCenter); model->item(r, c)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); data.node = matrixRows[r].node; data.abscissa = matrixRows[r].name; data.ordinate = matrixCols[c - 2].name; dataSource << data; } } for (int c = 0; c < col + 2; ++c) { if (c == 0) { model->setItem(row, c, new QStandardItem("技术重要度")); } else if (c > 1 && c < col + 2) { qreal value = 0; for (int r = 0; r < row; r++) { if (r < m_secondTableWeight.count()) { QStandardItem *item = model->item(r, c); QStringList content = item->text().trimmed().split("/"); double itemValue = 0; if (content.size() == 1) { itemValue = content[0].toDouble(); } else { itemValue = content[0].toDouble() / content[1].toDouble(); } double tempValue = 0; if (QString::number(m_secondTableWeight.at(r)->nodeValue) != "nan") { tempValue = m_secondTableWeight.at(r)->nodeValue; } value += itemValue * tempValue; } } model->setItem(row, c, new QStandardItem(QString::number(value))); } else { model->setItem(row, c, new QStandardItem("")); } model->item(row, c)->setTextAlignment(Qt::AlignCenter); model->item(row, c)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); } // 保存技术重要度 QList importList; for (int c = 0; c < col; c++) { QStandardItem *item = model->item(row, c + 2); TechnicalImport *import = new TechnicalImport(); import->engineerId = m_engineerId; import->expertId = m_expertId.toInt(); import->nodeName = matrixCols[c].name; import->nodeValue = item->text().toDouble(); importList.append(import); } if (DBServiceSet().QueryTechnicalImportInfoByEngineerId(m_expertId.toInt(), m_engineerId)) { DBServiceSet().UpdateTechnicalImportInfoList(importList); } else { DBServiceSet().AddTechnicalImportInfoList(importList); } } emit returnModel(model); paintDone = true; } void AnalysisTableWidget::paintCompositeMatrixTable(QList nodeValueInfoList, int type) { int row = matrixRows.count(); int col = matrixCols.count(); this->horizontalHeader()->setStretchLastSection(true); this->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); this->setSelectionMode(QAbstractItemView::SingleSelection); this->setSelectionBehavior(QAbstractItemView::SelectItems); model->setHorizontalHeaderItem(0, new QStandardItem("")); if (type == IndexType::IndexTech || type == IndexType::SchemaEval) { for (int c = 0; c < col; ++c) { model->setHorizontalHeaderItem(c + 1, new QStandardItem(matrixCols[c].name)); model->horizontalHeaderItem(c + 1)->setToolTip(matrixCols[c].remark); } model->setHorizontalHeaderItem(col + 1, new QStandardItem("权重")); model->horizontalHeaderItem(col + 1)->setToolTip("权重"); QStringList nodes; QVector nxn((int)qPow(row, 2), 0); for (int r = 0; r < row; ++r) { model->setItem(r, 0, new QStandardItem(matrixRows[r].name)); nodes << model->item(r, 0)->text().trimmed(); model->item(r, 0)->setToolTip(matrixRows[r].remark); model->item(r, 0)->setTextAlignment(Qt::AlignCenter); model->item(r, 0)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); for (int c = 1; c < col + 1; ++c) { AnalysisDataSource data; data.row = r; data.col = c; data.changed = true; for (NodeMatrixInfo *nodeMatrixInfo : nodeValueInfoList) { if (nodeMatrixInfo->abscissa == matrixRows[r].name && nodeMatrixInfo->ordinate == matrixCols[c - 1].name) { data.nodeValue = nodeMatrixInfo->nodeValue; } } model->setItem(r, c, new QStandardItem(data.nodeValue)); QStringList content = model->item(r, c)->text().trimmed().split("/"); if (content.size() == 1) { nxn[r * col + c - 1] = content[0].toDouble(); } else { nxn[r * col + c - 1] = content[0].toDouble() / content[1].toDouble(); } model->item(r, c)->setTextAlignment(Qt::AlignCenter); model->item(r, c)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); data.node = matrixRows[r].node; data.abscissa = matrixRows[r].name; data.ordinate = matrixCols[c - 1].name; dataSource << data; } } ConsistencyCheck cc(nodes, nxn); for (int r = 0; r < row; ++r) { model->setItem(r, col + 1, new QStandardItem(QString::number(cc.getWeights().at(r)))); model->item(r, col + 1)->setTextAlignment(Qt::AlignCenter); model->item(r, col + 1)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); } QStringList nodeList = cc.getNodes(); QVector weights = cc.getWeights(); QList list; for (int i = 0; i < weights.length(); i++) { DemandWeight *demandWeight = new DemandWeight(); demandWeight->nodeName = nodeList.at(i); demandWeight->nodeWeight = weights.at(i); demandWeight->nodeValue = weights.at(i); demandWeight->engineerId = m_engineerId; demandWeight->expertId = m_expertId; demandWeight->isValid = 1; demandWeight->tableIndex = m_tableIndex; demandWeight->tableMsg = m_table_msg; list.append(demandWeight); } if (DemandWeightService().QueryByTableIndexAndTableMsg(m_expertId, m_engineerId, m_tableIndex, m_table_msg)) { DemandWeightService().UpdateNodeValueList(list); } else { DemandWeightService().AddNodeWeightInfoList(list); } } else { // 获取到指标体系需求重要度 model->setHorizontalHeaderItem(1, new QStandardItem("权重")); model->horizontalHeaderItem(1)->setToolTip("权重"); for (int c = 0; c < col; ++c) { model->setHorizontalHeaderItem(c + 2, new QStandardItem(matrixCols[c].name)); model->horizontalHeaderItem(c + 2)->setToolTip(matrixCols[c].remark); } for (int r = 0; r < row; ++r) { model->setItem(r, 0, new QStandardItem(matrixRows[r].name)); model->item(r, 0)->setToolTip(matrixRows[r].remark); model->item(r, 0)->setTextAlignment(Qt::AlignCenter); model->item(r, 0)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); if (r >= m_secondTableWeight.count()) { model->setItem(r, 1, new QStandardItem("")); } else { model->setItem(r, 1, new QStandardItem(QString::number(m_secondTableWeight.at(r)->nodeValue))); } model->item(r, 1)->setTextAlignment(Qt::AlignCenter); model->item(r, 1)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); for (int c = 2; c < col + 2; ++c) { AnalysisDataSource data; data.row = r; data.col = c; data.changed = true; for (NodeMatrixInfo *nodeMatrixInfo : nodeValueInfoList) { if (nodeMatrixInfo->abscissa == matrixRows[r].name && nodeMatrixInfo->ordinate == matrixCols[c - 2].name) { data.nodeValue = nodeMatrixInfo->nodeValue; } } model->setItem(r, c, new QStandardItem(data.nodeValue)); model->item(r, c)->setTextAlignment(Qt::AlignCenter); model->item(r, c)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); data.node = matrixRows[r].node; data.abscissa = matrixRows[r].name; data.ordinate = matrixCols[c - 2].name; dataSource << data; } } for (int c = 0; c < col + 2; ++c) { if (c == 0) { model->setItem(row, c, new QStandardItem("技术重要度")); } else if (c > 1 && c < col + 2) { qreal value = 0; for (int r = 0; r < row; r++) { if (r < m_secondTableWeight.count()) { QStandardItem *item = model->item(r, c); QStringList content = item->text().trimmed().split("/"); double itemValue = 0; if (content.size() == 1) { itemValue = content[0].toDouble(); } else { itemValue = content[0].toDouble() / content[1].toDouble(); } value += itemValue * m_secondTableWeight.at(r)->nodeValue; } } model->setItem(row, c, new QStandardItem(QString::number(value))); } else { model->setItem(row, c, new QStandardItem("")); } model->item(row, c)->setTextAlignment(Qt::AlignCenter); model->item(row, c)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); } } emit returnModel(model); paintDone = true; } void AnalysisTableWidget::paintSecondMatrixTable(QList nodeValueInfoList, int page, int type, int index) { // qDebug() << "----------o---" << index; int row = matrixRows.count(); int col = matrixCols.count(); this->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); this->setSelectionMode(QAbstractItemView::SingleSelection); this->setSelectionBehavior(QAbstractItemView::SelectItems); model->setHorizontalHeaderItem(0, new QStandardItem("")); if (type == IndexType::IndexTech || type == IndexType::SchemaEval) { for (int c = 0; c < col; ++c) { model->setHorizontalHeaderItem(c + 1, new QStandardItem(matrixCols[c].name)); model->horizontalHeaderItem(c + 1)->setToolTip(matrixCols[c].remark); } model->setHorizontalHeaderItem(col + 1, new QStandardItem("权重")); model->horizontalHeaderItem(col + 1)->setToolTip("权重"); model->setHorizontalHeaderItem(col + 2, new QStandardItem("需求重要度")); model->horizontalHeaderItem(col + 2)->setToolTip("需求重要度"); QStringList nodes; QVector nxn((int)qPow(row, 2), 0); for (int r = 0; r < row; ++r) { model->setItem(r, 0, new QStandardItem(matrixRows[r].name)); nodes << model->item(r, 0)->text().trimmed(); model->item(r, 0)->setToolTip(matrixRows[r].remark); model->item(r, 0)->setTextAlignment(Qt::AlignCenter); model->item(r, 0)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); for (int c = 1; c < col + 1; ++c) { AnalysisDataSource data; data.row = r; data.col = c; data.changed = true; if (nodeValueInfoList.size() > 0) { for (NodeMatrixInfo *nodeMatrixInfo : nodeValueInfoList) { if (nodeMatrixInfo->abscissa == matrixRows[r].name && nodeMatrixInfo->ordinate == matrixCols[c - 1].name) { data.nodeValue = nodeMatrixInfo->nodeValue; } } } else { // data.nodeValue = "0"; data.nodeValue = ""; } model->setItem(r, c, new QStandardItem(data.nodeValue)); QStringList content = model->item(r, c)->text().trimmed().split("/"); if (content.size() == 1) { nxn[r * col + c - 1] = content[0].toDouble(); } else { nxn[r * col + c - 1] = content[0].toDouble() / content[1].toDouble(); } model->item(r, c)->setTextAlignment(Qt::AlignCenter); model->item(r, c)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); data.node = matrixRows[r].node; data.abscissa = matrixRows[r].name; data.ordinate = matrixCols[c - 1].name; dataSource << data; } } ConsistencyCheck cc(nodes, nxn); for (int r = 0; r < row; ++r) { model->setItem(r, col + 1, new QStandardItem(QString::number(cc.getWeights().at(r)) == "nan" ? "" : QString::number(cc.getWeights().at(r)))); model->item(r, col + 1)->setTextAlignment(Qt::AlignCenter); model->item(r, col + 1)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); qreal firstWeight = 0; if (m_firstTableWeight.size() > 0) { firstWeight = m_firstTableWeight.at(index - 1)->nodeWeight; } qreal value = cc.getWeights().at(r) * firstWeight; model->setItem(r, col + 2, new QStandardItem(QString::number(value) == "nan" ? "" : QString::number(value))); model->item(r, col + 2)->setTextAlignment(Qt::AlignCenter); model->item(r, col + 2)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); } // 校验成功持久化数据 QStringList nodeList = cc.getNodes(); QVector weights = cc.getWeights(); QList list; QList firstList; // DBServiceSet().QueryByTableIndexAndTableMsg(&firstList, m_expertId, m_engineerId, 0, m_table_msg); DemandWeightService().QueryByTableIndexAndTableMsg(&firstList, m_expertId, m_engineerId, page - 1, m_table_msg); double weight = 0; // if (firstList.size() != 0) { weight = m_firstTableWeight.at(index - 1)->nodeWeight; //} for (int i = 0; i < weights.length(); i++) { DemandWeight *demandWeight = new DemandWeight(); demandWeight->nodeName = nodeList.at(i); demandWeight->nodeWeight = weights.at(i); demandWeight->engineerId = m_engineerId; demandWeight->expertId = m_expertId; demandWeight->isValid = 1; demandWeight->tableIndex = m_tableIndex; demandWeight->tableMsg = m_table_msg; demandWeight->nodeValue = demandWeight->nodeWeight * weight; demandWeight->pageIndex = page; list.append(demandWeight); } if (DemandWeightService().QueryByTableIndexAndTableMsgAndPage(m_expertId, m_engineerId, m_tableIndex, m_table_msg, page)) { DemandWeightService().UpdateNodeValueList(list); } else { DemandWeightService().AddNodeWeightInfoList(list); } } else { model->setHorizontalHeaderItem(1, new QStandardItem("权重")); model->horizontalHeaderItem(1)->setToolTip("权重"); for (int c = 0; c < col; ++c) { model->setHorizontalHeaderItem(c + 2, new QStandardItem(matrixCols[c].name)); model->horizontalHeaderItem(c + 2)->setToolTip(matrixCols[c].remark); } for (int r = 0; r < row; ++r) { model->setItem(r, 0, new QStandardItem(matrixRows[r].name)); model->item(r, 0)->setToolTip(matrixRows[r].remark); model->item(r, 0)->setTextAlignment(Qt::AlignCenter); model->item(r, 0)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); if (r >= m_secondTableWeight.count()) { model->setItem(r, 1, new QStandardItem("1")); } else { model->setItem(r, 1, new QStandardItem(QString::number(m_secondTableWeight.at(r)->nodeValue) == "nan" ? "" : QString::number(m_secondTableWeight.at(r)->nodeValue))); } model->item(r, 1)->setTextAlignment(Qt::AlignCenter); model->item(r, 1)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); for (int c = 2; c < col + 2; ++c) { AnalysisDataSource data; data.row = r; data.col = c; data.changed = true; if (nodeValueInfoList.size() > 0) { for (NodeMatrixInfo *nodeMatrixInfo : nodeValueInfoList) { if (nodeMatrixInfo->abscissa == matrixRows[r].name && nodeMatrixInfo->ordinate == matrixCols[c - 2].name) { data.nodeValue = nodeMatrixInfo->nodeValue; } } } else { data.nodeValue = ""; } model->setItem(r, c, new QStandardItem(data.nodeValue)); model->item(r, c)->setTextAlignment(Qt::AlignCenter); model->item(r, c)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); data.node = matrixRows[r].node; data.abscissa = matrixRows[r].name; data.ordinate = matrixCols[c - 2].name; dataSource << data; } } for (int c = 0; c < col + 2; ++c) { if (c == 0) { model->setItem(row, c, new QStandardItem("技术重要度")); } else if (c > 1 && c < col + 2) { qreal value = 0; for (int r = 0; r < row; r++) { if (r < m_secondTableWeight.count()) { QStandardItem *item = model->item(r, c); QStringList content = item->text().trimmed().split("/"); double itemValue = 0; if (content.size() == 1) { itemValue = content[0].toDouble(); } else { itemValue = content[0].toDouble() / content[1].toDouble(); } double tempValue = 0; if (QString::number(m_secondTableWeight.at(r)->nodeValue) != "nan") { tempValue = m_secondTableWeight.at(r)->nodeValue; } value += itemValue * tempValue; } } model->setItem(row, c, new QStandardItem(QString::number(value))); } else { model->setItem(row, c, new QStandardItem("")); } model->item(row, c)->setTextAlignment(Qt::AlignCenter); model->item(row, c)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); } } emit returnModel(model); paintDone = true; // qDebug() << "--------ok"; } void AnalysisTableWidget::nodeValueChanged(QStandardItem *item) { if (paintDone) { // qDebug() << item->column() << item->row() << item->text(); } } bool AnalysisTableWidget::isDataReady() const { if (dataSource.count() == 0) { return false; } for (AnalysisDataSource data : dataSource) { if (data.changed == false) { return false; } } return true; }