#ifndef FLOWGRAPHMODEL_H #define FLOWGRAPHMODEL_H #include #include #include #include #include #include using ConnectionId = QtNodes::ConnectionId; using ConnectionPolicy = QtNodes::ConnectionPolicy; using NodeFlag = QtNodes::NodeFlag; using NodeId = QtNodes::NodeId; using NodeRole = QtNodes::NodeRole; using PortIndex = QtNodes::PortIndex; using PortRole = QtNodes::PortRole; using PortType = QtNodes::PortType; using StyleCollection = QtNodes::StyleCollection; using QtNodes::InvalidNodeId; /** * The class implements a bare minimum required to demonstrate a model-based * graph. */ class FlowGraphModel : public QtNodes::AbstractGraphModel { Q_OBJECT public: struct NodeGeometryData { QSize size; QPointF pos; }; public: FlowGraphModel(); ~FlowGraphModel() override; std::unordered_set allNodeIds() const override; std::unordered_set allConnectionIds(NodeId const nodeId) const override; std::unordered_set connections(NodeId nodeId, PortType portType, PortIndex portIndex) const override; bool connectionExists(ConnectionId const connectionId) const override; NodeId addNode(QString const nodeType = QString()) override; /** * Connection is possible when graph contains no connectivity data * in both directions `Out -> In` and `In -> Out`. */ bool connectionPossible(ConnectionId const connectionId) const override; void addConnection(ConnectionId const connectionId) override; bool nodeExists(NodeId const nodeId) const override; QVariant nodeData(NodeId nodeId, NodeRole role) const override; bool setNodeData(NodeId nodeId, NodeRole role, QVariant value) override; QVariant portData(NodeId nodeId, PortType portType, PortIndex portIndex, PortRole role) const override; bool setPortData(NodeId nodeId, PortType portType, PortIndex portIndex, QVariant const &value, PortRole role = PortRole::Data) override; bool deleteConnection(ConnectionId const connectionId) override; bool deleteNode(NodeId const nodeId) override; QJsonObject saveNode(NodeId const) const override; /// @brief Creates a new node based on the informatoin in `nodeJson`. /** * @param nodeJson conains a `NodeId`, node's position, internal node * information. */ void loadNode(QJsonObject const &nodeJson) override; NodeId newNodeId() override { return _nextNodeId++; } private: std::unordered_set _nodeIds; /// [Important] This is a user defined data structure backing your model. /// In your case it could be anything else representing a graph, for example, a /// table. Or a collection of structs with pointers to each other. Or an /// abstract syntax tree, you name it. /// /// This data structure contains the graph connectivity information in both /// directions, i.e. from Node1 to Node2 and from Node2 to Node1. std::unordered_set _connectivity; mutable std::unordered_map _nodeGeometryData; /// A convenience variable needed for generating unique node ids. unsigned int _nextNodeId; }; #endif // FLOWGRAPHMODEL_H