#include "MatterElementConfigWidget.h" #include "MatterElementItemDelegate.h" #include "ui_MatterElementConfigWidget.h" #include "dbService/ScaleInfoService.h" #include "dbService/GradeInfoService.h" #include "dbService/GradeIndexInfoService.h" #include "CMind.h" #include #include #include #include MatterElementConfigWidget::MatterElementConfigWidget(CMind *mind, int domainLevel, QWidget *parent) : QWidget(parent), ui(new Ui::MatterElementConfigWidget), m_domainLevel(domainLevel), m_mind(mind) { ui->setupUi(this); initScaleDivisionTable(); initConfigItems(); initDomainTable(); } MatterElementConfigWidget::~MatterElementConfigWidget() { delete ui; } void MatterElementConfigWidget::initScaleDivisionTable() { QTableView *t = ui->scaleDivisionTable; t->setAlternatingRowColors(false); 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->verticalHeader()->hide(); int projectId = m_mind->root().projectId; m_scaleDivisionmodel = new QStandardItemModel; t->setModel(m_scaleDivisionmodel); m_scaleDivisionmodel->setRowCount(1); m_scaleDivisionmodel->setColumnCount(7); const QStringList headers = { "定义", "很差", "较差", "一般", "较高", "很高", "相邻定义" }; m_scaleDivisionmodel->setHorizontalHeaderLabels(headers); QStandardItem *s = nullptr; MatterElementItemSpinDelegate *sd = new MatterElementItemSpinDelegate(t); QStringList originValues = { "1.0", "3.0", "5.0", "7.0", "9.0", "[2.0, 4.0, 6.0, 8.0]" }; QList scaleInfoList; if (!ScaleInfoService().QueryScaleByProjectId(&scaleInfoList, projectId)) { QMessageBox::warning(this, "警告", "数据库访问失败"); } else { if (scaleInfoList.size() == 0) { for (int num = 0; num < originValues.size(); ++num) { ScaleInfo sinfo; sinfo.projectId = projectId; sinfo.scaleName = headers.at(num + 1); sinfo.scaleValue = originValues.at(num); if (!ScaleInfoService().AddScaleInfo(sinfo)) { QMessageBox::warning(this, "警告", "数据库访问失败"); } } } else { for (const auto &si : scaleInfoList) { int loc = headers.indexOf(si->scaleName); originValues[loc - 1] = si->scaleValue; } } } qDeleteAll(scaleInfoList); for (int i = 0; i < headers.size(); ++i) { if (i == 0) { s = new QStandardItem("标度"); s->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable); s->setTextAlignment(Qt::AlignCenter); } else { s = new QStandardItem(originValues.at(i - 1)); s->setTextAlignment(Qt::AlignCenter); } m_scaleDivisionmodel->setItem(0, i, s); if (i > 0 && i < headers.size() - 1) { t->setItemDelegateForColumn(i, sd); } if (i == headers.size() - 1) { t->setItemDelegateForColumn(i, new MatterElementItemInsideDelegate); } } connect(ui->updateScaleBtn, &QToolButton::clicked, this, &MatterElementConfigWidget::onScaleValueUpdate); } static void clearTab(QTabWidget *tabWidget) { while (tabWidget->count() > 0) { auto widget = tabWidget->widget(0); // 获取当前页对应的widget tabWidget->removeTab(0); // 从QTabWidget中删除当前页 widget->setParent(nullptr); // 从父控件中删除widget对象 widget->deleteLater(); // 删除widget对象 } } void MatterElementConfigWidget::initDomainLevelLayout() { ui->levelLabel->setText(QString::number(m_domainLevel)); QList gradeInfoList; QMap levelMaps; int projectId = m_mind->root().projectId; if (!GradeInfoService().QueryGradeByProjectIdAndType(&gradeInfoList, projectId, 0)) { QMessageBox::warning(this, "警告", "数据库访问失败"); } else { if (gradeInfoList.size() == 0) { for (int lvl = 0; lvl < m_domainLevel; ++lvl) { levelMaps.insert(QString("%1级:").arg(lvl + 1), QString("E%1").arg(lvl + 1)); GradeInfo ginfo; ginfo.projectId = projectId; ginfo.gradeName = QString("%1级").arg(lvl + 1); ginfo.gradeValue = QString("E%1").arg(lvl + 1); ginfo.type = 0; if (!GradeInfoService().AddGradeInfo(ginfo)) { QMessageBox::warning(this, "警告", "数据库访问失败"); } } } else { for (const auto &gi : gradeInfoList) { levelMaps.insert(gi->gradeName, gi->gradeValue); if (levelMaps.size() >= m_domainLevel) { break; } } for (int lvl = gradeInfoList.size(); lvl < m_domainLevel; ++lvl) { levelMaps.insert(QString("%1级:").arg(lvl + 1), QString("E%1").arg(lvl + 1)); GradeInfo ginfo; ginfo.projectId = projectId; ginfo.gradeName = QString("%1级").arg(lvl + 1); ginfo.gradeValue = QString("E%1").arg(lvl + 1); ginfo.type = 0; if (!GradeInfoService().AddGradeInfo(ginfo)) { QMessageBox::warning(this, "警告", "数据库访问失败"); } } } } qDeleteAll(gradeInfoList); QFormLayout *formLayout = new QFormLayout; QMapIterator iter(levelMaps); while (iter.hasNext()) { iter.next(); auto le = new QLineEdit(iter.value()); le->setMinimumHeight(30); le->setMaximumWidth(80); m_levelEdits << le; formLayout->addRow(iter.key() + ":", le); } auto levelSaveBtn = new QPushButton("保存"); levelSaveBtn->setIcon(QIcon(":/qfluentwidgets/images/icons/Save_black.svg")); levelSaveBtn->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); levelSaveBtn->setMinimumHeight(30); levelSaveBtn->setMaximumWidth(80); formLayout->addRow("", levelSaveBtn); ui->levelWidget->setLayout(formLayout); connect(levelSaveBtn, &QPushButton::clicked, this, &MatterElementConfigWidget::onLevelSaveBtnClick); } void MatterElementConfigWidget::initConfigItems() { // 脑图层级 < 2 时无效 if (m_mind->levels() < 2) { return; } m_allConfigItems.clear(); // 使用倒数第二层节点及其子节点 for (CNodeData n : m_mind->nodesInLevel(m_mind->levels() - 1)) { QVector children; for (CNodeData sub : m_mind->subNodes(n)) { MEDomain d; d.name = sub.name; d.leftValues = QVector(m_domainLevel + 1, 0); d.rightValues = QVector(m_domainLevel + 1, 0); d.leftCloses = QVector(m_domainLevel + 1, false); d.rightCloses = QVector(m_domainLevel + 1, false); children.append(d); } MEConfigItem i; i.parentName = n.name; i.childs = children; m_allConfigItems.append(i); } QList gradeIndexInfoList; int projectId = m_mind->root().projectId; if (!GradeIndexInfoService().QueryGradeIndexInfoByProjectId(&gradeIndexInfoList, projectId)) { QMessageBox::warning(this, "警告", "数据库访问失败"); } else { if (gradeIndexInfoList.size() == 0) { for (const auto &configItem : m_allConfigItems) { for (int c = 0; c < configItem.childs.size(); ++c) { // 一个指标对应m_domainLevel+1个配置项 for (int domain = 0; domain < m_domainLevel + 1; ++domain) { GradeIndexInfo gii; gii.projectId = projectId; gii.gradeIndexName = configItem.childs.at(c).name; gii.gradeParentName = configItem.parentName; gii.gradeLevel = domain + 1; gii.gradeIndexValue = QString("%1%2, %3%4") .arg(configItem.childs.at(c).leftCloses.at(domain) ? "[" : "(") .arg(configItem.childs.at(c).leftValues.at(domain)) .arg(configItem.childs.at(c).rightValues.at(domain)) .arg(configItem.childs.at(c).rightCloses.at(domain) ? "]" : ")"); if (!GradeIndexInfoService().AddGradeIndexInfo(gii)) { QMessageBox::warning(this, "警告", "数据库访问失败"); } } } } } else { for (auto &configItem : m_allConfigItems) { for (int c = 0; c < configItem.childs.size(); ++c) { bool found = false; for (auto gindex : gradeIndexInfoList) { for (int domain = 0; domain < m_domainLevel + 1; ++domain) { // 默认指标名具有唯一性 if (gindex->gradeIndexName == configItem.childs[c].name && gindex->gradeLevel == domain + 1) { QString indexValue = gindex->gradeIndexValue.trimmed(); QStringList list = indexValue.mid(1, indexValue.size() - 2).replace(" ", "").split(","); configItem.childs[c].leftValues[domain] = list.at(0).toDouble(); configItem.childs[c].rightValues[domain] = list.at(1).toDouble(); configItem.childs[c].leftCloses[domain] = indexValue[0] == "["; configItem.childs[c].rightCloses[domain] = indexValue.right(1) == "]"; found = true; } } } if (!found) { for (int domain = 0; domain < m_domainLevel + 1; ++domain) { GradeIndexInfo gii; gii.projectId = projectId; gii.gradeIndexName = configItem.childs[c].name; gii.gradeParentName = configItem.parentName; gii.gradeLevel = domain + 1; gii.gradeIndexValue = QString("%1%2, %3%4") .arg(configItem.childs.at(c).leftCloses.at(domain) ? "[" : "(") .arg(configItem.childs.at(c).leftValues.at(domain)) .arg(configItem.childs.at(c).rightValues.at(domain)) .arg(configItem.childs.at(c).rightCloses.at(domain) ? "]" : ")"); if (!GradeIndexInfoService().AddGradeIndexInfo(gii)) { QMessageBox::warning(this, "警告", "数据库访问失败"); } } } } } } } qDeleteAll(gradeIndexInfoList); } void MatterElementConfigWidget::initDomainTable() { initDomainLevelLayout(); m_meRangeDelegate = new MatterElementItemRangeDelegate(this); const QStringList rangeItems = { "很差", "较差", "一般", "较高", "很高", "相邻较差", "相邻一般", "相邻较高", "相邻很高" }; QVector rangeValues; auto scaleValues = getScaleValues(); rangeValues << scaleValues.at(0) << scaleValues.at(1); m_meRangeDelegate->setOriginItems(rangeItems, rangeValues); QStringList domainLevels = getDomainLevels(); for (int pindex = 0; pindex < m_allConfigItems.size(); ++pindex) { QTableView *table = new QTableView; table->setAlternatingRowColors(false); table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); table->horizontalHeader()->setStyleSheet("QHeaderView::section{background:rgb(244,244,244);color: black;}"); table->verticalHeader()->setStyleSheet("QHeaderView::section{background:rgb(244,244,244);color: black;}"); table->verticalHeader()->setDefaultAlignment(Qt::AlignCenter); table->setSelectionMode(QAbstractItemView::SingleSelection); table->verticalHeader()->hide(); table->verticalHeader()->setDefaultSectionSize(60); table->horizontalHeader()->hide(); QStandardItemModel *tModel = new QStandardItemModel(table); m_domainModels << tModel; table->setModel(tModel); ui->tabWidget->addTab(table, m_allConfigItems.at(pindex).parentName); connect(tModel, &QStandardItemModel::itemChanged, this, &MatterElementConfigWidget::onTableItemChanged); int indexNumber = m_allConfigItems.at(pindex).childs.size(); tModel->setColumnCount(4 * m_domainLevel); tModel->setRowCount(indexNumber); for (int r = 0; r < m_domainLevel + 1; ++r) { QStandardItem *s = nullptr; if (r < m_domainLevel) { s = new QStandardItem(QString("R%1").arg(r + 1)); } else { s = new QStandardItem("Rp"); } s->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable); s->setTextAlignment(Qt::AlignCenter); if (r < m_domainLevel) { s->setToolTip(QString("经典域R%1").arg(r + 1)); } else { s->setToolTip("节域p"); } tModel->setItem(0, r * 4, s); table->setColumnWidth(r * 4, 20); table->setSpan(0, r * 4, indexNumber, 1); if (r < m_domainLevel) { s = new QStandardItem(domainLevels.at(r)); } else { s = new QStandardItem("节域p"); } s->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable); s->setTextAlignment(Qt::AlignCenter); tModel->setItem(0, r * 4 + 1, s); table->setSpan(0, r * 4 + 1, indexNumber, 1); for (int k = 0; k < indexNumber; ++k) { s = new QStandardItem(m_allConfigItems[pindex].childs[k].name); s->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable); s->setTextAlignment(Qt::AlignCenter); tModel->setItem(k, r * 4 + 2, s); s = new QStandardItem(); s->setTextAlignment(Qt::AlignCenter); if (r < m_domainLevel) { s->setText(QString("%1%2, %3%4") .arg(m_allConfigItems[pindex].childs[k].leftCloses[r] ? "[" : "(") .arg(m_allConfigItems[pindex].childs[k].leftValues[r]) .arg(m_allConfigItems[pindex].childs[k].rightValues[r]) .arg(m_allConfigItems[pindex].childs[k].rightCloses[r] ? "]" : ")")); tModel->setItem(k, r * 4 + 3, s); } } table->setItemDelegateForColumn(r * 4 + 3, m_meRangeDelegate); } } connect(ui->cleanBtn, &QToolButton::clicked, this, &MatterElementConfigWidget::onClearBtnClick); connect(ui->saveBtn, &QToolButton::clicked, this, &MatterElementConfigWidget::onSaveBtnClick); } QStringList MatterElementConfigWidget::getDomainLevels() const { QStringList lvls; for (int i = 0; i < m_levelEdits.size(); ++i) { QString v = m_levelEdits.at(i)->text().trimmed(); if (v.isEmpty()) { lvls << QString("E%1").arg(i + 1); } else { lvls << v; } } return lvls; } QVector> MatterElementConfigWidget::getScaleValues() const { QVector> newValues(2); for (int c = 1; c < m_scaleDivisionmodel->columnCount() - 1; ++c) { newValues[0] << m_scaleDivisionmodel->item(0, c)->text().toDouble(); } QString value = m_scaleDivisionmodel->item(0, m_scaleDivisionmodel->columnCount() - 1)->text(); QStringList tmp = value.mid(1, value.size() - 2).replace(" ", "").split(","); for (const auto &var : tmp) { newValues[1] << var.toDouble(); } return newValues; } void MatterElementConfigWidget::onLevelSaveBtnClick() { QStringList domainLevels = getDomainLevels(); for (auto model : m_domainModels) { for (int c = 0; c < domainLevels.size(); ++c) { model->item(0, c * 4 + 1)->setText(domainLevels.at(c)); } } // 更新数据库等级值 QList gradeInfoList; if (!GradeInfoService().QueryGradeByProjectIdAndType(&gradeInfoList, m_mind->root().projectId, 0)) { QMessageBox::warning(this, "警告", "数据库访问失败"); return; } for (auto gi : gradeInfoList) { int num = gi->gradeName.mid(0, gi->gradeName.size() - 1).toInt(); if (num < domainLevels.size() + 1) { if (gi->gradeValue != domainLevels.at(num - 1)) { gi->gradeValue = domainLevels.at(num - 1); if (!GradeInfoService().UpdateGradeInfo(*gi)) { QMessageBox::warning(this, "警告", "数据库访问失败"); return; } } } } qDeleteAll(gradeInfoList); } void MatterElementConfigWidget::onScaleValueUpdate() { ui->scaleDivisionTable->clearSelection(); // 清除表格选中 // 把值聚集起来 QVector rangeValues; auto scaleValues = getScaleValues(); rangeValues << scaleValues.at(0) << scaleValues.at(1); m_meRangeDelegate->updateOriginValues(rangeValues); // 更新数据库 QList scaleInfoList; if (!ScaleInfoService().QueryScaleByProjectId(&scaleInfoList, m_mind->root().projectId)) { QMessageBox::warning(this, "警告", "数据库访问失败"); return; } else { for (int c = 1; c < m_scaleDivisionmodel->columnCount(); ++c) { QString header = m_scaleDivisionmodel->horizontalHeaderItem(c)->text(); QString value = m_scaleDivisionmodel->item(0, c)->text(); for (auto si : scaleInfoList) { if (si->scaleName == header && value != si->scaleValue) { si->scaleValue = value; if (!ScaleInfoService().UpdateScaleInfo(*si)) { QMessageBox::warning(this, "警告", "数据库访问失败"); } } } } } qDeleteAll(scaleInfoList); } void MatterElementConfigWidget::onClearBtnClick() { for (auto &config : m_allConfigItems) { for (auto &child : config.childs) { for (int domain = 0; domain < m_domainLevel + 1; ++domain) { child.leftValues[domain] = child.rightValues[domain] = 0; } } } for (int pindex = 0; pindex < m_allConfigItems.size(); ++pindex) { QStandardItemModel *tModel = m_domainModels.at(pindex); int indexNumber = m_allConfigItems.at(pindex).childs.size(); for (int r = 0; r < m_domainLevel + 1; ++r) { for (int k = 0; k < indexNumber; ++k) { tModel->item(k, r * 4 + 3)->setText("(0, 0)"); } } } } void MatterElementConfigWidget::onSaveBtnClick() { for (int pindex = 0; pindex < m_allConfigItems.size(); ++pindex) { QStandardItemModel *tModel = m_domainModels.at(pindex); int indexNumber = m_allConfigItems.at(pindex).childs.size(); for (int r = 0; r < m_domainLevel + 1; ++r) { for (int k = 0; k < indexNumber; ++k) { QString itemTxt = tModel->item(k, r * 4 + 3)->text(); QStringList list = itemTxt.mid(1, itemTxt.size() - 2).replace(" ", "").split(","); m_allConfigItems[pindex].childs[k].leftValues[r] = list.at(0).toDouble(); m_allConfigItems[pindex].childs[k].rightValues[r] = list.at(1).toDouble(); m_allConfigItems[pindex].childs[k].leftCloses[r] = itemTxt[0] == "["; m_allConfigItems[pindex].childs[k].rightCloses[r] = itemTxt.right(1) == "]"; qDebug() << r << k << list; } } } QList gradeIndexInfoList; int projectId = m_mind->root().projectId; if (!GradeIndexInfoService().QueryGradeIndexInfoByProjectId(&gradeIndexInfoList, projectId)) { QMessageBox::warning(this, "警告", "数据库访问失败"); } else { if (gradeIndexInfoList.size() == 0) { // 防止数据库被异步删除 for (const auto &configItem : m_allConfigItems) { for (int c = 0; c < configItem.childs.size(); c++) { for (int domain = 0; domain < m_domainLevel + 1; ++domain) { GradeIndexInfo gii; gii.projectId = projectId; gii.gradeIndexName = configItem.childs[c].name; gii.gradeParentName = configItem.parentName; gii.gradeLevel = domain + 1; gii.gradeIndexValue = QString("%1%2, %3%4") .arg(configItem.childs[c].leftCloses.at(domain) ? "[" : "(") .arg(configItem.childs[c].leftValues.at(domain)) .arg(configItem.childs[c].rightValues.at(domain)) .arg(configItem.childs[c].rightCloses.at(domain) ? "]" : ")"); if (!GradeIndexInfoService().AddGradeIndexInfo(gii)) { QMessageBox::warning(this, "警告", "数据库访问失败"); } } } } } else { for (const auto &configItem : m_allConfigItems) { for (int c = 0; c < configItem.childs.size(); c++) { bool found = false; for (auto &gindex : gradeIndexInfoList) { for (int domain = 0; domain < m_domainLevel + 1; ++domain) { if ((gindex->gradeIndexName == configItem.childs[c].name) && (gindex->gradeLevel == domain + 1)) { QString indexValue = QString("%1%2, %3%4") .arg(configItem.childs[c].leftCloses.at(domain) ? "[" : "(") .arg(configItem.childs[c].leftValues.at(domain)) .arg(configItem.childs[c].rightValues.at(domain)) .arg(configItem.childs[c].rightCloses.at(domain) ? "]" : ")"); if (gindex->gradeIndexValue != indexValue) { gindex->gradeIndexValue = indexValue; if (!GradeIndexInfoService().UpdateGradeIndexInfo(*gindex)) { QMessageBox::warning(this, "警告", "数据库访问失败"); } } found = true; } } } if (!found) { for (int domain = 0; domain < m_domainLevel + 1; ++domain) { QString indexValue = QString("%1%2, %3%4") .arg(configItem.childs[c].leftCloses.at(domain) ? "[" : "(") .arg(configItem.childs[c].leftValues.at(domain)) .arg(configItem.childs[c].rightValues.at(domain)) .arg(configItem.childs[c].rightCloses.at(domain) ? "]" : ")"); GradeIndexInfo gii; gii.projectId = projectId; gii.gradeIndexName = configItem.childs[c].name; gii.gradeParentName = configItem.parentName; gii.gradeLevel = c + 1; gii.gradeIndexValue = indexValue; if (!GradeIndexInfoService().AddGradeIndexInfo(gii)) { QMessageBox::warning(this, "警告", "数据库访问失败"); } } } } } } } qDeleteAll(gradeIndexInfoList); } /// 第一个区间或最后一个区间更新时, 同时更新节域 void MatterElementConfigWidget::onTableItemChanged(QStandardItem *item) { /// 判断是否为第一个或最后一个区间 int index1 = 3; // 第一个区间列索引 int index2 = (m_domainLevel - 1) * 4 + 3; // 最后一个区间列索引 if (item->column() != index1 && item->column() != index2) { return; } QStandardItem *item1 = item->model()->item(item->row(), index1); QStandardItem *item2 = item->model()->item(item->row(), index2); if (item1 == nullptr || item2 == nullptr) { return; } /// 求节域值 QString left1, right1, left2, right2; QString sep = ", "; if (item1->text().length() > 2 && item1->text().contains(sep)) { left1 = item1->text().mid(1, item1->text().length() - 2).split(sep).first(); right1 = item1->text().mid(1, item1->text().length() - 2).split(sep).last(); } if (item2->text().length() > 2 && item2->text().contains(sep)) { left2 = item2->text().mid(1, item2->text().length() - 2).split(sep).first(); right2 = item2->text().mid(1, item2->text().length() - 2).split(sep).last(); } QString range = QString("(%1, %2)").arg(left1).arg(right2); if (left1.toDouble() < left2.toDouble()) { range = QString("%1%2%3").arg(item1->text().split(sep).first()).arg(sep).arg(item2->text().split(sep).last()); } else { range = QString("%1%2%3").arg(item2->text().split(sep).first()).arg(sep).arg(item1->text().split(sep).last()); } /// 显示节域值 int jointIndex = m_domainLevel * 4 + 3; // 节域列索引 QStandardItem *rangeItem = item->model()->item(item->row(), jointIndex); if (rangeItem) { rangeItem->setText(range); } else { rangeItem = new QStandardItem(range); rangeItem->setEditable(false); item->model()->setItem(item->row(), jointIndex, rangeItem); } } QList MatterElementConfigWidget::getAllConfigItems() const { return m_allConfigItems; }