#include "VControlObject.h" #include "VTableControl.h" #include "VButton.h" #include "VCheckBox.h" #include "VComboBox.h" #include "VLabel.h" #include "VLineEdit.h" #include "VListBox.h" #include "ValueControl.h" #include "VRadioBox.h" #include "VGroupBox.h" #include "VResult.h" #include "VImageControl.h" #include "VTableControl.h" #include "VCustomPlot.h" #include "VPieChart.h" #include "UiManager.h" VControlObject::VControlObject( CONTROL_PROPERTY* pProperty) : m_pWidget(nullptr) , m_pProxyWidget(nullptr) { //// 设置坐标和位置 //if (size != DEFAULT_CONTROL_SIZE) //{ // this->m_pWidget->resize(size); //} //this->m_pWidget->move(pos); if (pProperty != nullptr) { m_Property = *pProperty; } else { // 设置默认属性 m_Property.m_strText = (""); m_Property.m_strTip = ("Tip"); m_Property.m_clrText = QColor(240, 240, 240); m_Property.m_clrBg = QColor(70, 70, 70); m_Property.m_Font = QFont("微软雅黑", 9, 1); m_Property.m_bEnable = true; m_Property.m_DataLink.title = "链接"; } } VControlObject::~VControlObject() { } DataLink VControlObject::getDataLink() const { return this->m_Property.m_DataLink; } void VControlObject::setDataLink(const DataLink& datalink) { if (datalink != this->m_Property.m_DataLink) { this->m_Property.m_DataLink = datalink; } } QString VControlObject::getText() const { return this->m_Property.m_strText; } QString VControlObject::getTip() const { return this->m_Property.m_strTip; } QColor VControlObject::getTextColor() const { return this->m_Property.m_clrText; } QColor VControlObject::getBgColor() const { return this->m_Property.m_clrBg; } QFont VControlObject::getFont() const { return this->m_Property.m_Font; } bool VControlObject::getEnable() const { return this->m_Property.m_bEnable; } QString VControlObject::textColorQss(const QColor& color) { this->m_Property.m_clrText = color; QColor bgColor = getBgColor(); QString bgColorText = Utility::qColor2qString(bgColor); QString foreColorText = Utility::qColor2qString(color); // 设置前景色和背景色(所有的StyleSheet的内容需要一次性设置,要不其他属性会被默认属性覆盖) QString qss = QString("color:%1;background-color:%2;").arg(foreColorText).arg(bgColorText); return qss; } QString VControlObject::bgColorQss(const QColor& color) { this->m_Property.m_clrBg = color; // 用qss实现 QColor foreColor = getTextColor(); QString foreColorText = Utility::qColor2qString(foreColor); QString bgColorText = Utility::qColor2qString(color); // 设置前景色和背景色 QString qss = QString("color:%1;background-color:%2;").arg(foreColorText).arg(bgColorText); return qss; } ///// ///// 获取扩展属性 ///// ///// //CONTROL_PROPERTY_EX VControlObject::getPropertyEx() //{ // return m_PropertyEx; //} // // ///// ///// 设置扩展属性 ///// ///// //void VControlObject::setPropertyEx(const CONTROL_PROPERTY_EX& props) //{ // m_PropertyEx = props; //} /// /// 获得扩展属性的子分组数量 /// 2022-1-10,由于有多个子分组,所以此处需要传入ID /// /// int VControlObject::getExPropertyCountByID(const int groupID) { //return m_PropertyEx.m_subGroups[groupID].size(); return m_PropertyEx.m_groups[groupID].subGroups.size(); } /// /// 根据对应的子属性关联Key的名字来获取子分组数量 /// /// /// int VControlObject::getExPropertyCountByName(const QString& strKey) { // 枚举所有分组 for (int i = 0; i < m_PropertyEx.m_groups.size(); i++) { if (m_PropertyEx.m_groups[i].strRelationKey == strKey) { return m_PropertyEx.m_groups[i].subGroups.size(); } } return -1; } /// /// 根据属性名字获取当前子属性组的ID(注意此处传入的都是英文的属性名字) /// /// /// int VControlObject::getGroupIDByName(const QString& strKey) { int nIndex = 0; for (const PROPERTY_EX_GROUP& group : this->m_PropertyEx.m_groups) { if (strKey == group.strRelationKey) { return nIndex; } nIndex++; } return -1; } /// /// 更新扩展属性的数量(虚函数) /// /// void VControlObject::updateExPropertyCount(const int newCount, const QString& strPropName) { Q_UNUSED(newCount); Q_UNUSED(strPropName) } /// /// 修改扩展属性(虚函数) /// /// /// void VControlObject::changeExProperties(const QString strValueTile, const QVariant& newValue) { Q_UNUSED(strValueTile); Q_UNUSED(newValue); } /// /// 更新扩展属性字段 /// /// /// void VControlObject::updateExPropertyDataLink(const QString& strPropertyName, const QString& strLinkString) { DataLink datalink(strLinkString); this->updateExPropertyDataLink(strPropertyName, datalink); } /// /// 更新数据链接 /// /// /// void VControlObject::updateExPropertyDataLink(const QString& strPropertyName, const DataLink& newDatalink) { // 简单控件链接 if (!isComplexControl()) { if (strPropertyName == m_Property.m_DataLink.title) { m_Property.m_DataLink = newDatalink; } } // 复杂控件链接 else { // 刷新索引链接 if (strPropertyName == m_PropertyEx.m_refreshLink.title) { m_PropertyEx.m_refreshLink = newDatalink; } // 主数据链接 for (int i = 0; i < m_PropertyEx.m_mainDataLinks.size(); i++) { if (strPropertyName == m_PropertyEx.m_mainDataLinks[i].title) { m_PropertyEx.m_mainDataLinks[i] = newDatalink; } } // 继续查找所有分组中 for (int i = 0; i < m_PropertyEx.m_groups.size(); i++) { for (int j = 0; j < m_PropertyEx.m_groups.size(); j++) { PROPERTY_EX_SUBGROUPS& subGroups = m_PropertyEx.m_groups[j].subGroups; for (PROPERTY_EX_SUBGROUP& subGroup : subGroups) { for (int m = 0; m < subGroup.dataLinks.size(); m++) { if (subGroup.dataLinks[m].title == strPropertyName) { subGroup.dataLinks[m] = newDatalink; } } } } } } qDebug() << "VControlObject::updatePropertyExDataLink - update [" << strPropertyName << "] to [" << newDatalink.toString() << "]"; //// 根据属性名字找到对应的DataLink //DataLink& dataLink = this->getDataLinkByProperty(strPropertyName); //// 更新 //dataLink = newDatalink; //qDebug() << "VControlObject::updatePropertyExDataLink - update [" << strPropertyName // << "] to [" << newDatalink.toString() << "]"; //// 刷新索引链接 //if (strPropertyName == m_PropertyEx.m_refreshLink.title) //{ // m_PropertyEx.m_refreshLink = datalink; // qDebug() << "VControlObject::updatePropertyExDataLink - update [" << strPropertyName // << "] to [" << datalink.toString() << "]"; // return; //} //// 主数据链接 //for (int i = 0; i < m_PropertyEx.m_mainDataLinks.size(); i++) //{ // if (strPropertyName == m_PropertyEx.m_mainDataLinks[i].title) // { // m_PropertyEx.m_mainDataLinks[i] = datalink; // qDebug() << "VControlObject::updatePropertyExDataLink - update [" << strPropertyName // << "] to [" << datalink.toString() << "]"; // return; // } //} //// 继续查找所有分组中 //for (int i = 0; i < m_PropertyEx.m_groups.size(); i++) //{ // for (int j = 0; j < m_PropertyEx.m_groups.size(); j++) // { // PROPERTY_EX_SUBGROUPS& subGroups = m_PropertyEx.m_groups[j].subGroups; // for (PROPERTY_EX_SUBGROUP& subGroup : subGroups) // { // for (int m = 0; m < subGroup.dataLinks.size(); m++) // { // if (subGroup.dataLinks[m].title == strPropertyName) // { // subGroup.dataLinks[m] = datalink; // qDebug() << "VControlObject::updatePropertyExDataLink - update [" << strPropertyName // << "] to [" << datalink.toString() << "]"; // return; // } // } // } // } //} } /// /// 根据类型获取对应的VControlObject指针 /// /// VControlObject* VControlObject::controlPtr( QObject* object) { QString className = object->metaObject()->className(); // 转换为对应的指针 if (className == CLASS_NAME_TABLECONTROL) { return qobject_cast(object); } else if (className == CLASS_NAME_PIECHART) { return qobject_cast(object); } else if (className == CLASS_NAME_CUSTOMPLOT) { return qobject_cast(object); } else if (className == CLASS_NAME_IMAGECONTROL) { return qobject_cast(object); } else if (className == CLASS_NAME_BUTTON) { return qobject_cast(object); } else if (className == CLASS_NAME_LABEL) { return qobject_cast(object); } else if (className == CLASS_NAME_CHECKBOX) { return qobject_cast(object); } else if (className == CLASS_NAME_RADIOBOX) { return qobject_cast(object); } else if (className == CLASS_NAME_LINEDIT) { return qobject_cast(object); } else if (className == CLASS_NAME_COMBOBOX) { return qobject_cast(object); } else if (className == CLASS_NAME_LISTBOX) { return qobject_cast(object); } else if (className == CLASS_NAME_GROUPBOX) { return qobject_cast(object); } else if (className == CLASS_NAME_VALUECONTROL) { return qobject_cast(object); } else if (className == CLASS_NAME_RESULT) { return qobject_cast(object); } else { return nullptr; } } /// /// 从属性标题中获取序号 /// /// /// int VControlObject::indexOfProperty(const QString& propertyName) { // 从后往前提取出其中的数字 QString strValue; for (int i = propertyName.size() - 1; i >= 0; i--) { if (propertyName[i].isDigit()) { strValue = propertyName[i] + strValue; } } return strValue.toInt() - 1; } /// /// 是否是复杂控件(Table、Pie、Wave等) /// /// bool VControlObject::isComplexControl() { return VControlObject::isComplexControl(m_Type); } /// /// 是否是复杂控件(Table、Pie、VCustomPlot、Image 等) /// /// /// bool VControlObject::isComplexControl(VALUE_TYPE type) { return ( type == VALUE_TYPE::Control_PieChart || type == VALUE_TYPE::Control_CustomPlot || type == VALUE_TYPE::Control_Table || type == VALUE_TYPE::Control_Image ); } /// /// 根据属性名找到对应的DataLink /// /// /// DataLink VControlObject::getDataLinkByProperty(const QString& strPropertyName) { // 简单控件链接 if (!isComplexControl()) { if (strPropertyName == m_Property.m_DataLink.title) { return m_Property.m_DataLink; } } // 复杂控件链接 else { // 刷新索引链接 if (strPropertyName == m_PropertyEx.m_refreshLink.title) { return m_PropertyEx.m_refreshLink; } // 主数据链接 for (int i = 0; i < m_PropertyEx.m_mainDataLinks.size(); i++) { if (strPropertyName == m_PropertyEx.m_mainDataLinks[i].title) { return m_PropertyEx.m_mainDataLinks[i]; } } // 继续查找所有分组中 for (int i = 0; i < m_PropertyEx.m_groups.size(); i++) { for (int j = 0; j < m_PropertyEx.m_groups.size(); j++) { PROPERTY_EX_SUBGROUPS& subGroups = m_PropertyEx.m_groups[j].subGroups; for (PROPERTY_EX_SUBGROUP& subGroup : subGroups) { for (int m = 0; m < subGroup.dataLinks.size(); m++) { if (subGroup.dataLinks[m].title == strPropertyName) { return subGroup.dataLinks[m]; } } } } } } // 如果都没有找到的话就返回一个空的 return DataLink(); } /// /// 2022-9-10,为简单控件设置默认的DataLink /// void VControlObject::bindDefaultDataLink() { // 如果是正确的绑定,就不设置初始值。通常正确在之为 组名.工具名.端口名 if (this->m_Property.m_DataLink.value.size() == 3) { return; } // 先清理之前的数据 this->m_Property.m_DataLink.value.clear(); this->m_Property.m_DataLink.value << GROUP_NAME_SYSTEM; this->m_Property.m_DataLink.value << GROUP_GLOBAL_VARIABLE; this->m_Property.m_DataLink.value << SYSTEMVAR_NAME_EXECCOUNT; // 直接到UiManager绑定 g_pUiManager->bindDataLinkSyncInformation(this->m_Property.m_DataLink, this); } /// /// 2022-9-10,为复杂控件设置默认的刷新索引链接(refreshLink) /// void VControlObject::bindDefaultRefreshDataLink() { // 如果是正确的绑定,就不设置初始值。通常正确在之为 组名.工具名.端口名 if (this->m_PropertyEx.m_refreshLink.value.size() == 3) { return; } // 先清理之前的数据 this->m_PropertyEx.m_refreshLink.value.clear(); this->m_PropertyEx.m_refreshLink.value << GROUP_NAME_SYSTEM; this->m_PropertyEx.m_refreshLink.value << GROUP_GLOBAL_VARIABLE; this->m_PropertyEx.m_refreshLink.value << SYSTEMVAR_NAME_EXECCOUNT; // 直接到UiManager绑定 g_pUiManager->bindDataLinkSyncInformation(this->m_PropertyEx.m_refreshLink, this); } /// /// 获取控件的item /// /// QGraphicsProxyWidget* VControlObject::getItem() { return m_pProxyWidget; } ///// ///// 根据属性名设置对应的DataLink ///// ///// ///// ///// //bool VControlObject::setDataLinkByProperty(const QString& strPropertyName, const DataLink dataLink) //{ // // 简单控件链接 // if (!isComplexControl()) // { // if (strPropertyName == m_Property.m_DataLink.title) // { // m_Property.m_DataLink = dataLink; // // return true; // } // } // // 复杂控件链接 // else // { // // 刷新索引链接 // if (strPropertyName == m_PropertyEx.m_refreshLink.title) // { // m_PropertyEx.m_refreshLink = dataLink; // // return true; // } // // // // 主数据链接 // for (int i = 0; i < m_PropertyEx.m_mainDataLinks.size(); i++) // { // if (strPropertyName == m_PropertyEx.m_mainDataLinks[i].title) // { // m_PropertyEx.m_mainDataLinks[i] = dataLink; // // return true; // } // } // // // 继续查找所有分组中 // for (int i = 0; i < m_PropertyEx.m_groups.size(); i++) // { // for (int j = 0; j < m_PropertyEx.m_groups.size(); j++) // { // PROPERTY_EX_SUBGROUPS& subGroups = m_PropertyEx.m_groups[j].subGroups; // // for (PROPERTY_EX_SUBGROUP& subGroup : subGroups) // { // for (int m = 0; m < subGroup.dataLinks.size(); m++) // { // if (subGroup.dataLinks[m].title == strPropertyName) // { // subGroup.dataLinks[m] = dataLink; // // return true; // } // } // } // } // } // } // // return false; //}