123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- #pragma once
- #include "Common.h"
- #include "CommonDraw.h"
- class WindowAppItemLink;
- class WindowAppItemLinkLine : public QObject, public QGraphicsLineItem
- {
- Q_OBJECT
- Q_INTERFACES(QGraphicsItem)
- public:
- WindowAppItemLinkLine(
- QPointF ptStart,
- QPointF ptEnd,
- QGraphicsItemGroup* parent,
- LINK_MODE linkMode = LINK_MODE::LINK_NORMAL
- );
- // 设定本Item的自定义类型
- enum { Type = ITEM_TYPE_LINK_LINE };
- int type() const
- {
- // 针对该 item 启用 qgraphicsitem_cast
- return Type;
- }
- // 更新连线位置
- void updateLine(QPointF ptStart, QPointF ptEnd);
- // 更新线段的起点坐标
- void setP1(const QPointF pt);
- // 更新线段的终点坐标
- void setP2(const QPointF pt);
- // 是否是垂直线条
- bool isVertical();
- // 是否是水平线条
- bool isHorizontal();
- // 本线段是否支持拖拽
- bool isSupportDrag();
- // 设置本线段为起点连接线(正常模式下,起点连接线是不允许被拖动的)
- void setLinkStart(bool bEnd = true)
- {
- this->m_bLinkStartLine = bEnd;
- }
- // 设置本线段为终点连接线(终点连接线需要绘制箭头)
- void setLinkEnd(bool bEnd = true)
- {
- this->m_bLinkEndLine = bEnd;
- }
- QPointF startPoint()
- {
- return this->line().p1();
- }
- QPointF endPoint()
- {
- return this->line().p2();
- }
- int m_nLineIndex; // 线段在Group中的编号
- signals:
- // 连线被拖动,需要通知Group同时联动其他线段
- void lineMoveSignal(WindowAppItemLinkLine* item);
- // 连线的右键消息,需要通知Group展开统一的右键菜单
- void contexMenuSignal(QGraphicsSceneContextMenuEvent* event);
- // 线段选中消息,需要通知Group统一选中本组中所有线条
- void lineSelectedSignal(WindowAppItemLinkLine* item);
- protected slots:
- // 矩形边界
- QRectF boundingRect() const override;
- // 形状边界(为了扩大线条的可点击范围)
- QPainterPath shape() const override;
- // 绘制线条
- void paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
- QWidget* widget = nullptr) override;
- // 跳过Group,自行处理部分消息
- bool sceneEvent(QEvent* event) override;
- // 鼠标进入悬浮事件(用于显示拖动用的鼠标)
- void hoverEnterEvent(QGraphicsSceneHoverEvent* event) override;
- // 鼠标按下事件(用于触发Link拖动)
- void mousePressEvent(QGraphicsSceneMouseEvent* event) override;
- // 鼠标移动事件(用于拖动Link)
- void mouseMoveEvent(QGraphicsSceneMouseEvent* event) override;
- // 鼠标释放事件(用于鼠标还原以及接收拉伸状态)
- void mouseReleaseEvent(QGraphicsSceneMouseEvent* event) override;
- protected:
- bool m_bLinkStartLine; // 是否是link的起点线段
- bool m_bLinkEndLine; // 是否是link的终点线段
- bool m_bIsDragging; // 是否处于拖动状态
- // 连线的模式(正常模式和并行模式)
- LINK_MODE m_mode;
- WindowAppItemLink* m_parentGroup; // 父Group指针
- };
|