123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- #ifndef FLOWGRAPHMODEL_H
- #define FLOWGRAPHMODEL_H
- #include <QJsonObject>
- #include <QPointF>
- #include <QSize>
- #include <QtNodes/AbstractGraphModel>
- #include <QtNodes/ConnectionIdUtils>
- #include <QtNodes/StyleCollection>
- 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<NodeId> allNodeIds() const override;
- std::unordered_set<ConnectionId> allConnectionIds(NodeId const nodeId) const override;
- std::unordered_set<ConnectionId> 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<NodeId> _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<ConnectionId> _connectivity;
- mutable std::unordered_map<NodeId, NodeGeometryData> _nodeGeometryData;
- /// A convenience variable needed for generating unique node ids.
- unsigned int _nextNodeId;
- };
- #endif // FLOWGRAPHMODEL_H
|