123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- #include "SchemeFlowWidget.h"
- #include <dbService/ClassSet.h>
- #include <QGroupBox>
- #include <QCheckBox>
- #include <QBoxLayout>
- #include <QDebug>
- static std::shared_ptr<NodeDelegateModelRegistry> registerDataModels()
- {
- auto ret = std::make_shared<NodeDelegateModelRegistry>();
- ret->registerModel<FlowTemplateDataModel>();
- return ret;
- }
- static void setStyle_()
- {
- GraphicsViewStyle::setStyle(
- R"(
- {
- "GraphicsViewStyle": {
- "BackgroundColor": [255, 255, 255],
- "FineGridColor": [255, 255, 255],
- "CoarseGridColor": [255, 255, 255]
- }
- }
- )");
- NodeStyle::setNodeStyle(
- R"(
- {
- "NodeStyle": {
- "NormalBoundaryColor": "darkgray",
- "SelectedBoundaryColor": "deepskyblue",
- "GradientColor0": "mintcream",
- "GradientColor1": "mintcream",
- "GradientColor2": "mintcream",
- "GradientColor3": "mintcream",
- "ShadowColor": [200, 200, 200],
- "FontColor": [10, 10, 10],
- "FontColorFaded": [100, 100, 100],
- "ConnectionPointColor": "white",
- "PenWidth": 2.0,
- "HoveredPenWidth": 2.5,
- "ConnectionPointDiameter": 10.0,
- "Opacity": 1.0
- }
- }
- )");
- ConnectionStyle::setConnectionStyle(
- R"(
- {
- "ConnectionStyle": {
- "ConstructionColor": "gray",
- "NormalColor": "black",
- "SelectedColor": "gray",
- "SelectedHaloColor": "deepskyblue",
- "HoveredColor": "deepskyblue",
- "LineWidth": 3.0,
- "ConstructionLineWidth": 2.0,
- "PointDiameter": 10.0,
- "UseDataDefinedColors": false
- }
- }
- )");
- }
- SchemeFlowWidget::SchemeFlowWidget(ProjectInfo *proj, int indexType, QWidget *parent) : QWidget(parent)
- {
- setStyle_();
- initWidgets();
- initLayout();
- }
- SchemeFlowWidget::~SchemeFlowWidget()
- {
- delete graphModel;
- }
- void SchemeFlowWidget::initWidgets()
- {
- #if 1
- graphModel = new DataFlowModel(registerDataModels());
- // Initialize and connect two nodes.
- {
- NodeId id1 = graphModel->addNode(FlowTemplateData().type().id);
- graphModel->setNodeData(id1, NodeRole::Position, QPointF(0, 0));
- NodeId id2 = graphModel->addNode(FlowTemplateData().type().id);
- graphModel->setNodeData(id2, NodeRole::Position, QPointF(300, 300));
- graphModel->addConnection(ConnectionId { id1, 0, id2, 0 });
- }
- auto scene = new DataFlowGraphicsScene(*graphModel);
- QHBoxLayout *l = new QHBoxLayout(this);
- GraphicsView *view = new GraphicsView(scene);
- l->addWidget(view);
- QGroupBox *groupBox = new QGroupBox("Options");
- QCheckBox *cb1 = new QCheckBox("Nodes are locked");
- QCheckBox *cb2 = new QCheckBox("Connections detachable");
- cb2->setChecked(true);
- QVBoxLayout *vbl = new QVBoxLayout;
- vbl->addWidget(cb1);
- vbl->addWidget(cb2);
- vbl->addStretch();
- groupBox->setLayout(vbl);
- QObject::connect(cb1, &QCheckBox::stateChanged,
- [this](int state) { graphModel->setNodesLocked(state == Qt::Checked); });
- QObject::connect(cb2, &QCheckBox::stateChanged,
- [this](int state) { graphModel->setDetachPossible(state == Qt::Checked); });
- l->addWidget(groupBox);
- this->setWindowTitle("Locked Nodes and Connections");
- this->resize(800, 600);
- #else
- // Initialize and connect two nodes.
- {
- NodeId id1 = m_graphModel.addNode();
- m_graphModel.setNodeData(id1, NodeRole::Position, QPointF(0, 0));
- NodeId id2 = m_graphModel.addNode();
- m_graphModel.setNodeData(id2, NodeRole::Position, QPointF(300, 300));
- m_graphModel.addConnection(ConnectionId { id1, 0, id2, 0 });
- }
- auto scene = new BasicGraphicsScene(m_graphModel);
- m_view = new GraphicsView(scene, this);
- // // Setup context menu for creating new nodes.
- m_view->setContextMenuPolicy(Qt::ActionsContextMenu);
- QAction *createNodeAction = new QAction(QStringLiteral("Create Node"), m_view);
- QObject::connect(createNodeAction, &QAction::triggered, [&]() {
- // Mouse position in scene coordinates.
- QPointF posView = m_view->mapToScene(m_view->mapFromGlobal(QCursor::pos()));
- NodeId const newId = m_graphModel.addNode();
- m_graphModel.setNodeData(newId, NodeRole::Position, posView);
- });
- m_view->insertAction(m_view->actions().front(), createNodeAction);
- m_view->setWindowTitle("Simple Node Graph");
- m_view->resize(800, 600);
- #endif
- }
- void SchemeFlowWidget::initLayout()
- {
- m_layout = new QHBoxLayout(this);
- m_layout->addWidget(m_view);
- }
|