VTableControl.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. #include "VTableControl.h"
  2. #include "GvlManager.h"
  3. // 创建控件时默认的Table行数和列数
  4. #define DEFAULT_TABLECONTROL_COL_COUNT 1
  5. #define DEFAULT_TABLECONTROL_ROW_COUNT 4
  6. VTableControl::VTableControl(
  7. QWidget* parent,
  8. const QPoint& pos,
  9. const QSize& size,
  10. CONTROL_PROPERTY* pProperty,
  11. CONTROL_PROPERTY_EX* pPropertyEx
  12. )
  13. : QTableWidget(parent)
  14. , VControlObject(pProperty)
  15. {
  16. // 设置默认属性(初始化值或者外部传入值)
  17. QTableWidget::setFont(m_Property.m_Font);
  18. QTableWidget::setEnabled(m_Property.m_bEnable);
  19. m_pWidget = this;
  20. // 控件类型
  21. m_Type = VALUE_TYPE::Control_Table;
  22. // 初始化UI风格
  23. this->initStyle();
  24. // 设置尺寸
  25. if (size == DEFAULT_CONTROL_SIZE)
  26. {
  27. this->resize(DEFAULT_TABLE_SIZE);
  28. }
  29. else
  30. {
  31. this->resize(size);
  32. }
  33. // 设置中心点坐标
  34. QPoint tempPos;
  35. tempPos.setX(pos.x() - width() / 2);
  36. tempPos.setY(pos.y() - height() / 2);
  37. // 设置位置
  38. this->move(tempPos);
  39. // 初始化扩展属性
  40. if (pPropertyEx == nullptr)
  41. {
  42. // 初始化行数和列数
  43. setRowCount(DEFAULT_TABLECONTROL_ROW_COUNT);
  44. setColCount(DEFAULT_TABLECONTROL_COL_COUNT);
  45. // 初始化扩展属性
  46. this->initPropertyEx();
  47. }
  48. else
  49. {
  50. this->m_PropertyEx = *pPropertyEx;
  51. // 初始化表格的行和列
  52. QTableWidget::setRowCount(this->m_Property.m_nRowCount);
  53. QTableWidget::setColumnCount(this->m_Property.m_nColCount);
  54. // 初始化表头
  55. this->initTableHeader();
  56. }
  57. }
  58. VTableControl::~VTableControl()
  59. {
  60. }
  61. /// <summary>
  62. /// 初始化UI风格
  63. /// </summary>
  64. void VTableControl::initStyle()
  65. {
  66. // 设置表头字体
  67. QFont font = this->horizontalHeader()->font();
  68. font.setBold(true);
  69. this->horizontalHeader()->setFont(font);
  70. // 设置文字左对齐
  71. this->horizontalHeader()->setDefaultAlignment(Qt::AlignCenter);
  72. // 设置为不可编辑
  73. this->setEditTriggers(QAbstractItemView::NoEditTriggers);
  74. // 设置为整行选中模式
  75. this->setSelectionBehavior(QAbstractItemView::SelectRows);
  76. this->setSelectionMode(QAbstractItemView::SingleSelection);
  77. }
  78. /// <summary>
  79. /// 初始化Table表头
  80. /// </summary>
  81. void VTableControl::initTableHeader()
  82. {
  83. // 初始化表头文字
  84. QStringList strHeader;
  85. const QVector<PROPERTY_EX_SUBGROUP>& subGroups = this->m_PropertyEx.m_groups[GROUP_INDEX_TABLE].subGroups;
  86. for (PROPERTY_EX_SUBGROUP subGroup : subGroups)
  87. {
  88. strHeader << subGroup.strValue;
  89. }
  90. this->setHorizontalHeaderLabels(strHeader);
  91. }
  92. void VTableControl::setTextColor(const QColor& color)
  93. {
  94. QString qss = VControlObject::textColorQss(color);
  95. this->setStyleSheet(qss);
  96. }
  97. //// 属性的实现函数
  98. //
  99. //void VLineEdit::setText(const QString& title)
  100. //{
  101. // if (this->m_Property.m_strText != title)
  102. // {
  103. // this->m_Property.m_strText = title;
  104. // QLineEdit::setText(title);
  105. // }
  106. //}
  107. //
  108. //
  109. //void VLineEdit::setTip(const QString& tip)
  110. //{
  111. // if (this->m_Property.m_strTip != tip)
  112. // {
  113. // this->m_Property.m_strTip = tip;
  114. // QLineEdit::setToolTip(tip);
  115. // }
  116. //}
  117. //
  118. //
  119. //void VLineEdit::setTextColor(const QColor& color)
  120. //{
  121. // QString qss = VControlObject::textColorQss(color);
  122. //
  123. // this->setStyleSheet(qss);
  124. //}
  125. //
  126. //void VLineEdit::setBgColor(const QColor& color)
  127. //{
  128. // QString qss = VControlObject::bgColorQss(color);
  129. //
  130. // this->setStyleSheet(qss);
  131. //}
  132. void VTableControl::setFont(const QFont& font)
  133. {
  134. if (this->m_Property.m_Font != font)
  135. {
  136. this->m_Property.m_Font = font;
  137. QTableWidget::setFont(font);
  138. }
  139. }
  140. void VTableControl::setEnable(const bool enable)
  141. {
  142. if (this->m_Property.m_bEnable != enable)
  143. {
  144. this->m_Property.m_bEnable = enable;
  145. QTableWidget::setEnabled(enable);
  146. }
  147. }
  148. int VTableControl::getRowCount()
  149. {
  150. return m_Property.m_nRowCount;
  151. }
  152. void VTableControl::setRowCount(const int row)
  153. {
  154. if (this->m_Property.m_nRowCount != row)
  155. {
  156. this->m_Property.m_nRowCount = row;
  157. QTableWidget::setRowCount(row);
  158. }
  159. }
  160. int VTableControl::getColCount()
  161. {
  162. return m_Property.m_nColCount;
  163. }
  164. void VTableControl::setColCount(const int col)
  165. {
  166. if (this->m_Property.m_nColCount != col)
  167. {
  168. this->m_Property.m_nColCount = col;
  169. QTableWidget::setColumnCount(col);
  170. }
  171. }
  172. /// <summary>
  173. /// 初始化扩展属性
  174. /// </summary>
  175. void VTableControl::initPropertyEx()
  176. {
  177. // 扩展属性的总名称
  178. m_PropertyEx.m_strTitle = TABLE_PROPERTY_EX_NAME;
  179. // 用于触发刷新的数据链接(数据连接没有默认值)
  180. m_PropertyEx.m_refreshLink.title = TABLE_PROPERTY_EX_REFRESHLINK_NAME;
  181. // 强制需要设置
  182. m_PropertyEx.m_refreshLink.bForce = true;
  183. // 初始化每一个扩展属性组(Table控件只有一个属性组)
  184. PROPERTY_EX_GROUP group;
  185. // 列信息属性
  186. group.strTitle = TABLE_PROPERTY_EX_GROUP_NAME;
  187. group.strRelationKey = TABLE_COL_COUNT_NAME;
  188. //初始化列信息每一个子分组
  189. for (int i = 0; i < m_Property.m_nColCount; i++)
  190. {
  191. PROPERTY_EX_SUBGROUP subGroup;
  192. this->initSubGroup(subGroup, i, GROUP_INDEX_TABLE);
  193. // 保存子分组
  194. group.subGroups.push_back(subGroup);
  195. }
  196. // 保存本子分组定义信息
  197. m_PropertyEx.m_groups.push_back(group);
  198. }
  199. /// <summary>
  200. /// 初始化一个扩展属性组
  201. /// </summary>
  202. /// <param name="subItem"></param>
  203. /// <param name="nIndex"></param>
  204. void VTableControl::initSubGroup(PROPERTY_EX_SUBGROUP& subGroup, int nIndex, int nSubGroupID)
  205. {
  206. if (nSubGroupID == GROUP_INDEX_TABLE)
  207. {
  208. QString idx = QString::number(nIndex + 1);
  209. // 子分组名称
  210. subGroup.strTitle = TABLE_PROPERTY_EX_SUBGROUP_NAME + idx;
  211. // 列名称 + 数值
  212. subGroup.strValueName = TABLE_PROPERTY_EX_VALUE_NAME + idx;
  213. subGroup.strValue = TABLE_PROPERTY_EX_VALUE_NAME + idx;
  214. // 数据链接名称
  215. DataLink dataLink;
  216. dataLink.title = TABLE_PROPERTY_EX_LINK_NAME + idx;
  217. dataLink.defaultValue = g_pGvlManager->getDefaultValueByName(DEFAULT_VALUE_0);
  218. subGroup.dataLinks.push_back(dataLink);
  219. }
  220. }
  221. /// <summary>
  222. /// 调整列属性数量(虚函数)
  223. /// </summary>
  224. /// <param name="fixCount"></param>
  225. void VTableControl::updateExPropertyCount(const int fixCount, const QString& strPropName)
  226. {
  227. // 获取属性的当前数量
  228. int curCount = this->getExPropertyCountByName(strPropName);
  229. // 子分组编号
  230. int curGroup = this->getGroupIDByName(strPropName);
  231. // 检查有效性
  232. if (curGroup < 0)
  233. {
  234. qDebug() << "[Error] VTableControl::updateExPropertyCount - nGroupID in invalid: " << curGroup;
  235. return;
  236. }
  237. // 如果需要增加
  238. if (fixCount > 0)
  239. {
  240. int nEnd = curCount + fixCount;
  241. for (int i = curCount; i < nEnd; i++)
  242. {
  243. PROPERTY_EX_SUBGROUP subItem;
  244. this->initSubGroup(subItem, i, curGroup);
  245. m_PropertyEx.m_groups[curGroup].subGroups.push_back(subItem);
  246. }
  247. }
  248. // 如果需要减少
  249. else if (fixCount < 0)
  250. {
  251. for (int i = 0; i < qAbs(fixCount); i++)
  252. {
  253. // 去掉对应的数据结构信息
  254. m_PropertyEx.m_groups[curGroup].subGroups.pop_back();
  255. }
  256. }
  257. }
  258. /// <summary>
  259. /// 修改扩展属性(虚函数)
  260. /// </summary>
  261. /// <param name="strValueTile"></param>
  262. /// <param name="newValue"></param>
  263. void VTableControl::changeExProperties(QString strValueTitle, const QVariant& newValue)
  264. {
  265. // 如果改变了列标题
  266. if (strValueTitle.contains(TABLE_PROPERTY_EX_VALUE_NAME))
  267. {
  268. // 获取修改属性的列索引
  269. int nColIndex = strValueTitle.remove(TABLE_PROPERTY_EX_VALUE_NAME).toInt() - 1;
  270. // 修改对应Table的数据结构内容
  271. this->updateColTitle(newValue.toString(), nColIndex);
  272. }
  273. // 如果改变了数据链接(此处应该不需要手工更新)
  274. else if (strValueTitle.contains(TABLE_PROPERTY_EX_REFRESHLINK_NAME)
  275. || strValueTitle.contains(TABLE_PROPERTY_EX_LINK_NAME)
  276. )
  277. {
  278. // pTable->updatePropertyExDataLink(strValueTitle, value.toString());
  279. }
  280. // Error:不应该执行到这里
  281. else
  282. {
  283. qDebug() << "[Error]: VTableControl::changeExProperties - invalid strValueTitle:" << strValueTitle;
  284. }
  285. }
  286. /// <summary>
  287. /// 调整列标题
  288. /// </summary>
  289. /// <param name="newTitle"></param>
  290. /// <param name="nIndex"></param>
  291. void VTableControl::updateColTitle(const QString& newValue, const int nIndex)
  292. {
  293. // 更新列标题
  294. QString& strValue = m_PropertyEx.m_groups[GROUP_INDEX_TABLE].subGroups[nIndex].strValue;
  295. if (strValue != newValue)
  296. {
  297. strValue = newValue;
  298. }
  299. qDebug() << "VTableControl::updateColTitle - update ["
  300. << m_PropertyEx.m_groups[GROUP_INDEX_TABLE].subGroups[nIndex].strValueName << "] to [" << newValue << "]";
  301. }
  302. /// <summary>
  303. /// 刷新一整行数据
  304. /// </summary>
  305. /// <param name="vars"></param>
  306. /// <param name="nRowIndex"></param>
  307. void VTableControl::updateRowFromVariables(const RUNTIME_SYNC_VARS& sync_vars, int nRowIndex)
  308. {
  309. // 将整列值更新到Table控件中(一次更新一整列)
  310. int nColCount = this->columnCount();
  311. for (int i = 0; i < nColCount; i++)
  312. {
  313. QString strValue = sync_vars[i].pVariable->getValueString();
  314. this->setItem(
  315. nRowIndex,
  316. i,
  317. new QTableWidgetItem(strValue)
  318. );
  319. }
  320. }