#pragma once
#include "Common.h"
// 各个列的编号
// 标准模式
#define GVL_COL_INDEX 0
#define GVL_COL_NAME 1
#define GVL_COL_TYPE 2
#define GVL_COL_VALUE 3
#define GVL_COL_SERIALIZED 4
#define GVL_COL_COMMENT 5
// DB模式
#define DB_COL_INDEX 0
#define DB_COL_NAME 1
#define DB_COL_TYPE 2
#define DB_COL_VALUE 3
#define DB_COL_ADDRESS 4
#define DB_COL_ACCESS 5
#define DB_COL_COMMENT 6
// 默认值名字
#define DEFAULT_VALUE_0 "DEFAULT_VALUE_0"
#define DEFAULT_VALUE_NULLSTRING "DEFAULT_VALUE_NULLSTRING"
///
/// 用于管理变量的类(包括全局变量和局部变量)
/// 每一个组都类似于一个命名空间,不同命名空间的是可以重名的
/// 局部变量也可以和全局变量重名,只要不在一个命名空间即可
///
class Document;
class GvlManager : public QObject
{
Q_OBJECT;
// 单例模式
private:
GvlManager();
public:
// 获取实例
static GvlManager* getInstance()
{
if (m_pGvlManager == nullptr)
{
m_pGvlManager = new GvlManager();
}
return m_pGvlManager;
}
public:
// 添加新的变量组
GVL* addNewGroup(const QString& strGroup,
TOOL_TYPE varType,
GVL_MODE gvlMode
);
// 添加新的变量
bool addNewVariable(
const QString& strGroup,
VARIABLE* pNewVar
);
// 2022-3-8添加新的默认变量(和正常变量分开存储)
bool addNewDefaultVariable(VARIABLE* pNewVar);
// 绑定UI和变量
void bindUIAndVariable(QTableWidget* pTable, QString& strVarFullName);
// 获取全部的变量
GVLS* getAllGvls()
{
return &m_Gvls;
}
// 2022-9-1 获取全部带有Event触发特性的变量
QHash getAllEventVariables();
// 获取需要序列化的分组(跳过系统内置变量)
QList getAllSerializedGvls();
// 获取指定的变量分组(含全局和局部变量)
GVL* getGvl(const QString& strGroupName)
{
return m_Gvls.value(strGroupName);
}
//// 获取指定分组中的序列化变量(含全局和局部变量)
//QVector getSerializedVariablesByGroup(const QString& strGroupName);
// 根据名字获取指定变量
VARIABLE* getVariableByName(const QString& strGroupName, const QString& strVarName);
// 根据名字获取指定的默认值
const VARIABLE* getDefaultValueByName(const QString& strValueName) const;
// 获取本分组的变量数
int getVariablesCountByGroup(const QString& strGroupName);
// 删除指定变量分组
void removeGvl(const QString& strGroupName);
// 数据结构重置
void reset();
// 删除一个变量(并释放内存)
void delVariable(const QString& strGroup, const int& nIndex);
// 移除一个变量(无需释放内存)
void removeVariable(const QString& strGroup, const int& nIndex);
// 移除一个变量(无需释放内存)
void removeVariable(const QString& strGroup, const QString& strName);
// 变量交换顺序
void exchangeVariable(const QString& strGroup, const int& nIndex1, const int& nIndex2);
// 2022-3-2 绑定硬件组态指针和DB数据表
void bindHdwToolAndDB(TOOL* pTool, const QString& strDBGroup);
// 2022-3-8 解绑硬件组态和指针
// (此处只是添加了函数,但是尚未调用,需要额外加一下,在删除硬件工具和删除DB数据表的时候都需要调用)
void unbindHdwToolAndDB(TOOL* pTool, const QString& strDBGroup);
// 根据DB的名字找到对应的Dll指针
TOOL* getHdwToolByDbName(const QString& strDbGroup)
{
return m_DBToTool.value(strDbGroup);
}
//// 更新序号
//void updateIndex(const QString& strGroup, const int& nIndex, const QString& newValue);
//// 更新变量名字
//void updateName(const QString& strGroup, const int& nIndex, const QString& newName);
// 更新持久化状态
void updatePersit(const QString& strGroup, const int& nIndex, bool newValue);
// 更新Value
bool updateValue(const QString& strGroup, const int& nIndex, const QString& newValue);
// 更新Comment
bool updateComment(const QString& strGroup, const int& nIndex, const QString& newComment);
// 更新通讯地址(仅DB模式)
bool updateDbCommAddress(const QString& strGroup, const int& nIndex, const QString& newAddress);
// 检查是否与本组变量重名
bool isDuplicated(const QString& strGroup, const QString& newName);
// 检查用户输入的变量类型和数值是否匹配
bool isValueValid(const QString& newType, const QString& newValue);
bool isValueValid(const VALUE_TYPE newType, const QString& newValue);
// 获取指定变量的引用计数
int getRefCount(const QString& strGroup, const int& nIndex)
{
return m_Gvls[strGroup]->Variables[nIndex]->nRefCount;
}
// 本分组是否是全局变量分组
bool isGlobalGvl(const QString& strGroup);
// 本分组是否是硬件变量分组
bool isHardwareGvl(const QString& strGroup);
// 本分组是否是Pou局部变量分组
bool isLocalGvl(const QString& strGroup);
// 输出一个全局变量的详细信息
void debugVariable(const VARIABLE* var);
// 输出目前所有的全局变量信息
void debugAllVariables();
// 数据结构序列化
// friend QDataStream& operator<<(QDataStream& out, const GvlManager& w);
// 数据结构反序列化
// friend QDataStream& operator>>(QDataStream& in, GvlManager& w);
protected slots:
// 2021-11-13 接收表格中的变量刷新显示Event
virtual void customEvent(QEvent* event) override;
private:
// 将变量同步更新到Table中
void syncValueToTable(const QString& strFullName, const QString& strValueString);
// 2022-2-15 创建内置的全局变量组(System)
void initSystemGvl();
// 2022-2-20 绑定SYSTEM全局变量和类中的关键变量
void bindSystemGvl();
// 2022-3-7 创建默认值的变量组(DefaultValue)
// (目前只有复杂控件的DataLink在使用,本组变量是只读的,不允许修改)
void initDefaultValues();
//// 设置变量数值的模板函数
//template
//void setValue(T value, VARIABLE_VALUE& varValue);
//// 检查变量是否已经Valid
//void updateValueValid(
// const QString& strGroup,
// const int& nIndex,
// const QString& newType,
// const QString& newValue
//);
//// 输出一个全局变量的详细信息(For Debug)
//void debugVariable(const VARIABLE* var);
//// 输出目前所有的全局变量信息
//void debugAllVariables();
private:
// 变量集合(Key为分组的名字,Value为本分组所有的变量信息,按照序号排列)
GVLS m_Gvls;
// 默认值的变量集合(为了和正常的变量区分开,单独分组)
GVL* m_defaultValueGvl;
// 变量名称与界面指针对应关系(用于变量数据同步在表格中的更新)
QMap m_GvlTables;
static GvlManager* m_pGvlManager;
// 2022-3-8 增加,保存DB和Dll指针的对应关系,用于DB数据变动后及时通知Dll工具
QMap m_DBToTool;
};