You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
AppFlow/CFDStruct/CUIProperty/CUIConfig.cpp

238 lines
6.7 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "CUIConfig.h"
#include "CUISigsCenter.h"
#include <QDebug>
#include <QTimer>
#include "CUIDefine.h"
/**
* @brief CUIConfig::CUIConfig 构造函数
* @param property 组件的属性
*/
CUIConfig::CUIConfig(QMap<QString, QVariant> property) {
setDefault();
this->property = property;
this->m_sigsCenter = CUISigsCenter::getInstance();
}
/**
* @brief CUIConfig::CUIConfig 构造函数
*
* @details {key:val},{CUIConfig*}
*
* map : 存当前组件的配置信息
*
* vector : 存当前组件的子组件的配置信息
*
* {"type",""}:类型选择可选Widget, GroupBox, LineEdit, TabWidget, ComboBox, Item, PushButton
*
* @subsection LineEdit 单行输入框
* - "name" (string) : 组件名称,显示与对应的label
* - "data_type" (string) : 组件处理的数据类型,可选"string" "int" "double"
* - "initial_value_" (string) : 初始值
* - "check_range_" ("true"/"false") : 是否有检查范围
* - "range_min_" (int/double) : 范围最小值,存储格式为string,自动转换为对应类型的值
* - "range_max_" (int/double) : 范围最大值,存储格式为string,自动转换为对应类型的值
* - "inclusive_" ("true"/"false") : 是否包含最大最小值
* - "required_" ("true"/"false") : 该组件信息是否必填
*
* @subsection Widget 小组件
* - "layout" (string):布局类型
* - "name" (string):组件名称,用于TabWidget对单个Tab进行命名
*
* @subsection GroupBox 组件组
* - "name" : 组名
* - "layout" : 布局名
* - sub : 子组件组
*
* @subsection TabWidget 标签页
* - sub : 子组件组,需有"name"属性作为标签名
*
* @subsection ComboBox 下拉框
* - "name" : 组件名称,与label中显示
* - sub : 子项目组,其"type"只能是"Item"
*
* @subsubsection Item 下拉框的项目
* - "name" : 项目名
* - "data_type" : 项目携带数据类型 "string"(default) "int" "double"
* - "data" : 项目携带数据
*
* @subsection PushButton 按钮
* - "name" : 按钮名
*
* @subsection CheckBox 正方形选择按钮
* - "name" : 组件名
*
* @subsection ButtonBox 单选组
* 继承groupbox配置buttongroup多选一
* - "name" : 组名
* - sub : 按钮
*
* @subsection RadioButton 圆形选择按钮
* - "name" : 组件名
*
* @param property 组件的属性
* @param sub 该组件的子组件配置的列表
*/
CUIConfig::CUIConfig(QMap<QString, QVariant> property, QVector<CUIConfig*> sub) {
setDefault();
this->property = property;
this->sub = sub;
this->m_sigsCenter = CUISigsCenter::getInstance();
}
/**
* @brief CUIConfig::printConfig 打印当前配置信息
*/
void CUIConfig::printConfig() {
// qDebug() << "{";
// for(auto it = property.begin(); it != property.end(); it++) {
// qDebug() << it.key() << ' ' << it.value();
// }
// for(auto it : sub) {
// it->printConfig();
// }
// qDebug() << "}";
}
QString CUIConfig::getOriginValueString() {
QVariant pValue = property["value_origin"];
// 如果没有设置原始值
if (pValue == QVariant::Invalid) {
return QString("");
}
if (property["value_type"] == CUI_DATA_TYPE_INT) {
int* ptr = qvariant_cast<int*>(pValue);
return QString::number(*ptr);
} else if (property["value_type"] == CUI_DATA_TYPE_DOUBLE) {
double* ptr = qvariant_cast<double*>(pValue);
return QString::number(*ptr);
} else if (property["value_type"] == CUI_DATA_TYPE_STRING) {
QString* ptr = qvariant_cast<QString*>(pValue);
return *ptr;
}
return QString("");
}
void CUIConfig::setValue(QVariant value) {
bool changed = false;
QVariant pValue = property["value_origin"];
if (property["value_type"] == CUI_DATA_TYPE_INT) {
int* ptr = qvariant_cast<int*>(pValue);
if (*ptr != value.toInt()) {
*ptr = value.toInt();
changed = true;
}
} else if (property["value_type"] == CUI_DATA_TYPE_DOUBLE) {
double* ptr = qvariant_cast<double*>(pValue);
if (*ptr != value.toDouble()) {
*ptr = value.toDouble();
changed = true;
}
} else if (property["value_type"] == CUI_DATA_TYPE_STRING) {
QString* ptr = qvariant_cast<QString*>(pValue);
if (*ptr != value.toString()) {
*ptr = value.toString();
changed = true;
}
}
// 如果需要通知,则通知参数值发生修改
if (changed && this->getPropertyOriginValue("semaphore") != QVariant::Invalid) {
QTimer::singleShot(50, m_sigsCenter, [ = ]() {
int semaphore = this->getPropertyOriginValue("semaphore").toInt();
emit m_sigsCenter->sig_cuiPropertyChanged(semaphore);
});
}
}
/**
* @brief CUIConfig::getPropertyValue 获取属性值
* @param key 属性名
* @return
*/
QString CUIConfig::getPropertyValue(QString key) {
if (property.contains(key)) {
return qvariant_cast<QString>(property[key]);
}
return QString();
}
/**
* @brief CUIConfig::getPropertyOriginValue 获取属性地址
* @param key 属性名
* @return
*/
QVariant CUIConfig::getPropertyOriginValue(QString key) {
if (property.contains(key)) {
return property[key];
}
return QVariant::Invalid;
}
QVector<CUIConfig*> CUIConfig::getSub() {
return this->sub;
}
/**
* @brief CUIConfig::setDefault 初始化属性
*/
void CUIConfig::setDefault() {
QString type = qvariant_cast<QString>(property["type"]);
if (type == "LineEdit") {
setDefault("name", "");
setDefault("data_type", "string"); // 数据类型
setDefault("initial_value_", ""); // 初始值
setDefault("check_range_", false); // 是否检查范围
setDefault("range_min_", ""); // 最小值
setDefault("range_max_", ""); // 最大值
setDefault("inclusive_min", true); // 左范围是否包括
setDefault("inclusive_max", true); // 右范围是否包括
setDefault("required_", true); // 是否为必选
} else if (type == "Widget") {
setDefault("layout", "QVBoxLayout");
setDefault("name", "");
} else if (type == "GroupBox") {
setDefault("name", "");
setDefault("layout", "QVBoxLayout");
} else if (type == "TabWidget") {
} else if (type == "ComboBox") {
setDefault("name", "");
} else if (type == "Item") {
setDefault("name", "");
setDefault("data_type", "string");
setDefault("data", "");
}
}
/**
* @brief CUIConfig::setDefault 设置某项属性
* @param key
* @param val
*/
void CUIConfig::setDefault(QString key, QVariant val) {
if (!property.contains(key)) {
property[key] = val;
}
}