ShemeFlowPanel.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "DataFlowModel.h"
  2. #include "FlowGraphModel.h"
  3. #include "FlowTemplateDataModel.h"
  4. #include "ShemeFlowPanel.h"
  5. #include "FlowGraphNodeWidget.h"
  6. #include <QtNodes/BasicGraphicsScene>
  7. #include <QtNodes/ConnectionStyle>
  8. #include <QtNodes/DataFlowGraphicsScene>
  9. #include <QtNodes/GraphicsView>
  10. #include <QtNodes/GraphicsViewStyle>
  11. #include <QtNodes/NodeDelegateModelRegistry>
  12. #include <QCheckBox>
  13. #include <QGroupBox>
  14. #include <QHBoxLayout>
  15. using QtNodes::BasicGraphicsScene;
  16. using QtNodes::ConnectionStyle;
  17. using QtNodes::DataFlowGraphicsScene;
  18. using QtNodes::GraphicsView;
  19. using QtNodes::GraphicsViewStyle;
  20. using QtNodes::NodeDelegateModelRegistry;
  21. using QtNodes::NodeRole;
  22. using QtNodes::NodeStyle;
  23. static std::shared_ptr<NodeDelegateModelRegistry> registerDataModels()
  24. {
  25. auto ret = std::make_shared<NodeDelegateModelRegistry>();
  26. ret->registerModel<FlowTemplateDataModel>();
  27. ret->registerModel<FlowIndexDataModel>();
  28. ret->registerModel<FlowSampleDataModel>();
  29. ret->registerModel<FlowPCADataModel>();
  30. ret->registerModel<FlowWeightDataModel>();
  31. ret->registerModel<FlowResultDataModel>();
  32. ret->registerModel<FlowReportDataModel>();
  33. ret->registerModel<FlowEffiLevDataModel>();
  34. ret->registerModel<FlowSchemeDataModel>();
  35. ret->registerModel<FlowEffiDataModel>();
  36. return ret;
  37. }
  38. static FlowGraphModel graphModel;
  39. static void setStyle_()
  40. {
  41. GraphicsViewStyle::setStyle(
  42. R"(
  43. {
  44. "GraphicsViewStyle": {
  45. "BackgroundColor": [255, 255, 255],
  46. "FineGridColor": [255, 255, 255],
  47. "CoarseGridColor": [255, 255, 255]
  48. }
  49. }
  50. )");
  51. NodeStyle::setNodeStyle(
  52. R"(
  53. {
  54. "NodeStyle": {
  55. "NormalBoundaryColor": "darkgray",
  56. "SelectedBoundaryColor": "deepskyblue",
  57. "GradientColor0": "mintcream",
  58. "GradientColor1": "mintcream",
  59. "GradientColor2": "mintcream",
  60. "GradientColor3": "mintcream",
  61. "ShadowColor": [200, 200, 200],
  62. "FontColor": [10, 10, 10],
  63. "FontColorFaded": [100, 100, 100],
  64. "ConnectionPointColor": "white",
  65. "PenWidth": 2.0,
  66. "HoveredPenWidth": 2.5,
  67. "ConnectionPointDiameter": 10.0,
  68. "Opacity": 1.0
  69. }
  70. }
  71. )");
  72. ConnectionStyle::setConnectionStyle(
  73. R"(
  74. {
  75. "ConnectionStyle": {
  76. "ConstructionColor": "gray",
  77. "NormalColor": "black",
  78. "SelectedColor": "gray",
  79. "SelectedHaloColor": "deepskyblue",
  80. "HoveredColor": "deepskyblue",
  81. "LineWidth": 3.0,
  82. "ConstructionLineWidth": 2.0,
  83. "PointDiameter": 10.0,
  84. "UseDataDefinedColors": false
  85. }
  86. }
  87. )");
  88. }
  89. ShemeFlowPanel::ShemeFlowPanel(QWidget *parent) : QWidget(parent)
  90. {
  91. setStyle_();
  92. #if 1
  93. DataFlowModel *graphModel = new DataFlowModel(registerDataModels());
  94. // Initialize and connect two nodes.
  95. {
  96. NodeId id1 = graphModel->addNode(FlowIndexData().type().id);
  97. graphModel->setNodeData(id1, NodeRole::Position, QPointF(0, 0));
  98. NodeId id2 = graphModel->addNode(FlowSampleData().type().id);
  99. graphModel->setNodeData(id2, NodeRole::Position, QPointF(200, 0));
  100. graphModel->addConnection(ConnectionId { id1, 0, id2, 0 });
  101. NodeId id3 = graphModel->addNode(FlowPCAData().type().id);
  102. graphModel->setNodeData(id3, NodeRole::Position, QPointF(400, 0));
  103. graphModel->addConnection(ConnectionId { id2, 0, id3, 0 });
  104. NodeId id4 = graphModel->addNode(FlowWeightData().type().id);
  105. graphModel->setNodeData(id4, NodeRole::Position, QPointF(600, 0));
  106. graphModel->addConnection(ConnectionId { id3, 0, id4, 0 });
  107. NodeId id5 = graphModel->addNode(FlowResultData().type().id);
  108. graphModel->setNodeData(id5, NodeRole::Position, QPointF(0, 200));
  109. graphModel->addConnection(ConnectionId { id4, 0, id5, 0 });
  110. NodeId id6 = graphModel->addNode(FlowReportData().type().id);
  111. graphModel->setNodeData(id6, NodeRole::Position, QPointF(200, 200));
  112. graphModel->addConnection(ConnectionId { id5, 0, id6, 0 });
  113. NodeId id7 = graphModel->addNode(FlowEffiLevData().type().id);
  114. graphModel->setNodeData(id7, NodeRole::Position, QPointF(400, 200));
  115. graphModel->addConnection(ConnectionId { id6, 0, id7, 0 });
  116. NodeId id8 = graphModel->addNode(FlowSchemeData().type().id);
  117. graphModel->setNodeData(id8, NodeRole::Position, QPointF(600, 200));
  118. graphModel->addConnection(ConnectionId { id7, 0, id8, 0 });
  119. NodeId id9 = graphModel->addNode(FlowEffiData().type().id);
  120. graphModel->setNodeData(id9, NodeRole::Position, QPointF(0, 400));
  121. graphModel->addConnection(ConnectionId { id8, 0, id9, 0 });
  122. }
  123. auto scene = new DataFlowGraphicsScene(*graphModel);
  124. QHBoxLayout *l = new QHBoxLayout(this);
  125. GraphicsView *view = new GraphicsView(scene);
  126. l->addWidget(view);
  127. QGroupBox *groupBox = new QGroupBox("Options");
  128. QCheckBox *cb1 = new QCheckBox("Nodes are locked");
  129. QCheckBox *cb2 = new QCheckBox("Connections detachable");
  130. QVBoxLayout *vbl = new QVBoxLayout;
  131. vbl->addWidget(cb1);
  132. vbl->addWidget(cb2);
  133. vbl->addStretch();
  134. groupBox->setLayout(vbl);
  135. QObject::connect(cb1, &QCheckBox::stateChanged,
  136. [graphModel](int state) { graphModel->setNodesLocked(state == Qt::Checked); });
  137. QObject::connect(cb2, &QCheckBox::stateChanged,
  138. [graphModel](int state) { graphModel->setDetachPossible(state == Qt::Checked); });
  139. l->addWidget(groupBox);
  140. graphModel->setNodesLocked(true);
  141. graphModel->setDetachPossible(false);
  142. cb1->setChecked(true);
  143. cb2->setChecked(false);
  144. this->setWindowTitle("Locked Nodes and Connections");
  145. this->resize(800, 600);
  146. #else
  147. // Initialize and connect two nodes.
  148. {
  149. NodeId id1 = graphModel.addNode();
  150. graphModel.setNodeData(id1, NodeRole::Position, QPointF(0, 0));
  151. NodeId id2 = graphModel.addNode();
  152. graphModel.setNodeData(id2, NodeRole::Position, QPointF(300, 300));
  153. graphModel.addConnection(ConnectionId { id1, 0, id2, 0 });
  154. }
  155. auto scene = new BasicGraphicsScene(graphModel);
  156. GraphicsView *view = new GraphicsView(scene);
  157. // // Setup context menu for creating new nodes.
  158. view->setContextMenuPolicy(Qt::ActionsContextMenu);
  159. QAction *createNodeAction = new QAction(QStringLiteral("Create Node"), view);
  160. QObject::connect(createNodeAction, &QAction::triggered, [&]() {
  161. // Mouse position in scene coordinates.
  162. QPointF posView = view->mapToScene(view->mapFromGlobal(QCursor::pos()));
  163. NodeId const newId = graphModel.addNode();
  164. graphModel.setNodeData(newId, NodeRole::Position, posView);
  165. });
  166. view->insertAction(view->actions().front(), createNodeAction);
  167. view->setWindowTitle("Simple Node Graph");
  168. view->resize(800, 600);
  169. QHBoxLayout *l = new QHBoxLayout(this);
  170. l->addWidget(view);
  171. #endif
  172. }