WindowAppBlockStandard.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "WindowAppBlockStandard.h"
  2. #include "WindowAppPouFrame.h"
  3. #include "WindowAppItemInterface.h"
  4. #include "Pou.h"
  5. #include "DialogBlockProperty.h"
  6. #include "TaskManager.h"
  7. #include "WindowAppTaskView.h"
  8. // bShowOnly 是否仅供展示使用,展示使用的话不提供右键菜单以及其他互动功能
  9. WindowAppBlockStandard::WindowAppBlockStandard(TOOL* pTool, POU* Pou, bool bShowOnly, QGraphicsObject* parent):
  10. WindowAppBlockStandardBase(pTool, Pou, bShowOnly, parent)
  11. {
  12. }
  13. /// <summary>
  14. /// 添加接口(Standard工具需要额外添加ToolInterface)
  15. /// </summary>
  16. void WindowAppBlockStandard::addItemInterfaces()
  17. {
  18. // 添加标准接口
  19. WindowAppBlockStandardBase::addItemInterfaces();
  20. // 添加ToolStart接口
  21. WindowAppBlockStandardBase::addItemToolInterfaces();
  22. }
  23. /// <summary>
  24. /// 双击弹出设置对话框
  25. /// </summary>
  26. /// <param name="event"></param>
  27. void WindowAppBlockStandard::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event)
  28. {
  29. // 只有鼠标左键才响应
  30. if (event->button() == Qt::LeftButton && !m_bShowOnly)
  31. {
  32. // qDebug() << this->scenePos();
  33. m_pPou->ShowToolDialog(this);
  34. }
  35. }
  36. /// <summary>
  37. /// 2022-3-10 根据Dll中的信息动态添加接口
  38. /// </summary>
  39. /// <param name="infList"></param>
  40. bool WindowAppBlockStandard::addDynamicInterfacesFromDll(const QList<DLL_INF>& infList, QString& strReason)
  41. {
  42. //// 此处需要去掉const属性了,因为需要动态给Tool增加端口
  43. //TOOL* pTool = const_cast<TOOL*>(this->toolInfo);
  44. for (const DLL_INF& inf : infList)
  45. {
  46. // 首先检查Tool中是否已经有此名字的接口了
  47. if (m_toolInfo->contains(inf.strName))
  48. {
  49. strReason = "[Error] Interface [" + inf.strName + " is already exist.";
  50. return false;
  51. }
  52. // 建立对应的Interface数据结构
  53. _INTERFACE* newInf = new _INTERFACE(m_toolInfo);
  54. newInf->basedFrom(inf);
  55. // 设置全名
  56. newInf->strFullName = m_toolInfo->strInstanceName + "." + newInf->strName;
  57. // 添加到Tool数据结构中
  58. m_toolInfo->addInterface(newInf);
  59. // 生成对应的InterfaceItem
  60. WindowAppItemInterface* pNewItemInf = new WindowAppItemInterface(
  61. newInf,
  62. m_pPou,
  63. this->m_toolInfo,
  64. m_bShowOnly,
  65. this
  66. );
  67. if (!m_bShowOnly)
  68. {
  69. // Pou中保存此接口的对应关系
  70. m_pPou->registerInterface(pNewItemInf, newInf);
  71. }
  72. // 保存接口Item信息用于绘制
  73. m_itemInterfaces.append(pNewItemInf);
  74. }
  75. // 重新计算矩形区域
  76. this->updateRect();
  77. // 接口全部动态添加完毕之后,更新Interface的位置
  78. WindowAppBlockBase::updateInterfacesPostion();
  79. return true;
  80. }
  81. /// <summary>
  82. /// 2022-3-11 根据Dll中的信息动态删除接口
  83. /// </summary>
  84. /// <param name="infList"></param>
  85. bool WindowAppBlockStandard::deleteDynamicInterfacesFromDll(const QList<DLL_INF>& infList, QString& strReason)
  86. {
  87. // 取得Tool指针
  88. // TOOL* pTool = const_cast<TOOL*>(this->toolInfo);
  89. // 遍历需要删除的接口
  90. for (const DLL_INF& inf : infList)
  91. {
  92. // 取得接口指针
  93. _INTERFACE* pDelInf = m_toolInfo->getInterfaceByName(inf.strName);
  94. if (pDelInf == nullptr)
  95. {
  96. strReason = "[Error] WindowAppBlockStandard::delDynamicInterfacesFromDll - but "
  97. + inf.strName + " is not exist.";
  98. return false;
  99. }
  100. // 取得接口指针的图形单元
  101. WindowAppItemInterface* pInfItem = m_pPou->GetInterfaceItemByName(pDelInf->strFullName);
  102. if (pInfItem == nullptr)
  103. {
  104. strReason = "[Error] WindowAppBlockStandard::delDynamicInterfacesFromDll - but item "
  105. + pDelInf->strFullName + " is not exist.";
  106. return false;
  107. }
  108. // 只允许删除动态接口
  109. if (!pDelInf->bDynamic)
  110. {
  111. strReason = "[Error] " + inf.strName + " is not dynamic intrface, can't be delete!";
  112. return false;
  113. }
  114. // 被引用的接口不允许删除
  115. if (pDelInf->nRefCount > 0)
  116. {
  117. strReason = "[Error] Can't delete interface[" + pDelInf->strFullName + "] : ref count > 0";
  118. return false;
  119. }
  120. // 层层检查完毕之后,开始执行删除动作
  121. // Pou相关接口中删除此接口相关数据结构
  122. m_pPou->DelInterface(pDelInf->strFullName, pInfItem, pDelInf);
  123. // m_itemInterfaces 中删除
  124. QVector<WindowAppItemInterface*>::iterator itr = m_itemInterfaces.begin();
  125. for (; itr != m_itemInterfaces.end();)
  126. {
  127. if (*itr == pInfItem)
  128. {
  129. // 要从scene()中移除这个Item,要不界面上不会消失
  130. scene()->removeItem(*itr);
  131. itr = m_itemInterfaces.erase(itr);
  132. break;
  133. }
  134. else
  135. {
  136. itr++;
  137. }
  138. }
  139. // 最后从Tool中删除此接口数据结构
  140. m_toolInfo->delInterfaceByName(inf.strName);
  141. }
  142. // 本功能块重绘
  143. this->updatePosition();
  144. return true;
  145. }