#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 static const char *kTabName = "tabName"; EXDataItemModel::EXDataItemModel(QObject *parent) : QStandardItemModel(parent) { } QVariant EXDataItemModel::data(const QModelIndex &index, int role) const { QVariant value = QStandardItemModel::data(index, role); if (Qt::TextAlignmentRole == role) { value = Qt::AlignCenter; } return value; } 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 = QFUser::currentUser(); 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); m_save->setHidden(true); } 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::setupModels() { // 按照脑图层级创建各个表的 models m_models.clear(); QList hHeaders; QList vHeaders; for (int i = 1; i < m_mind1->levels(); i++) { QList modelList; // 方案优选和综合效能的评估数据采集页面只有一级, 不用切换页面 if (m_process.type == SchemePlanManager::ImportEvalData && (m_process.indexType == ProjectManager::OptimalIndex || m_process.indexType == ProjectManager::EfficiencyIndex)) { vHeaders = m_mind1->leaves(); EXDataItemModel *model = new EXDataItemModel(this); updateModel(model, hHeaders, vHeaders); modelList.append(model); m_models[i] = modelList; break; } QList nodes = m_mind1->nodesInLevel(i); for (int j = 0; j < nodes.count(); j++) { CNodeData node = nodes[j]; EXDataItemModel *model = new EXDataItemModel(this); connect(model, &QStandardItemModel::itemChanged, this, &EXDataTableView::itemChanged); if (m_process.type == SchemePlanManager::ImportWeightData) { hHeaders = m_mind1->subNodes(node); vHeaders = hHeaders; for (int i = 0; i < vHeaders.count(); i++) { CNodeData vNode = vHeaders[i]; for (int j = 0; j < hHeaders.count(); j++) { CNodeData hNode = hHeaders[j]; 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) { if (m_process.indexType == ProjectManager::TechIndex) { hHeaders = m_mind1->subNodes(node); vHeaders = m_mind2->leaves(); for (int i = 0; i < vHeaders.count(); i++) { CNodeData vNode = vHeaders[i]; for (int j = 0; j < hHeaders.count(); j++) { CNodeData hNode = hHeaders[j]; QStandardItem *item = new QStandardItem(); model->setItem(i, j, item); } } } } model->setProperty(kTabName, node.name); updateModel(model, hHeaders, vHeaders); modelList.append(model); } m_models[i] = modelList; } setCurrentPage(1); } void EXDataTableView::setupTabWidget() { m_dataTab->clear(); QList list = m_models[m_currentPage]; for (int j = 0; j < list.size(); j++) { EXDataItemModel *model = list[j]; 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); t->setModel(model); m_dataTab->addTab(t, model->property(kTabName).toString()); connect(t, &QTableView::clicked, this, &EXDataTableView::slotItemClicked); if (m_process.type == SchemePlanManager::ImportEvalData) { if (m_process.indexType == ProjectManager::TechIndex) { // t->setItemDelegate(m_comboDelegate); } } } } int EXDataTableView::currentPage() const { return m_currentPage; } void EXDataTableView::setCurrentPage(int p) { if (p < 1 || p > m_models.size()) { return; } m_currentPage = p; m_previous->setEnabled(p > 1); m_next->setEnabled(p < m_models.size()); setupTabWidget(); m_pageLab->setText(QString("共 %1 页, 当前第 %2 页").arg(m_models.size()).arg(p)); } CMind *EXDataTableView::mind1() const { return m_mind1; } CMind *EXDataTableView::mind2() const { return m_mind2; } bool EXDataTableView::checkDataComplete() const { for (QList modelList : m_models) { for (EXDataItemModel *model : modelList) { for (int r = 0; r < model->rowCount(); ++r) { for (int c = 0; c < model->columnCount(); ++c) { QStandardItem *item = model->item(r, c); if (item == nullptr || item->text().length() <= 0) { return false; } } } } } return true; } void EXDataTableView::exportData(const QString &path) { QString filePath = path; QString fileName = m_user->userName + "-" + ProjectManager::nameOfIndexType((ProjectManager::IndexType)m_process.indexType) + "-" + m_proj->projectName; 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()) { qDebug() << "--remove"; file.remove(); } for (int i = 1; i <= m_models.size(); i++) { QList mList = m_models[i]; for (int j = 0; j < mList.count(); j++) { EXDataItemModel *model = mList[j]; m_export->addSheet(model->property(kTabName).toString()); int row = model->rowCount(); int col = model->columnCount(); m_export->setColumnWidth(1, 20); for (int c = 2; c < col + 2; c++) { QStandardItem *hHead = model->horizontalHeaderItem(c - 2); m_export->write(1, c, hHead->text()); m_export->setColumnWidth(c, hHead->text().size() == 0 ? 40 : hHead->text().size() * 4); } for (int r = 2; r < row + 2; r++) { QStandardItem *vHead = model->verticalHeaderItem(r - 2); m_export->write(r, 1, vHead->text()); for (int c = 2; c < col + 2; c++) { QStandardItem *item = model->item(r - 2, c - 2); m_export->write(r, c, item->text()); } } } } m_export->addSheet("专家信息"); m_export->write(1, 1, "ID"); m_export->write(1, 2, "账号"); m_export->write(1, 3, "单位"); m_export->write(1, 4, "职务"); m_export->write(1, 5, "专业"); m_export->write(1, 6, "联系方式"); m_export->write(1, 7, "填写时间"); m_export->write(1, 8, "备注"); m_export->write(2, 1, m_user->id); m_export->write(2, 2, m_user->userName); m_export->write(2, 3, m_user->workPosition); m_export->write(2, 4, m_user->post); m_export->write(2, 5, m_user->major); m_export->write(2, 6, m_user->phone); m_export->write(2, 7, m_user->writeTime); m_export->write(2, 8, m_user->remark); qDebug() << filePath; 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); EXDataItemModel *model = (EXDataItemModel *)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::updateModel(EXDataItemModel *model, QList &hHeaders, QList &vHeaders) { for (int i = 0; i < hHeaders.size(); i++) { CNodeData node = hHeaders[i]; QStandardItem *item = new QStandardItem(node.name); item->setToolTip(node.remark); model->setHorizontalHeaderItem(i, item); } for (int i = 0; i < vHeaders.size(); i++) { CNodeData node = vHeaders[i]; QStandardItem *item = new QStandardItem(node.name); item->setToolTip(node.remark); model->setVerticalHeaderItem(i, item); } } 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(); EXDataItemModel *model = (EXDataItemModel *)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); EXDataItemModel *model = (EXDataItemModel *)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); } void EXDataTableView::itemChanged(QStandardItem *item) { emit signalItemEdited(); if (m_process.type == SchemePlanManager::ImportWeightData) { return; } QString Pattern("(\\d*\\.?\\d*)"); // 匹配非负数 QRegExp reg(Pattern); if (!reg.exactMatch(item->text())) { item->setText(""); } }