123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686 |
- #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;
- }
- ///// <summary>
- ///// 获取扩展属性
- ///// </summary>
- ///// <returns></returns>
- //CONTROL_PROPERTY_EX VControlObject::getPropertyEx()
- //{
- // return m_PropertyEx;
- //}
- //
- //
- ///// <summary>
- ///// 设置扩展属性
- ///// </summary>
- ///// <param name="props"></param>
- //void VControlObject::setPropertyEx(const CONTROL_PROPERTY_EX& props)
- //{
- // m_PropertyEx = props;
- //}
- /// <summary>
- /// 获得扩展属性的子分组数量
- /// 2022-1-10,由于有多个子分组,所以此处需要传入ID
- /// </summary>
- /// <returns></returns>
- int VControlObject::getExPropertyCountByID(const int groupID)
- {
- //return m_PropertyEx.m_subGroups[groupID].size();
- return m_PropertyEx.m_groups[groupID].subGroups.size();
- }
- /// <summary>
- /// 根据对应的子属性关联Key的名字来获取子分组数量
- /// </summary>
- /// <param name="strKey"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 根据属性名字获取当前子属性组的ID(注意此处传入的都是英文的属性名字)
- /// </summary>
- /// <param name="strKey"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 更新扩展属性的数量(虚函数)
- /// </summary>
- /// <param name="fixCount"></param>
- void VControlObject::updateExPropertyCount(const int newCount, const QString& strPropName)
- {
- Q_UNUSED(newCount);
- Q_UNUSED(strPropName)
- }
- /// <summary>
- /// 修改扩展属性(虚函数)
- /// </summary>
- /// <param name="strValueTile"></param>
- /// <param name="newValue"></param>
- void VControlObject::changeExProperties(const QString strValueTile, const QVariant& newValue)
- {
- Q_UNUSED(strValueTile);
- Q_UNUSED(newValue);
- }
- /// <summary>
- /// 更新扩展属性字段
- /// </summary>
- /// <param name="strPropertyName"></param>
- /// <param name="datalink"></param>
- void VControlObject::updateExPropertyDataLink(const QString& strPropertyName, const QString& strLinkString)
- {
- DataLink datalink(strLinkString);
- this->updateExPropertyDataLink(strPropertyName, datalink);
- }
- /// <summary>
- /// 更新数据链接
- /// </summary>
- /// <param name="strPropertyName"></param>
- /// <param name="datalink"></param>
- 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;
- // }
- // }
- // }
- // }
- //}
- }
- /// <summary>
- /// 根据类型获取对应的VControlObject指针
- /// </summary>
- /// <returns></returns>
- VControlObject* VControlObject::controlPtr( QObject* object)
- {
- QString className = object->metaObject()->className();
- // 转换为对应的指针
- if (className == CLASS_NAME_TABLECONTROL)
- {
- return qobject_cast<VTableControl*>(object);
- }
- else if (className == CLASS_NAME_PIECHART)
- {
- return qobject_cast<VPieChart*>(object);
- }
- else if (className == CLASS_NAME_CUSTOMPLOT)
- {
- return qobject_cast<VCustomPlot*>(object);
- } else if (className == CLASS_NAME_IMAGECONTROL)
- {
- return qobject_cast<VImageControl*>(object);
- }
- else if (className == CLASS_NAME_BUTTON)
- {
- return qobject_cast<VButton*>(object);
- }
- else if (className == CLASS_NAME_LABEL)
- {
- return qobject_cast<VLabel*>(object);
- }
- else if (className == CLASS_NAME_CHECKBOX)
- {
- return qobject_cast<VCheckBox*>(object);
- }
- else if (className == CLASS_NAME_RADIOBOX)
- {
- return qobject_cast<VRadioBox*>(object);
- }
- else if (className == CLASS_NAME_LINEDIT)
- {
- return qobject_cast<VLineEdit*>(object);
- }
- else if (className == CLASS_NAME_COMBOBOX)
- {
- return qobject_cast<VComboBox*>(object);
- }
- else if (className == CLASS_NAME_LISTBOX)
- {
- return qobject_cast<VListBox*>(object);
- }
- else if (className == CLASS_NAME_GROUPBOX)
- {
- return qobject_cast<VGroupBox*>(object);
- }
- else if (className == CLASS_NAME_VALUECONTROL)
- {
- return qobject_cast<ValueControl*>(object);
- }
- else if (className == CLASS_NAME_RESULT)
- {
- return qobject_cast<VResult*>(object);
- }
-
- else
- {
- return nullptr;
- }
- }
- /// <summary>
- /// 从属性标题中获取序号
- /// </summary>
- /// <param name="propertyName"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 是否是复杂控件(Table、Pie、Wave等)
- /// </summary>
- /// <returns></returns>
- bool VControlObject::isComplexControl()
- {
- return VControlObject::isComplexControl(m_Type);
- }
- /// <summary>
- /// 是否是复杂控件(Table、Pie、VCustomPlot、Image 等)
- /// </summary>
- /// <param name="type"></param>
- /// <returns></returns>
- 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
- );
- }
- /// <summary>
- /// 根据属性名找到对应的DataLink
- /// </summary>
- /// <param name="propertyName"></param>
- /// <returns></returns>
- 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();
- }
- /// <summary>
- /// 2022-9-10,为简单控件设置默认的DataLink
- /// </summary>
- 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);
- }
- /// <summary>
- /// 2022-9-10,为复杂控件设置默认的刷新索引链接(refreshLink)
- /// </summary>
- 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);
- }
- /// <summary>
- /// 获取控件的item
- /// </summary>
- /// <returns></returns>
- QGraphicsProxyWidget* VControlObject::getItem()
- {
- return m_pProxyWidget;
- }
- ///// <summary>
- ///// 根据属性名设置对应的DataLink
- ///// </summary>
- ///// <param name="strPropertyName"></param>
- ///// <param name="dataLink"></param>
- ///// <returns></returns>
- //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;
- //}
|