324 lines
8.4 KiB
C++
324 lines
8.4 KiB
C++
#include "graphicsItem/graphicsBaseItem.h"
|
||
#include "graphicsItem/handleRect.h"
|
||
#include "graphicsItem/handleText.h"
|
||
#include "graphicsItem/itemPort.h"
|
||
#include "graphicsItem/electricConnectLineItem.h"
|
||
#include <QGraphicsScene>
|
||
#include <QJsonArray>
|
||
|
||
|
||
GraphicsBaseItem::GraphicsBaseItem(QGraphicsItem *parent)
|
||
: AbstractShapeType<QGraphicsItem>(parent)
|
||
,_property(nullptr)
|
||
{
|
||
m_type = T_item;
|
||
_portId = 1;
|
||
_lastPort = -1;
|
||
_itemChanged = false;
|
||
//初始化缩放操作用的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,&GraphicsBaseItem::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);
|
||
setAcceptHoverEvents(true);
|
||
}
|
||
|
||
GraphicsBaseItem::~GraphicsBaseItem()
|
||
{
|
||
|
||
}
|
||
|
||
|
||
void GraphicsBaseItem::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 GraphicsBaseItem::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 GraphicsBaseItem::moveOperationCopy(const QPointF& distance)
|
||
{
|
||
if(m_pOperationCopy)
|
||
m_pOperationCopy->setPos(m_movingIniPos + distance);
|
||
}
|
||
|
||
void GraphicsBaseItem::rotateOperationCopy(const double& dAngle)
|
||
{
|
||
if(m_pOperationCopy)
|
||
m_pOperationCopy->setRotation(dAngle);
|
||
}
|
||
|
||
void GraphicsBaseItem::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 GraphicsBaseItem::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 GraphicsBaseItem::contextMenuEvent(QGraphicsSceneContextMenuEvent* event)
|
||
{
|
||
Q_UNUSED(event);
|
||
}
|
||
|
||
void GraphicsBaseItem::setLabelTag(const QString& name)
|
||
{
|
||
m_vecHanle[H_textCaption]->setText(name);
|
||
}
|
||
|
||
QString GraphicsBaseItem::getLabelTag() const
|
||
{
|
||
return m_vecHanle[H_textCaption]->getText();
|
||
}
|
||
|
||
void GraphicsBaseItem::setLabelCurrent(const QString& str)
|
||
{
|
||
m_vecHanle[H_textCurrent]->setText(str);
|
||
}
|
||
|
||
QString GraphicsBaseItem::getLabelCurrent() const
|
||
{
|
||
return m_vecHanle[H_textCurrent]->getText();
|
||
}
|
||
|
||
void GraphicsBaseItem::setLabelVoltage(const QString& str)
|
||
{
|
||
m_vecHanle[h_textVoltage]->setText(str);
|
||
}
|
||
|
||
QString GraphicsBaseItem::getLabelVoltage() const
|
||
{
|
||
return m_vecHanle[h_textVoltage]->getText();
|
||
}
|
||
|
||
int GraphicsBaseItem::addPort(PortState typ,QPointF vec)
|
||
{
|
||
int ntagId = -1;
|
||
for(ntagId = H_connect;ntagId < 999;++ntagId) //添加到未占用位置
|
||
{
|
||
if(!m_vecHanle.contains(ntagId))
|
||
break;
|
||
}
|
||
if(typ == p_movable)
|
||
{
|
||
ItemPort* pPort = new ItemPort(this);
|
||
pPort->setType(T_lineInOut);
|
||
pPort->setTag(ntagId);
|
||
m_vecHanle.insert(ntagId,pPort);
|
||
pPort->setPos(vec);
|
||
pPort->setParent(this);
|
||
|
||
m_mapPort.insert(QString::number(_portId++),pPort);
|
||
}
|
||
return ntagId;
|
||
}
|
||
|
||
void GraphicsBaseItem::addPort(PortState typ,int ntagId,QPointF vec)
|
||
{
|
||
if(typ == p_movable)
|
||
{
|
||
ItemPort* pPort = new ItemPort(this);
|
||
pPort->setType(T_lineInOut);
|
||
pPort->setTag(ntagId);
|
||
m_vecHanle.insert(ntagId,pPort);
|
||
pPort->setPos(vec);
|
||
pPort->setParent(this);
|
||
|
||
m_mapPort.insert(QString::number(_portId++),pPort);
|
||
}
|
||
}
|
||
|
||
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::updateByProperty()
|
||
{
|
||
if(_property)
|
||
{
|
||
setLabelTag(_property->tag());
|
||
}
|
||
}
|
||
|
||
void GraphicsBaseItem::setProperty(BaseProperty* p)
|
||
{
|
||
if(_property) //已经有对象
|
||
{
|
||
disconnect(_property,&BaseProperty::updateData,this,&GraphicsBaseItem::onUpdateData); //断开老数据
|
||
}
|
||
connect(p,&BaseProperty::updateData,this,&GraphicsBaseItem::onUpdateData);
|
||
_property = p;
|
||
}
|
||
|
||
void GraphicsBaseItem::unbindProperty()
|
||
{
|
||
_property = nullptr;
|
||
}
|
||
|
||
void GraphicsBaseItem::showPropertyDlg(QWidget* p)
|
||
{
|
||
|
||
}
|
||
|
||
void GraphicsBaseItem::updateConnectData()
|
||
{
|
||
QJsonObject obj;
|
||
QJsonArray arr;
|
||
if(_property)
|
||
{
|
||
for(auto ptr:m_mapPort)
|
||
{
|
||
if(ptr->connected())
|
||
{
|
||
QJsonObject port;
|
||
port["portId"] = ptr->getTag();
|
||
auto pLine = ptr->getConnectPtr();
|
||
port["connectedItem"] = pLine->getOppositeId(m_itemId).toString();
|
||
arr.push_back(port);
|
||
}
|
||
}
|
||
|
||
obj["port"] = arr;
|
||
_property->setContext(obj);
|
||
}
|
||
}
|
||
|
||
void GraphicsBaseItem::onEditNameFinish(const QString& str)
|
||
{
|
||
emit ifExist(m_itemId,str,m_Itemtype,this);
|
||
}
|
||
|
||
void GraphicsBaseItem::onUpdateData()
|
||
{
|
||
updateByProperty();
|
||
}
|
||
/****************************属性****************************/
|
||
|
||
BaseProperty::BaseProperty(QObject* parent)
|
||
: QObject(parent)
|
||
{
|
||
nType = 0; //设备类型
|
||
nPageId = 0; //暂定为创建本数据的图Id(待定)
|
||
bInService = true;
|
||
nState = 1;
|
||
|
||
_dataChanged = false;
|
||
_prepareDelete = false;
|
||
|
||
sGrid=QString("1"); //暂时修改,数据库字段不为空
|
||
sZone=QString("1");
|
||
sStation=QString("1");
|
||
}
|
||
|
||
BaseProperty::~BaseProperty()
|
||
{
|
||
|
||
}
|