VControlObject.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. #include "VControlObject.h"
  2. #include "VTableControl.h"
  3. #include "VButton.h"
  4. #include "VCheckBox.h"
  5. #include "VComboBox.h"
  6. #include "VLabel.h"
  7. #include "VLineEdit.h"
  8. #include "VListBox.h"
  9. #include "ValueControl.h"
  10. #include "VRadioBox.h"
  11. #include "VGroupBox.h"
  12. #include "VResult.h"
  13. #include "VImageControl.h"
  14. #include "VTableControl.h"
  15. #include "VCustomPlot.h"
  16. #include "VPieChart.h"
  17. #include "UiManager.h"
  18. VControlObject::VControlObject( CONTROL_PROPERTY* pProperty)
  19. : m_pWidget(nullptr)
  20. , m_pProxyWidget(nullptr)
  21. {
  22. //// 设置坐标和位置
  23. //if (size != DEFAULT_CONTROL_SIZE)
  24. //{
  25. // this->m_pWidget->resize(size);
  26. //}
  27. //this->m_pWidget->move(pos);
  28. if (pProperty != nullptr)
  29. {
  30. m_Property = *pProperty;
  31. }
  32. else
  33. {
  34. // 设置默认属性
  35. m_Property.m_strText = ("");
  36. m_Property.m_strTip = ("Tip");
  37. m_Property.m_clrText = QColor(240, 240, 240);
  38. m_Property.m_clrBg = QColor(70, 70, 70);
  39. m_Property.m_Font = QFont("微软雅黑", 9, 1);
  40. m_Property.m_bEnable = true;
  41. m_Property.m_DataLink.title = "链接";
  42. }
  43. }
  44. VControlObject::~VControlObject()
  45. {
  46. }
  47. DataLink VControlObject::getDataLink() const
  48. {
  49. return this->m_Property.m_DataLink;
  50. }
  51. void VControlObject::setDataLink(const DataLink& datalink)
  52. {
  53. if (datalink != this->m_Property.m_DataLink)
  54. {
  55. this->m_Property.m_DataLink = datalink;
  56. }
  57. }
  58. QString VControlObject::getText() const
  59. {
  60. return this->m_Property.m_strText;
  61. }
  62. QString VControlObject::getTip() const
  63. {
  64. return this->m_Property.m_strTip;
  65. }
  66. QColor VControlObject::getTextColor() const
  67. {
  68. return this->m_Property.m_clrText;
  69. }
  70. QColor VControlObject::getBgColor() const
  71. {
  72. return this->m_Property.m_clrBg;
  73. }
  74. QFont VControlObject::getFont() const
  75. {
  76. return this->m_Property.m_Font;
  77. }
  78. bool VControlObject::getEnable() const
  79. {
  80. return this->m_Property.m_bEnable;
  81. }
  82. QString VControlObject::textColorQss(const QColor& color)
  83. {
  84. this->m_Property.m_clrText = color;
  85. QColor bgColor = getBgColor();
  86. QString bgColorText = Utility::qColor2qString(bgColor);
  87. QString foreColorText = Utility::qColor2qString(color);
  88. // 设置前景色和背景色(所有的StyleSheet的内容需要一次性设置,要不其他属性会被默认属性覆盖)
  89. QString qss = QString("color:%1;background-color:%2;").arg(foreColorText).arg(bgColorText);
  90. return qss;
  91. }
  92. QString VControlObject::bgColorQss(const QColor& color)
  93. {
  94. this->m_Property.m_clrBg = color;
  95. // 用qss实现
  96. QColor foreColor = getTextColor();
  97. QString foreColorText = Utility::qColor2qString(foreColor);
  98. QString bgColorText = Utility::qColor2qString(color);
  99. // 设置前景色和背景色
  100. QString qss = QString("color:%1;background-color:%2;").arg(foreColorText).arg(bgColorText);
  101. return qss;
  102. }
  103. ///// <summary>
  104. ///// 获取扩展属性
  105. ///// </summary>
  106. ///// <returns></returns>
  107. //CONTROL_PROPERTY_EX VControlObject::getPropertyEx()
  108. //{
  109. // return m_PropertyEx;
  110. //}
  111. //
  112. //
  113. ///// <summary>
  114. ///// 设置扩展属性
  115. ///// </summary>
  116. ///// <param name="props"></param>
  117. //void VControlObject::setPropertyEx(const CONTROL_PROPERTY_EX& props)
  118. //{
  119. // m_PropertyEx = props;
  120. //}
  121. /// <summary>
  122. /// 获得扩展属性的子分组数量
  123. /// 2022-1-10,由于有多个子分组,所以此处需要传入ID
  124. /// </summary>
  125. /// <returns></returns>
  126. int VControlObject::getExPropertyCountByID(const int groupID)
  127. {
  128. //return m_PropertyEx.m_subGroups[groupID].size();
  129. return m_PropertyEx.m_groups[groupID].subGroups.size();
  130. }
  131. /// <summary>
  132. /// 根据对应的子属性关联Key的名字来获取子分组数量
  133. /// </summary>
  134. /// <param name="strKey"></param>
  135. /// <returns></returns>
  136. int VControlObject::getExPropertyCountByName(const QString& strKey)
  137. {
  138. // 枚举所有分组
  139. for (int i = 0; i < m_PropertyEx.m_groups.size(); i++)
  140. {
  141. if (m_PropertyEx.m_groups[i].strRelationKey == strKey)
  142. {
  143. return m_PropertyEx.m_groups[i].subGroups.size();
  144. }
  145. }
  146. return -1;
  147. }
  148. /// <summary>
  149. /// 根据属性名字获取当前子属性组的ID(注意此处传入的都是英文的属性名字)
  150. /// </summary>
  151. /// <param name="strKey"></param>
  152. /// <returns></returns>
  153. int VControlObject::getGroupIDByName(const QString& strKey)
  154. {
  155. int nIndex = 0;
  156. for (const PROPERTY_EX_GROUP& group : this->m_PropertyEx.m_groups)
  157. {
  158. if (strKey == group.strRelationKey)
  159. {
  160. return nIndex;
  161. }
  162. nIndex++;
  163. }
  164. return -1;
  165. }
  166. /// <summary>
  167. /// 更新扩展属性的数量(虚函数)
  168. /// </summary>
  169. /// <param name="fixCount"></param>
  170. void VControlObject::updateExPropertyCount(const int newCount, const QString& strPropName)
  171. {
  172. Q_UNUSED(newCount);
  173. Q_UNUSED(strPropName)
  174. }
  175. /// <summary>
  176. /// 修改扩展属性(虚函数)
  177. /// </summary>
  178. /// <param name="strValueTile"></param>
  179. /// <param name="newValue"></param>
  180. void VControlObject::changeExProperties(const QString strValueTile, const QVariant& newValue)
  181. {
  182. Q_UNUSED(strValueTile);
  183. Q_UNUSED(newValue);
  184. }
  185. /// <summary>
  186. /// 更新扩展属性字段
  187. /// </summary>
  188. /// <param name="strPropertyName"></param>
  189. /// <param name="datalink"></param>
  190. void VControlObject::updateExPropertyDataLink(const QString& strPropertyName, const QString& strLinkString)
  191. {
  192. DataLink datalink(strLinkString);
  193. this->updateExPropertyDataLink(strPropertyName, datalink);
  194. }
  195. /// <summary>
  196. /// 更新数据链接
  197. /// </summary>
  198. /// <param name="strPropertyName"></param>
  199. /// <param name="datalink"></param>
  200. void VControlObject::updateExPropertyDataLink(const QString& strPropertyName, const DataLink& newDatalink)
  201. {
  202. // 简单控件链接
  203. if (!isComplexControl())
  204. {
  205. if (strPropertyName == m_Property.m_DataLink.title)
  206. {
  207. m_Property.m_DataLink = newDatalink;
  208. }
  209. }
  210. // 复杂控件链接
  211. else
  212. {
  213. // 刷新索引链接
  214. if (strPropertyName == m_PropertyEx.m_refreshLink.title)
  215. {
  216. m_PropertyEx.m_refreshLink = newDatalink;
  217. }
  218. // 主数据链接
  219. for (int i = 0; i < m_PropertyEx.m_mainDataLinks.size(); i++)
  220. {
  221. if (strPropertyName == m_PropertyEx.m_mainDataLinks[i].title)
  222. {
  223. m_PropertyEx.m_mainDataLinks[i] = newDatalink;
  224. }
  225. }
  226. // 继续查找所有分组中
  227. for (int i = 0; i < m_PropertyEx.m_groups.size(); i++)
  228. {
  229. for (int j = 0; j < m_PropertyEx.m_groups.size(); j++)
  230. {
  231. PROPERTY_EX_SUBGROUPS& subGroups = m_PropertyEx.m_groups[j].subGroups;
  232. for (PROPERTY_EX_SUBGROUP& subGroup : subGroups)
  233. {
  234. for (int m = 0; m < subGroup.dataLinks.size(); m++)
  235. {
  236. if (subGroup.dataLinks[m].title == strPropertyName)
  237. {
  238. subGroup.dataLinks[m] = newDatalink;
  239. }
  240. }
  241. }
  242. }
  243. }
  244. }
  245. qDebug() << "VControlObject::updatePropertyExDataLink - update [" << strPropertyName
  246. << "] to [" << newDatalink.toString() << "]";
  247. //// 根据属性名字找到对应的DataLink
  248. //DataLink& dataLink = this->getDataLinkByProperty(strPropertyName);
  249. //// 更新
  250. //dataLink = newDatalink;
  251. //qDebug() << "VControlObject::updatePropertyExDataLink - update [" << strPropertyName
  252. // << "] to [" << newDatalink.toString() << "]";
  253. //// 刷新索引链接
  254. //if (strPropertyName == m_PropertyEx.m_refreshLink.title)
  255. //{
  256. // m_PropertyEx.m_refreshLink = datalink;
  257. // qDebug() << "VControlObject::updatePropertyExDataLink - update [" << strPropertyName
  258. // << "] to [" << datalink.toString() << "]";
  259. // return;
  260. //}
  261. //// 主数据链接
  262. //for (int i = 0; i < m_PropertyEx.m_mainDataLinks.size(); i++)
  263. //{
  264. // if (strPropertyName == m_PropertyEx.m_mainDataLinks[i].title)
  265. // {
  266. // m_PropertyEx.m_mainDataLinks[i] = datalink;
  267. // qDebug() << "VControlObject::updatePropertyExDataLink - update [" << strPropertyName
  268. // << "] to [" << datalink.toString() << "]";
  269. // return;
  270. // }
  271. //}
  272. //// 继续查找所有分组中
  273. //for (int i = 0; i < m_PropertyEx.m_groups.size(); i++)
  274. //{
  275. // for (int j = 0; j < m_PropertyEx.m_groups.size(); j++)
  276. // {
  277. // PROPERTY_EX_SUBGROUPS& subGroups = m_PropertyEx.m_groups[j].subGroups;
  278. // for (PROPERTY_EX_SUBGROUP& subGroup : subGroups)
  279. // {
  280. // for (int m = 0; m < subGroup.dataLinks.size(); m++)
  281. // {
  282. // if (subGroup.dataLinks[m].title == strPropertyName)
  283. // {
  284. // subGroup.dataLinks[m] = datalink;
  285. // qDebug() << "VControlObject::updatePropertyExDataLink - update [" << strPropertyName
  286. // << "] to [" << datalink.toString() << "]";
  287. // return;
  288. // }
  289. // }
  290. // }
  291. // }
  292. //}
  293. }
  294. /// <summary>
  295. /// 根据类型获取对应的VControlObject指针
  296. /// </summary>
  297. /// <returns></returns>
  298. VControlObject* VControlObject::controlPtr( QObject* object)
  299. {
  300. QString className = object->metaObject()->className();
  301. // 转换为对应的指针
  302. if (className == CLASS_NAME_TABLECONTROL)
  303. {
  304. return qobject_cast<VTableControl*>(object);
  305. }
  306. else if (className == CLASS_NAME_PIECHART)
  307. {
  308. return qobject_cast<VPieChart*>(object);
  309. }
  310. else if (className == CLASS_NAME_CUSTOMPLOT)
  311. {
  312. return qobject_cast<VCustomPlot*>(object);
  313. } else if (className == CLASS_NAME_IMAGECONTROL)
  314. {
  315. return qobject_cast<VImageControl*>(object);
  316. }
  317. else if (className == CLASS_NAME_BUTTON)
  318. {
  319. return qobject_cast<VButton*>(object);
  320. }
  321. else if (className == CLASS_NAME_LABEL)
  322. {
  323. return qobject_cast<VLabel*>(object);
  324. }
  325. else if (className == CLASS_NAME_CHECKBOX)
  326. {
  327. return qobject_cast<VCheckBox*>(object);
  328. }
  329. else if (className == CLASS_NAME_RADIOBOX)
  330. {
  331. return qobject_cast<VRadioBox*>(object);
  332. }
  333. else if (className == CLASS_NAME_LINEDIT)
  334. {
  335. return qobject_cast<VLineEdit*>(object);
  336. }
  337. else if (className == CLASS_NAME_COMBOBOX)
  338. {
  339. return qobject_cast<VComboBox*>(object);
  340. }
  341. else if (className == CLASS_NAME_LISTBOX)
  342. {
  343. return qobject_cast<VListBox*>(object);
  344. }
  345. else if (className == CLASS_NAME_GROUPBOX)
  346. {
  347. return qobject_cast<VGroupBox*>(object);
  348. }
  349. else if (className == CLASS_NAME_VALUECONTROL)
  350. {
  351. return qobject_cast<ValueControl*>(object);
  352. }
  353. else if (className == CLASS_NAME_RESULT)
  354. {
  355. return qobject_cast<VResult*>(object);
  356. }
  357. else
  358. {
  359. return nullptr;
  360. }
  361. }
  362. /// <summary>
  363. /// 从属性标题中获取序号
  364. /// </summary>
  365. /// <param name="propertyName"></param>
  366. /// <returns></returns>
  367. int VControlObject::indexOfProperty(const QString& propertyName)
  368. {
  369. // 从后往前提取出其中的数字
  370. QString strValue;
  371. for (int i = propertyName.size() - 1; i >= 0; i--)
  372. {
  373. if (propertyName[i].isDigit())
  374. {
  375. strValue = propertyName[i] + strValue;
  376. }
  377. }
  378. return strValue.toInt() - 1;
  379. }
  380. /// <summary>
  381. /// 是否是复杂控件(Table、Pie、Wave等)
  382. /// </summary>
  383. /// <returns></returns>
  384. bool VControlObject::isComplexControl()
  385. {
  386. return VControlObject::isComplexControl(m_Type);
  387. }
  388. /// <summary>
  389. /// 是否是复杂控件(Table、Pie、VCustomPlot、Image 等)
  390. /// </summary>
  391. /// <param name="type"></param>
  392. /// <returns></returns>
  393. bool VControlObject::isComplexControl(VALUE_TYPE type)
  394. {
  395. return (
  396. type == VALUE_TYPE::Control_PieChart
  397. || type == VALUE_TYPE::Control_CustomPlot
  398. || type == VALUE_TYPE::Control_Table
  399. || type == VALUE_TYPE::Control_Image
  400. );
  401. }
  402. /// <summary>
  403. /// 根据属性名找到对应的DataLink
  404. /// </summary>
  405. /// <param name="propertyName"></param>
  406. /// <returns></returns>
  407. DataLink VControlObject::getDataLinkByProperty(const QString& strPropertyName)
  408. {
  409. // 简单控件链接
  410. if (!isComplexControl())
  411. {
  412. if (strPropertyName == m_Property.m_DataLink.title)
  413. {
  414. return m_Property.m_DataLink;
  415. }
  416. }
  417. // 复杂控件链接
  418. else
  419. {
  420. // 刷新索引链接
  421. if (strPropertyName == m_PropertyEx.m_refreshLink.title)
  422. {
  423. return m_PropertyEx.m_refreshLink;
  424. }
  425. // 主数据链接
  426. for (int i = 0; i < m_PropertyEx.m_mainDataLinks.size(); i++)
  427. {
  428. if (strPropertyName == m_PropertyEx.m_mainDataLinks[i].title)
  429. {
  430. return m_PropertyEx.m_mainDataLinks[i];
  431. }
  432. }
  433. // 继续查找所有分组中
  434. for (int i = 0; i < m_PropertyEx.m_groups.size(); i++)
  435. {
  436. for (int j = 0; j < m_PropertyEx.m_groups.size(); j++)
  437. {
  438. PROPERTY_EX_SUBGROUPS& subGroups = m_PropertyEx.m_groups[j].subGroups;
  439. for (PROPERTY_EX_SUBGROUP& subGroup : subGroups)
  440. {
  441. for (int m = 0; m < subGroup.dataLinks.size(); m++)
  442. {
  443. if (subGroup.dataLinks[m].title == strPropertyName)
  444. {
  445. return subGroup.dataLinks[m];
  446. }
  447. }
  448. }
  449. }
  450. }
  451. }
  452. // 如果都没有找到的话就返回一个空的
  453. return DataLink();
  454. }
  455. /// <summary>
  456. /// 2022-9-10,为简单控件设置默认的DataLink
  457. /// </summary>
  458. void VControlObject::bindDefaultDataLink()
  459. {
  460. // 如果是正确的绑定,就不设置初始值。通常正确在之为 组名.工具名.端口名
  461. if (this->m_Property.m_DataLink.value.size() == 3)
  462. {
  463. return;
  464. }
  465. // 先清理之前的数据
  466. this->m_Property.m_DataLink.value.clear();
  467. this->m_Property.m_DataLink.value << GROUP_NAME_SYSTEM;
  468. this->m_Property.m_DataLink.value << GROUP_GLOBAL_VARIABLE;
  469. this->m_Property.m_DataLink.value << SYSTEMVAR_NAME_EXECCOUNT;
  470. // 直接到UiManager绑定
  471. g_pUiManager->bindDataLinkSyncInformation(this->m_Property.m_DataLink, this);
  472. }
  473. /// <summary>
  474. /// 2022-9-10,为复杂控件设置默认的刷新索引链接(refreshLink)
  475. /// </summary>
  476. void VControlObject::bindDefaultRefreshDataLink()
  477. {
  478. // 如果是正确的绑定,就不设置初始值。通常正确在之为 组名.工具名.端口名
  479. if (this->m_PropertyEx.m_refreshLink.value.size() == 3)
  480. {
  481. return;
  482. }
  483. // 先清理之前的数据
  484. this->m_PropertyEx.m_refreshLink.value.clear();
  485. this->m_PropertyEx.m_refreshLink.value << GROUP_NAME_SYSTEM;
  486. this->m_PropertyEx.m_refreshLink.value << GROUP_GLOBAL_VARIABLE;
  487. this->m_PropertyEx.m_refreshLink.value << SYSTEMVAR_NAME_EXECCOUNT;
  488. // 直接到UiManager绑定
  489. g_pUiManager->bindDataLinkSyncInformation(this->m_PropertyEx.m_refreshLink, this);
  490. }
  491. /// <summary>
  492. /// 获取控件的item
  493. /// </summary>
  494. /// <returns></returns>
  495. QGraphicsProxyWidget* VControlObject::getItem()
  496. {
  497. return m_pProxyWidget;
  498. }
  499. ///// <summary>
  500. ///// 根据属性名设置对应的DataLink
  501. ///// </summary>
  502. ///// <param name="strPropertyName"></param>
  503. ///// <param name="dataLink"></param>
  504. ///// <returns></returns>
  505. //bool VControlObject::setDataLinkByProperty(const QString& strPropertyName, const DataLink dataLink)
  506. //{
  507. // // 简单控件链接
  508. // if (!isComplexControl())
  509. // {
  510. // if (strPropertyName == m_Property.m_DataLink.title)
  511. // {
  512. // m_Property.m_DataLink = dataLink;
  513. //
  514. // return true;
  515. // }
  516. // }
  517. // // 复杂控件链接
  518. // else
  519. // {
  520. // // 刷新索引链接
  521. // if (strPropertyName == m_PropertyEx.m_refreshLink.title)
  522. // {
  523. // m_PropertyEx.m_refreshLink = dataLink;
  524. //
  525. // return true;
  526. // }
  527. //
  528. //
  529. // // 主数据链接
  530. // for (int i = 0; i < m_PropertyEx.m_mainDataLinks.size(); i++)
  531. // {
  532. // if (strPropertyName == m_PropertyEx.m_mainDataLinks[i].title)
  533. // {
  534. // m_PropertyEx.m_mainDataLinks[i] = dataLink;
  535. //
  536. // return true;
  537. // }
  538. // }
  539. //
  540. // // 继续查找所有分组中
  541. // for (int i = 0; i < m_PropertyEx.m_groups.size(); i++)
  542. // {
  543. // for (int j = 0; j < m_PropertyEx.m_groups.size(); j++)
  544. // {
  545. // PROPERTY_EX_SUBGROUPS& subGroups = m_PropertyEx.m_groups[j].subGroups;
  546. //
  547. // for (PROPERTY_EX_SUBGROUP& subGroup : subGroups)
  548. // {
  549. // for (int m = 0; m < subGroup.dataLinks.size(); m++)
  550. // {
  551. // if (subGroup.dataLinks[m].title == strPropertyName)
  552. // {
  553. // subGroup.dataLinks[m] = dataLink;
  554. //
  555. // return true;
  556. // }
  557. // }
  558. // }
  559. // }
  560. // }
  561. // }
  562. //
  563. // return false;
  564. //}