123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809 |
- #include "UiManager.h"
- #include "WindowRuntime.h"
- #include "PouManager.h"
- #include "GvlManager.h"
- #include "WindowAppUiScene.h"
- #include "VPControls.h"
- // 字符串之间的间隔,用于Tip 与 Text 之间的组装
- #define STRING_INTERVAL ": "
- UiManager::UiManager()
- {
- m_pMainPage = nullptr;
- }
- /// <summary>
- /// 注册新的UIView
- /// </summary>
- /// <param name="pView"></param>
- void UiManager::registerNewUi(WindowAppUiView* pView)
- {
- QString strName = pView->m_strPageName;
- if (m_pAllPages.contains(strName))
- {
- m_pAllPages[strName] = pView;
- return;
- }
- m_pAllPages.insert(strName, pView);
- }
- /// <summary>
- /// 删除指定页面(此处需要检查是否删除的是主页)
- /// </summary>
- /// <param name="strName"></param>
- bool UiManager::deleteUi(const QString& strName, bool bForce)
- {
- // 如果删除的页面是主页,则提示是否确认删除,并且需要指定新的主页
- if (m_pMainPage!=nullptr
- && strName == m_pMainPage->m_strPageName)
- {
- // 是否不弹提示强制删除
- if (!bForce)
- {
- QMessageBox::StandardButton ret = Utility::VPQuesitonMessageBox("确定要删除主页[" + strName + "吗? 你需要指定新的主页后,才可以执行发布操作.");
- if (ret != QMessageBox::Yes)
- {
- return false;
- }
- }
- m_pMainPage = nullptr;
- }
- // 数据结构中删除
- m_pAllPages.remove(strName);
- qDebug() << "[UiManager] Delete UI " << strName << ".";
- return true;
- }
- /// <summary>
- /// 注册新的控件
- /// </summary>
- /// <param name="pNewControl"></param>
- /// <param name="pView"></param>
- void UiManager::regiterNewControl(VControlObject* pNewControl, WindowAppUiView* pView)
- {
- m_AllControls.insert(pNewControl->m_pWidget, pView);
- qDebug() << "[UiManager] UiManager::regiterNewControl - " << pNewControl << " in " << pView->m_strPageName;
- }
- /// <summary>
- /// 删除一个控件
- /// </summary>
- /// <param name="pDelControl"></param>
- void UiManager::deleteControl(QWidget* pDelControl)
- {
- m_AllControls.remove(pDelControl);
- // 删除本UI控件和其他的关联关系
- m_UiToDllControls.remove(pDelControl);
- m_UiToRtControls.remove(pDelControl);
- m_UiControlToValues.remove(pDelControl);
- QHash<QWidget*, QWidget*>::iterator it = m_DllToUiControls.begin();
- for (; it != m_DllToUiControls.end();)
- {
- if (it.value() == pDelControl)
- {
- m_DllToUiControls.erase(it);
- break;
- }
- else
- {
- it++;
- }
- }
- QHash<QWidget*, QWidget*>::iterator it2 = m_RtToUiControls.begin();
- for (; it2 != m_RtToUiControls.end();)
- {
- if (it2.value() == pDelControl)
- {
- m_RtToUiControls.erase(it2);
- break;
- }
- else
- {
- it2++;
- }
- }
- QHashIterator<VARIABLE*, QWidget*> it3(m_ValueToUiControls);
- while (it3.hasNext())
- {
- QList<QWidget*> pWidgets = m_ValueToUiControls.values(it3.next().key());
- for (QWidget* pWidget : pWidgets)
- {
- if (pWidget == pDelControl)
- {
- // 2021-11-9 取消DataLink标识
- it3.key()->bDataLink = false;
- // 减小引用计数
- if (it3.key()->nRefCount > 0)
- {
- it3.key()->nRefCount--;
- }
- m_ValueToUiControls.remove(it3.key(), pDelControl);
-
- break;
- }
- }
- }
- }
- /// <summary>
- /// 获取所有的Ui名称
- /// </summary>
- /// <returns></returns>
- QStringList UiManager::getAllUiNames()
- {
- // return this->m_UiNames;
- QStringList listNames;
- QMapIterator<QString, WindowAppUiView*> it(m_pAllPages);
- while (it.hasNext())
- {
- listNames.push_back(it.next().key());
- }
- return listNames;
- }
- /// <summary>
- /// 获取指定名字的页面
- /// </summary>
- /// <param name="strName"></param>
- /// <returns></returns>
- WindowAppUiView* UiManager::getPageByName(const QString& strName)
- {
- if (m_pAllPages.contains(strName))
- {
- return m_pAllPages[strName];
- }
- else
- {
- return nullptr;
- }
- }
- /// <summary>
- /// 检查各个页面中携带的属性是否正确有效
- /// </summary>
- /// <returns></returns>
- bool UiManager::checkAll()
- {
- QMapIterator<QString, WindowAppUiView*> it(m_pAllPages);
- while (it.hasNext())
- {
- // 依次检测每一个UI页面的属性设置
- bool bRet = it.next().value()->uiScene()->checkAllProperties();
- if (!bRet)
- {
- return false;
- }
- }
- return true;
- }
- /// <summary>
- /// 发布所有页面到Runtime执行(所有的关于主页的检查都放到这里来执行)
- /// </summary>
- bool UiManager::publishAll()
- {
- // 首先检查是否设定了主页
- if (m_pMainPage == nullptr)
- {
- CRITICAL_MESSAGE("Error: 请先设置主页面");
- return false;
- }
- // 检查各个页面中携带的属性是否正确有效(仅检查必填项)
- bool bRet = checkAll();
- if (!bRet)
- {
- return false;
- }
- // 然后将主页和所有页面都传输到Runtime发布
- return g_pRuntime->publishAllPages(m_pMainPage, m_pAllPages);
- }
- /// <summary>
- /// 设定主页
- /// </summary>
- /// <param name="strName"></param>
- bool UiManager::setMainPage(const QString& strName)
- {
- if (m_pAllPages.contains(strName))
- {
- m_pMainPage = m_pAllPages[strName];
- qDebug() << "[UiManager] Set " << strName << " as MainPage.";
- return true;
- }
- else
- {
- qWarning() << "[UiManager] Set " << strName << " as MainPage failed.";
- return false;
- }
- }
- /// <summary>
- /// 获取主页
- /// </summary>
- /// <returns></returns>
- WindowAppUiView* UiManager::getMainPage()
- {
- return this->m_pMainPage;
- }
- /// <summary>
- /// 序列化所有的WindowAppUiView*
- /// </summary>
- /// <param name="out"></param>
- /// <returns></returns>
- bool UiManager::serialized(QDataStream& out)
- {
- // 首先写入UI的总数量
- out << m_pAllPages.size();
- // 首先写入主页
- out << m_pMainPage;
- // 遍历所有的UIView,执行序列化
- QMapIterator<QString, WindowAppUiView*> it(m_pAllPages);
- while (it.hasNext())
- {
- it.next();
- // 跳过主页
- if (it.value() != m_pMainPage)
- {
- out << it.value();
- }
- }
- return true;
- }
- //=====================================================================
- //
- // 同步相关代码
- //
- //=====================================================================
- /// <summary>
- /// 接收来自其他控件和变量的同步Event
- /// </summary>
- /// <param name="event"></param>
- /// <returns></returns>
- void UiManager::customEvent(QEvent* event)
- {
- // qDebug() << "UiManager::event " << event->type();
- // 如果是控件同步消息
- if (event->type() == CONTROL_EVENT_TYPEID)
- {
- qDebug() << "UiManager::event - CONTROL_EVENT";
- SyncControlEvent* pValueEvent = dynamic_cast<SyncControlEvent*> (event);
- // 取出参数执行同步
- syncFromDll(
- pValueEvent->m_pSrcControl,
- pValueEvent->m_SyncMsg
- );
- }
- // 如果是数值同步消息
- else if (event->type() == VALUE_EVENT_TYPEID)
- {
- vDebug() << "VALUE_EVENT";
- SyncValueEvent* pValueEvent = dynamic_cast<SyncValueEvent* > (event);
- // 取出参数
- QList<VARIABLE*> syncValues = pValueEvent->getSyncValues();
- for (VARIABLE* syncVal : syncValues)
- {
- this->syncFromValue(syncVal);
- }
- }
- // 如果是硬件数值同步消息
- else if (event->type() == HDVALUE_EVENT_TYPEID)
- {
- qDebug() << "UiManager::event - HDVALUE_EVENT";
- SyncHdValueEvent* pValueEvent = dynamic_cast<SyncHdValueEvent*> (event);
-
- // 如果当前组不是硬件组态,不继续执行后续代码 ,为了解决硬件工具放在 POU 中时候,主动触发了ui刷新
- if (pValueEvent != nullptr && pValueEvent->groupName() != CATEGORY_TOOL_HARDWARE)
- {
- return;
- }
- // 首先需要去Pou中取出此接口
- POU* pHdPou = g_pPouManager->getHdwPou();
- _INTERFACE* pInf = nullptr;
- // 第一种方式
- if (!pValueEvent->m_strInstanceName.isEmpty() && !pValueEvent->m_strInfName.isEmpty())
- {
- QString infFullName = pValueEvent->m_strInstanceName + "." + pValueEvent->m_strInfName;
- pInf = pHdPou->GetInterface(infFullName);
- }
- // 第二种方式
- else if(pValueEvent->m_pSrcValue!=nullptr)
- {
- pInf = pHdPou->GetInterfaceByValuePtr(pValueEvent->m_pSrcValue);
- }
- else
- {
- qDebug() << "[Error] UiManager::customEvent - but SyncHdValueEvent invalid.";
- }
- // 如果接口成功取到,则执行同步
- if (pInf != nullptr)
- {
- // 执行同步
- this->syncFromValue(pInf);
- }
- else
- {
- qWarning() << "UiManager::customEvent - Can't find update interface, may be not a hardward interface.";
- }
- }
- //return QObject::event(event);
- }
- /// <summary>
- /// 绑定dll中的控件与UI控件
- /// </summary>
- /// <param name="pDllControl"></param>
- /// <param name="pUiControl"></param>
- void UiManager::bindDllControlWithUiControl(QWidget* pDllControl, VControlObject* pUiControl)
- {
- m_DllToUiControls.insert(pDllControl, pUiControl->m_pWidget);
- m_UiToDllControls.insert(pUiControl->m_pWidget, pDllControl);
- // 刚绑定时,从dll向UI做一次同步
- this->syncFromDllFirst(pDllControl, pUiControl);
- }
- /// <summary>
- /// 2022-9-13,解除dll中的控件与UI控件的绑定
- /// </summary>
- /// <param name="pDllControl"></param>
- /// <param name="pUiControl"></param>
- void UiManager::unbindDllControlWithUiControl(QWidget* pDllControl, VControlObject* pUiControl)
- {
- m_DllToUiControls.remove(pDllControl);
- m_UiToDllControls.remove(pUiControl->m_pWidget);
- }
- /// <summary>
- /// 绑定UI控件与Runtime控件用于同步
- /// </summary>
- /// <param name="pUiControl"></param>
- /// <param name="pRtControl"></param>
- void UiManager::bindUiControlWithRtControl(QWidget* pUiControl, QWidget* pRtControl)
- {
- m_UiToRtControls.insert(pUiControl, pRtControl);
- m_RtToUiControls.insert(pRtControl, pUiControl);
- }
- /// <summary>
- /// 2022-9-13,解除UI控件与Runtime控件的绑定
- /// </summary>
- /// <param name="pUiControl"></param>
- /// <param name="pRtControl"></param>
- void UiManager::unbindUiControlWithRtControl(QWidget* pUiControl, QWidget* pRtControl)
- {
- m_UiToRtControls.remove(pUiControl);
- m_RtToUiControls.remove(pRtControl);
- }
- /// <summary>
- /// 绑定数值变量与UI控件用于同步
- /// </summary>
- /// <param name="pValue"></param>
- /// <param name="pUiControl"></param>
- void UiManager::bindValueWithUiControl(VARIABLE* val, QWidget* pUiControl)
- {
- //LINK_VALUE lv;
- //lv.direction = dir;
- //lv.value = value;
- m_ValueToUiControls.insertMulti(val, pUiControl);
- m_UiControlToValues.insert(pUiControl, val);
- //// For Debug
- //foreach(const auto & key, m_ValueToUiControls.keys())
- //{
- // qDebug() << "bindValueWithUiControl: " << key->strFullName << " - " << m_ValueToUiControls.value(key);
- //}
- // 刚绑定时,从Value向UI做一次同步
- this->syncFromValue(val);
- }
- /// <summary>
- /// 2022-9-13,解绑数值变量与UI控件(在控件变更了主索引绑定变量时,需要做解绑动作)
- /// </summary>
- /// <param name="val"></param>
- /// <param name="pUiControl"></param>
- void UiManager::unbindValueWithUiControl(VARIABLE* val, QWidget* pUiControl)
- {
- m_UiControlToValues.remove(pUiControl);
- QMutableHashIterator<VARIABLE*, QWidget*> iter(m_ValueToUiControls);
- while (iter.hasNext())
- {
- if (iter.next().key() == val)
- {
- QList<QWidget*> pWidgets = m_ValueToUiControls.values(iter.key());
- for (QWidget* pWidget : pWidgets)
- {
- if (pWidget == pUiControl)
- {
- m_ValueToUiControls.remove(iter.key(), pUiControl);
- return;
- }
- }
- }
- }
- }
- /// <summary>
- /// 绑定DataLink中的信息用于后续同步(仅 UI->Dll)
- /// </summary>
- bool UiManager::bindDataLinkSyncInformation(const DataLink& dataLink, VControlObject* pVControl)
- {
- // 2022-2-9 ,解决绑定Button相关数据连接中出现的bug(也不清楚是怎么出现的,先暂时这么处理一下看起来好像没问题)
- if (pVControl->m_Type == VALUE_TYPE::Control_Button)
- {
- pVControl->m_Property.m_DataLink = dataLink;
- return true;
- }
- POU* pPou = nullptr;
- TOOL* pTool = nullptr;
- _INTERFACE* pInf = nullptr;
- // 解析出Tool信息
- this->parsePouToolFromDataLink(dataLink, pPou, pTool, pInf);
- if (pInf == nullptr)
- {
- vDebug() << "[Error] Abandon bind Datalink, Reason: Parse datalink failed.";
- return false;
- }
- // 如果是控件类型
- if (pInf->Type == INF_TYPE::INF_TYPE_CONTROL)
- {
- return this->bindSyncByControl(pTool, pInf, pVControl);
- }
- // 数值类型或者普通数据类型的接口类型
- else if (pInf->Type == INF_TYPE::INF_TYPE_VALUE
- || pInf->Type == INF_TYPE::INF_TYPE_STANDARD
- )
- {
- // 如果是普通类型控件
- if (!pVControl->isComplexControl())
- {
- this->bindSyncByValue(pTool, pInf, pVControl);
- }
- // 2021-12-7 复杂类型控件
- else
- {
- // 2022-2-10 绑定dll中的信息用于后续同步(复杂类型,暂未绑定,仅引用计数 + 1)
- // 由于不需要执行三方同步,此处不需要绑定
- this->bindSyncByComplex(pTool, pInf);
- }
- }
- // 未处理类型
- else
- {
- qWarning() << "DialogDataLink::bindSyncFromDll - Unsupported interface category : " << (int)pInf->Type;
- }
- return true;
- }
- /// <summary>
- /// 2022-9-13,解绑DataLine信息(在控件变更了主索引绑定变量时,需要做解绑动作)
- /// </summary>
- /// <param name="val"></param>
- /// <param name="pUiControl"></param>
- bool UiManager::unbindDataLinkSyncInformation(const DataLink& dataLink, VControlObject* pVControl)
- {
- if (pVControl->m_Type == VALUE_TYPE::Control_Button)
- {
- pVControl->m_Property.m_DataLink = dataLink;
- return true;
- }
- POU* pPou = nullptr;
- TOOL* pTool = nullptr;
- _INTERFACE* pDllInf = nullptr;
- // 解析出Tool信息
- this->parsePouToolFromDataLink(dataLink, pPou, pTool, pDllInf);
- if (pDllInf == nullptr)
- {
- return false;
- }
- // 如果是控件类型
- if (pDllInf->Type == INF_TYPE::INF_TYPE_CONTROL)
- {
- return this->unbindSyncByControl(pTool, pDllInf, pVControl);
- }
- // 数值类型或者普通数据类型的接口类型
- else if (pDllInf->Type == INF_TYPE::INF_TYPE_VALUE
- || pDllInf->Type == INF_TYPE::INF_TYPE_STANDARD
- )
- {
- // 如果是普通类型控件
- if (!pVControl->isComplexControl())
- {
- this->unbindSyncByValue(pTool, pDllInf, pVControl);
- }
- // 2021-12-7 复杂类型控件
- else
- {
- this->unbindSyncByComplex(pTool, pDllInf);
- }
- }
- // 未处理类型
- else
- {
- vDebug() << "Unsupported interface category : " << (int)pDllInf->Type;
- }
- return true;
- }
- /// <summary>
- /// 绑定dll中的信息用于后续同步(控件类型)
- /// </summary>
- /// <returns></returns>
- bool UiManager::bindSyncByControl(TOOL* pTool, _INTERFACE* pDllInf, VControlObject* pVControl)
- {
- // 因为dll->UI只能是一对一,所以此处需要判断一下引用计数
- if (pDllInf->nRefCount > 0)
- {
- vDebug() << ("Error: Interface[" + pDllInf->strNameWithType + "] ref count >0 : " + pDllInf->nRefCount + ".");
- return false;
- }
- // 引用计数 + 1
- pDllInf->nRefCount++;
- // TODO:回调函数方式已经废弃(只是暂时留着备用)
- // dll->UI同步:给dll设置UI同步回调函数
- CONTROL_CALLBACK callBack = std::bind(
- &UiManager::syncFromDll,
- g_pUiManager,
- std::placeholders::_1,
- std::placeholders::_2
- );
- pTool->pDllPtr->setControlCallback(callBack);
- // 设置同步event的target(这个为目前统一使用的方式,回调函数的方式已经废弃了)
- pTool->pDllPtr->setSyncTarget(g_pUiManager);
- // UI->Dll同步:信息存储至UiManager统一处理
- QWidget* pDllControl = (QWidget*)pTool->pDllPtr->Interface(pDllInf->nIndex).value.Ptr;
- g_pUiManager->bindDllControlWithUiControl(pDllControl, pVControl);
- return true;
- }
- /// <summary>
- /// 2022-9-13 解绑dll中的控件信息
- /// </summary>
- /// <param name="pTool"></param>
- /// <param name="pDllInf"></param>
- /// <param name="pVControl"></param>
- /// <returns></returns>
- bool UiManager::unbindSyncByControl(TOOL* pTool, _INTERFACE* pDllInf, VControlObject* pVControl)
- {
- if (pDllInf->nRefCount > 0)
- {
- vDebug() << ("Error: Interface[" + pDllInf->strNameWithType + "] ref count >0 : " + pDllInf->nRefCount + ".");
- return false;
- }
- // 引用计数 - 1
- pDllInf->nRefCount--;
- // 解绑UI->Dll的同步信息
- QWidget* pDllControl = (QWidget*)pTool->pDllPtr->Interface(pDllInf->nIndex).value.Ptr;
- g_pUiManager->unbindDllControlWithUiControl(pDllControl, pVControl);
- return true;
- }
- /// <summary>
- /// 绑定接口信息用于后续同步(有可能是Dll接口,有可能是数值类型,数值可对应多个UI控件)
- /// </summary>
- /// <param name="pTool"></param>
- /// <param name="pDllInf"></param>
- /// <returns></returns>
- bool UiManager::bindSyncByValue(TOOL* pTool, _INTERFACE* pInf, VControlObject* pVControl)
- {
- // 输出接口 - Text控件,一对多,输入接口 - Text控件,一对一
- // 如果是输入接口,要检查引用计数
- if (pInf->Direction == INF_DIRECTION::INF_DIR_IN
- && pInf->nRefCount > 0)
- {
- vDebug() << ("Error: Interface[" + pInf->strNameWithType + "] ref count >0 : " + pInf->nRefCount + ".");
- return false;
- }
- // 引用计数 + 1
- pInf->nRefCount++;
- // Value->UI同步,给Tool设置同步事件接收对象
- // 如果是Tool中的Value接口,需要绑定变更的回调函数(针对dll中的变量接口)
- if (pTool->pDllPtr != nullptr)
- {
- pTool->pDllPtr->setSyncTarget(g_pUiManager);
- // UI->Value单向同步:信息存储至UiManager统一处理
- // VALUE& value = pTool->pDllPtr->Interface(pDllInf->nIndex).value;
- // g_pUiManager->bindValueWithUiControl(pDllInf, m_pControlObject->m_pWidget);
- }
- else
- {
- // TODO: 此处缺一个设置event target的位置(针对局部变量和全局变量)
- // VALUE& value = pDllInf->value;
- }
- pInf->bDataLink = true;
- g_pUiManager->bindValueWithUiControl(pInf, pVControl->m_pWidget);
- vDebug() << "Bind [" << pInf->strFullName << "] " << " - " << pVControl->m_pWidget;
- return true;
- }
- /// <summary>
- /// 解绑数值接口的绑定信息(有可能是Dll接口,有可能是数值类型,数值可对应多个UI控件)
- /// </summary>
- /// <param name="pTool"></param>
- /// <param name="pDllInf"></param>
- /// <param name="pVControl"></param>
- /// <returns></returns>
- bool UiManager::unbindSyncByValue(TOOL* pTool, _INTERFACE* pInf, VControlObject* pVControl)
- {
- Q_UNUSED(pTool);
- // 引用计数 - 1
- pInf->nRefCount--;
- pInf->bDataLink = false;
- g_pUiManager->unbindValueWithUiControl(pInf, pVControl->m_pWidget);
- vDebug() << "Unbind [" << pInf->strFullName << "] - " << pVControl->m_pWidget;
- return true;
- }
- /// <summary>
- /// 绑定dll中的信息用于后续同步(复杂类型,暂未绑定,仅引用计数 + 1)
- /// </summary>
- /// <param name="pTool"></param>
- /// <param name="pDllInf"></param>
- /// <returns></returns>
- bool UiManager::bindSyncByComplex(TOOL* pTool, _INTERFACE* pDllInf)
- {
- Q_UNUSED(pTool);
- if (pDllInf->Direction == INF_DIRECTION::INF_DIR_IN
- && pDllInf->nRefCount > 0)
- {
- vDebug() << ("Error: Interface[" + pDllInf->strNameWithType + "] ref count >0 : " + pDllInf->nRefCount + ".");
- return false;
- }
- // 引用计数 + 1
- pDllInf->nRefCount++;
- return true;
- }
- /// <summary>
- /// 2022-9-13 解绑dll中的信息(复杂类型,暂未绑定,仅引用计数 - 1)
- /// </summary>
- /// <param name="pTool"></param>
- /// <param name="pDllInf"></param>
- /// <returns></returns>
- bool UiManager::unbindSyncByComplex(TOOL* pTool, _INTERFACE* pDllInf)
- {
- Q_UNUSED(pTool);
- if (pDllInf->Direction == INF_DIRECTION::INF_DIR_IN
- && pDllInf->nRefCount > 0)
- {
- vDebug() << ("Error: Interface[" + pDllInf->strNameWithType + "] ref count >0 : " + pDllInf->nRefCount + ".");
- return false;
- }
- // 引用计数 - 1
- pDllInf->nRefCount--;
- return true;
- }
- /// <summary>
- /// 还原反序列化中控件的DataLink关系(简单控件)
- /// </summary>
- /// <param name="prop"></param>
- /// <returns></returns>
- bool UiManager::restoreDataLinks(const CONTROL_PROPERTY& prop, VControlObject* pUiControl)
- {
- if (prop.m_DataLink.isValid())
- {
- this->bindDataLinkSyncInformation(prop.m_DataLink, pUiControl);
- }
- return true;
- }
- /// <summary>
- /// 还原反序列化中控件的DataLink关系(复杂控件)
- /// </summary>
- /// <param name="propex"></param>
- /// <returns></returns>
- bool UiManager::restoreDataLinks(const CONTROL_PROPERTY_EX& propex, VControlObject* pUiControl)
- {
- bool bRet = true;
- // 索引链接
- this->bindDataLinkSyncInformation(propex.m_refreshLink, pUiControl);
- // 主数据链接
- for (const DataLink& dataLink : propex.m_mainDataLinks)
- {
- if (dataLink.isValid())
- {
- this->bindDataLinkSyncInformation(dataLink, pUiControl);
- }
- else
- {
- qDebug() << ("[Error] UiManager::restoreDataLinks - Property datalink[" + dataLink.title + "] invalid.");
- bRet = false;
- }
- }
- // 子分组中的数据链接
- for (int i = 0; i < propex.m_groups.size(); i++)
- {
- const PROPERTY_EX_SUBGROUPS& subGroups = propex.m_groups[i].subGroups;
- for (int j = 0; j < subGroups.size(); j++)
- {
- for (const DataLink& dataLink : subGroups[j].dataLinks)
- {
- if (dataLink.isValid())
- {
- this->bindDataLinkSyncInformation(dataLink, pUiControl);
- }
- else
- {
- qDebug() << ("[Error] UiManager::restoreDataLinks - Property datalink[" + dataLink.title + "] invalid.");
- bRet = false;
- }
- }
- }
- }
- return bRet;
- }
- /// <summary>
- /// 用于Dll中的控件事件触发回调(dll中的控件向UI界面同步)
- /// </summary>
- /// <param name=""></param>
- /// <param name="msg"></param>
- void UiManager::syncFromDll(QWidget* pDllControl, UI_SYNC_MSG msg)
- {
- if (pDllControl == nullptr)
- {
- qWarning() << "[Error] UiManager::syncFromDll - pDllControl is nullptr.";
- return;
- }
- // 根据不同的消息执行不同的处理
- if (m_DllToUiControls.contains(pDllControl))
- {
- QWidget* pUiWidget = m_DllToUiControls.value(pDllControl);
- bool bRet = false;
- switch (msg)
- {
- case UI_SYNC_MSG::EDIT_TEXT_CHANGED:
- {
- QLineEdit* pDllEdit = qobject_cast<QLineEdit*>(pDllControl);
- VLineEdit* pVEdit = qobject_cast<VLineEdit*>(pUiWidget);
- bRet = syncEditControl(pDllEdit, pVEdit, SYNC_MODE::FROM_DLL);
- if (bRet)
- {
- qDebug() << "UiManager::syncFromDll - VLineEdit:" << pVEdit->text();
- }
- }
- break;
- case UI_SYNC_MSG::COMBO_SEL_CHANGED:
- {
- QComboBox* pDllCombo = qobject_cast<QComboBox*>(pDllControl);
- VComboBox* pVCombo = qobject_cast<VComboBox*>(pUiWidget);
- bRet = syncComboControl(pDllCombo, pVCombo, SYNC_MODE::FROM_DLL);
- if (bRet)
- {
- qDebug() << "UiManager::syncFromDll - VComboBox:" << pVCombo->currentIndex();
- }
- }
- break;
- case UI_SYNC_MSG::LIST_SEL_CHANGED:
- {
- QListWidget* pDllList = qobject_cast<QListWidget*>(pDllControl);
- VListBox* pVList = qobject_cast<VListBox*>(pUiWidget);
- bRet = syncListControl(pDllList, pVList, SYNC_MODE::FROM_DLL);
- qDebug() << "UiManager::syncFromDll - VListBox:" << pDllList->currentRow();
- }
- break;
- case UI_SYNC_MSG::RADIOBOX_CHANGED:
- {
- QRadioButton* pDllRadio= qobject_cast<QRadioButton*>(pDllControl);
- VRadioBox* pVRadio = qobject_cast<VRadioBox*>(pUiWidget);
- //// 首次同步需要把名字也复制一下
- //if (bFirst)
- //{
- // pVRadio->setText(pDllRadio->text());
- //}
- pVRadio->setChecked(pDllRadio->isChecked());
- qDebug() << "UiManager::syncFromDll - VRadioBox:" << pDllRadio->isChecked();
- }
- break;
- case UI_SYNC_MSG::CHECKBOX_CHANGED:
- {
- QCheckBox* pDllCheck = qobject_cast<QCheckBox*>(pDllControl);
- VCheckBox* pVCheck = qobject_cast<VCheckBox*>(pUiWidget);
- //// 首次同步需要把名字也复制一下
- //if (bFirst)
- //{
- // pVCheck->setText(pDllCheck->text());
- //}
- pVCheck->setCheckState(pDllCheck->checkState());
- qDebug() << "UiManager::syncFromDll - VCheckBox:" << pDllCheck->checkState();
- }
- break;
- default:
- {
- qWarning() << "UiManager::syncFromDll - Unknown sync message : " << (int)msg;
- }
- break;
- }
- // 同步完后,通知UI界面刷新此控件(同时更新属性表)
- if (m_AllControls.contains(pUiWidget))
- {
- m_AllControls.value(pUiWidget)->uiScene()->refreshControl(pUiWidget);
- }
- }
- }
- /// <summary>
- /// 用于UI界面向Dll中的控件同步
- /// </summary>
- /// <param name="pWidget"></param>
- /// <param name="msg"></param>
- void UiManager::syncToDll(QWidget* pUiControl, UI_SYNC_MSG msg)
- {
- // 根据不同的消息执行不同的处理
- if (m_UiToDllControls.contains(pUiControl))
- {
- bool bRet = false;
- switch (msg)
- {
- case UI_SYNC_MSG::EDIT_TEXT_CHANGED:
- {
- VLineEdit* pVEdit = qobject_cast<VLineEdit*>(pUiControl);
- QLineEdit* pDllEdit = qobject_cast<QLineEdit*>(m_UiToDllControls.value(pUiControl));
- // pDllEdit->setText(pVEdit->text());
- bRet = syncEditControl(pVEdit, pDllEdit, SYNC_MODE::TO_DLL);
- if (bRet)
- {
- qDebug() << "UiManager::syncToDll - QLineEdit:" << pVEdit->text();
- }
- }
- break;
- case UI_SYNC_MSG::COMBO_SEL_CHANGED:
- {
- VComboBox* pUi = qobject_cast<VComboBox*>(pUiControl);
- QComboBox* pDll = qobject_cast<QComboBox*>(m_UiToDllControls.value(pUiControl));
- bRet = syncComboControl(pUi, pDll, SYNC_MODE::TO_DLL);
- if (bRet)
- {
- qDebug() << "UiManager::syncToDll - QComboBox:" << pDll->currentIndex();
- }
- }
- break;
- case UI_SYNC_MSG::LIST_SEL_CHANGED:
- {
- VListBox* pUi = qobject_cast<VListBox*>(pUiControl);
- QListWidget* pDll = qobject_cast<QListWidget*>(m_UiToDllControls.value(pUiControl));
- bRet = syncListControl(pUi, pDll, SYNC_MODE::TO_DLL);
- if (bRet)
- {
- qDebug() << "UiManager::syncToDll - QListWidget:" << pDll->currentRow();
- }
- }
- break;
- case UI_SYNC_MSG::RADIOBOX_CHANGED:
- break;
- case UI_SYNC_MSG::CHECKBOX_CHANGED:
- break;
- default:
- {
- qWarning() << "UiManager::syncToDll - Unknown sync message : " << (int)msg;
- }
- break;
- }
- }
- else
- {
- //qWarning() << "UiManager::syncToDll - Error : Can't find this control - " << pControl->getText();
- }
- }
- /// <summary>
- /// 刚绑定时,从dll向UI做一次同步
- /// </summary>
- /// <param name="pDllControl"></param>
- void UiManager::syncFromDllFirst(QWidget* pDllControl, VControlObject* pUiControl)
- {
- // 根据同步控件的类型执行不同的同步动作
- VALUE_TYPE controlType = pUiControl->m_Type;
- // 逐步增加如下需要同步的类型
- switch (controlType)
- {
- case VALUE_TYPE::Control_LineEdit:
- {
- QLineEdit* pDllEdit = qobject_cast<QLineEdit*>(pDllControl);
- VLineEdit* pVEdit = qobject_cast<VLineEdit*>(pUiControl->m_pWidget);
- bool bRet = syncEditControl(pDllEdit, pVEdit, SYNC_MODE::FROM_DLL, true);
- if (bRet)
- {
- qDebug() << "UiManager::syncFromDllFirst - VLineEdit:" << pVEdit->text();
- }
- }
- //this->syncFromDll(pDllControl, UI_SYNC_MSG::EDIT_TEXT_CHANGED, true);
- break;
- case VALUE_TYPE::Control_ComboBox:
- {
- QComboBox* pDll= qobject_cast<QComboBox*>(pDllControl);
- VComboBox* pUi = qobject_cast<VComboBox*>(pUiControl->m_pWidget);
- bool bRet = syncComboControl(pDll, pUi, SYNC_MODE::FROM_DLL, true);
- if (bRet)
- {
- qDebug() << "UiManager::syncFromDllFirst - VComboBox:" << pUi->currentIndex();
- }
- }
- //this->syncFromDll(pDllControl, UI_SYNC_MSG::COMBO_SEL_CHANGED, true);
- break;
- case VALUE_TYPE::Control_RadioBox:
- {
- }
- //this->syncFromDll(pDllControl, UI_SYNC_MSG::RADIOBOX_CHANGED, true);
- break;
- case VALUE_TYPE::Control_CheckBox:
- {
- }
- //this->syncFromDll(pDllControl, UI_SYNC_MSG::CHECKBOX_CHANGED, true);
- break;
- case VALUE_TYPE::Control_Listbox:
- {
- QListWidget* pDll = qobject_cast<QListWidget*>(pDllControl);
- VListBox* pUi = qobject_cast<VListBox*>(pUiControl->m_pWidget);
- bool bRet = syncListControl(pDll, pUi, SYNC_MODE::FROM_DLL, true);
- if (bRet)
- {
- qDebug() << "UiManager::syncFromDllFirst - VListBox:" << pUi->currentIndex();
- }
- }
- //this->syncFromDll(pDllControl, UI_SYNC_MSG::LIST_SEL_CHANGED, true);
- break;
- default:
- break;
- }
- }
- /// <summary>
- /// 用于Runtime界面向UI中的控件同步
- /// </summary>
- /// <param name="pControl"></param>
- /// <param name="msg"></param>
- void UiManager::syncFromRuntime(QWidget* pRtControl, UI_SYNC_MSG msg)
- {
- if (m_RtToUiControls.contains(pRtControl))
- {
- QWidget* pUiWidget = m_RtToUiControls.value(pRtControl);
- bool bRet = false;
- switch (msg)
- {
- case UI_SYNC_MSG::EDIT_TEXT_CHANGED:
- {
- QLineEdit* pRtEdit = qobject_cast<QLineEdit*>(pRtControl);
- QLineEdit* pUiEdit = qobject_cast<QLineEdit*>(pUiWidget);
- bRet = syncEditControl(pRtEdit, pUiEdit, SYNC_MODE::FROM_RUNTIME, false);
- if (bRet)
- {
- qDebug() << "UiManager::syncFromRuntime - QLineEdit:" << pRtEdit->text();
- }
- }
- break;
- case UI_SYNC_MSG::COMBO_SEL_CHANGED:
- {
- QComboBox* pRt= qobject_cast<QComboBox*>(pRtControl);
- QComboBox* pUi = qobject_cast<QComboBox*>(pUiWidget);
- bRet = syncComboControl(pRt, pUi, SYNC_MODE::FROM_RUNTIME, false);
- if (bRet)
- {
- qDebug() << "UiManager::syncFromRuntime - QComboBox:" << pUi->currentIndex();
- }
- }
- break;
- case UI_SYNC_MSG::LIST_SEL_CHANGED:
- {
- QListWidget* pRt = qobject_cast<QListWidget*>(pRtControl);
- QListWidget* pUi = qobject_cast<QListWidget*>(pUiWidget);
- bRet = syncListControl(pRt, pUi, SYNC_MODE::FROM_RUNTIME, false);
- if (bRet)
- {
- qDebug() << "UiManager::syncFromRuntime - QListWidget:" << pUi->currentRow();
- }
- }
- break;
- case UI_SYNC_MSG::RADIOBOX_CHANGED:
- {
- }
- break;
- case UI_SYNC_MSG::CHECKBOX_CHANGED:
- {
- }
- break;
- case UI_SYNC_MSG::VALUE_CHANGED:
- {
- QLineEdit* pRt = qobject_cast<QLineEdit*>(pRtControl);
- QLineEdit* pUi = qobject_cast<QLineEdit*>(pUiWidget);
- bRet = syncValueControl(pRt, pUi, SYNC_MODE::TO_RUNTIME);
- if (bRet)
- {
- qDebug() << "UiManager::syncFromRuntime - ValueControl:" << pUi->text();
- }
- }
- break;
- default:
- {
- qWarning() << "UiManager::syncFromRuntime - Unsupported msg : " << (int)msg;
- }
- break;
- }
- // 同步完后,通知UI界面刷新此控件(同时更新属性表)
- // 2022-9-15,只有数据有变动的时候才需要刷新
- if (bRet && m_AllControls.contains(pUiWidget))
- {
- m_AllControls.value(pUiWidget)->uiScene()->refreshControl(pUiWidget);
- }
- }
- else
- {
- //qWarning() << "UiManager::syncFromRuntime - Error : Can't find this control.";
- }
- }
- /// <summary>
- /// 用于UI向Runtime界面中的控件同步
- /// </summary>
- /// <param name="pControl"></param>
- /// <param name="msg"></param>
- void UiManager::syncToRuntime(QWidget* pUiControl, UI_SYNC_MSG msg, bool bFirst)
- {
- // qDebug() << "UiManager::syncToRuntime - msg:" << (int)msg;
- if (m_UiToRtControls.contains(pUiControl))
- {
- bool bRet = false;
- switch (msg)
- {
- case UI_SYNC_MSG::EDIT_TEXT_CHANGED:
- {
- QLineEdit* pUiEdit = qobject_cast<QLineEdit*>(pUiControl);
- QLineEdit* pRtEdit = qobject_cast<QLineEdit*>(m_UiToRtControls.value(pUiControl));
- bRet = syncEditControl(pUiEdit, pRtEdit, SYNC_MODE::TO_RUNTIME, bFirst);
- if (bRet)
- {
- qDebug() << "UiManager::syncToRuntime - QLineEdit:" << pRtEdit->text();
- }
- }
- break;
- case UI_SYNC_MSG::COMBO_SEL_CHANGED:
- {
- QComboBox* pUiCombo = qobject_cast<QComboBox*>(pUiControl);
- QComboBox* pRtCombo = qobject_cast<QComboBox*>(m_UiToRtControls.value(pUiControl));
- bRet = syncComboControl(pUiCombo, pRtCombo, SYNC_MODE::TO_RUNTIME, bFirst);
- if (bRet)
- {
- qDebug() << "UiManager::syncToRuntime - QComboBox:" << pRtCombo->currentIndex();
- }
- }
- break;
- case UI_SYNC_MSG::LIST_SEL_CHANGED:
- {
- QListWidget* pUi = qobject_cast<QListWidget*>(pUiControl);
- QListWidget* pRt= qobject_cast<QListWidget*>(m_UiToRtControls.value(pUiControl));
- // 首次同步需要同步一下所有的选择项
- if (bFirst)
- {
- for (int i = 0; i < pUi->count(); i++)
- {
- pRt->insertItem(i, pUi->item(i)->text());
- }
- }
- pRt->setCurrentRow(pUi->currentRow());
- qDebug() << "UiManager::syncToRuntime - VListBox:" << pRt->currentRow();
- }
- break;
- case UI_SYNC_MSG::RADIOBOX_CHANGED:
- {
- QRadioButton* pUi = qobject_cast<QRadioButton*>(pUiControl);
- QRadioButton* pRt = qobject_cast<QRadioButton*>(m_UiToRtControls.value(pUiControl));
- //// 首次同步需要把名字也复制一下
- //if (bFirst)
- //{
- // pRt->setText(pUi->text());
- //}
- pRt->setChecked(pUi->isChecked());
- qDebug() << "UiManager::syncToRuntime - VRadioBox:" << pRt->isChecked();
- }
- break;
- case UI_SYNC_MSG::CHECKBOX_CHANGED:
- {
- QCheckBox* pUi = qobject_cast<QCheckBox*>(pUiControl);
- QCheckBox* pRt = qobject_cast<QCheckBox*>(m_UiToRtControls.value(pUiControl));
- //// 首次同步需要把名字也复制一下
- //if (bFirst)
- //{
- // pRt->setText(pUi->text());
- //}
- pRt->setCheckState(pUi->checkState());
- qDebug() << "UiManager::syncToRuntime - VCheckBox:" << pRt->checkState();
- }
- break;
- case UI_SYNC_MSG::VALUE_CHANGED:
- {
- QLineEdit* pUi = qobject_cast<QLineEdit*>(pUiControl);
- QLineEdit* pRt = qobject_cast<QLineEdit*>(m_UiToRtControls.value(pUiControl));
- bRet = syncValueControl(pUi, pRt, SYNC_MODE::TO_RUNTIME);
- if (bRet)
- {
- qDebug() << "UiManager::syncToRuntime - ValueControl:" << pRt->text();
- }
- }
- break;
- default:
- break;
- }
- }
- else
- {
- //qWarning() << "UiManager::syncToRuntime - Error : Can't find this control.";
- }
- }
- /// <summary>
- /// 用于数值变更触发回调(只有ValueControl可以关联数值)
- /// </summary>
- /// <param name="pInf"></param>
- void UiManager::syncFromValue(VARIABLE* pInf)
- {
- // 根据不同的数值类型进行不同的转换
- if (m_ValueToUiControls.contains(pInf))
- {
- QList<QWidget*> pWidgets = m_ValueToUiControls.values(pInf);
- // 遍历所有与此Value关联的控件,逐一更新
- for (QWidget* pUiWidget : pWidgets)
- {
- QString strValueText = pInf->getValueString();
- vDebug() << pUiWidget << " - syncFromValue [" << pInf->strName << "](" << strValueText << ").";
- // 同步到ValueControl
- this->syncValueControl(pUiWidget, strValueText, pInf->value.type, SYNC_MODE::FROM_VALUE);
- // 同步完后,通知UI界面刷新此控件(同时更新属性表)
- // 2022-5-22 此处在多个属性表同时高速刷新时,会造成控件多个属性表之间切换,所以临时屏蔽。
- //if (m_AllControls.contains(pUiWidget))
- //{
- // m_AllControls.value(pUiWidget)->refreshControl(pUiWidget);
- //}
- }
- }
- else
- {
- //qWarning() << "UiManager::syncFromValue - Error : Can't find this value.";
- }
- }
- /// <summary>
- /// 用于UI变更向数值单向同步
- /// </summary>
- /// <param name="pControl"></param>
- void UiManager::syncToValue(QWidget* pUiControl)
- {
- if (m_UiControlToValues.contains(pUiControl))
- {
- VARIABLE* linkValue = m_UiControlToValues.value(pUiControl);
- QString className = pUiControl->metaObject()->className();
- QString strValue;
- if (className == CLASS_NAME_VALUECONTROL)
- {
- ValueControl* pVControl = qobject_cast<ValueControl*>(pUiControl);
- if (pVControl != nullptr)
- {
- strValue = pVControl->text();
- }
- }
- else if (className == CLASS_NAME_RESULT)
- {
- VResult* pVControl = qobject_cast<VResult*>(pUiControl);
- if (pVControl != nullptr)
- {
- strValue = pVControl->text();
- }
- }
-
- // 2021-11-2增加,如果对应的接口是一个输出接口,那么忽略这个赋值(输出接口不允许从UI输入)
- if (linkValue->Direction == INF_DIRECTION::INF_DIR_OUT)
- {
- qDebug() << "UiManager::syncToValue - ignore: this LinkValue is INF_DIR_OUT.";
- return;
- }
- // 直接将字符串拷贝过去
- //QString strValue = pVControl->toPlainText();
- //linkValue->setValue(strValue);
- // 根据不同的数值类型进行不同的转换
- bool bRet = false;
- switch (linkValue->value.type)
- {
- case VALUE_TYPE::Type_Int:
- {
- // 获取控件数值
- int controlValue = strValue.toInt();
- bRet = linkValue->setValue<int>(controlValue);
- if (bRet)
- {
- // vDebug() << "Tpye:" << Utility::getTypeString(VALUE_TYPE::Type_Int) << " value:" << controlValue;
- vDebug() << pUiControl << " - syncToValue [" << linkValue->strName << "](" << controlValue << ").";
- }
- }
- break;
- default:
- {
- vDebug() << "[Error] : " << (int)linkValue->value.type;
- }
- break;
- }
- }
- else
- {
- //qWarning() << "UiManager::syncToValue - Error : Can't find this control.";
- }
- }
- /// <summary>
- /// 同步Edit控件
- /// </summary>
- /// <param name="pSrc"></param>
- /// <param name="pDst"></param>
- /// <param name="mode"></param>
- /// <param name="bFirst"></param>
- /// <returns></returns>
- bool UiManager::syncEditControl(QLineEdit* pSrc, QLineEdit* pDst, SYNC_MODE mode, bool bFirst)
- {
- Q_UNUSED(bFirst);
- QString strSrc = pSrc->text();
- QString strDst = pDst->text();
- if (strDst != strSrc)
- {
- if (mode == SYNC_MODE::TO_DLL)
- {
- pDst->setText(strSrc);
- }
- else
- {
- VLineEdit* vEdit = qobject_cast<VLineEdit*>(pDst);
- vEdit->setText(strSrc);
- }
- return true;
- }
- return false;
- }
- /// <summary>
- /// 同步Combo控件
- /// </summary>
- /// <param name="pSrc"></param>
- /// <param name="pDst"></param>
- /// <param name="mode"></param>
- /// <param name="bFirst"></param>
- /// <returns></returns>
- bool UiManager::syncComboControl(QComboBox* pSrc, QComboBox* pDst, SYNC_MODE mode, bool bFirst /*= false*/)
- {
- Q_UNUSED(mode);
- // 首次同步需要同步Combo列表项
- if (bFirst)
- {
- for (int i = 0; i < pSrc->count(); i++)
- {
- pDst->insertItem(i, pSrc->itemText(i));
- }
- }
- int nIndexSrc = pSrc->currentIndex();
- int nIndexDst = pDst->currentIndex();
- if (nIndexDst != nIndexSrc)
- {
- pDst->setCurrentIndex(pSrc->currentIndex());
- return true;
- }
- return false;
- }
- /// <summary>
- /// 同步list控件
- /// </summary>
- /// <param name="pSrc"></param>
- /// <param name="pDst"></param>
- /// <param name="mode"></param>
- /// <param name="bFirst"></param>
- /// <returns></returns>
- bool UiManager::syncListControl(QListWidget* pSrc, QListWidget* pDst, SYNC_MODE mode, bool bFirst /*= false*/)
- {
- Q_UNUSED(mode);
- // 首次同步需要同步List列表项
- if (bFirst)
- {
- for (int i = 0; i < pSrc->count(); i++)
- {
- pDst->insertItem(i, pSrc->item(i)->text());
- }
- }
- int nIndexSrc = pSrc->currentRow();
- int nIndexDst = pDst->currentRow();
- if (nIndexDst != nIndexSrc)
- {
- pDst->setCurrentRow(nIndexSrc);
- return true;
- }
- return false;
- }
- /// <summary>
- /// 同步Value Control(数值)
- /// </summary>
- /// <param name="pValueControl"></param>
- /// <param name="strNewValue"></param>
- /// <param name="type"></param>
- /// <returns></returns>
- bool UiManager::syncValueControl(QWidget* pValueControl, QString strNewValue, VALUE_TYPE type, SYNC_MODE mode)
- {
- Q_UNUSED(mode);
- Q_UNUSED(type);
- if (pValueControl == nullptr)
- {
- return false;
- }
- QString className = pValueControl->metaObject()->className();
- if (className == CLASS_NAME_VALUECONTROL)
- {
- ValueControl* pControl = qobject_cast<ValueControl*>(pValueControl);
- if (pValueControl == nullptr || pControl == nullptr)
- {
- return false;
- }
- QString strCurrentText = pControl->text();
- // 如果文字相同则不更新了,防止消息死循环
- if (strCurrentText == strNewValue)
- {
- return false;
- }
- // 设置新值
- pControl->setText(strNewValue);
- }
- else if (className == CLASS_NAME_RESULT)
- {
- VResult* pControl = qobject_cast<VResult*>(pValueControl);
- if (pValueControl == nullptr || pControl == nullptr)
- {
- return false;
- }
- QString strCurrentText = pControl->text();
- QString strCurrentTip = pControl->toolTip();
- // 剔除掉 Tip 文本
- strCurrentText = strCurrentText.replace(QRegExp(strCurrentTip + STRING_INTERVAL), "");
- // 如果文字相同则不更新了,防止消息死循环
- if (strCurrentText == strNewValue)
- {
- return false;
- }
- // 设置新值
- pControl->setText(strCurrentTip + STRING_INTERVAL + strNewValue);
- //pControl->setText(strCurrentTip);
- }
- else
- {
- return false;
- }
-
- //if (mode == SYNC_MODE::FROM_VALUE)
- //{
- // qDebug() << "UiManager::syncFromValue - tpye:" << Utility::getTypeString(type) << " value:"
- // << strNewValue << " toControl:" << pControl->m_pWidget;
- //}
- //else if (mode == SYNC_MODE::TO_VALUE)
- //{
- // qDebug() << "UiManager::syncToValue - tpye:" << Utility::getTypeString(type) << " value:"
- // << strNewValue << " fromControl:" << pControl->m_pWidget;;
- //}
- return true;
- }
- /// <summary>
- /// 同步Value Control(控件)
- /// </summary>
- /// <param name="pSrc"></param>
- /// <param name="pDst"></param>
- /// <param name="mode"></param>
- /// <returns></returns>
- bool UiManager::syncValueControl(QLineEdit* pSrc, QLineEdit* pDst, SYNC_MODE mode)
- {
- Q_UNUSED(mode);
- QString strSrc = pSrc->text();
- QString strDst = pDst->text();
- if (strDst != strSrc)
- {
- if (mode == SYNC_MODE::TO_DLL)
- {
- pDst->setText(strSrc);
- }
- else
- {
- QString className = pDst->metaObject()->className();
- if (className == CLASS_NAME_VALUECONTROL)
- {
- ValueControl* pVControl = qobject_cast<ValueControl*>(pDst);
- if (pVControl != nullptr)
- {
- pVControl->setText(strSrc);
- }
- }
- else if (className == CLASS_NAME_RESULT)
- {
- VResult* pVControl = qobject_cast<VResult*>(pDst);
- if (pVControl != nullptr)
- {
- pVControl->setText(strSrc);
- }
- }
- else
- {
- return false;
- }
- }
- return true;
- }
- return false;
- }
- ///// <summary>
- ///// 删除所有UI
- ///// </summary>
- //void UiManager::deleteAllUIs()
- //{
- //
- //}
- /// <summary>
- /// 重置所有UI页面以及数据结构(用于反序列化使用)
- /// </summary>
- void UiManager::reset()
- {
- m_pMainPage = nullptr;
-
- m_pAllPages.clear();
- m_AllControls.clear();
-
- m_DllToUiControls.clear();
- m_UiToDllControls.clear();
- m_UiToRtControls.clear();
- m_RtToUiControls.clear();
- m_UiControlToValues.clear();
- m_ValueToUiControls.clear();
- }
- /// <summary>
- /// 2022-2-12 根据控件指针查找对应的WindowAppUiView指针
- /// </summary>
- /// <param name="pUiControl"></param>
- /// <returns></returns>
- WindowAppUiView* UiManager::getViewByControl(VControlObject* pUiControl)
- {
- return m_AllControls.value(pUiControl->m_pWidget);
- }
- /// <summary>
- /// 刷新指定控件的选中效果(用于适配geometry的实时更新)
- /// </summary>
- /// <param name="pUiControl"></param>
- void UiManager::refreshControlDrawPoint(VControlObject* pUiControl)
- {
- WindowAppUiView* pUiView = m_AllControls.value(pUiControl->m_pWidget);
- if (pUiView)
- {
- pUiView->uiScene()->refreshDrawPoint(pUiControl->m_pWidget);
- }
- }
- /// <summary>
- /// 2022-2-10 从DataLink中解析Tool信息
- /// </summary>
- /// <param name="dataLink"></param>
- /// <param name="pPou"></param>
- /// <param name="pTool"></param>
- /// <param name="pDllInf"></param>
- void UiManager::parsePouToolFromDataLink(
- const DataLink& dataLink,
- POU*& pPou,
- TOOL*& pTool,
- _INTERFACE*& pDllInf
- )
- {
- pPou = nullptr;
- pTool = nullptr;
- pDllInf = nullptr;
- if (dataLink.value.size() < 3)
- {
- return;
- }
- // 从DataLink信息中解析出接口信息
- QString strCategory = dataLink.value.at(0);
- QString strCmd = dataLink.value.at(1);
- QString strDetail = dataLink.value.at(2);
- // 2021-10-28 解析需要处理局部和全局变量
- bool bBindVariables = false;
- if (strCmd.indexOf(GROUP_GLOBAL_VARIABLE) >= 0
- || strCmd.indexOf(GROUP_LOCAL_VARIABLE) >= 0)
- {
- bBindVariables = true;
- }
- // Tool接口
- if (!bBindVariables)
- {
- // 根据名字找到对应的_INTERFACE*
- pPou = g_pPouManager->getPouByName(strCategory);
- if (pPou != nullptr)
- {
- pTool = pPou->GetToolByName(strCmd);
- if (pTool != nullptr)
- {
- pDllInf = pTool->getInterfaceByName(strDetail);
- }
- else
- {
- qWarning() << "GetToolByName " << strCmd << " Error";
- }
- }
- }
- // 局部和全局变量
- else
- {
- strCategory.remove(VAR_SUFFIX);
- strCmd = strCategory;
- pTool = g_pGvlManager->getGvl(strCmd);
- pDllInf = pTool->getInterfaceByName(strDetail);
- }
- }
- /// <summary>
- /// 2022-2-10 从DataLink中解析Interface信息
- /// </summary>
- /// <param name="dataLink"></param>
- /// <param name="pDllInf"></param>
- void UiManager::parseToolInfFromDataLink( const DataLink& dataLink, _INTERFACE*& pDllInf)
- {
- POU* pPou = nullptr;
- TOOL* pTool = nullptr;
- pDllInf = nullptr;
- this->parsePouToolFromDataLink(dataLink, pPou, pTool, pDllInf);
- }
|