427 lines
12 KiB
C++
427 lines
12 KiB
C++
#include "graphicsItem/graphicsBaseItem.h"
|
||
#include "graphicsItem/handleRect.h"
|
||
#include "graphicsItem/handleText.h"
|
||
#include "graphicsItem/itemPort.h"
|
||
#include "baseProperty.h"
|
||
#include "dataBase.h"
|
||
#include <QGraphicsScene>
|
||
#include <QJsonArray>
|
||
#include <QPainter>
|
||
|
||
|
||
GraphicsBaseItem::GraphicsBaseItem(QGraphicsItem *parent)
|
||
: AbstractShapeType<QGraphicsItem>(parent)
|
||
,_property(nullptr)
|
||
,_pEntity(nullptr)
|
||
{
|
||
m_type = T_item;
|
||
_itemChanged = false;
|
||
m_touched = false;
|
||
|
||
setFlag(QGraphicsItem::ItemIsSelectable, true);
|
||
setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
|
||
setAcceptHoverEvents(true);
|
||
}
|
||
|
||
GraphicsBaseItem::~GraphicsBaseItem()
|
||
{
|
||
foreach (int key, m_vecHanle.keys())
|
||
{
|
||
ItemControlHandle* pHandle = m_vecHanle.value(key);
|
||
if (pHandle)
|
||
{
|
||
delete pHandle;
|
||
pHandle = nullptr;
|
||
}
|
||
}
|
||
}
|
||
|
||
int GraphicsBaseItem::addPort(PortState typ,QPointF vec,QString id,HandleType hType,PortPos pos)
|
||
{
|
||
int ntagId = -1;
|
||
for(ntagId = H_connect;ntagId < 999;++ntagId) //添加到未占用位置
|
||
{
|
||
if(!m_vecHanle.contains(ntagId))
|
||
break;
|
||
}
|
||
ItemPort* pPort = new ItemPort(this);
|
||
if(id.isEmpty())
|
||
{
|
||
pPort->setId(QUuid::createUuid().toString());
|
||
}
|
||
else
|
||
{
|
||
pPort->setId(id);
|
||
}
|
||
if(typ == p_movable)
|
||
{
|
||
pPort->setType(T_lineInOut);
|
||
}
|
||
else
|
||
{
|
||
pPort->setType(hType);
|
||
pPort->setPortPos(pos);
|
||
}
|
||
pPort->setTag(ntagId);
|
||
m_vecHanle.insert(ntagId,pPort);
|
||
pPort->setPos(vec);
|
||
pPort->setParent(this);
|
||
|
||
m_mapPort.insert(pPort->getId(),pPort);
|
||
return ntagId;
|
||
}
|
||
|
||
void GraphicsBaseItem::setEntity(PowerEntity* pEntity)
|
||
{
|
||
_pEntity = pEntity;
|
||
}
|
||
|
||
PowerEntity* GraphicsBaseItem::entity()
|
||
{
|
||
return _pEntity;
|
||
}
|
||
|
||
void GraphicsBaseItem::setProperty(ModelProperty* p)
|
||
{
|
||
if(_property) //已经有对象
|
||
{
|
||
disconnect(_property,&ModelProperty::updateData,this,&GraphicsBaseItem::onUpdateData); //断开老数据
|
||
}
|
||
connect(p,&ModelProperty::updateData,this,&GraphicsBaseItem::onUpdateData);
|
||
_property = p;
|
||
}
|
||
|
||
void GraphicsBaseItem::renderSelectBackground(QPainter* painter)
|
||
{
|
||
int nPenWidth = 1;
|
||
painter->setPen(QPen(QColor(135,206,235,180))); //蓝色的外框
|
||
|
||
painter->setBrush(QBrush(QColor(135,206,235,180)));
|
||
|
||
double dx = m_boundingRect_selected.x();
|
||
double dy = m_boundingRect_selected.y();
|
||
double dWidth = m_boundingRect_selected.width();
|
||
double dHeight = m_boundingRect_selected.height();
|
||
|
||
|
||
//画弧线0度是3点钟方向
|
||
if(dWidth > dHeight)
|
||
{
|
||
painter->drawRect(m_boundingRect_selected);
|
||
painter->drawChord(dx-dHeight*0.5,dy,dHeight,dHeight,90*16,180*16);
|
||
painter->drawChord(dx+dWidth-dHeight*0.5,dy,dHeight,dHeight,(-90)*16,180*16);
|
||
}
|
||
else if(dWidth < dHeight)
|
||
{
|
||
painter->drawRect(m_boundingRect_selected);
|
||
painter->drawChord(dx,dy-dWidth*0.5,dWidth,dWidth,0*16,180*16);
|
||
painter->drawChord(dx,dy+dHeight-dWidth*0.5,dWidth,dWidth,180*16,180*16);
|
||
//painter->drawChord(dx+dWidth-dHeight*0.5,dy,dHeight,dHeight,(-90)*16,180*16);
|
||
}
|
||
else
|
||
{
|
||
painter->drawEllipse(QPointF(dx+dWidth*0.5,dy+dHeight*0.5),dHeight*0.5,dHeight*0.5);
|
||
}
|
||
}
|
||
|
||
ItemPort* GraphicsBaseItem::getPortById(QString id) const
|
||
{
|
||
for(auto iter:m_mapPort)
|
||
{
|
||
QString portId = iter->getId();
|
||
if(portId == id)
|
||
return iter;
|
||
}
|
||
return nullptr;
|
||
}
|
||
|
||
ItemPort* GraphicsBaseItem::getPortPtr(int n) const
|
||
{
|
||
for(auto iter:m_mapPort)
|
||
{
|
||
int tag = iter->getTag();
|
||
if(tag == n)
|
||
return iter;
|
||
}
|
||
return nullptr;
|
||
}
|
||
|
||
ItemControlHandle* GraphicsBaseItem::getHandlePtr(int n) const
|
||
{
|
||
for(auto iter:m_vecHanle)
|
||
{
|
||
int tag = iter->getTag();
|
||
if(tag == n)
|
||
return iter;
|
||
}
|
||
return nullptr;
|
||
}
|
||
|
||
|
||
void GraphicsBaseItem::onUpdateData()
|
||
{
|
||
updateByProperty();
|
||
}
|
||
|
||
void GraphicsBaseItem::initialPortsByDatabase()
|
||
{
|
||
QMap<int,componentTypeInfo> mapType = DataBase::GetInstance()->getAllComponentType();
|
||
|
||
if(_property == nullptr){
|
||
qDebug()<<"item property null";
|
||
return;
|
||
}
|
||
if(mapType.contains(_property->type()))
|
||
{
|
||
QJsonArray nodesJsonArray = mapType[_property->type()].config["port"].toArray();
|
||
|
||
for (QJsonValueRef nodeJson : nodesJsonArray)
|
||
{
|
||
QJsonObject node = nodeJson.toObject();
|
||
double dX = node["xRatio"].toDouble();
|
||
double dY = node["yRatio"].toDouble();
|
||
int movable = node["movable"].toInt();
|
||
int portType = node["portType"].toInt();
|
||
int portLocate = node["portLocate"].toInt();
|
||
|
||
addPort(PortState(movable),QPointF(boundingRect().left() + boundingRect().width() * dX, boundingRect().top()+boundingRect().height() *dY),QUuid::createUuid().toString(),HandleType(portType),PortPos(portLocate));
|
||
}
|
||
}
|
||
}
|
||
/********************************基模****************************************/
|
||
GraphicsBaseModelItem::GraphicsBaseModelItem(QGraphicsItem *parent)
|
||
:GraphicsBaseItem(parent)
|
||
{
|
||
|
||
}
|
||
GraphicsBaseModelItem::~GraphicsBaseModelItem()
|
||
{
|
||
|
||
}
|
||
|
||
QVariant GraphicsBaseModelItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant& value)
|
||
{
|
||
if (change == QGraphicsItem::ItemSelectedHasChanged)
|
||
{
|
||
setHandleVisible(value.toBool());
|
||
}
|
||
return QGraphicsItem::itemChange(change, value);
|
||
}
|
||
|
||
/********************************工程模**************************************/
|
||
|
||
GraphicsProjectModelItem::GraphicsProjectModelItem(QGraphicsItem *parent)
|
||
:GraphicsBaseItem(parent)
|
||
{
|
||
_lastPort = -1;
|
||
//初始化缩放操作用的handle
|
||
//m_vecHanle.reserve(H_left);
|
||
for(int i = H_leftTop; i <= H_left; i++)
|
||
{
|
||
ItemControlHandle* pHandle = new HandleRect(this);
|
||
pHandle->setType(T_resize);
|
||
pHandle->setTag(i);
|
||
m_vecHanle.insert(i-1,pHandle);
|
||
}
|
||
for(int i = H_rotate_leftTop; i <= H_rotate_leftBottom; i++)
|
||
{
|
||
ItemControlHandle* pHandle = new HandleRect(this);
|
||
pHandle->setType(T_rotate);
|
||
pHandle->setTag(i);
|
||
m_vecHanle.insert(i-1,pHandle);
|
||
}
|
||
|
||
HandleText* pHandle = new HandleText(this);
|
||
pHandle->setType(T_text);
|
||
pHandle->setTag(H_textCaption);
|
||
pHandle->setText(QString("uname"));
|
||
pHandle->setPos(30,-30);
|
||
pHandle->setParent(this);
|
||
m_vecHanle.insert(H_textCaption,pHandle);
|
||
connect(pHandle,&HandleText::editFinish,this,&GraphicsProjectModelItem::onEditNameFinish);
|
||
|
||
HandleText* pCurrent = new HandleText(this); //电流
|
||
pCurrent->setEditable(false);
|
||
pCurrent->setType(T_text);
|
||
pCurrent->setTag(H_textCurrent);
|
||
pCurrent->setText(QString("I:"));
|
||
pCurrent->setPos(-30,-30);
|
||
pCurrent->setParent(this);
|
||
m_vecHanle.insert(H_textCurrent,pCurrent);
|
||
|
||
HandleText* pVoltage = new HandleText(this); //电压
|
||
pVoltage->setType(T_text);
|
||
pVoltage->setTag(h_textVoltage);
|
||
pVoltage->setText(QString("V:"));
|
||
pVoltage->setPos(-30,30);
|
||
pVoltage->setParent(this);
|
||
m_vecHanle.insert(h_textVoltage,pVoltage);
|
||
|
||
setFlag(QGraphicsItem::ItemIsMovable, true);
|
||
setFlag(QGraphicsItem::ItemIsSelectable, true);
|
||
setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
|
||
}
|
||
|
||
GraphicsProjectModelItem::~GraphicsProjectModelItem()
|
||
{
|
||
|
||
}
|
||
|
||
|
||
void GraphicsProjectModelItem::createOperationCopy()
|
||
{
|
||
m_pOperationCopy = new QGraphicsPathItem(this->shape());
|
||
m_pOperationCopy->setPen(Qt::DashLine);
|
||
m_pOperationCopy->setPos(this->pos());
|
||
m_pOperationCopy->setTransformOriginPoint(this->transformOriginPoint());
|
||
m_pOperationCopy->setTransform(this->transform());
|
||
m_pOperationCopy->setRotation(this->rotation());
|
||
m_pOperationCopy->setScale(this->scale());
|
||
m_pOperationCopy->setZValue(this->zValue());
|
||
|
||
QGraphicsScene* scene = this->scene();
|
||
if(scene && m_pOperationCopy)
|
||
{
|
||
scene->addItem(m_pOperationCopy);
|
||
m_movingIniPos = this->pos();
|
||
}
|
||
}
|
||
|
||
void GraphicsProjectModelItem::removeOperationCopy()
|
||
{
|
||
QGraphicsScene* scene = this->scene();
|
||
if(scene && m_pOperationCopy)
|
||
{
|
||
if(this->pos() != m_pOperationCopy->pos())
|
||
this->setPos(m_pOperationCopy->pos()); //本体移动到副本的位置
|
||
if(this->rotation() != m_pOperationCopy->rotation())
|
||
this->setRotation(m_pOperationCopy->rotation()); //本体旋转至副本的角度
|
||
|
||
scene->removeItem(m_pOperationCopy);
|
||
delete m_pOperationCopy;
|
||
m_pOperationCopy = nullptr;
|
||
}
|
||
}
|
||
|
||
void GraphicsProjectModelItem::moveOperationCopy(const QPointF& distance)
|
||
{
|
||
if(m_pOperationCopy)
|
||
m_pOperationCopy->setPos(m_movingIniPos + distance);
|
||
}
|
||
|
||
void GraphicsProjectModelItem::rotateOperationCopy(const double& dAngle)
|
||
{
|
||
if(m_pOperationCopy)
|
||
m_pOperationCopy->setRotation(dAngle);
|
||
}
|
||
|
||
void GraphicsProjectModelItem::syncRotationDataFromParent(const double& data)
|
||
{
|
||
//m_dSyncRotationByParent = rotation() + data;
|
||
m_dSyncRotationByParent += data;
|
||
//让角度保持在正负180的区间,也就是上下两个半圈,这样易于象限判断
|
||
if (m_dSyncRotationByParent > 180)
|
||
m_dSyncRotationByParent -= 360;
|
||
if (m_dSyncRotationByParent < -180)
|
||
m_dSyncRotationByParent += 360;
|
||
}
|
||
|
||
QVariant GraphicsProjectModelItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant& value)
|
||
{
|
||
if (change == QGraphicsItem::ItemSelectedHasChanged)
|
||
{
|
||
QGraphicsItemGroup *group = dynamic_cast<QGraphicsItemGroup *>(parentItem());
|
||
if(!group)
|
||
setHandleVisible(value.toBool());
|
||
//setFunctionHandleVisible(false);
|
||
else //在某一组群中,由组群展示是否选中,自身不做展示
|
||
{
|
||
setSelected(false);
|
||
return QVariant::fromValue<bool>(false);
|
||
}
|
||
}
|
||
return QGraphicsItem::itemChange(change, value);
|
||
}
|
||
|
||
void GraphicsProjectModelItem::contextMenuEvent(QGraphicsSceneContextMenuEvent* event)
|
||
{
|
||
Q_UNUSED(event);
|
||
}
|
||
|
||
void GraphicsProjectModelItem::setLabelTag(const QString& name)
|
||
{
|
||
m_vecHanle[H_textCaption]->setText(name);
|
||
}
|
||
|
||
QString GraphicsProjectModelItem::getLabelTag() const
|
||
{
|
||
return m_vecHanle[H_textCaption]->getText();
|
||
}
|
||
|
||
void GraphicsProjectModelItem::setLabelCurrent(const QString& str)
|
||
{
|
||
m_vecHanle[H_textCurrent]->setText(str);
|
||
}
|
||
|
||
QString GraphicsProjectModelItem::getLabelCurrent() const
|
||
{
|
||
return m_vecHanle[H_textCurrent]->getText();
|
||
}
|
||
|
||
void GraphicsProjectModelItem::setLabelVoltage(const QString& str)
|
||
{
|
||
m_vecHanle[h_textVoltage]->setText(str);
|
||
}
|
||
|
||
QString GraphicsProjectModelItem::getLabelVoltage() const
|
||
{
|
||
return m_vecHanle[h_textVoltage]->getText();
|
||
}
|
||
|
||
void GraphicsProjectModelItem::updateByProperty()
|
||
{
|
||
if(_property)
|
||
{
|
||
setLabelTag(_property->tag());
|
||
}
|
||
}
|
||
|
||
void GraphicsProjectModelItem::unbindProperty()
|
||
{
|
||
_property = nullptr;
|
||
}
|
||
|
||
|
||
void GraphicsProjectModelItem::updateConnectData()
|
||
{
|
||
QJsonObject obj;
|
||
QJsonArray arr;
|
||
if(_property)
|
||
{
|
||
for(auto ptr:m_mapPort)
|
||
{
|
||
//if(ptr->connected())
|
||
{
|
||
QJsonObject port;
|
||
port["portId"] = ptr->getId();
|
||
auto pLine = ptr->getConnectPtr();
|
||
port["x"] = ptr->pos().x();
|
||
port["y"] = ptr->pos().y();
|
||
port["locate"] = ptr->portPos();
|
||
port["portType"] = ptr->getType();
|
||
arr.push_back(port);
|
||
}
|
||
}
|
||
|
||
obj["port"] = arr;
|
||
obj["metaModel"] = _property->metaModelName();
|
||
_property->setContext(obj);
|
||
}
|
||
}
|
||
|
||
void GraphicsProjectModelItem::onEditNameFinish(const QString& str)
|
||
{
|
||
emit ifExist(m_itemId,str,m_Itemtype,this);
|
||
}
|
||
|