SchemeFlowWidget.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include "SchemeFlowWidget.h"
  2. #include <dbService/ClassSet.h>
  3. #include <QGroupBox>
  4. #include <QCheckBox>
  5. #include <QBoxLayout>
  6. #include <QDebug>
  7. static std::shared_ptr<NodeDelegateModelRegistry> registerDataModels()
  8. {
  9. auto ret = std::make_shared<NodeDelegateModelRegistry>();
  10. ret->registerModel<FlowTemplateDataModel>();
  11. return ret;
  12. }
  13. static void setStyle_()
  14. {
  15. GraphicsViewStyle::setStyle(
  16. R"(
  17. {
  18. "GraphicsViewStyle": {
  19. "BackgroundColor": [255, 255, 255],
  20. "FineGridColor": [255, 255, 255],
  21. "CoarseGridColor": [255, 255, 255]
  22. }
  23. }
  24. )");
  25. NodeStyle::setNodeStyle(
  26. R"(
  27. {
  28. "NodeStyle": {
  29. "NormalBoundaryColor": "darkgray",
  30. "SelectedBoundaryColor": "deepskyblue",
  31. "GradientColor0": "mintcream",
  32. "GradientColor1": "mintcream",
  33. "GradientColor2": "mintcream",
  34. "GradientColor3": "mintcream",
  35. "ShadowColor": [200, 200, 200],
  36. "FontColor": [10, 10, 10],
  37. "FontColorFaded": [100, 100, 100],
  38. "ConnectionPointColor": "white",
  39. "PenWidth": 2.0,
  40. "HoveredPenWidth": 2.5,
  41. "ConnectionPointDiameter": 10.0,
  42. "Opacity": 1.0
  43. }
  44. }
  45. )");
  46. ConnectionStyle::setConnectionStyle(
  47. R"(
  48. {
  49. "ConnectionStyle": {
  50. "ConstructionColor": "gray",
  51. "NormalColor": "black",
  52. "SelectedColor": "gray",
  53. "SelectedHaloColor": "deepskyblue",
  54. "HoveredColor": "deepskyblue",
  55. "LineWidth": 3.0,
  56. "ConstructionLineWidth": 2.0,
  57. "PointDiameter": 10.0,
  58. "UseDataDefinedColors": false
  59. }
  60. }
  61. )");
  62. }
  63. SchemeFlowWidget::SchemeFlowWidget(ProjectInfo *proj, int indexType, QWidget *parent) : QWidget(parent)
  64. {
  65. setStyle_();
  66. initWidgets();
  67. initLayout();
  68. }
  69. SchemeFlowWidget::~SchemeFlowWidget()
  70. {
  71. delete graphModel;
  72. }
  73. void SchemeFlowWidget::initWidgets()
  74. {
  75. #if 1
  76. graphModel = new DataFlowModel(registerDataModels());
  77. // Initialize and connect two nodes.
  78. {
  79. NodeId id1 = graphModel->addNode(FlowTemplateData().type().id);
  80. graphModel->setNodeData(id1, NodeRole::Position, QPointF(0, 0));
  81. NodeId id2 = graphModel->addNode(FlowTemplateData().type().id);
  82. graphModel->setNodeData(id2, NodeRole::Position, QPointF(300, 300));
  83. graphModel->addConnection(ConnectionId { id1, 0, id2, 0 });
  84. }
  85. auto scene = new DataFlowGraphicsScene(*graphModel);
  86. QHBoxLayout *l = new QHBoxLayout(this);
  87. GraphicsView *view = new GraphicsView(scene);
  88. l->addWidget(view);
  89. QGroupBox *groupBox = new QGroupBox("Options");
  90. QCheckBox *cb1 = new QCheckBox("Nodes are locked");
  91. QCheckBox *cb2 = new QCheckBox("Connections detachable");
  92. cb2->setChecked(true);
  93. QVBoxLayout *vbl = new QVBoxLayout;
  94. vbl->addWidget(cb1);
  95. vbl->addWidget(cb2);
  96. vbl->addStretch();
  97. groupBox->setLayout(vbl);
  98. QObject::connect(cb1, &QCheckBox::stateChanged,
  99. [this](int state) { graphModel->setNodesLocked(state == Qt::Checked); });
  100. QObject::connect(cb2, &QCheckBox::stateChanged,
  101. [this](int state) { graphModel->setDetachPossible(state == Qt::Checked); });
  102. l->addWidget(groupBox);
  103. this->setWindowTitle("Locked Nodes and Connections");
  104. this->resize(800, 600);
  105. #else
  106. // Initialize and connect two nodes.
  107. {
  108. NodeId id1 = m_graphModel.addNode();
  109. m_graphModel.setNodeData(id1, NodeRole::Position, QPointF(0, 0));
  110. NodeId id2 = m_graphModel.addNode();
  111. m_graphModel.setNodeData(id2, NodeRole::Position, QPointF(300, 300));
  112. m_graphModel.addConnection(ConnectionId { id1, 0, id2, 0 });
  113. }
  114. auto scene = new BasicGraphicsScene(m_graphModel);
  115. m_view = new GraphicsView(scene, this);
  116. // // Setup context menu for creating new nodes.
  117. m_view->setContextMenuPolicy(Qt::ActionsContextMenu);
  118. QAction *createNodeAction = new QAction(QStringLiteral("Create Node"), m_view);
  119. QObject::connect(createNodeAction, &QAction::triggered, [&]() {
  120. // Mouse position in scene coordinates.
  121. QPointF posView = m_view->mapToScene(m_view->mapFromGlobal(QCursor::pos()));
  122. NodeId const newId = m_graphModel.addNode();
  123. m_graphModel.setNodeData(newId, NodeRole::Position, posView);
  124. });
  125. m_view->insertAction(m_view->actions().front(), createNodeAction);
  126. m_view->setWindowTitle("Simple Node Graph");
  127. m_view->resize(800, 600);
  128. #endif
  129. }
  130. void SchemeFlowWidget::initLayout()
  131. {
  132. m_layout = new QHBoxLayout(this);
  133. m_layout->addWidget(m_view);
  134. }