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.
nmWTAI-Platform/Src/nmNum/nmData/nmDataLayer.cpp

181 lines
5.5 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 "nmDataLayer.h"
#include <QByteArray>
nmDataLayer::nmDataLayer()
{
m_dTopDepth = 0.0;
m_dBottomDepth = 0.0;
m_dThickness = 0.0;
m_bLegacyGap = false;
}
nmDataLayer::nmDataLayer(const nmDataLayer &other)
{
// 基类保持独立的 QObject 身份,只拷贝层业务字段;不能复制另一对象的父子关系和连接。
m_sName = other.m_sName;
m_dTopDepth = other.m_dTopDepth;
m_dBottomDepth = other.m_dBottomDepth;
m_dThickness = other.m_dThickness;
m_bLegacyGap = other.m_bLegacyGap;
m_clrLayer = other.m_clrLayer;
}
nmDataLayer::~nmDataLayer()
{
}
nmDataLayer &nmDataLayer::operator = (const nmDataLayer &other)
{
// 赋值不替换当前对象本身,已有观察者仍引用同一个 QObject;几何检查由上层统一执行。
m_sName = other.m_sName;
m_dTopDepth = other.m_dTopDepth;
m_dBottomDepth = other.m_dBottomDepth;
m_dThickness = other.m_dThickness;
m_bLegacyGap = other.m_bLegacyGap;
m_clrLayer = other.m_clrLayer;
return *this;
}
// 序列化 nmDataLayer 为 RapidJSON Value
rapidjson::Value nmDataLayer::ToJsonValue(rapidjson::Document::AllocatorType& allocator) const
{
// 创建一个 RapidJSON 对象类型的值
rapidjson::Value layObject(rapidjson::kObjectType); // 单层 JSON 对象,名称、几何及旧格式标志共同保存。
// 显式提供 UTF-8 字节长度,并让分配器复制字符串,避免保存临时 QByteArray 的悬空地址。
const QByteArray name = m_sName.toUtf8(); // 层名称的 UTF-8 字节缓冲,写入 JSON 时复制到分配器。
layObject.AddMember("name", rapidjson::Value(name.constData(),
static_cast<rapidjson::SizeType>(name.size()), allocator), allocator);
layObject.AddMember("top", m_dTopDepth, allocator);
layObject.AddMember("bottom", m_dBottomDepth, allocator);
layObject.AddMember("thickness", m_dThickness, allocator);
layObject.AddMember("isChecked", m_bLegacyGap, allocator);
// 颜色序列化为一个子对象,包含 r/g/b/a
rapidjson::Value colorObject(rapidjson::kObjectType); // 与旧工程格式一致的 RGBA 颜色子对象。
colorObject.AddMember("r", m_clrLayer.red(), allocator);
colorObject.AddMember("g", m_clrLayer.green(), allocator);
colorObject.AddMember("b", m_clrLayer.blue(), allocator);
colorObject.AddMember("a", m_clrLayer.alpha(), allocator);
layObject.AddMember("color", colorObject, allocator);
return layObject;
}
// 从 RapidJSON Value 反序列化数据到 nmDataLayer
void nmDataLayer::FromJsonValue(const rapidjson::Value& jsonValue)
{
if (!jsonValue.IsObject()) return;
// 兼容旧工程没有 name 字段的情况,不继承之前对象残留的名称。
m_sName = jsonValue.HasMember("name") && jsonValue["name"].IsString()
? QString::fromUtf8(jsonValue["name"].GetString(), jsonValue["name"].GetStringLength())
: QString();
// 数值字段按旧文件原样读入,不根据厚度反算底深,防止加载时抹掉历史边界信息。
if (jsonValue.HasMember("top") && jsonValue["top"].IsNumber())
{
m_dTopDepth = jsonValue["top"].GetDouble();
}
if (jsonValue.HasMember("bottom") && jsonValue["bottom"].IsNumber())
{
m_dBottomDepth = jsonValue["bottom"].GetDouble();
}
if (jsonValue.HasMember("thickness") && jsonValue["thickness"].IsNumber())
{
m_dThickness = jsonValue["thickness"].GetDouble();
}
if (jsonValue.HasMember("isChecked") && jsonValue["isChecked"].IsBool())
{
m_bLegacyGap = jsonValue["isChecked"].GetBool();
}
// 反序列化颜色
if (jsonValue.HasMember("color") && jsonValue["color"].IsObject())
{
const rapidjson::Value& colorValue = jsonValue["color"]; // 借用输入 JSON 中的颜色子对象。
int r = 0, g = 0, b = 0, a = 255; // RGBA 通道值;缺少通道时沿用不透明黑色的相应默认分量。
if (colorValue.HasMember("r") && colorValue["r"].IsInt())
r = colorValue["r"].GetInt();
if (colorValue.HasMember("g") && colorValue["g"].IsInt())
g = colorValue["g"].GetInt();
if (colorValue.HasMember("b") && colorValue["b"].IsInt())
b = colorValue["b"].GetInt();
if (colorValue.HasMember("a") && colorValue["a"].IsInt())
a = colorValue["a"].GetInt();
m_clrLayer = QColor(r, g, b, a);
}
}
QString nmDataLayer::type() const
{
// 继续使用原有类型标识和表名,新增层名称字段不改变工程中既有的数据类型映射。
return nmDataModelType::sTypeNumDataLayer;
}
QString nmDataLayer::getTableName()
{
return nmDataTableName::sTableNumDataLayer;
}
void nmDataLayer::setTop(double value)
{
m_dTopDepth = value;
}
void nmDataLayer::setName(const QString& value)
{
m_sName = value;
}
QString nmDataLayer::getName() const
{
return m_sName;
}
double nmDataLayer::getTop() const
{
return m_dTopDepth;
}
void nmDataLayer::setBottom(double value)
{
m_dBottomDepth = value;
}
double nmDataLayer::getBottom() const
{
return m_dBottomDepth;
}
void nmDataLayer::setThickness(double value)
{
m_dThickness = value;
}
double nmDataLayer::getThickness() const
{
return m_dThickness;
}
void nmDataLayer::setIsChecked(bool value)
{
m_bLegacyGap = value;
}
bool nmDataLayer::getIsChecked() const
{
return m_bLegacyGap;
}
void nmDataLayer::setColor(const QColor& value)
{
m_clrLayer = value;
}
QColor nmDataLayer::getColor() const
{
return m_clrLayer;
}