222 lines
6.4 KiB
C++
222 lines
6.4 KiB
C++
#include "util/connectingSelector.h"
|
|
#include "graphicsItem/itemControlHandle.h"
|
|
#include "graphicsItem/itemPort.h"
|
|
#include "graphicsItem/electricConnectLineItem.h"
|
|
#include <QGraphicsSceneMouseEvent>
|
|
#include <QGraphicsView>
|
|
#include <graphicsItem/graphicsBaseItem.h>
|
|
|
|
ConnectingSelector::ConnectingSelector(QObject *parent)
|
|
: BaseSelector(parent)
|
|
,m_pConnectingItem(nullptr)
|
|
,m_pTouchedItem(nullptr)
|
|
{
|
|
m_type = ST_connecting;
|
|
m_bReadyConnect = false;
|
|
}
|
|
ConnectingSelector::~ConnectingSelector()
|
|
{
|
|
|
|
}
|
|
|
|
void ConnectingSelector::mousePressEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene)
|
|
{
|
|
ms_ptMouseLast = event->scenePos();
|
|
|
|
}
|
|
|
|
bool ConnectingSelector::targetCouldConnect(GraphicsBaseItem* p,QPointF pos)
|
|
{
|
|
GraphicsItemType iType = p->getItemType();
|
|
if(iType == GIT_bus)
|
|
{
|
|
setTargetHighLight(true);
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
int nHandle = p->collidesWithHandle(pos);
|
|
if(nHandle >= H_connect ) //连接控制点
|
|
{
|
|
setTargetHighLight(false);
|
|
p->setLastPort(nHandle);
|
|
ItemPort* pt = p->getPortPtr(nHandle);
|
|
if(pt)
|
|
{
|
|
HandleType targetDir = pt->getType();
|
|
bool bCon = pt->connected();
|
|
|
|
if(m_pConnectingItem)
|
|
{
|
|
ItemPort* ptSrc = m_pConnectingItem->getPortPtr(ms_nDragHandle);
|
|
HandleType sourceDir = ptSrc->getType();
|
|
|
|
if((targetDir != sourceDir) && !bCon)
|
|
{
|
|
setTargetHighLight(true);
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else //超出了触碰范围
|
|
{
|
|
setTargetHighLight(false);
|
|
p->setLastPort(-1);
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void ConnectingSelector::setTargetHighLight(bool val)
|
|
{
|
|
if(m_pTouchedItem)
|
|
{
|
|
GraphicsItemType iType = m_pTouchedItem->getItemType();
|
|
if(iType == GIT_bus)
|
|
{
|
|
m_pTouchedItem->setTouched(val);
|
|
}
|
|
else
|
|
{
|
|
int n = m_pTouchedItem->getLastPort();
|
|
if(n != -1)
|
|
m_pTouchedItem->setHandleVisible(n,val);
|
|
}
|
|
}
|
|
}
|
|
|
|
void ConnectingSelector::createConnectLline(GraphicsBaseItem* connectingItem,GraphicsBaseItem* touchedItem,DesignerScene* scene)
|
|
{
|
|
ElectricConnectLineItem* pItem = new ElectricConnectLineItem();
|
|
pItem->setItemId(++_Id);
|
|
pItem->setItemType(GIT_connectLine);
|
|
scene->addItem(pItem);
|
|
|
|
ItemPort* ptSrc = connectingItem->getPortPtr(ms_nDragHandle);
|
|
int srcId = connectingItem->itemId();
|
|
int srcPort = ptSrc->getTag();
|
|
HandleType srcType = ptSrc->getType();
|
|
pItem->setStartPoint(ptSrc->scenePos());
|
|
ptSrc->setConnect(pItem);
|
|
|
|
ItemPort* ptDest = nullptr;
|
|
if(touchedItem->getItemType() == GIT_bus) //母线动态创建port
|
|
{
|
|
int nId = touchedItem->addPort(p_movable,touchedItem->mapFromScene(ms_ptMouseLast));
|
|
if(nId != -1)
|
|
{
|
|
ptDest = touchedItem->getPortPtr(nId);
|
|
pItem->setEndPoint(ptDest->scenePos());
|
|
}
|
|
}
|
|
else
|
|
{
|
|
int nLastPort = touchedItem->getLastPort();
|
|
ptDest = touchedItem->getPortPtr(nLastPort);
|
|
pItem->setEndPoint(ptDest->scenePos());
|
|
}
|
|
|
|
if(ptDest != nullptr)
|
|
{
|
|
int destId = touchedItem->itemId();
|
|
int destPort = ptDest->getTag();
|
|
HandleType destType = ptDest->getType();
|
|
|
|
pItem->calculatePath();
|
|
pItem->setConnection(Connection(srcId,srcPort,srcType,destId,destPort,destType));
|
|
ptDest->setConnect(pItem);
|
|
|
|
m_graphicsItem[sceneName()].insert(QString::number(pItem->itemId()),pItem); //插入连接线到总表
|
|
}
|
|
}
|
|
|
|
void ConnectingSelector::mouseMoveEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene)
|
|
{
|
|
ms_ptMouseLast = event->scenePos();
|
|
if(m_pConnectingItem == nullptr)
|
|
{
|
|
QList<QGraphicsItem *> items = scene->selectedItems();
|
|
if (items.count() == 1)
|
|
{
|
|
GraphicsBaseItem* item = qgraphicsitem_cast<GraphicsBaseItem*>(items.first());
|
|
if(item)
|
|
{
|
|
if(ms_nDragHandle != H_none)
|
|
{
|
|
item->setState(S_prepareConnect);
|
|
QPointF start = item->mapFromScene(ms_ptMouseDown);
|
|
QPointF end = item->mapFromScene(ms_ptMouseLast);
|
|
item->setBeginConnectPos(start);
|
|
item->setEndConnectPos(end);
|
|
m_pConnectingItem = item;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if(m_pConnectingItem)
|
|
{
|
|
QPointF end = m_pConnectingItem->mapFromScene(ms_ptMouseLast);
|
|
m_pConnectingItem->setEndConnectPos(end);
|
|
|
|
QList<QGraphicsItem *> items = scene->items(ms_ptMouseLast);
|
|
if (items.count() == 1)
|
|
{
|
|
GraphicsBaseItem* item = dynamic_cast<GraphicsBaseItem*>(items.first());
|
|
if(item)
|
|
{
|
|
int n1 = item->itemId();
|
|
int n2 = m_pConnectingItem->itemId();
|
|
if(n1 != n2) //判断两个对象是否相同
|
|
{
|
|
m_pTouchedItem = item;
|
|
m_bReadyConnect = targetCouldConnect(item,ms_ptMouseLast);
|
|
}
|
|
}
|
|
}
|
|
if(items.isEmpty())
|
|
{
|
|
//todo取消选中
|
|
m_bReadyConnect = false;
|
|
setTargetHighLight(false);
|
|
if(m_pTouchedItem)
|
|
{
|
|
m_pTouchedItem->setLastPort(-1);
|
|
m_pTouchedItem = nullptr;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
void ConnectingSelector::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene)
|
|
{
|
|
if(m_bReadyConnect)
|
|
{
|
|
if(m_pConnectingItem && m_pTouchedItem)
|
|
{
|
|
createConnectLline(m_pConnectingItem,m_pTouchedItem,scene);
|
|
}
|
|
}
|
|
|
|
if(m_pConnectingItem)
|
|
{
|
|
m_pConnectingItem->setState(S_normal);
|
|
}
|
|
if(m_pTouchedItem)
|
|
{
|
|
setTargetHighLight(false);
|
|
m_pTouchedItem->setLastPort(-1);
|
|
m_pTouchedItem = nullptr;
|
|
}
|
|
m_pConnectingItem = nullptr;
|
|
ms_nDragHandle = H_none;
|
|
setCursor(scene, Qt::ArrowCursor);
|
|
scene->callParentEvent(event);
|
|
emit setWorkingSelector(ST_base);
|
|
}
|
|
|