123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493 |
- #include "ToolDialog.h"
- QEvent::Type SyncControlEvent::m_EventType = QEvent::None;
- QEvent::Type SyncHdValueEvent::m_EventType = QEvent::None;
- ToolDialogImpl::ToolDialogImpl(QWidget *parent)
- : DllToolDialog(parent)
- {
- ui.setupUi(this);
- this->setWindowFlags(Qt::Dialog | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint /*| Qt::WindowStaysOnTopHint*/);
- // connect(this, SIGNAL(sigUpdateUI()), this, SLOT(on_UpdateUI())/*, Qt::QueuedConnection*/);
- //m_Value = 0;
- //m_Value2 = 20;
- //m_Value3 = 0;
- m_intInput = 10;
- m_intOutput = 3;
- m_nIndex = 0;
- m_nIndex2 = 0;
- m_strInput = "strInput";
- m_strOutput = "strOutput";
- //m_intValuePtr = 200;
- m_pInt = nullptr;
- // 为导出的测试控件赋值
- m_expCheck = ui.expCheck;
- m_expCombo = ui.expCombo;
- m_expList = ui.expList;
- m_expRadio = ui.expRadio;
- // m_expText = ui.expText;
- m_expButton = ui.expButton;
- m_expEdit = ui.expEdit;
- // 初始化Combo和List供测试
- QStringList comList;
- comList << "Com1" << "Com2" << "Com3";
- m_expCombo->addItems(comList);
- m_expCombo->setCurrentIndex(0);
- QStringList testList;
- testList << "List1" << "List2" << "List3";
- m_expList->insertItems(0, testList);
- m_expList->setCurrentRow(0);
- // 设置Timer
- m_Timer.setInterval(20000);
- m_Timer2.setInterval(40000);
- connect(&m_Timer, SIGNAL(timeout()), this, SLOT(onTimer()));
- connect(&m_Timer2, SIGNAL(timeout()), this, SLOT(onTimer2()));
- m_Timer.start();
- m_Timer2.start();
- // 绑定测试LineEdit变动信号
- connect(ui.expEdit, SIGNAL(textChanged(QString)), this, SLOT(OnTestEditChange(QString)));
- // 绑定测试ComboBox变动信号
- connect(ui.expCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(OnTestComboChange(int)));
- // 绑定测试ListBox变动信号
- connect(ui.expList, SIGNAL(currentRowChanged(int)), this, SLOT(OnTestListChange(int)));
- // 绑定测试RadioBox变动信号
- connect(ui.expRadio, SIGNAL(toggled(bool)), this, SLOT(OnTestRadioChange(bool)));
- // 绑定测试CheckBox变动信号
- connect(ui.expCheck, SIGNAL(stateChanged(int)), this, SLOT(OnTestCheckChange(int)));
- on_pushButton_clicked();
- //// 模拟延迟
- //Utility::qSleep(3000);
- }
- ToolDialogImpl::~ToolDialogImpl()
- {
- }
- VPEnum::RETURN_VALUE ToolDialogImpl::Execute()
- {
- int a = 100;
- // 测试索引连接,每次+1
- m_nIndex++;
- if (m_nIndex >= 6)
- {
- m_nIndex = 0;
- }
- m_nIndex2++;
- if (m_nIndex2 >= 4)
- {
- m_nIndex2 = 0;
- }
- //m_Value3 = m_Value + m_Value2;
- // qDebug() << "[Dll] ValueTest tool Execute start.";
- // 为了测试加一个延时
- //Utility::qSleep(2000);
- // for test
- m_intInput += 10;
- m_intOutput = m_intInput + 10;
- m_strInput += "p";
- m_strOutput = m_strInput + "p";
- //n += 10;
- //m_strOutput = m_strInput + str;
- //str += "a";
- //if (m_pInt != nullptr)
- //{
- // *m_pInt = *m_pInt + 20;
- //}
- on_pushButton_clicked();
- // qDebug() << "[Dll] ValueTest tool Execute finished.";
- QDateTime time = QDateTime::currentDateTime();
- m_strOutput = time.toString("yyyy.MM.dd hh:mm:ss.zzz ddd");
- // 测试硬件工具的接口更新(这种方法仅可用于硬件工具)
- this->syncHdValueToUi(&m_intOutput);
- //// 将变动的变量统一加入到event参数中
- //
- //SyncValueEvent* valueEvent = new SyncValueEvent();
- //// 数值变动1
- //SYNC_VALUE syncValue1;
- //syncValue1.m_pSrcValue = &m_intOutput;
- //syncValue1.m_ValueType = VALUE_TYPE::Type_Int;
- //valueEvent->addSyncValue(syncValue1);
- //// 数值变动2
- //SYNC_VALUE syncValue2;
- //syncValue1.m_pSrcValue = &m_strOutput;
- //syncValue2.m_ValueType = VALUE_TYPE::Type_String;
- //valueEvent->addSyncValue(syncValue2);
- //// 发送 value 同步 event
- //QCoreApplication::sendEvent(m_pSyncTarget, valueEvent);
- return VPEnum::RETURN_VALUE::Success;
- }
- void ToolDialogImpl::Running(bool bRun)
- {
- }
- bool ToolDialogImpl::Serialized(QDataStream& ar, bool bIsOut)
- {
- int paranum;//参数数量
- if (bIsOut)//保存参数流程
- {
- paranum = 1;
- ar << paranum;//先保存参数数量
- ar << (int)1 << m_intInput;
-
- }
- else//加载参数流程,参数加载顺序一定要跟保存顺序一致
- {
- int para;
- ar >> paranum;//读取参数数量
- for (int i = 0; i < paranum; i++)
- {
- ar >> para;
- switch (para)
- {
- case 1: ar >> m_intInput; break;
-
-
- default:
- {
- qWarning() << "Serialized(In) Error";
- return false;
- }
- break;
- }
- }
- //hwndUnit->getHWndCtrl()->usem_pRoiController(&m_pRoiController);
- }
- return true;
- }
- //void ToolDialogImpl::on_UpdateUI()
- //{
- // // 2021-10-24,Tool execute完毕之后需要向UI同步数值变更(有几个数值变更就写几个)
- //
- // if (valueCallback != nullptr)
- // {
- // // 向UI同步
- // valueCallback(&m_intOutput, VALUE_TYPE::Type_Int);
- //
- // valueCallback(&m_strOutput, VALUE_TYPE::Type_String);
- //
- //
- // qDebug() << "ValueTest dll value change [m_intOutput] - " << m_intOutput;
- // }
- // else
- // {
- // qDebug() << "ValueTest dll value change [m_intOutput] - " << m_intOutput << ", but valueCallback is nullptr";
- // }
- //
- //}
- /// <summary>
- /// 确定
- /// </summary>
- void ToolDialogImpl::on_btnOK_clicked()
- {
- this->hide();
- }
- /// <summary>
- /// 取消按钮
- /// </summary>
- void ToolDialogImpl::on_btnCancel_clicked()
- {
- // 将本工具的恢复到打开工具之前的状态
- RecoverData();
- }
- /// <summary>
- /// 测试按钮
- /// </summary>
- void ToolDialogImpl::on_btnExecute_clicked()
- {
- QElapsedTimer toolTimer;
- toolTimer.start();
- // 发送事件
- ToolEvent* pToolEvent = new ToolEvent(m_strPouName, m_strInstanceName, TOOL_EVENT_TYPE::TOOL_TRIGGER);
- QCoreApplication::sendEvent(m_pEventTarget, pToolEvent);
- // 统计返回值
- VPEnum::RETURN_VALUE ret = pToolEvent->ret;
- double nExecTime = toolTimer.elapsed();
- QString str;
- str = QString("耗时: %1 ms").arg(nExecTime, 0, 'G', 5);
- ui.label_time->setText(str);
- str = QString("状态: %1 ").arg(QMetaEnum::fromType<VPEnum::RETURN_VALUE>().key((short)ret));
- ui.label_state->setText(str);
- delete pToolEvent;
- }
- /// <summary>
- /// Refresh Button
- /// </summary>
- void ToolDialogImpl::on_pushButton_clicked()
- {
- // QMessageBox::information(nullptr, "Value Test", "Refresh Button", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
- // 将每个按钮的最新值写入到控件中
- ui.editIntInput->setText(QString::number(m_intInput));
- ui.editIntOutput->setText(QString::number(m_intOutput));
- ui.editStringInput->setText(m_strInput);
- ui.editStringOutput->setText(m_strOutput);
- //ui.editIntP->setText(QString::asprintf("%0X", m_pInt));
- //ui.editIntP2->setText(QString::number(*m_pInt));
- if (m_pInt != nullptr)
- {
- ui.editIntP->setText(QString::asprintf("%0X", m_pInt));
- ui.editIntP2->setText(QString::number(*m_pInt));
- }
- }
- /// <summary>
- /// Update Button,将界面中的值写入到变量中
- /// </summary>
- void ToolDialogImpl::on_updateButton_clicked()
- {
- m_intInput = ui.editIntInput->text().toInt();
- m_intOutput = ui.editIntOutput->text().toInt();
- m_strInput = ui.editStringInput->text();
- m_strOutput = ui.editStringOutput->text();
- // *m_pInt = ui.editIntP2->text().toInt();
- if (m_pInt != nullptr)
- {
- *m_pInt = ui.editIntP2->text().toInt();
- }
- QMessageBox::information(nullptr, "Value Test", "Update ok!");
- }
- /// <summary>
- /// ExpButton设置
- /// </summary>
- void ToolDialogImpl::on_expButton_clicked()
- {
- QMessageBox::information(nullptr, "Value Test Dialog", "Export Button!");
- }
- /// <summary>
- /// 发送事件测试
- /// </summary>
- void ToolDialogImpl::on_btnSendEvent_clicked()
- {
- if (m_pEventTarget != nullptr)
- {
- // 2022-9-19MEMO:此处的Event会发送到TaskManager中,以 TRIGGER_TASK_THREAD 的方式执行,
- // 然后触发到已经启动并且处于等待状态的Task自动执行
- QCoreApplication::sendEvent(m_pEventTarget, &m_eventOut);
- vDebug() << "Send event from ValueTest dll...";
- }
- }
- void ToolDialogImpl::OnTestEditChange(const QString& str)
- {
- // qDebug() << "ValueTest dll OnTestEditChange() - " << str;
- if (controlCallback != nullptr)
- {
- // 向UI同步(回调方式)
- // controlCallback(ui.expEdit, UI_SYNC_MSG::EDIT_TEXT_CHANGED);
- // 向UI同步 postEvent方式
- this->syncControlToUi(ui.expEdit, UI_SYNC_MSG::EDIT_TEXT_CHANGED);
- vDebug() << "ValueTest dll OnTestEditChange() - " << str;
- }
- else
- {
- vDebug() << "ValueTest dll OnTestEditChange() - " << str << ", but callback is nullptr";
- }
-
- }
- /// <summary>
- /// 测试ComboBox变更消息
- /// </summary>
- /// <param name="nIndex"></param>
- void ToolDialogImpl::OnTestComboChange(const int nIndex)
- {
- if (controlCallback != nullptr)
- {
- // 向UI同步(回调方式)
- // controlCallback(ui.expCombo, UI_SYNC_MSG::COMBO_SEL_CHANGED);
- // 向UI同步 postEvent方式
- this->syncControlToUi(ui.expCombo, UI_SYNC_MSG::COMBO_SEL_CHANGED);
- vDebug() << "ValueTest dll OnTestComboChange()";
- }
- else
- {
- vDebug() << "ValueTest dll OnTestComboChange() , but callback is nullptr";
- }
- }
- /// <summary>
- /// 测试ListBox变更消息
- /// </summary>
- void ToolDialogImpl::OnTestListChange(int nRow)
- {
- if (controlCallback != nullptr)
- {
- // 向UI同步(回调方式)
- // controlCallback(ui.expList, UI_SYNC_MSG::LIST_SEL_CHANGED);
- // 向UI同步 postEvent方式
- this->syncControlToUi(ui.expList, UI_SYNC_MSG::LIST_SEL_CHANGED);
- vDebug() << "ValueTest dll OnTestListChange() - index : " << nRow;
- }
- else
- {
- vDebug() << "ValueTest dll OnTestListChange() , but callback is nullptr";
- }
- }
- /// <summary>
- /// 测试RadioBox变更消息
- /// </summary>
- void ToolDialogImpl::OnTestRadioChange(bool checked)
- {
- if (controlCallback != nullptr)
- {
- // 向UI同步(回调方式)
- controlCallback(ui.expRadio, UI_SYNC_MSG::RADIOBOX_CHANGED);
- // 向UI同步(postEvent方式)
- this->syncControlToUi(ui.expRadio, UI_SYNC_MSG::RADIOBOX_CHANGED);
- vDebug() << "ValueTest dll OnTestRadioChange() - checked : " << checked;
- }
- else
- {
- vDebug() << "ValueTest dll OnTestRadioChange() , but callback is nullptr";
- }
- }
- /// <summary>
- /// 测试CheckBox变更消息
- /// </summary>
- void ToolDialogImpl::OnTestCheckChange(int state)
- {
- if (controlCallback != nullptr)
- {
- // 向UI同步(回调方式)
- // controlCallback(ui.expCheck, UI_SYNC_MSG::CHECKBOX_CHANGED);
- // 向UI同步(postEvent方式)
- this->syncControlToUi(ui.expCheck, UI_SYNC_MSG::CHECKBOX_CHANGED);
- vDebug() << "ValueTest dll OnTestCheckChange() - state : " << state;
- }
- else
- {
- vDebug() << "ValueTest dll OnTestCheckChange() , but callback is nullptr";
- }
- }
- void ToolDialogImpl::onTimer()
- {
- // 触发工具内部事件供测试
- if (m_pEventTarget != nullptr)
- {
- // 此处的event变量会导致堆栈崩溃问题,临时屏蔽
- // 2022-0-19 原因找到了,因为postEvent是会自动释放Event变量,所以自动释放 m_eventOut 的时候会崩溃
- //QCoreApplication::postEvent(m_pEventTarget, &m_eventOut);
- }
- // qDebug() << "[Dll] m_eventOut wakeAll.";
- // 激活事件
- //m_eventOut.wakeAll();
- }
- void ToolDialogImpl::onTimer2()
- {
- // 触发工具内部事件供测试
- if (m_pEventTarget != nullptr)
- {
- // QCoreApplication::postEvent(m_pEventTarget, &m_eventOut2);
- }
- // qDebug() << "[Dll] m_eventOut2 wakeAll.";
- // 激活事件
- //m_eventOut2.wakeAll();
- }
|