#include "WindowAppBlockStandard.h"
#include "WindowAppPouFrame.h"
#include "WindowAppItemInterface.h"
#include "Pou.h"
#include "DialogBlockProperty.h"
#include "TaskManager.h"
#include "WindowAppTaskView.h"
// bShowOnly 是否仅供展示使用,展示使用的话不提供右键菜单以及其他互动功能
WindowAppBlockStandard::WindowAppBlockStandard(TOOL* pTool, POU* Pou, bool bShowOnly, QGraphicsObject* parent):
WindowAppBlockStandardBase(pTool, Pou, bShowOnly, parent)
{
}
///
/// 添加接口(Standard工具需要额外添加ToolInterface)
///
void WindowAppBlockStandard::addItemInterfaces()
{
// 添加标准接口
WindowAppBlockStandardBase::addItemInterfaces();
// 添加ToolStart接口
WindowAppBlockStandardBase::addItemToolInterfaces();
}
///
/// 双击弹出设置对话框
///
///
void WindowAppBlockStandard::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event)
{
// 只有鼠标左键才响应
if (event->button() == Qt::LeftButton && !m_bShowOnly)
{
// qDebug() << this->scenePos();
m_pPou->ShowToolDialog(this);
}
}
///
/// 2022-3-10 根据Dll中的信息动态添加接口
///
///
bool WindowAppBlockStandard::addDynamicInterfacesFromDll(const QList& infList, QString& strReason)
{
//// 此处需要去掉const属性了,因为需要动态给Tool增加端口
//TOOL* pTool = const_cast(this->toolInfo);
for (const DLL_INF& inf : infList)
{
// 首先检查Tool中是否已经有此名字的接口了
if (m_toolInfo->contains(inf.strName))
{
strReason = "[Error] Interface [" + inf.strName + " is already exist.";
return false;
}
// 建立对应的Interface数据结构
_INTERFACE* newInf = new _INTERFACE(m_toolInfo);
newInf->basedFrom(inf);
// 设置全名
newInf->strFullName = m_toolInfo->strInstanceName + "." + newInf->strName;
// 添加到Tool数据结构中
m_toolInfo->addInterface(newInf);
// 生成对应的InterfaceItem
WindowAppItemInterface* pNewItemInf = new WindowAppItemInterface(
newInf,
m_pPou,
this->m_toolInfo,
m_bShowOnly,
this
);
if (!m_bShowOnly)
{
// Pou中保存此接口的对应关系
m_pPou->registerInterface(pNewItemInf, newInf);
}
// 保存接口Item信息用于绘制
m_itemInterfaces.append(pNewItemInf);
}
// 重新计算矩形区域
this->updateRect();
// 接口全部动态添加完毕之后,更新Interface的位置
WindowAppBlockBase::updateInterfacesPostion();
return true;
}
///
/// 2022-3-11 根据Dll中的信息动态删除接口
///
///
bool WindowAppBlockStandard::deleteDynamicInterfacesFromDll(const QList& infList, QString& strReason)
{
// 取得Tool指针
// TOOL* pTool = const_cast(this->toolInfo);
// 遍历需要删除的接口
for (const DLL_INF& inf : infList)
{
// 取得接口指针
_INTERFACE* pDelInf = m_toolInfo->getInterfaceByName(inf.strName);
if (pDelInf == nullptr)
{
strReason = "[Error] WindowAppBlockStandard::delDynamicInterfacesFromDll - but "
+ inf.strName + " is not exist.";
return false;
}
// 取得接口指针的图形单元
WindowAppItemInterface* pInfItem = m_pPou->GetInterfaceItemByName(pDelInf->strFullName);
if (pInfItem == nullptr)
{
strReason = "[Error] WindowAppBlockStandard::delDynamicInterfacesFromDll - but item "
+ pDelInf->strFullName + " is not exist.";
return false;
}
// 只允许删除动态接口
if (!pDelInf->bDynamic)
{
strReason = "[Error] " + inf.strName + " is not dynamic intrface, can't be delete!";
return false;
}
// 被引用的接口不允许删除
if (pDelInf->nRefCount > 0)
{
strReason = "[Error] Can't delete interface[" + pDelInf->strFullName + "] : ref count > 0";
return false;
}
// 层层检查完毕之后,开始执行删除动作
// Pou相关接口中删除此接口相关数据结构
m_pPou->DelInterface(pDelInf->strFullName, pInfItem, pDelInf);
// m_itemInterfaces 中删除
QVector::iterator itr = m_itemInterfaces.begin();
for (; itr != m_itemInterfaces.end();)
{
if (*itr == pInfItem)
{
// 要从scene()中移除这个Item,要不界面上不会消失
scene()->removeItem(*itr);
itr = m_itemInterfaces.erase(itr);
break;
}
else
{
itr++;
}
}
// 最后从Tool中删除此接口数据结构
m_toolInfo->delInterfaceByName(inf.strName);
}
// 本功能块重绘
this->updatePosition();
return true;
}