123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- #pragma once
- #include "Common.h"
- #include "CommonDraw.h"
- #include "WindowAppItemLinkLine.h"
- ///////////////////////////////////////////////////////////////////
- // 用于绘制功能块连接线的Item
- //
- // Link操作的基本规则:
- //
- // 1. 只能从输出接口向输入接口连接,反之,输入接口无法连接到输出接口
- // 2. 一个输出接口可以连接到多个输入接口,反之,一个输入接口只能连接一个输出接口
- // 3. 如果输入输出的类型不匹配,也不允许连接
- class WindowAppItemInterface;
- class WindowAppItemLinkLine;
- class WindowAppItemLink : public QObject, public QGraphicsItemGroup
- {
- Q_OBJECT
- // 添加这一行,否则会出现警告:Warning: qobject_cast to QGraphicsItem will not work!
- Q_INTERFACES(QGraphicsItem)
- public:
- WindowAppItemLink(
- WindowAppItemInterface* startItem,
- WindowAppItemInterface* endItem,
- LINK_MODE linkMode = LINK_MODE::LINK_NORMAL,
- QVector<QLineF> linePoints = QVector<QLineF>()
- );
- ~WindowAppItemLink();
- // 设定本Item的自定义类型
- enum { Type = ITEM_TYPE_LINK };
- int type() const
- {
- // 针对该 item 启用 qgraphicsitem_cast
- return Type;
- }
- //// 更新连接线的位置
- //void updatePosition();
- // 根据起点和终点信息更新连接线段信息(自动方式连接)
- void updateLinkLinesAuto();
- // 根据移动的功能块进行对应的连接线断调整(手动方式连接)
- void updateLinkLinesManual(QGraphicsItem* pBlock, bool bStart);
- WindowAppItemInterface* startItem() const
- {
- return m_startInfItem;
- }
- WindowAppItemInterface* endItem() const
- {
- return m_endInfItem;
- }
- // 本Link多条线段中的起始线段(和起始接口相连)
- QLineF startLine() const
- {
- return QLineF(
- m_linkLines[0]->startPoint(),
- m_linkLines[0]->endPoint()
- );
- }
- // 本Link多条线段中的结束线段(和终点接口相连)
- QLineF endLine() const
- {
- return QLineF(
- m_linkLines[m_nLineCount - 1]->startPoint(),
- m_linkLines[m_nLineCount - 1]->endPoint()
- );
- }
- // Group内的线段准备拖拽时,是否允许其移动(必须在合法的范围内拖动)
- bool canMove(QPointF nextP1, QPointF nextP2);
- // 获取所有线段坐标(用于序列化)
- QVector<QLineF> getAllLinkLinePoints();
- // 将Link设置为Movable(包括Group和所有的子线段)
- void setMovable(bool bEnable = true);
- protected slots:
- // 当Link线段移动时,同步调整其他线段的位置
- void onLineMove(WindowAppItemLinkLine* moveItem);
- // 显示右键菜单
- void contextMenuEvent(QGraphicsSceneContextMenuEvent* event) override;
- protected:
- // 初始化连接线段
- void initLines(QVector<QLineF> linePoints = QVector<QLineF>());
- // 初始化右键菜单
- void createContextMenu();
- // 菜单 - Cut
- void onMenuCut();
- // 菜单 - Copy
- void onMenuCopy();
- // 菜单 - Paste
- void onMenuPaste();
- // 菜单 - Delete
- void onMenuDelete();
- // 按照正常模式更新线段(自动连接模式)
- void updateNormalLines();
- // 按照并行模式更新线段(自动连接模式)
- void updateParallelLines();
-
- // 按照正常模式更新线段(手动连接模式,起点功能块拖动时)
- void updateNormalLinesByStart();
- // 按照正常模式更新线段(手动连接模式,终点功能块拖动时)
- void updateNormalLinesByEnd();
- // 按照并行模式更新线段(手动连接模式,起点功能块拖动时)
- void updateParallelLinesByStart();
- // 按照并行模式更新线段(手动连接模式,终点功能块拖动时)
- void updateParallelLinesByEnd();
- // 线段被拖动时,同步移动其他线段(普通模式)
- void moveNormalLines(WindowAppItemLinkLine* item, QPointF movePt1, QPointF movePt2);
- // 线段被拖动时,同步移动其他线段(并行模式)
- void moveParallelLines(WindowAppItemLinkLine* item, QPointF movePt1, QPointF movePt2);
- protected:
- // 待连接的起点和终点Item
- WindowAppItemInterface* m_startInfItem;
- WindowAppItemInterface* m_endInfItem;
- // 本组连线的线段组
- QVector<WindowAppItemLinkLine*> m_linkLines;
- // link的右键菜单
- QMenu* contextMenu;
- QAction* cutAction;
- QAction* copyAction;
- QAction* pasteAction;
- QAction* deleteAction;
- // 连线的模式(正常模式和并行模式)
- LINK_MODE m_mode;
- // 当前需要绘制的线段数量
- int m_nLineCount;
-
- };
|