123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- #include "DialogWait.h"
- #include "Common.h"
- #include "Utility"
- #include "DialogWaitSelection.h"
- DialogWait::DialogWait(
- VARIABLE* var,
- bool skipWait,
- VALUE* waitValue,
- int nTimeout,
- QWidget *parent
- )
- : QDialog(parent)
- , m_selVariable(var)
- , m_tmpSelection(nullptr)
- , m_WaitValue(waitValue)
- , m_nTimeout(nTimeout)
- , m_bSkipWait(skipWait)
- {
- ui.setupUi(this);
- // 对话框初始化
- this->initUI();
- }
- DialogWait::~DialogWait()
- {
- }
- /// <summary>
- /// 对话框初始化
- /// </summary>
- void DialogWait::initUI()
- {
- this->setWindowTitle(("WaitTool Option"));
- // 设置对话框风格
- this->setAttribute(Qt::WA_QuitOnClose);
- this->setWindowModality(Qt::ApplicationModal);
- // 初始化绑定显示
- if (m_selVariable != nullptr)
- {
- ui.editEvent->setText(m_selVariable->strFullName);
- }
- // 设置Edit控件不可编辑
- ui.editEvent->setReadOnly(true);
- // 默认跳过单步执行时的等待
- ui.checkWait->setChecked(m_bSkipWait);
- // 默认的等待超时时间
- ui.editTimeOut->setText(QString::number(m_nTimeout));
- // 初始化WaitValue相关界面
- this->initWaitValueControls();
- // 槽函数
- connect(ui.okButton, SIGNAL(clicked()), this, SLOT(onButtonOKClicked()));
- connect(ui.buttonSel, SIGNAL(clicked()), this, SLOT(onButtonSelClicked()));
- connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
- // 设置固定窗体大小
- this->setFixedSize(363, 315);
- }
- /// <summary>
- /// 根据用户上一轮设置的WaitValue初始化界面
- /// </summary>
- bool DialogWait::initWaitValueControls()
- {
- // 值类型ComboBox(目前只支持int、bool、String)
- ui.comboType->addItem(Utility::getTypeString(VALUE_TYPE::Type_Int));
- ui.comboType->addItem(Utility::getTypeString(VALUE_TYPE::Type_Bool));
- ui.comboType->addItem(Utility::getTypeString(VALUE_TYPE::Type_String));
-
- //如果尚未初始化,则显示默认值
- if (m_WaitValue->type == VALUE_TYPE::Type_Unknown)
- {
- ui.comboType->setCurrentIndex(0);
- ui.editValue->setText("");
- return true;
- }
- // 否则根据初始化的值来显示
- if (m_WaitValue->type == VALUE_TYPE::Type_Int)
- {
- ui.comboType->setCurrentIndex(0);
- }
- else if (m_WaitValue->type == VALUE_TYPE::Type_Bool)
- {
- ui.comboType->setCurrentIndex(1);
- }
- else if (m_WaitValue->type == VALUE_TYPE::Type_String)
- {
- ui.comboType->setCurrentIndex(2);
- }
- else
- {
- vDebug() << "[Error] Unsupported WaitValue init type: " << m_WaitValue->type;
- return false;
- }
-
- // 显示WaitValue的默认值
- ui.editValue->setText(m_WaitValue->toString());
- return true;
- }
- /// <summary>
- /// OK按钮
- /// </summary>
- void DialogWait::onButtonOKClicked()
- {
- if (ui.editEvent->text().isEmpty())
- {
- CRITICAL_MESSAGE("Please select variable first!");
- return;
- }
- // 获取用户输入
- // (增加这个判断,是为了防止用户没有点击选择框就直接点击保存的情况,此时针对用户的选择就不做改变)
- if (m_tmpSelection)
- {
- m_selVariable = m_tmpSelection;
- }
- // 获取用户设置的等待超时
- if (ui.editTimeOut->text().toInt() < 0)
- {
- CRITICAL_MESSAGE("Timeout is invalid.");
- return;
- }
- // 保存用户设置的等待变量
- bool bSave = this->saveWaitValue();
- // 保存等待变量异常
- if (!bSave)
- {
- return;
- }
- // 用户设置的超时时间
- m_nTimeout = ui.editTimeOut->text().toInt();
- // 是否跳过等待
- m_bSkipWait = ui.checkWait->isChecked();
-
- this->accept();
- }
- /// <summary>
- /// 将用户输入的等待变量转换成VARIABLE*保存
- /// </summary>
- bool DialogWait::saveWaitValue()
- {
- QString typeText = ui.comboType->currentText();
- QString valueText = ui.editValue->text();
- // 如果用户留空,那么则把数值清空
- if (valueText.isEmpty())
- {
- m_WaitValue->clear();
- return true;
- }
- // 然后,只有DB类型的变量才可以输入等待数值
- if (m_selVariable->parent()->gvlMode != GVL_MODE::GVL_DB)
- {
- CRITICAL_MESSAGE(" Setting wait value is only supported by DB type.");
- return false;
- }
- // 否则需要首先检查等待变量的类型和绑定变量的类型是否一致
- if (typeText != Utility::getTypeString(m_selVariable->value.type))
- {
- CRITICAL_MESSAGE("Wait value type is diffrent with selected value.");
- return false;
- }
- // Int
- if (typeText == Utility::getTypeString(VALUE_TYPE::Type_Int))
- {
- int nValue = valueText.toInt();
- m_WaitValue->type = VALUE_TYPE::Type_Int;
- m_WaitValue->setValue<int>(nValue);
- }
- // Bool
- else if (typeText == Utility::getTypeString(VALUE_TYPE::Type_Bool))
- {
- bool bValue;
- if (valueText == STRING_TRUE)
- {
- bValue = true;
- }
- else
- {
- bValue = false;
- }
- m_WaitValue->type = VALUE_TYPE::Type_Bool;
- m_WaitValue->setValue<bool>(bValue);
- }
- // String
- else if (typeText == Utility::getTypeString(VALUE_TYPE::Type_String))
- {
- m_WaitValue->type = VALUE_TYPE::Type_String;
- m_WaitValue->setValue<QString>(valueText);
- }
- // Error
- else
- {
- vDebug() << "[Error] Unsupported WaitValue type: " << typeText;
- CRITICAL_MESSAGE("Wait value is invalid.");
- return false;
- }
- vDebug() << " Set wait value = " << m_WaitValue->toString();
- return true;
- }
- /// <summary>
- /// ... 按钮
- /// </summary>
- void DialogWait::onButtonSelClicked()
- {
- DialogWaitSelection dialogWaitSel;
- if (dialogWaitSel.exec() == DialogWaitSelection::Accepted)
- {
- // 临时保存,用户选择的变量
- this->m_tmpSelection = dialogWaitSel.m_selVariable;
- // 显示用户的选择
- ui.editEvent->setText(this->m_tmpSelection->strFullName);
- }
- }
|