#include "EXDataTableView.h" #include "EXDataViewDelegate.h" #include "ProjectManager.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include EXDataTableView::EXDataTableView(SchemePlanManager::SchemeProcessInfo process, QWidget *parent) : QWidget(parent), m_process(process) { m_mind1 = new CMind(this); m_mind2 = new CMind(this); initWidget(); initLayout(); connectSignalsAndSlots(); m_comboDelegate = new EXDataTableComboDelegate(this); m_user = new QFUser(); UserService().QueryUserInfoById(m_user, 62); m_export = new QXlsx::Document(this); } SchemePlanManager::SchemeProcessInfo EXDataTableView::process() const { return m_process; } void EXDataTableView::setProjectInfo(ProjectInfo *proj) { m_proj = proj; } void EXDataTableView::initWidget() { m_dataTab = new QTabWidget(this); m_dataTab->setTabPosition(QTabWidget::South); m_pageLab = new QLabel(this); m_previous = new PushButton("上一级指标", this); m_next = new PushButton("下一级指标", this); m_save = new PushButton("保存", this); } void EXDataTableView::initLayout() { m_layout = new QVBoxLayout(this); m_layout->addWidget(m_dataTab); m_pageLayout = new QHBoxLayout(); m_layout->addLayout(m_pageLayout); m_pageLayout->setSpacing(10); m_pageLayout->addStretch(); m_pageLayout->addWidget(m_previous); m_pageLayout->addWidget(m_pageLab); m_pageLayout->addWidget(m_next); m_pageLayout->addStretch(); m_pageLayout->addWidget(m_save); } void EXDataTableView::connectSignalsAndSlots() { connect(m_previous, &PushButton::clicked, this, &EXDataTableView::slotPrevious); connect(m_next, &PushButton::clicked, this, &EXDataTableView::slotNext); connect(m_dataTab, &QTabWidget::currentChanged, this, &EXDataTableView::slotTabCurrentChanged); connect(m_save, &PushButton::clicked, this, &EXDataTableView::slotSave); } void EXDataTableView::setupTabWidget() { /// 创建 tableView 并添加进 tabWidget /// 这个过程中会触发 tabWidget 的 currentChanged, /// 所以使用 m_isSettingTable 标记此过程, 以采取必要措施来规避一些异常操作 m_isSettingTable = true; m_dataTab->clear(); QList nodeList = m_mind1->nodesInLevel(m_currentPage); for (int i = 0; i < nodeList.count(); i++) { CNodeData n = nodeList[i]; QTableView *t = new QTableView(m_dataTab); t->setAlternatingRowColors(m_mind2->nodeList().count() > 0); t->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); t->horizontalHeader()->setStyleSheet("QHeaderView::section{background:rgb(244,244,244);color: black;}"); t->verticalHeader()->setStyleSheet("QHeaderView::section{background:rgb(244,244,244);color: black;}"); t->verticalHeader()->setDefaultAlignment(Qt::AlignCenter); t->setSelectionMode(QAbstractItemView::SingleSelection); m_dataTab->addTab(t, n.name); QList modelList; if (m_models.keys().contains(m_currentPage)) { modelList = m_models[m_currentPage]; } QStandardItemModel *model; if (modelList.count() <= i) { model = new QStandardItemModel(t); modelList.append(model); } else { model = modelList[i]; } m_models[m_currentPage] = modelList; t->setModel(model); connect(t, &QTableView::clicked, this, &EXDataTableView::slotItemClicked); } m_isSettingTable = false; } int EXDataTableView::currentPage() const { return m_currentPage; } void EXDataTableView::setCurrentPage(int p) { if (p < 1 || p >= m_mind1->levels()) { return; } m_currentPage = p; m_previous->setEnabled(p > 1); m_next->setEnabled(p < m_mind1->levels() - 1); setupTabWidget(); m_pageLab->setText(QString("共 %1 页, 当前第 %2 页").arg(m_mind1->levels() - 1).arg(p)); updateCurrentTable(); } void EXDataTableView::updateCurrentTable() { int c = m_dataTab->currentIndex(); QTableView *table = (QTableView *)m_dataTab->widget(c); if (table == nullptr || table->model() == nullptr || m_isSettingTable) { return; } m_hNodes.clear(); m_vNodes.clear(); m_values.clear(); QStandardItemModel *model = (QStandardItemModel *)table->model(); /// 设置顶部水平方向标题 int hIndex = -1; int dimensionIndex = -1; // 量纲所在列 int typeIndex = -1; // 指标类型所在列 // 指标 CNodeData n = m_mind1->nodesInLevel(m_currentPage)[c]; QList hList = m_mind1->subNodes(n); // 以下情况需要显示指标 // 导入权重分析数据 // 导入需求分析评估的评估数据 if (m_process.type == SchemePlanManager::ImportWeightData || m_process.indexType == ProjectManager::TechIndex) { for (CNodeData node : hList) { QStandardItem *item = new QStandardItem(node.name); item->setToolTip(node.remark); model->setHorizontalHeaderItem(++hIndex, item); m_hNodes.append(node.name); } } // 导入方案优选评估或效能评估的评估数据时, 需要显示量纲 if (m_process.type == SchemePlanManager::ImportEvalData && (m_process.indexType == ProjectManager::OptimalIndex || m_process.indexType == ProjectManager::EfficiencyIndex)) { QStandardItem *item = new QStandardItem("指标量纲"); model->setHorizontalHeaderItem(++hIndex, item); dimensionIndex = hIndex; } // 导入方案优选评估的评估数据时, 需要显示指标类型 if (m_process.type == SchemePlanManager::ImportEvalData && m_process.indexType == ProjectManager::OptimalIndex) { QStandardItem *item = new QStandardItem("指标类型"); model->setHorizontalHeaderItem(++hIndex, item); typeIndex = hIndex; } /// 设置左侧垂直方向标题 QList vList; // 导入权重分析的专家数据时, 显示指标 if (m_process.type == SchemePlanManager::ImportWeightData) { vList = hList; } // 导入评估数据时, 显示最后一级指标 if (m_process.type == SchemePlanManager::ImportEvalData) { if (m_process.indexType == ProjectManager::TechIndex) { vList = m_mind2->leaves(); } else { vList = m_mind1->leaves(); } } for (int i = 0; i < vList.count(); i++) { CNodeData node = vList[i]; QStandardItem *item = new QStandardItem(QString(" %1 ").arg(node.name)); item->setToolTip(node.remark); model->setVerticalHeaderItem(i, item); table->setRowHeight(i, 35); m_vNodes.append(node.name); } /// 填充量纲和指标类型 for (int i = 0; i < vList.count(); i++) { CNodeData vNode = vList[i]; QStandardItem *d = new QStandardItem(); d->setData(Qt::AlignCenter, Qt::TextAlignmentRole); // 单元格文字居中 if (dimensionIndex >= 0) { d->setText(vNode.dimension); model->setItem(i, dimensionIndex, d); } if (typeIndex >= 0) { model->setItem(i, typeIndex, d); } table->setRowHeight(i, 35); } if (m_process.type == SchemePlanManager::ImportWeightData) { for (int i = 0; i < vList.count(); i++) { CNodeData vNode = vList[i]; for (int j = 0; j < hList.count(); j++) { CNodeData hNode = hList[i]; QStandardItem *item = new QStandardItem(); item->setData(Qt::AlignCenter, Qt::TextAlignmentRole); item->setEditable(false); if (i == j) { item->setText("1"); } if (i >= j) { item->setBackground(QBrush(QColor("lightgray"))); } model->setItem(i, j, item); } } } if (m_process.type == SchemePlanManager::ImportEvalData || m_process.indexType == ProjectManager::TechIndex) { for (int i = 0; i < vList.count(); i++) { for (int j = 0; j < hList.count(); j++) { QStandardItem *item = new QStandardItem(); item->setData(Qt::AlignCenter, Qt::TextAlignmentRole); model->setItem(i, j, item); table->setItemDelegate(m_comboDelegate); } } } } CMind *EXDataTableView::mind1() const { return m_mind1; } CMind *EXDataTableView::mind2() const { return m_mind2; } void EXDataTableView::exportData() { qDebug() << __FUNCTION__ << __LINE__ << endl; //文件夹路径 QFileDialog::Options options; options |= QFileDialog::DontUseNativeDialog; QString filePath = QFileDialog::getExistingDirectory(nullptr, "导出资源包", "/", options); if (filePath.isEmpty()) { return; } QString fileName = m_user->userName + "-" + ProjectManager::nameOfIndexType((ProjectManager::IndexType)m_process.indexType) + "-" + m_proj->projectName; // QString filePath = ""; // filePath = QCoreApplication::applicationDirPath(); QDir dirReportPath(filePath); if (!dirReportPath.exists()) { if (dirReportPath.mkpath(filePath)) { filePath = filePath + "/" + fileName + tr(".xlsx"); } } else { filePath = filePath + "/" + fileName + tr(".xlsx"); } QFile file(fileName); if (file.exists()) { file.remove(); } for (int i = 1; i < m_mind1->levels(); i++) { if (m_models.keys().contains(i) == false) { continue; } QList nodes = m_mind1->nodesInLevel(i); for (int j = 0; j < m_models[i].count(); j++) { CNodeData node = nodes[j]; QStandardItemModel *model = m_models[i][j]; m_export->addSheet(node.name); int row = model->rowCount(); int col = model->columnCount(); m_export->setColumnWidth(1, 20); for (int c = 2; c < col + 2; c++) { QStandardItem *head = model->horizontalHeaderItem(c - 2); m_export->write(1, c, head->text()); m_export->setColumnWidth(c, head->text().size() == 0 ? 40 : head->text().size() * 4); } for (int r = 2; r < row + 2; r++) { QStandardItem *head = model->verticalHeaderItem(r - 2); m_export->write(r, 1, head->text()); for (int c = 2; c < col + 2; c++) { QStandardItem *head = model->item(r - 2, c - 2); m_export->write(r, c, head->text()); } } } m_export->saveAs(filePath); } } void EXDataTableView::editItemData(const QModelIndex &index, const QString &val) { int c = m_dataTab->currentIndex(); QTableView *table = (QTableView *)m_dataTab->widget(c); QStandardItemModel *model = (QStandardItemModel *)table->model(); model->itemFromIndex(index)->setText(val); QString symmetry; if (val.startsWith("1/")) { symmetry = val.split("/")[1]; } else { if (val == "0" || val == "1") { symmetry = val; } else { symmetry = "1/" + val; } } model->item(index.column(), index.row())->setText(symmetry); } void EXDataTableView::slotPrevious() { setCurrentPage(m_currentPage - 1); } void EXDataTableView::slotNext() { setCurrentPage(m_currentPage + 1); } void EXDataTableView::slotTabCurrentChanged(int c) { Q_UNUSED(c) updateCurrentTable(); } void EXDataTableView::slotItemClicked(const QModelIndex &index) { if (index.row() >= index.column()) { return; } if (m_process.type != SchemePlanManager::ImportWeightData) { return; } QStringList l = { "1/9", "1/7", "1/5", "1/3", "1", "3", "5", "7", "9" }; QTableView *table = (QTableView *)sender(); QStandardItemModel *model = (QStandardItemModel *)table->model(); // SchemeBar *scheme = // new SchemeBar(model->item(index.row(), 0)->text(), // model->horizontalHeaderItem(index.column())->text(), l); SchemeBar *scheme = new SchemeBar(model->verticalHeaderItem(index.row())->text(), model->horizontalHeaderItem(index.column())->text(), l); scheme->setModal(true); scheme->setAttribute(Qt::WA_DeleteOnClose); connect(scheme, &SchemeBar::setValue, [=](QString val) { editItemData(index, val); }); scheme->show(); QPoint p = QCursor::pos(); if (p.x() + scheme->width() + 10 >= QApplication::desktop()->width()) { p.setX(QApplication::desktop()->width() - 10 - scheme->width()); } scheme->move(p); } void EXDataTableView::slotSave() { return; int c = m_dataTab->currentIndex(); QTableView *table = (QTableView *)m_dataTab->widget(c); QStandardItemModel *model = (QStandardItemModel *)table->model(); QList values; if (m_process.type == SchemePlanManager::ImportWeightData) { for (int i = 0; i < model->rowCount(); i++) { QStandardItem *row = model->verticalHeaderItem(i); for (int j = 0; j < model->columnCount(); j++) { QStandardItem *col = model->horizontalHeaderItem(j); QStandardItem *item = model->item(i, j); NodeMatrixInfo *info = new NodeMatrixInfo(); info->expertName = m_user->userName; info->expertId = QString("%1").arg(m_user->id); info->engineerId = m_process.projectId; info->mindId = m_process.dSource; info->abscissa = col->text(); info->ordinate = row->text(); info->writeDate = QDateTime::currentDateTime(); info->mark = QString("%1").arg(m_currentPage); info->nodeValue = item->text(); info->tableMsg = QString("%1").arg(m_process.indexType); values.append(info); } } } NodeMatrixService().AddNodeMatrixInfoList2(values); }