123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- #include "IndexSystemWidget.h"
- #include "EditNodeWidget.h"
- #include "ImportIndexWidget.h"
- #include <dbService/ClassSet.h>
- #include <dbService/CNodeDataService.h>
- #include <dbService/NodeMatrixService.h>
- #include <dbService/UserConfigService.h>
- #include <CMindView.h>
- #include <CMind.h>
- #include <CNodeItem.h>
- #include <Widgets/Menu.h>
- #include <QLayout>
- #include <QMenu>
- #include <QContextMenuEvent>
- #include <QTabWidget>
- #include <QDir>
- #include <QImageWriter>
- #include <QDebug>
- 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<CNodeData> 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<UserConfig *> 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<CNodeData> 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<CNodeData> 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<CNodeData> 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();
- }
|