FlowGraphModel.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #ifndef FLOWGRAPHMODEL_H
  2. #define FLOWGRAPHMODEL_H
  3. #include <QJsonObject>
  4. #include <QPointF>
  5. #include <QSize>
  6. #include <QtNodes/AbstractGraphModel>
  7. #include <QtNodes/ConnectionIdUtils>
  8. #include <QtNodes/StyleCollection>
  9. using ConnectionId = QtNodes::ConnectionId;
  10. using ConnectionPolicy = QtNodes::ConnectionPolicy;
  11. using NodeFlag = QtNodes::NodeFlag;
  12. using NodeId = QtNodes::NodeId;
  13. using NodeRole = QtNodes::NodeRole;
  14. using PortIndex = QtNodes::PortIndex;
  15. using PortRole = QtNodes::PortRole;
  16. using PortType = QtNodes::PortType;
  17. using StyleCollection = QtNodes::StyleCollection;
  18. using QtNodes::InvalidNodeId;
  19. /**
  20. * The class implements a bare minimum required to demonstrate a model-based
  21. * graph.
  22. */
  23. class FlowGraphModel : public QtNodes::AbstractGraphModel
  24. {
  25. Q_OBJECT
  26. public:
  27. struct NodeGeometryData
  28. {
  29. QSize size;
  30. QPointF pos;
  31. };
  32. public:
  33. FlowGraphModel();
  34. ~FlowGraphModel() override;
  35. std::unordered_set<NodeId> allNodeIds() const override;
  36. std::unordered_set<ConnectionId> allConnectionIds(NodeId const nodeId) const override;
  37. std::unordered_set<ConnectionId> connections(NodeId nodeId, PortType portType, PortIndex portIndex) const override;
  38. bool connectionExists(ConnectionId const connectionId) const override;
  39. NodeId addNode(QString const nodeType = QString()) override;
  40. /**
  41. * Connection is possible when graph contains no connectivity data
  42. * in both directions `Out -> In` and `In -> Out`.
  43. */
  44. bool connectionPossible(ConnectionId const connectionId) const override;
  45. void addConnection(ConnectionId const connectionId) override;
  46. bool nodeExists(NodeId const nodeId) const override;
  47. QVariant nodeData(NodeId nodeId, NodeRole role) const override;
  48. bool setNodeData(NodeId nodeId, NodeRole role, QVariant value) override;
  49. QVariant portData(NodeId nodeId, PortType portType, PortIndex portIndex, PortRole role) const override;
  50. bool setPortData(NodeId nodeId, PortType portType, PortIndex portIndex, QVariant const &value,
  51. PortRole role = PortRole::Data) override;
  52. bool deleteConnection(ConnectionId const connectionId) override;
  53. bool deleteNode(NodeId const nodeId) override;
  54. QJsonObject saveNode(NodeId const) const override;
  55. /// @brief Creates a new node based on the informatoin in `nodeJson`.
  56. /**
  57. * @param nodeJson conains a `NodeId`, node's position, internal node
  58. * information.
  59. */
  60. void loadNode(QJsonObject const &nodeJson) override;
  61. NodeId newNodeId() override { return _nextNodeId++; }
  62. private:
  63. std::unordered_set<NodeId> _nodeIds;
  64. /// [Important] This is a user defined data structure backing your model.
  65. /// In your case it could be anything else representing a graph, for example, a
  66. /// table. Or a collection of structs with pointers to each other. Or an
  67. /// abstract syntax tree, you name it.
  68. ///
  69. /// This data structure contains the graph connectivity information in both
  70. /// directions, i.e. from Node1 to Node2 and from Node2 to Node1.
  71. std::unordered_set<ConnectionId> _connectivity;
  72. mutable std::unordered_map<NodeId, NodeGeometryData> _nodeGeometryData;
  73. /// A convenience variable needed for generating unique node ids.
  74. unsigned int _nextNodeId;
  75. };
  76. #endif // FLOWGRAPHMODEL_H