#include "IndexSystemWidget.h" #include "EditNodeWidget.h" #include "ImportIndexWidget.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include IndexSystemWidget::IndexSystemWidget(ProjectInfo *proj, QWidget *parent) : EvalWidget(proj, parent) { setTitle("指标体系设计"); initWidgets(); initLayout(); connectSignalsAndSlots(); } void IndexSystemWidget::setType(int type) { EvalWidget::setType(type); setupTabWidget(); } void IndexSystemWidget::contextMenuEvent(QContextMenuEvent *event) { RoundMenu *menu = new RoundMenu(); CMindView *m = (CMindView *)m_tab->currentWidget(); if (m->root() == nullptr) { QAction *act3 = new QAction("创建指标体系"); menu->addAction(act3); connect(act3, &QAction::triggered, this, &IndexSystemWidget::slotCreateRootNode); QAction *act4 = new QAction("导入指标体系"); menu->addAction(act4); connect(act4, &QAction::triggered, this, &IndexSystemWidget::slotImportIndex); menu->exec(event->globalPos() + QPoint(-40, -20)); } else { // QAction *act2 = new QAction("清空"); // menu->addAction(act2); // connect(act2, &QAction::triggered, this, &IndexSystemWidget::slotClearAllNodes); } QWidget::contextMenuEvent(event); } void IndexSystemWidget::setupTabWidget() { m_tab->clear(); for (int i : indexList()) { CMindView *m = new CMindView(this); ProjectManager::IndexType t = (ProjectManager::IndexType)i; QString s = ProjectManager::nameOfIndexType(t); m_tab->addTab(m, s); QList list; bool ret = CNodeDataService().QueryAll(list, proj()->id, t); if (ret) { m->setNodeList(list); m->refreshItems(); saveIndexPic(list, i); } connect(m, &CMindView::sigEditNode, this, &IndexSystemWidget::slotEditNode); connect(m, &CMindView::sigAddSubNode, this, &IndexSystemWidget::slotAddSubNode); connect(m->mind(), &CMind::sigRemoveNode, this, &IndexSystemWidget::slotRemoveNode); connect(m, &CMindView::sigNodeChanged, this, &IndexSystemWidget::slotUpdateNode); } } void IndexSystemWidget::initWidgets() { m_editNode = new EditNodeWidget(this); m_importIndex = new ImportIndexWidget(this); } void IndexSystemWidget::initLayout() { } void IndexSystemWidget::connectSignalsAndSlots() { connect(m_tab, &QTabWidget::currentChanged, this, &IndexSystemWidget::slotTabCurrentChanged); connect(m_editNode, &EditNodeWidget::sigSaveNode, this, &IndexSystemWidget::slotNodeEdited); connect(m_importIndex, &ImportIndexWidget::sigImport, this, &IndexSystemWidget::slotImportIndexConfirmed); } void IndexSystemWidget::addNode(CNodeData node) { CMindView *m = (CMindView *)m_tab->currentWidget(); if (m->mind()->canAddNode(node)) { int id = CNodeDataService().AddCNodeData(node); if (id >= 0) { node.id = id; m->addNode(node); } } saveIndexPic(); } bool IndexSystemWidget::hasData(QString indexName) const { bool ret = NodeMatrixService().hasMeasureData(m_proj->id, indexName); if (ret == true) { return true; } QList cfgList; ret = UserConfigService().QueryUserConfigListInfoByEngineerId(&cfgList, m_proj->id); if (ret == false) { return false; } for (UserConfig *cfg : cfgList) { ret = NodeMatrixService().hasExpertData(m_proj->id, indexName, cfg->userId); if (ret) { return true; } } return false; } void IndexSystemWidget::saveIndexPic() { return; if (indexList().size() <= 0 || m_tab->currentIndex() < 0) { return; } QString curPath = QDir::currentPath(); QString picDir = curPath + "/index_pics/" + "/"; QDir dir(picDir); if (!dir.exists()) { dir.mkpath(picDir); } QString name = QString("%1_%2").arg(m_proj->id).arg(indexList()[m_tab->currentIndex()]); QString filePath = picDir + name + ".png"; CMindView *view = (CMindView *)m_tab->currentWidget(); CMindView *m = new CMindView(); m->setNodeList(view->mind()->nodeList()); int sceneWidth = m->sceneRect().width(); int sceneHeight = m->sceneRect().height(); QRectF sourceRect = QRectF(0, 0, sceneWidth, sceneHeight); QRectF targetRect = QRectF(10, 10, sceneWidth, sceneHeight); QPixmap pixmap(QSize(sceneWidth + 20, sceneHeight + 20)); pixmap.fill(QColor(244, 244, 255)); QPainter painter(&pixmap); painter.setRenderHints(QPainter::Antialiasing); m->scene()->render(&painter, targetRect, sourceRect); delete m; QImageWriter writer(filePath); writer.setFormat("PNG"); bool ret = writer.write(pixmap.toImage()); qDebug() << __FUNCTION__ << __LINE__ << ret << endl; } void IndexSystemWidget::saveIndexPic(QList nodeList, int indexType) { return; QString curPath = QDir::currentPath(); QString picDir = curPath + "/index_pics/" + "/"; QDir dir(picDir); if (!dir.exists()) { dir.mkpath(picDir); } QString name = QString("%1_%2").arg(m_proj->id).arg(indexType); QString filePath = picDir + name + ".png"; CMindView *m = new CMindView(); m->setNodeList(nodeList); m->refreshItems(); int sceneWidth = m->sceneRect().width(); int sceneHeight = m->sceneRect().height(); QRectF sourceRect = QRectF(0, 0, sceneWidth, sceneHeight); QRectF targetRect = QRectF(10, 10, sceneWidth, sceneHeight); QPixmap pixmap(QSize(sceneWidth + 20, sceneHeight + 20)); pixmap.fill(QColor(244, 244, 255)); QPainter painter(&pixmap); painter.setRenderHints(QPainter::Antialiasing); m->scene()->render(&painter, targetRect, sourceRect); delete m; QImageWriter writer(filePath); writer.setFormat("PNG"); bool ret = writer.write(pixmap.toImage()); qDebug() << __FUNCTION__ << __LINE__ << ret << endl; } void IndexSystemWidget::slotTabCurrentChanged(int c) { bool ret = hasData(m_tab->tabText(c)); CMindView *m = (CMindView *)m_tab->currentWidget(); if (m != nullptr) { m->setAllowEdit(!ret); QList list = m->mind()->nodeList(); m->setNodeList(list); } saveIndexPic(); } void IndexSystemWidget::slotSelectAllNodes() { } void IndexSystemWidget::slotClearAllNodes() { CMindView *m = (CMindView *)m_tab->currentWidget(); m->clear(); } void IndexSystemWidget::slotCreateRootNode() { int t = indexList()[m_tab->currentIndex()]; CNodeData n = CNodeData(m_proj->id, t, 0); n.name = m_proj->projectName; addNode(n); } void IndexSystemWidget::slotImportIndex() { m_importIndex->show(); } void IndexSystemWidget::slotEditNode(CNodeData n) { m_editNode->setNode(n); m_editNode->show(); } void IndexSystemWidget::slotAddSubNode(int pNumber) { if (pNumber < 0) { return; } CMindView *m = (CMindView *)m_tab->currentWidget(); CNodeData data = m->root()->data(); CNodeData n = CNodeData(data.projectId, data.indexType, m->mind()->maxNumber() + 1, pNumber); addNode(n); } void IndexSystemWidget::slotUpdateNode(CNodeData node) { CNodeDataService().UpdateCNodeData(node); saveIndexPic(); } void IndexSystemWidget::slotRemoveNode(int id) { CNodeDataService().DeleteCNodeDataById(id); saveIndexPic(); } void IndexSystemWidget::slotNodeEdited(CNodeData node) { // 在弹窗中编辑节点后, 先保存数据库, 再更新界面 bool ret = CNodeDataService().UpdateCNodeData(node); if (ret) { CMindView *m = (CMindView *)m_tab->currentWidget(); m->updateNode(node); } saveIndexPic(); } void IndexSystemWidget::slotImportIndexConfirmed(int projId, int indexType) { m_importIndex->close(); QList list; bool ret = CNodeDataService().QueryAll(list, projId, indexType); if (ret == false) { return; } CMindView *m = (CMindView *)m_tab->currentWidget(); int t = indexList()[m_tab->currentIndex()]; for (int i = 0; i < list.size(); i++) { list[i].projectId = m_proj->id; list[i].indexType = t; list[i].isEffective = 0; int id = CNodeDataService().AddCNodeData(list[i]); if (id >= 0) { list[i].id = id; m->addNode(list[i]); } } m->refreshItems(); }