#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()
{
}
///
/// 初始化UI风格
///
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);
}
///
/// 初始化Table表头
///
void VTableControl::initTableHeader()
{
// 初始化表头文字
QStringList strHeader;
const QVector& 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);
}
}
///
/// 初始化扩展属性
///
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);
}
///
/// 初始化一个扩展属性组
///
///
///
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);
}
}
///
/// 调整列属性数量(虚函数)
///
///
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();
}
}
}
///
/// 修改扩展属性(虚函数)
///
///
///
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;
}
}
///
/// 调整列标题
///
///
///
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 << "]";
}
///
/// 刷新一整行数据
///
///
///
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)
);
}
}