123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- #include "VTableControl.h"
- #include "GvlManager.h"
- // 创建控件时默认的Table行数和列数
- #define DEFAULT_TABLECONTROL_COL_COUNT 1
- #define DEFAULT_TABLECONTROL_ROW_COUNT 4
- VTableControl::VTableControl(
- QWidget* parent,
- const QPoint& pos,
- const QSize& size,
- CONTROL_PROPERTY* pProperty,
- CONTROL_PROPERTY_EX* pPropertyEx
- )
- : QTableWidget(parent)
- , VControlObject(pProperty)
- {
- // 设置默认属性(初始化值或者外部传入值)
- QTableWidget::setFont(m_Property.m_Font);
- QTableWidget::setEnabled(m_Property.m_bEnable);
- m_pWidget = this;
- // 控件类型
- m_Type = VALUE_TYPE::Control_Table;
- // 初始化UI风格
- this->initStyle();
- // 设置尺寸
- if (size == DEFAULT_CONTROL_SIZE)
- {
- this->resize(DEFAULT_TABLE_SIZE);
- }
- else
- {
- this->resize(size);
- }
- // 设置中心点坐标
- QPoint tempPos;
- tempPos.setX(pos.x() - width() / 2);
- tempPos.setY(pos.y() - height() / 2);
- // 设置位置
- this->move(tempPos);
- // 初始化扩展属性
- if (pPropertyEx == nullptr)
- {
- // 初始化行数和列数
- setRowCount(DEFAULT_TABLECONTROL_ROW_COUNT);
- setColCount(DEFAULT_TABLECONTROL_COL_COUNT);
- // 初始化扩展属性
- this->initPropertyEx();
- }
- else
- {
- this->m_PropertyEx = *pPropertyEx;
- // 初始化表格的行和列
- QTableWidget::setRowCount(this->m_Property.m_nRowCount);
- QTableWidget::setColumnCount(this->m_Property.m_nColCount);
- // 初始化表头
- this->initTableHeader();
- }
-
- }
- VTableControl::~VTableControl()
- {
- }
- /// <summary>
- /// 初始化UI风格
- /// </summary>
- void VTableControl::initStyle()
- {
- // 设置表头字体
- QFont font = this->horizontalHeader()->font();
- font.setBold(true);
- this->horizontalHeader()->setFont(font);
- // 设置文字左对齐
- this->horizontalHeader()->setDefaultAlignment(Qt::AlignCenter);
- // 设置为不可编辑
- this->setEditTriggers(QAbstractItemView::NoEditTriggers);
- // 设置为整行选中模式
- this->setSelectionBehavior(QAbstractItemView::SelectRows);
- this->setSelectionMode(QAbstractItemView::SingleSelection);
- }
- /// <summary>
- /// 初始化Table表头
- /// </summary>
- void VTableControl::initTableHeader()
- {
- // 初始化表头文字
- QStringList strHeader;
- const QVector<PROPERTY_EX_SUBGROUP>& subGroups = this->m_PropertyEx.m_groups[GROUP_INDEX_TABLE].subGroups;
- for (PROPERTY_EX_SUBGROUP subGroup : subGroups)
- {
- strHeader << subGroup.strValue;
- }
- this->setHorizontalHeaderLabels(strHeader);
- }
- void VTableControl::setTextColor(const QColor& color)
- {
- QString qss = VControlObject::textColorQss(color);
- this->setStyleSheet(qss);
- }
- //// 属性的实现函数
- //
- //void VLineEdit::setText(const QString& title)
- //{
- // if (this->m_Property.m_strText != title)
- // {
- // this->m_Property.m_strText = title;
- // QLineEdit::setText(title);
- // }
- //}
- //
- //
- //void VLineEdit::setTip(const QString& tip)
- //{
- // if (this->m_Property.m_strTip != tip)
- // {
- // this->m_Property.m_strTip = tip;
- // QLineEdit::setToolTip(tip);
- // }
- //}
- //
- //
- //void VLineEdit::setTextColor(const QColor& color)
- //{
- // QString qss = VControlObject::textColorQss(color);
- //
- // this->setStyleSheet(qss);
- //}
- //
- //void VLineEdit::setBgColor(const QColor& color)
- //{
- // QString qss = VControlObject::bgColorQss(color);
- //
- // this->setStyleSheet(qss);
- //}
- void VTableControl::setFont(const QFont& font)
- {
- if (this->m_Property.m_Font != font)
- {
- this->m_Property.m_Font = font;
- QTableWidget::setFont(font);
- }
- }
- void VTableControl::setEnable(const bool enable)
- {
- if (this->m_Property.m_bEnable != enable)
- {
- this->m_Property.m_bEnable = enable;
- QTableWidget::setEnabled(enable);
- }
- }
- int VTableControl::getRowCount()
- {
- return m_Property.m_nRowCount;
- }
- void VTableControl::setRowCount(const int row)
- {
- if (this->m_Property.m_nRowCount != row)
- {
- this->m_Property.m_nRowCount = row;
- QTableWidget::setRowCount(row);
- }
- }
- int VTableControl::getColCount()
- {
- return m_Property.m_nColCount;
- }
- void VTableControl::setColCount(const int col)
- {
- if (this->m_Property.m_nColCount != col)
- {
- this->m_Property.m_nColCount = col;
- QTableWidget::setColumnCount(col);
- }
- }
- /// <summary>
- /// 初始化扩展属性
- /// </summary>
- void VTableControl::initPropertyEx()
- {
- // 扩展属性的总名称
- m_PropertyEx.m_strTitle = TABLE_PROPERTY_EX_NAME;
- // 用于触发刷新的数据链接(数据连接没有默认值)
- m_PropertyEx.m_refreshLink.title = TABLE_PROPERTY_EX_REFRESHLINK_NAME;
- // 强制需要设置
- m_PropertyEx.m_refreshLink.bForce = true;
- // 初始化每一个扩展属性组(Table控件只有一个属性组)
- PROPERTY_EX_GROUP group;
- // 列信息属性
- group.strTitle = TABLE_PROPERTY_EX_GROUP_NAME;
- group.strRelationKey = TABLE_COL_COUNT_NAME;
- //初始化列信息每一个子分组
- for (int i = 0; i < m_Property.m_nColCount; i++)
- {
- PROPERTY_EX_SUBGROUP subGroup;
- this->initSubGroup(subGroup, i, GROUP_INDEX_TABLE);
- // 保存子分组
- group.subGroups.push_back(subGroup);
- }
- // 保存本子分组定义信息
- m_PropertyEx.m_groups.push_back(group);
- }
- /// <summary>
- /// 初始化一个扩展属性组
- /// </summary>
- /// <param name="subItem"></param>
- /// <param name="nIndex"></param>
- void VTableControl::initSubGroup(PROPERTY_EX_SUBGROUP& subGroup, int nIndex, int nSubGroupID)
- {
- if (nSubGroupID == GROUP_INDEX_TABLE)
- {
- QString idx = QString::number(nIndex + 1);
- // 子分组名称
- subGroup.strTitle = TABLE_PROPERTY_EX_SUBGROUP_NAME + idx;
- // 列名称 + 数值
- subGroup.strValueName = TABLE_PROPERTY_EX_VALUE_NAME + idx;
- subGroup.strValue = TABLE_PROPERTY_EX_VALUE_NAME + idx;
- // 数据链接名称
- DataLink dataLink;
- dataLink.title = TABLE_PROPERTY_EX_LINK_NAME + idx;
- dataLink.defaultValue = g_pGvlManager->getDefaultValueByName(DEFAULT_VALUE_0);
- subGroup.dataLinks.push_back(dataLink);
- }
- }
- /// <summary>
- /// 调整列属性数量(虚函数)
- /// </summary>
- /// <param name="fixCount"></param>
- void VTableControl::updateExPropertyCount(const int fixCount, const QString& strPropName)
- {
- // 获取属性的当前数量
- int curCount = this->getExPropertyCountByName(strPropName);
- // 子分组编号
- int curGroup = this->getGroupIDByName(strPropName);
- // 检查有效性
- if (curGroup < 0)
- {
- qDebug() << "[Error] VTableControl::updateExPropertyCount - nGroupID in invalid: " << curGroup;
- return;
- }
- // 如果需要增加
- if (fixCount > 0)
- {
- int nEnd = curCount + fixCount;
- for (int i = curCount; i < nEnd; i++)
- {
- PROPERTY_EX_SUBGROUP subItem;
- this->initSubGroup(subItem, i, curGroup);
- m_PropertyEx.m_groups[curGroup].subGroups.push_back(subItem);
- }
- }
- // 如果需要减少
- else if (fixCount < 0)
- {
- for (int i = 0; i < qAbs(fixCount); i++)
- {
- // 去掉对应的数据结构信息
- m_PropertyEx.m_groups[curGroup].subGroups.pop_back();
- }
- }
- }
- /// <summary>
- /// 修改扩展属性(虚函数)
- /// </summary>
- /// <param name="strValueTile"></param>
- /// <param name="newValue"></param>
- void VTableControl::changeExProperties(QString strValueTitle, const QVariant& newValue)
- {
- // 如果改变了列标题
- if (strValueTitle.contains(TABLE_PROPERTY_EX_VALUE_NAME))
- {
- // 获取修改属性的列索引
- int nColIndex = strValueTitle.remove(TABLE_PROPERTY_EX_VALUE_NAME).toInt() - 1;
- // 修改对应Table的数据结构内容
- this->updateColTitle(newValue.toString(), nColIndex);
- }
- // 如果改变了数据链接(此处应该不需要手工更新)
- else if (strValueTitle.contains(TABLE_PROPERTY_EX_REFRESHLINK_NAME)
- || strValueTitle.contains(TABLE_PROPERTY_EX_LINK_NAME)
- )
- {
- // pTable->updatePropertyExDataLink(strValueTitle, value.toString());
- }
- // Error:不应该执行到这里
- else
- {
- qDebug() << "[Error]: VTableControl::changeExProperties - invalid strValueTitle:" << strValueTitle;
- }
- }
- /// <summary>
- /// 调整列标题
- /// </summary>
- /// <param name="newTitle"></param>
- /// <param name="nIndex"></param>
- void VTableControl::updateColTitle(const QString& newValue, const int nIndex)
- {
- // 更新列标题
- QString& strValue = m_PropertyEx.m_groups[GROUP_INDEX_TABLE].subGroups[nIndex].strValue;
- if (strValue != newValue)
- {
- strValue = newValue;
- }
-
- qDebug() << "VTableControl::updateColTitle - update ["
- << m_PropertyEx.m_groups[GROUP_INDEX_TABLE].subGroups[nIndex].strValueName << "] to [" << newValue << "]";
- }
- /// <summary>
- /// 刷新一整行数据
- /// </summary>
- /// <param name="vars"></param>
- /// <param name="nRowIndex"></param>
- void VTableControl::updateRowFromVariables(const RUNTIME_SYNC_VARS& sync_vars, int nRowIndex)
- {
- // 将整列值更新到Table控件中(一次更新一整列)
- int nColCount = this->columnCount();
- for (int i = 0; i < nColCount; i++)
- {
- QString strValue = sync_vars[i].pVariable->getValueString();
- this->setItem(
- nRowIndex,
- i,
- new QTableWidgetItem(strValue)
- );
- }
- }
|