DialogWait.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #include "DialogWait.h"
  2. #include "Common.h"
  3. #include "Utility"
  4. #include "DialogWaitSelection.h"
  5. DialogWait::DialogWait(
  6. VARIABLE* var,
  7. bool skipWait,
  8. VALUE* waitValue,
  9. int nTimeout,
  10. QWidget *parent
  11. )
  12. : QDialog(parent)
  13. , m_selVariable(var)
  14. , m_tmpSelection(nullptr)
  15. , m_WaitValue(waitValue)
  16. , m_nTimeout(nTimeout)
  17. , m_bSkipWait(skipWait)
  18. {
  19. ui.setupUi(this);
  20. // 对话框初始化
  21. this->initUI();
  22. }
  23. DialogWait::~DialogWait()
  24. {
  25. }
  26. /// <summary>
  27. /// 对话框初始化
  28. /// </summary>
  29. void DialogWait::initUI()
  30. {
  31. this->setWindowTitle(("WaitTool Option"));
  32. // 设置对话框风格
  33. this->setAttribute(Qt::WA_QuitOnClose);
  34. this->setWindowModality(Qt::ApplicationModal);
  35. // 初始化绑定显示
  36. if (m_selVariable != nullptr)
  37. {
  38. ui.editEvent->setText(m_selVariable->strFullName);
  39. }
  40. // 设置Edit控件不可编辑
  41. ui.editEvent->setReadOnly(true);
  42. // 默认跳过单步执行时的等待
  43. ui.checkWait->setChecked(m_bSkipWait);
  44. // 默认的等待超时时间
  45. ui.editTimeOut->setText(QString::number(m_nTimeout));
  46. // 初始化WaitValue相关界面
  47. this->initWaitValueControls();
  48. // 槽函数
  49. connect(ui.okButton, SIGNAL(clicked()), this, SLOT(onButtonOKClicked()));
  50. connect(ui.buttonSel, SIGNAL(clicked()), this, SLOT(onButtonSelClicked()));
  51. connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
  52. // 设置固定窗体大小
  53. this->setFixedSize(363, 315);
  54. }
  55. /// <summary>
  56. /// 根据用户上一轮设置的WaitValue初始化界面
  57. /// </summary>
  58. bool DialogWait::initWaitValueControls()
  59. {
  60. // 值类型ComboBox(目前只支持int、bool、String)
  61. ui.comboType->addItem(Utility::getTypeString(VALUE_TYPE::Type_Int));
  62. ui.comboType->addItem(Utility::getTypeString(VALUE_TYPE::Type_Bool));
  63. ui.comboType->addItem(Utility::getTypeString(VALUE_TYPE::Type_String));
  64. //如果尚未初始化,则显示默认值
  65. if (m_WaitValue->type == VALUE_TYPE::Type_Unknown)
  66. {
  67. ui.comboType->setCurrentIndex(0);
  68. ui.editValue->setText("");
  69. return true;
  70. }
  71. // 否则根据初始化的值来显示
  72. if (m_WaitValue->type == VALUE_TYPE::Type_Int)
  73. {
  74. ui.comboType->setCurrentIndex(0);
  75. }
  76. else if (m_WaitValue->type == VALUE_TYPE::Type_Bool)
  77. {
  78. ui.comboType->setCurrentIndex(1);
  79. }
  80. else if (m_WaitValue->type == VALUE_TYPE::Type_String)
  81. {
  82. ui.comboType->setCurrentIndex(2);
  83. }
  84. else
  85. {
  86. vDebug() << "[Error] Unsupported WaitValue init type: " << m_WaitValue->type;
  87. return false;
  88. }
  89. // 显示WaitValue的默认值
  90. ui.editValue->setText(m_WaitValue->toString());
  91. return true;
  92. }
  93. /// <summary>
  94. /// OK按钮
  95. /// </summary>
  96. void DialogWait::onButtonOKClicked()
  97. {
  98. if (ui.editEvent->text().isEmpty())
  99. {
  100. CRITICAL_MESSAGE("Please select variable first!");
  101. return;
  102. }
  103. // 获取用户输入
  104. // (增加这个判断,是为了防止用户没有点击选择框就直接点击保存的情况,此时针对用户的选择就不做改变)
  105. if (m_tmpSelection)
  106. {
  107. m_selVariable = m_tmpSelection;
  108. }
  109. // 获取用户设置的等待超时
  110. if (ui.editTimeOut->text().toInt() < 0)
  111. {
  112. CRITICAL_MESSAGE("Timeout is invalid.");
  113. return;
  114. }
  115. // 保存用户设置的等待变量
  116. bool bSave = this->saveWaitValue();
  117. // 保存等待变量异常
  118. if (!bSave)
  119. {
  120. return;
  121. }
  122. // 用户设置的超时时间
  123. m_nTimeout = ui.editTimeOut->text().toInt();
  124. // 是否跳过等待
  125. m_bSkipWait = ui.checkWait->isChecked();
  126. this->accept();
  127. }
  128. /// <summary>
  129. /// 将用户输入的等待变量转换成VARIABLE*保存
  130. /// </summary>
  131. bool DialogWait::saveWaitValue()
  132. {
  133. QString typeText = ui.comboType->currentText();
  134. QString valueText = ui.editValue->text();
  135. // 如果用户留空,那么则把数值清空
  136. if (valueText.isEmpty())
  137. {
  138. m_WaitValue->clear();
  139. return true;
  140. }
  141. // 然后,只有DB类型的变量才可以输入等待数值
  142. if (m_selVariable->parent()->gvlMode != GVL_MODE::GVL_DB)
  143. {
  144. CRITICAL_MESSAGE(" Setting wait value is only supported by DB type.");
  145. return false;
  146. }
  147. // 否则需要首先检查等待变量的类型和绑定变量的类型是否一致
  148. if (typeText != Utility::getTypeString(m_selVariable->value.type))
  149. {
  150. CRITICAL_MESSAGE("Wait value type is diffrent with selected value.");
  151. return false;
  152. }
  153. // Int
  154. if (typeText == Utility::getTypeString(VALUE_TYPE::Type_Int))
  155. {
  156. int nValue = valueText.toInt();
  157. m_WaitValue->type = VALUE_TYPE::Type_Int;
  158. m_WaitValue->setValue<int>(nValue);
  159. }
  160. // Bool
  161. else if (typeText == Utility::getTypeString(VALUE_TYPE::Type_Bool))
  162. {
  163. bool bValue;
  164. if (valueText == STRING_TRUE)
  165. {
  166. bValue = true;
  167. }
  168. else
  169. {
  170. bValue = false;
  171. }
  172. m_WaitValue->type = VALUE_TYPE::Type_Bool;
  173. m_WaitValue->setValue<bool>(bValue);
  174. }
  175. // String
  176. else if (typeText == Utility::getTypeString(VALUE_TYPE::Type_String))
  177. {
  178. m_WaitValue->type = VALUE_TYPE::Type_String;
  179. m_WaitValue->setValue<QString>(valueText);
  180. }
  181. // Error
  182. else
  183. {
  184. vDebug() << "[Error] Unsupported WaitValue type: " << typeText;
  185. CRITICAL_MESSAGE("Wait value is invalid.");
  186. return false;
  187. }
  188. vDebug() << " Set wait value = " << m_WaitValue->toString();
  189. return true;
  190. }
  191. /// <summary>
  192. /// ... 按钮
  193. /// </summary>
  194. void DialogWait::onButtonSelClicked()
  195. {
  196. DialogWaitSelection dialogWaitSel;
  197. if (dialogWaitSel.exec() == DialogWaitSelection::Accepted)
  198. {
  199. // 临时保存,用户选择的变量
  200. this->m_tmpSelection = dialogWaitSel.m_selVariable;
  201. // 显示用户的选择
  202. ui.editEvent->setText(this->m_tmpSelection->strFullName);
  203. }
  204. }