NodeConnectionInteraction.hpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #pragma once
  2. #include <memory>
  3. #include <QtCore/QPointF>
  4. #include "Definitions.hpp"
  5. namespace QtNodes {
  6. class ConnectionGraphicsObject;
  7. class NodeGraphicsObject;
  8. class BasicGraphicsScene;
  9. /// Class wraps conecting and disconnecting checks.
  10. /**
  11. * An instance should be created on the stack and destroyed
  12. * automatically when the operation is completed
  13. */
  14. class NodeConnectionInteraction
  15. {
  16. public:
  17. NodeConnectionInteraction(NodeGraphicsObject &ngo,
  18. ConnectionGraphicsObject &cgo,
  19. BasicGraphicsScene &scene);
  20. /**
  21. * Can connect when following conditions are met:
  22. * 1. Connection 'requires' a port.
  23. * 2. Connection loose end is above the node port.
  24. * 3. Source and target `nodeId`s are different.
  25. * 4. GraphModel permits connection.
  26. */
  27. bool canConnect(PortIndex *portIndex) const;
  28. /// Creates a new connectino if possible.
  29. /**
  30. * 1. Check conditions from 'canConnect'.
  31. * 2. Creates new connection with `GraphModel::addConnection`.
  32. * 3. Adjust connection geometry.
  33. */
  34. bool tryConnect() const;
  35. /**
  36. * 1. Delete connection with `GraphModel::deleteConnection`.
  37. * 2. Create a "draft" connection with incomplete `ConnectionId`.
  38. * 3. Repaint both previously connected nodes.
  39. */
  40. bool disconnect(PortType portToDisconnect) const;
  41. private:
  42. PortType connectionRequiredPort() const;
  43. QPointF connectionEndScenePosition(PortType) const;
  44. QPointF nodePortScenePosition(PortType portType, PortIndex portIndex) const;
  45. PortIndex nodePortIndexUnderScenePoint(PortType portType, QPointF const &p) const;
  46. private:
  47. NodeGraphicsObject &_ngo;
  48. ConnectionGraphicsObject &_cgo;
  49. BasicGraphicsScene &_scene;
  50. };
  51. } // namespace QtNodes