WindowAppItemLinkLine.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #pragma once
  2. #include "Common.h"
  3. #include "CommonDraw.h"
  4. class WindowAppItemLink;
  5. class WindowAppItemLinkLine : public QObject, public QGraphicsLineItem
  6. {
  7. Q_OBJECT
  8. Q_INTERFACES(QGraphicsItem)
  9. public:
  10. WindowAppItemLinkLine(
  11. QPointF ptStart,
  12. QPointF ptEnd,
  13. QGraphicsItemGroup* parent,
  14. LINK_MODE linkMode = LINK_MODE::LINK_NORMAL
  15. );
  16. // 设定本Item的自定义类型
  17. enum { Type = ITEM_TYPE_LINK_LINE };
  18. int type() const
  19. {
  20. // 针对该 item 启用 qgraphicsitem_cast
  21. return Type;
  22. }
  23. // 更新连线位置
  24. void updateLine(QPointF ptStart, QPointF ptEnd);
  25. // 更新线段的起点坐标
  26. void setP1(const QPointF pt);
  27. // 更新线段的终点坐标
  28. void setP2(const QPointF pt);
  29. // 是否是垂直线条
  30. bool isVertical();
  31. // 是否是水平线条
  32. bool isHorizontal();
  33. // 本线段是否支持拖拽
  34. bool isSupportDrag();
  35. // 设置本线段为起点连接线(正常模式下,起点连接线是不允许被拖动的)
  36. void setLinkStart(bool bEnd = true)
  37. {
  38. this->m_bLinkStartLine = bEnd;
  39. }
  40. // 设置本线段为终点连接线(终点连接线需要绘制箭头)
  41. void setLinkEnd(bool bEnd = true)
  42. {
  43. this->m_bLinkEndLine = bEnd;
  44. }
  45. QPointF startPoint()
  46. {
  47. return this->line().p1();
  48. }
  49. QPointF endPoint()
  50. {
  51. return this->line().p2();
  52. }
  53. int m_nLineIndex; // 线段在Group中的编号
  54. signals:
  55. // 连线被拖动,需要通知Group同时联动其他线段
  56. void lineMoveSignal(WindowAppItemLinkLine* item);
  57. // 连线的右键消息,需要通知Group展开统一的右键菜单
  58. void contexMenuSignal(QGraphicsSceneContextMenuEvent* event);
  59. // 线段选中消息,需要通知Group统一选中本组中所有线条
  60. void lineSelectedSignal(WindowAppItemLinkLine* item);
  61. protected slots:
  62. // 矩形边界
  63. QRectF boundingRect() const override;
  64. // 形状边界(为了扩大线条的可点击范围)
  65. QPainterPath shape() const override;
  66. // 绘制线条
  67. void paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
  68. QWidget* widget = nullptr) override;
  69. // 跳过Group,自行处理部分消息
  70. bool sceneEvent(QEvent* event) override;
  71. // 鼠标进入悬浮事件(用于显示拖动用的鼠标)
  72. void hoverEnterEvent(QGraphicsSceneHoverEvent* event) override;
  73. // 鼠标按下事件(用于触发Link拖动)
  74. void mousePressEvent(QGraphicsSceneMouseEvent* event) override;
  75. // 鼠标移动事件(用于拖动Link)
  76. void mouseMoveEvent(QGraphicsSceneMouseEvent* event) override;
  77. // 鼠标释放事件(用于鼠标还原以及接收拉伸状态)
  78. void mouseReleaseEvent(QGraphicsSceneMouseEvent* event) override;
  79. protected:
  80. bool m_bLinkStartLine; // 是否是link的起点线段
  81. bool m_bLinkEndLine; // 是否是link的终点线段
  82. bool m_bIsDragging; // 是否处于拖动状态
  83. // 连线的模式(正常模式和并行模式)
  84. LINK_MODE m_mode;
  85. WindowAppItemLink* m_parentGroup; // 父Group指针
  86. };