WindowAppItemLink.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #pragma once
  2. #include "Common.h"
  3. #include "CommonDraw.h"
  4. #include "WindowAppItemLinkLine.h"
  5. ///////////////////////////////////////////////////////////////////
  6. // 用于绘制功能块连接线的Item
  7. //
  8. // Link操作的基本规则:
  9. //
  10. // 1. 只能从输出接口向输入接口连接,反之,输入接口无法连接到输出接口
  11. // 2. 一个输出接口可以连接到多个输入接口,反之,一个输入接口只能连接一个输出接口
  12. // 3. 如果输入输出的类型不匹配,也不允许连接
  13. class WindowAppItemInterface;
  14. class WindowAppItemLinkLine;
  15. class WindowAppItemLink : public QObject, public QGraphicsItemGroup
  16. {
  17. Q_OBJECT
  18. // 添加这一行,否则会出现警告:Warning: qobject_cast to QGraphicsItem will not work!
  19. Q_INTERFACES(QGraphicsItem)
  20. public:
  21. WindowAppItemLink(
  22. WindowAppItemInterface* startItem,
  23. WindowAppItemInterface* endItem,
  24. LINK_MODE linkMode = LINK_MODE::LINK_NORMAL,
  25. QVector<QLineF> linePoints = QVector<QLineF>()
  26. );
  27. ~WindowAppItemLink();
  28. // 设定本Item的自定义类型
  29. enum { Type = ITEM_TYPE_LINK };
  30. int type() const
  31. {
  32. // 针对该 item 启用 qgraphicsitem_cast
  33. return Type;
  34. }
  35. //// 更新连接线的位置
  36. //void updatePosition();
  37. // 根据起点和终点信息更新连接线段信息(自动方式连接)
  38. void updateLinkLinesAuto();
  39. // 根据移动的功能块进行对应的连接线断调整(手动方式连接)
  40. void updateLinkLinesManual(QGraphicsItem* pBlock, bool bStart);
  41. WindowAppItemInterface* startItem() const
  42. {
  43. return m_startInfItem;
  44. }
  45. WindowAppItemInterface* endItem() const
  46. {
  47. return m_endInfItem;
  48. }
  49. // 本Link多条线段中的起始线段(和起始接口相连)
  50. QLineF startLine() const
  51. {
  52. return QLineF(
  53. m_linkLines[0]->startPoint(),
  54. m_linkLines[0]->endPoint()
  55. );
  56. }
  57. // 本Link多条线段中的结束线段(和终点接口相连)
  58. QLineF endLine() const
  59. {
  60. return QLineF(
  61. m_linkLines[m_nLineCount - 1]->startPoint(),
  62. m_linkLines[m_nLineCount - 1]->endPoint()
  63. );
  64. }
  65. // Group内的线段准备拖拽时,是否允许其移动(必须在合法的范围内拖动)
  66. bool canMove(QPointF nextP1, QPointF nextP2);
  67. // 获取所有线段坐标(用于序列化)
  68. QVector<QLineF> getAllLinkLinePoints();
  69. // 将Link设置为Movable(包括Group和所有的子线段)
  70. void setMovable(bool bEnable = true);
  71. protected slots:
  72. // 当Link线段移动时,同步调整其他线段的位置
  73. void onLineMove(WindowAppItemLinkLine* moveItem);
  74. // 显示右键菜单
  75. void contextMenuEvent(QGraphicsSceneContextMenuEvent* event) override;
  76. protected:
  77. // 初始化连接线段
  78. void initLines(QVector<QLineF> linePoints = QVector<QLineF>());
  79. // 初始化右键菜单
  80. void createContextMenu();
  81. // 菜单 - Cut
  82. void onMenuCut();
  83. // 菜单 - Copy
  84. void onMenuCopy();
  85. // 菜单 - Paste
  86. void onMenuPaste();
  87. // 菜单 - Delete
  88. void onMenuDelete();
  89. // 按照正常模式更新线段(自动连接模式)
  90. void updateNormalLines();
  91. // 按照并行模式更新线段(自动连接模式)
  92. void updateParallelLines();
  93. // 按照正常模式更新线段(手动连接模式,起点功能块拖动时)
  94. void updateNormalLinesByStart();
  95. // 按照正常模式更新线段(手动连接模式,终点功能块拖动时)
  96. void updateNormalLinesByEnd();
  97. // 按照并行模式更新线段(手动连接模式,起点功能块拖动时)
  98. void updateParallelLinesByStart();
  99. // 按照并行模式更新线段(手动连接模式,终点功能块拖动时)
  100. void updateParallelLinesByEnd();
  101. // 线段被拖动时,同步移动其他线段(普通模式)
  102. void moveNormalLines(WindowAppItemLinkLine* item, QPointF movePt1, QPointF movePt2);
  103. // 线段被拖动时,同步移动其他线段(并行模式)
  104. void moveParallelLines(WindowAppItemLinkLine* item, QPointF movePt1, QPointF movePt2);
  105. protected:
  106. // 待连接的起点和终点Item
  107. WindowAppItemInterface* m_startInfItem;
  108. WindowAppItemInterface* m_endInfItem;
  109. // 本组连线的线段组
  110. QVector<WindowAppItemLinkLine*> m_linkLines;
  111. // link的右键菜单
  112. QMenu* contextMenu;
  113. QAction* cutAction;
  114. QAction* copyAction;
  115. QAction* pasteAction;
  116. QAction* deleteAction;
  117. // 连线的模式(正常模式和并行模式)
  118. LINK_MODE m_mode;
  119. // 当前需要绘制的线段数量
  120. int m_nLineCount;
  121. };