IndexSystemWidget.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. #include "IndexSystemWidget.h"
  2. #include "EditNodeWidget.h"
  3. #include "ImportIndexWidget.h"
  4. #include <dbService/ClassSet.h>
  5. #include <dbService/CNodeDataService.h>
  6. #include <dbService/NodeMatrixService.h>
  7. #include <dbService/UserConfigService.h>
  8. #include <CMindView.h>
  9. #include <CMind.h>
  10. #include <CNodeItem.h>
  11. #include <Widgets/Menu.h>
  12. #include <QLayout>
  13. #include <QMenu>
  14. #include <QContextMenuEvent>
  15. #include <QTabWidget>
  16. #include <QDir>
  17. #include <QImageWriter>
  18. #include <QDebug>
  19. IndexSystemWidget::IndexSystemWidget(ProjectInfo *proj, QWidget *parent) : EvalWidget(proj, parent)
  20. {
  21. setTitle("指标体系设计");
  22. initWidgets();
  23. initLayout();
  24. connectSignalsAndSlots();
  25. }
  26. void IndexSystemWidget::setType(int type)
  27. {
  28. EvalWidget::setType(type);
  29. setupTabWidget();
  30. }
  31. void IndexSystemWidget::contextMenuEvent(QContextMenuEvent *event)
  32. {
  33. RoundMenu *menu = new RoundMenu();
  34. CMindView *m = (CMindView *)m_tab->currentWidget();
  35. if (m->root() == nullptr) {
  36. QAction *act3 = new QAction("创建指标体系");
  37. menu->addAction(act3);
  38. connect(act3, &QAction::triggered, this, &IndexSystemWidget::slotCreateRootNode);
  39. QAction *act4 = new QAction("导入指标体系");
  40. menu->addAction(act4);
  41. connect(act4, &QAction::triggered, this, &IndexSystemWidget::slotImportIndex);
  42. menu->exec(event->globalPos() + QPoint(-40, -20));
  43. } else {
  44. // QAction *act2 = new QAction("清空");
  45. // menu->addAction(act2);
  46. // connect(act2, &QAction::triggered, this, &IndexSystemWidget::slotClearAllNodes);
  47. }
  48. QWidget::contextMenuEvent(event);
  49. }
  50. void IndexSystemWidget::setupTabWidget()
  51. {
  52. m_tab->clear();
  53. for (int i : indexList()) {
  54. CMindView *m = new CMindView(this);
  55. ProjectManager::IndexType t = (ProjectManager::IndexType)i;
  56. QString s = ProjectManager::nameOfIndexType(t);
  57. m_tab->addTab(m, s);
  58. QList<CNodeData> list;
  59. bool ret = CNodeDataService().QueryAll(list, proj()->id, t);
  60. if (ret) {
  61. m->setNodeList(list);
  62. m->refreshItems();
  63. saveIndexPic(list, i);
  64. }
  65. connect(m, &CMindView::sigEditNode, this, &IndexSystemWidget::slotEditNode);
  66. connect(m, &CMindView::sigAddSubNode, this, &IndexSystemWidget::slotAddSubNode);
  67. connect(m->mind(), &CMind::sigRemoveNode, this, &IndexSystemWidget::slotRemoveNode);
  68. connect(m, &CMindView::sigNodeChanged, this, &IndexSystemWidget::slotUpdateNode);
  69. }
  70. }
  71. void IndexSystemWidget::initWidgets()
  72. {
  73. m_editNode = new EditNodeWidget(this);
  74. m_importIndex = new ImportIndexWidget(this);
  75. }
  76. void IndexSystemWidget::initLayout() { }
  77. void IndexSystemWidget::connectSignalsAndSlots()
  78. {
  79. connect(m_tab, &QTabWidget::currentChanged, this, &IndexSystemWidget::slotTabCurrentChanged);
  80. connect(m_editNode, &EditNodeWidget::sigSaveNode, this, &IndexSystemWidget::slotNodeEdited);
  81. connect(m_importIndex, &ImportIndexWidget::sigImport, this, &IndexSystemWidget::slotImportIndexConfirmed);
  82. }
  83. void IndexSystemWidget::addNode(CNodeData node)
  84. {
  85. CMindView *m = (CMindView *)m_tab->currentWidget();
  86. if (m->mind()->canAddNode(node)) {
  87. int id = CNodeDataService().AddCNodeData(node);
  88. if (id >= 0) {
  89. node.id = id;
  90. m->addNode(node);
  91. }
  92. }
  93. saveIndexPic();
  94. }
  95. bool IndexSystemWidget::hasData(QString indexName) const
  96. {
  97. bool ret = NodeMatrixService().hasMeasureData(m_proj->id, indexName);
  98. if (ret == true) {
  99. return true;
  100. }
  101. QList<UserConfig *> cfgList;
  102. ret = UserConfigService().QueryUserConfigListInfoByEngineerId(&cfgList, m_proj->id);
  103. if (ret == false) {
  104. return false;
  105. }
  106. for (UserConfig *cfg : cfgList) {
  107. ret = NodeMatrixService().hasExpertData(m_proj->id, indexName, cfg->userId);
  108. if (ret) {
  109. return true;
  110. }
  111. }
  112. return false;
  113. }
  114. void IndexSystemWidget::saveIndexPic()
  115. {
  116. return;
  117. if (indexList().size() <= 0 || m_tab->currentIndex() < 0) {
  118. return;
  119. }
  120. QString curPath = QDir::currentPath();
  121. QString picDir = curPath + "/index_pics/" + "/";
  122. QDir dir(picDir);
  123. if (!dir.exists()) {
  124. dir.mkpath(picDir);
  125. }
  126. QString name = QString("%1_%2").arg(m_proj->id).arg(indexList()[m_tab->currentIndex()]);
  127. QString filePath = picDir + name + ".png";
  128. CMindView *view = (CMindView *)m_tab->currentWidget();
  129. CMindView *m = new CMindView();
  130. m->setNodeList(view->mind()->nodeList());
  131. int sceneWidth = m->sceneRect().width();
  132. int sceneHeight = m->sceneRect().height();
  133. QRectF sourceRect = QRectF(0, 0, sceneWidth, sceneHeight);
  134. QRectF targetRect = QRectF(10, 10, sceneWidth, sceneHeight);
  135. QPixmap pixmap(QSize(sceneWidth + 20, sceneHeight + 20));
  136. pixmap.fill(QColor(244, 244, 255));
  137. QPainter painter(&pixmap);
  138. painter.setRenderHints(QPainter::Antialiasing);
  139. m->scene()->render(&painter, targetRect, sourceRect);
  140. delete m;
  141. QImageWriter writer(filePath);
  142. writer.setFormat("PNG");
  143. bool ret = writer.write(pixmap.toImage());
  144. qDebug() << __FUNCTION__ << __LINE__ << ret << endl;
  145. }
  146. void IndexSystemWidget::saveIndexPic(QList<CNodeData> nodeList, int indexType)
  147. {
  148. return;
  149. QString curPath = QDir::currentPath();
  150. QString picDir = curPath + "/index_pics/" + "/";
  151. QDir dir(picDir);
  152. if (!dir.exists()) {
  153. dir.mkpath(picDir);
  154. }
  155. QString name = QString("%1_%2").arg(m_proj->id).arg(indexType);
  156. QString filePath = picDir + name + ".png";
  157. CMindView *m = new CMindView();
  158. m->setNodeList(nodeList);
  159. m->refreshItems();
  160. int sceneWidth = m->sceneRect().width();
  161. int sceneHeight = m->sceneRect().height();
  162. QRectF sourceRect = QRectF(0, 0, sceneWidth, sceneHeight);
  163. QRectF targetRect = QRectF(10, 10, sceneWidth, sceneHeight);
  164. QPixmap pixmap(QSize(sceneWidth + 20, sceneHeight + 20));
  165. pixmap.fill(QColor(244, 244, 255));
  166. QPainter painter(&pixmap);
  167. painter.setRenderHints(QPainter::Antialiasing);
  168. m->scene()->render(&painter, targetRect, sourceRect);
  169. delete m;
  170. QImageWriter writer(filePath);
  171. writer.setFormat("PNG");
  172. bool ret = writer.write(pixmap.toImage());
  173. qDebug() << __FUNCTION__ << __LINE__ << ret << endl;
  174. }
  175. void IndexSystemWidget::slotTabCurrentChanged(int c)
  176. {
  177. bool ret = hasData(m_tab->tabText(c));
  178. CMindView *m = (CMindView *)m_tab->currentWidget();
  179. if (m != nullptr) {
  180. m->setAllowEdit(!ret);
  181. QList<CNodeData> list = m->mind()->nodeList();
  182. m->setNodeList(list);
  183. }
  184. saveIndexPic();
  185. }
  186. void IndexSystemWidget::slotSelectAllNodes() { }
  187. void IndexSystemWidget::slotClearAllNodes()
  188. {
  189. CMindView *m = (CMindView *)m_tab->currentWidget();
  190. m->clear();
  191. }
  192. void IndexSystemWidget::slotCreateRootNode()
  193. {
  194. int t = indexList()[m_tab->currentIndex()];
  195. CNodeData n = CNodeData(m_proj->id, t, 0);
  196. n.name = m_proj->projectName;
  197. addNode(n);
  198. }
  199. void IndexSystemWidget::slotImportIndex()
  200. {
  201. m_importIndex->show();
  202. }
  203. void IndexSystemWidget::slotEditNode(CNodeData n)
  204. {
  205. m_editNode->setNode(n);
  206. m_editNode->show();
  207. }
  208. void IndexSystemWidget::slotAddSubNode(int pNumber)
  209. {
  210. if (pNumber < 0) {
  211. return;
  212. }
  213. CMindView *m = (CMindView *)m_tab->currentWidget();
  214. CNodeData data = m->root()->data();
  215. CNodeData n = CNodeData(data.projectId, data.indexType, m->mind()->maxNumber() + 1, pNumber);
  216. addNode(n);
  217. }
  218. void IndexSystemWidget::slotUpdateNode(CNodeData node)
  219. {
  220. CNodeDataService().UpdateCNodeData(node);
  221. saveIndexPic();
  222. }
  223. void IndexSystemWidget::slotRemoveNode(int id)
  224. {
  225. CNodeDataService().DeleteCNodeDataById(id);
  226. saveIndexPic();
  227. }
  228. void IndexSystemWidget::slotNodeEdited(CNodeData node)
  229. {
  230. // 在弹窗中编辑节点后, 先保存数据库, 再更新界面
  231. bool ret = CNodeDataService().UpdateCNodeData(node);
  232. if (ret) {
  233. CMindView *m = (CMindView *)m_tab->currentWidget();
  234. m->updateNode(node);
  235. }
  236. saveIndexPic();
  237. }
  238. void IndexSystemWidget::slotImportIndexConfirmed(int projId, int indexType)
  239. {
  240. m_importIndex->close();
  241. QList<CNodeData> list;
  242. bool ret = CNodeDataService().QueryAll(list, projId, indexType);
  243. if (ret == false) {
  244. return;
  245. }
  246. CMindView *m = (CMindView *)m_tab->currentWidget();
  247. int t = indexList()[m_tab->currentIndex()];
  248. for (int i = 0; i < list.size(); i++) {
  249. list[i].projectId = m_proj->id;
  250. list[i].indexType = t;
  251. list[i].isEffective = 0;
  252. int id = CNodeDataService().AddCNodeData(list[i]);
  253. if (id >= 0) {
  254. list[i].id = id;
  255. m->addNode(list[i]);
  256. }
  257. }
  258. m->refreshItems();
  259. }