DialogBlockProperty.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include "DialogBlockProperty.h"
  2. #include "WindowAppBlockStandard.h"
  3. #include "WindowAppItemInterface.h"
  4. #include "Pou.h"
  5. // 对话框默认标题
  6. #define DEFAULT_WINDOW_TITLE QString("Property Sheets - ")
  7. DialogBlockProperty::DialogBlockProperty(TOOL* tool, POU* Pou, QWidget *parent)
  8. : QDialog(parent)
  9. , m_toolInfo(tool)
  10. , m_pPou(Pou)
  11. {
  12. ui.setupUi(this);
  13. // 初始化界面
  14. this->initUI();
  15. }
  16. DialogBlockProperty::~DialogBlockProperty()
  17. {
  18. }
  19. /// <summary>
  20. /// 对话框初始化
  21. /// </summary>
  22. void DialogBlockProperty::initUI()
  23. {
  24. // 根据工具名字设置窗口标题
  25. this->setWindowTitle(DEFAULT_WINDOW_TITLE + m_toolInfo->strInstanceName);
  26. // 设置默认Sheet
  27. ui.tabSettings->setCurrentIndex(0);
  28. // 根据工具的信息填充UI界面
  29. ui.editInfo->setText(m_toolInfo->strInfo);
  30. ui.editName->setText(m_toolInfo->strInstanceName);
  31. ui.editBeforeDelay->setText(QString::number(m_toolInfo->execParams.nPreDelay));
  32. ui.editAfterDelay->setText(QString::number(m_toolInfo->execParams.nPostDelay));
  33. // 保存工具的旧名字备用
  34. blockSettings.strOldInstanceName = m_toolInfo->strInstanceName;
  35. // 设置工具是否可用
  36. if (m_toolInfo->bEnable)
  37. {
  38. ui.checkEnableTool->setChecked(true);
  39. }
  40. else
  41. {
  42. ui.checkEnableTool->setChecked(false);
  43. }
  44. QRegExp regExp("[0-9]+$");//只能输入数字
  45. ui.editBeforeDelay->setValidator(new QRegExpValidator(regExp, this));
  46. ui.editAfterDelay->setValidator(new QRegExpValidator(regExp, this));
  47. // 绘制工具图像
  48. this->initToolBlock();
  49. // 初始化设置工具Index的ComboBox
  50. this->initComboToolIndex();
  51. // 槽函数
  52. connect(ui.okButton, SIGNAL(clicked()), this, SLOT(onButtonOkClicked()));
  53. connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
  54. // 设置固定窗体大小
  55. this->setFixedSize(538, 391);
  56. //this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
  57. }
  58. /// <summary>
  59. /// 绘制工具的图像(以及CheckBox)
  60. /// </summary>
  61. void DialogBlockProperty::initToolBlock()
  62. {
  63. // 设置为不可交互
  64. // ui.viewBlockProperty->setInteractive(false);
  65. // 设置不传递任何消息到子控件
  66. // ui.viewBlockProperty->setAttribute(Qt::WA_TransparentForMouseEvents, true);
  67. // 初始化scene
  68. QGraphicsScene* blockScene = new QGraphicsScene(this);
  69. blockScene->setSceneRect(QRectF(0, 0, 0, 0));
  70. ui.viewBlockProperty->setScene(blockScene);
  71. // 新建一个功能块信息
  72. WindowAppBlockStandard* newItem = new WindowAppBlockStandard(m_toolInfo, m_pPou, true);
  73. // scene中添加功能块
  74. blockScene->addItem(newItem);
  75. // 在界面中心位置添加此Block
  76. newItem->setPos(QPoint(0,0));
  77. // 添加功能块接口
  78. newItem->addItemInterfaces();
  79. // 根据接口情况动态添加checkbox
  80. for (int i = 0; i < newItem->m_itemInterfaces.size(); i++)
  81. {
  82. WindowAppItemInterface* pInfItem = newItem->m_itemInterfaces[i];
  83. // 跳过ToolStart接口, 并行接口,goto 接口,非标准接口
  84. if (pInfItem->m_infInfo->isToolStart()
  85. || pInfItem->m_infInfo->isParallelToolStart()
  86. || pInfItem->m_infInfo->isParallelToolEnd()
  87. || pInfItem->m_infInfo->isGotoToolEnd()
  88. || pInfItem->m_infInfo->Type != INF_TYPE::INF_TYPE_STANDARD
  89. )
  90. {
  91. continue;
  92. }
  93. // 获取本接口的真实线段位置
  94. QLineF realLine = QLineF(
  95. pInfItem->mapToScene(pInfItem->line().p1()),
  96. pInfItem->mapToScene(pInfItem->line().p2())
  97. );
  98. // 添加Checkbox
  99. QCheckBox* pNewCheckBox = new QCheckBox();
  100. if (pInfItem->m_infInfo->nRefCount > 0)
  101. {
  102. pNewCheckBox->setText(QString::number(pInfItem->m_infInfo->nRefCount));
  103. pNewCheckBox->setEnabled(false);
  104. }
  105. // 添加到scene中
  106. QGraphicsProxyWidget* pCheckWidget = blockScene->addWidget(pNewCheckBox);
  107. if (pInfItem->m_infInfo->isDirInput())
  108. {
  109. // 微调一下偏移
  110. QPointF pos = QPointF(
  111. realLine.p1().x() - pNewCheckBox->geometry().width() - 5,
  112. realLine.p1().y() - pNewCheckBox->geometry().height() / 2
  113. );
  114. pCheckWidget->setPos(pos);
  115. }
  116. else
  117. {
  118. // 微调一下偏移
  119. QPointF pos = QPointF(
  120. realLine.p1().x() + pNewCheckBox->geometry().width() + 5,
  121. realLine.p1().y() - pNewCheckBox->geometry().height() / 2
  122. );
  123. pCheckWidget->setPos(pos);
  124. }
  125. // 根据接口启用与否打钩
  126. pNewCheckBox->setChecked(pInfItem->m_infInfo->bEnable);
  127. infCheckBoxs.push_back(pNewCheckBox);
  128. }
  129. }
  130. /// <summary>
  131. /// 2022-9-28 初始化设置工具Index的ComboBox
  132. /// </summary>
  133. void DialogBlockProperty::initComboToolIndex()
  134. {
  135. // 取出工具当前Index
  136. int nCurrentIndex = this->m_toolInfo->nIndex;
  137. // 取出当前Pou所有工具的Index
  138. int nTotalIndex = this->m_pPou->GetIndexedToolsCount();
  139. // ComboBox中的数字为当前工具所有可以选的Index合集
  140. for (int i = 1; i <= nTotalIndex; i++)
  141. {
  142. ui.comboIndex->addItem(QString::number(i));
  143. }
  144. // 将当前Tool的Index设置为ComboBox的默认文字
  145. ui.comboIndex->setCurrentIndex(nCurrentIndex);
  146. }
  147. /// <summary>
  148. /// 保存用户的设置项
  149. /// </summary>
  150. void DialogBlockProperty::saveBlockSettings()
  151. {
  152. for (int i = 0; i < infCheckBoxs.size(); i++)
  153. {
  154. blockSettings.infEnables.push_back(infCheckBoxs[i]->isChecked());
  155. }
  156. blockSettings.bToolEnable = ui.checkEnableTool->isChecked();
  157. blockSettings.strNewInstanceName = ui.editName->text();
  158. blockSettings.strInfo = ui.editInfo->toPlainText();
  159. blockSettings.nInDelay = ui.editBeforeDelay->text().toInt();
  160. blockSettings.nOutDelay = ui.editAfterDelay->text().toInt();
  161. // 2022-9-28,保存用户输入的新索引号,注意这里值要-1,因为界面上的值是从1开始的,实际功能块是从0开始的
  162. blockSettings.nNewIndex = ui.comboIndex->currentText().toInt() - 1;
  163. }
  164. /// <summary>
  165. /// Ok按钮
  166. /// </summary>
  167. void DialogBlockProperty::onButtonOkClicked()
  168. {
  169. // 首先检查用户设置的实例名称是否重名,要是有重名的则报错
  170. if ( blockSettings.strOldInstanceName!=ui.editName->text() &&
  171. m_pPou->isInstanceNameDuplicated(ui.editName->text())
  172. )
  173. {
  174. Utility::VPCriticalMessageBox(QString(ui.editName->text() + "is duplicated!"));
  175. return;
  176. }
  177. // 保存用户的设置项
  178. saveBlockSettings();
  179. // 关闭对话框
  180. this->accept();
  181. }