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/nmDataOutline.cpp

599 lines
20 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 "nmDataOutline.h"
#include "nmDataJsonTools.h"
#include <QtAlgorithms>
// Constructor
nmDataOutline::nmDataOutline()
: m_bSyncingGeometryAttributes(false)
, m_sName()
, m_vecPts()
, m_vFlowTypeList()
, m_eOutlineType(NM_Rect_Outline_Type)
, m_ptCenter()
, m_dRadius(0.0)
, m_bPlotVisible(true)
, m_pen(QBrush(Qt::black), 0.0)
, m_bHasPenStyle(false)
, m_clrBackgrd(QColor(0, 0, 255))
, m_nBackgrdAlpha(0)
, m_bHasVisualProps(false)
{
initGeometryAttributes();
}
nmDataOutline::nmDataOutline(const nmDataOutline& other)
: m_bSyncingGeometryAttributes(false) {
initGeometryAttributes();
*this = other;
}
nmDataOutline& nmDataOutline::operator=(const nmDataOutline& other) {
if (this != &other) {
m_dRadius = other.m_dRadius;
m_vecPts = other.m_vecPts;
m_vFlowTypeList = other.m_vFlowTypeList;
m_eOutlineType = other.m_eOutlineType;
m_ptCenter = other.m_ptCenter;
m_sName = other.m_sName;
m_bPlotVisible = other.m_bPlotVisible;
m_pen = other.m_pen;
m_bHasPenStyle = other.m_bHasPenStyle;
m_clrBackgrd = other.m_clrBackgrd;
m_nBackgrdAlpha = other.m_nBackgrdAlpha;
m_bHasVisualProps = other.m_bHasVisualProps;
syncGeometryAttributes();
}
return *this;
}
// Destructor
nmDataOutline::~nmDataOutline() {
clearPointAttributes();
}
// 序列化 nmDataOutline 为 RapidJSON Value
rapidjson::Value nmDataOutline::ToJsonValue(rapidjson::Document::AllocatorType& allocator) const
{
// 创建一个 RapidJSON 对象类型的值
rapidjson::Value outlineObject(rapidjson::kObjectType);
// 与 FromJsonValue 的 UTF-8 读取一致,保留用户手动填写的中文名称。
const QByteArray nameUtf8 = m_sName.toUtf8();
outlineObject.AddMember("OutlineName", rapidjson::Value(nameUtf8.constData(),
static_cast<rapidjson::SizeType>(nameUtf8.size()), allocator).Move(), allocator);
// 序列化点
rapidjson::Value pointsArray(rapidjson::kArrayType); // 创建一个 RapidJSON 数组类型的值
// 遍历点列表
foreach (const QPointF& point , m_vecPts) {
rapidjson::Value pointObject(rapidjson::kObjectType); // 创建一个点对象
pointObject.AddMember("x", point.x(), allocator); // 添加x坐标
pointObject.AddMember("y", point.y(), allocator); // 添加y坐标
pointsArray.PushBack(pointObject, allocator); // 将点对象推入数组
}
outlineObject.AddMember("OutlinePoints", pointsArray, allocator); // 将点数组添加到JSON对象
// 边界压力类型列表
rapidjson::Value flowArry(rapidjson::kArrayType);
foreach (int type, getFlowTypeList()) {
flowArry.PushBack(type, allocator);
}
outlineObject.AddMember("FlowTypeList", flowArry, allocator);
// 边界类型(枚举转 int)
outlineObject.AddMember("OutlineType", static_cast<int>(m_eOutlineType), allocator);
// 如果是圆形,写入 Center 和 Radius
if (m_eOutlineType == NM_Data_Outline_Type::NM_Round_Outline_Type) {
rapidjson::Value cen(rapidjson::kObjectType);
cen.AddMember("x", m_ptCenter.x(), allocator);
cen.AddMember("y", m_ptCenter.y(), allocator);
outlineObject.AddMember("Center", cen, allocator);
outlineObject.AddMember("Radius", m_dRadius, allocator);
}
// 序列化图元可见性 (m_bPlotVisible)
outlineObject.AddMember("PlotVisible", m_bPlotVisible, allocator); // 使用m_bPlotVisible
// 序列化视觉属性
// 未打开Map时,边界只有几何数据,视觉属性尚未由图元初始化。
// 必须显式保存该状态,避免加载时把默认的0线宽误当成有效样式。
outlineObject.AddMember("HasVisualProps", m_bHasVisualProps, allocator);
outlineObject.AddMember("PenWidth", m_pen.widthF(), allocator);
if (m_bHasVisualProps && m_bHasPenStyle) {
outlineObject.AddMember("PenColor", static_cast<unsigned>(m_pen.color().rgba()), allocator);
outlineObject.AddMember("PenStyle", static_cast<int>(m_pen.style()), allocator);
if (m_pen.style() == Qt::CustomDashLine) {
rapidjson::Value pattern(rapidjson::kArrayType);
foreach (qreal length, m_pen.dashPattern()) {
pattern.PushBack(length, allocator);
}
outlineObject.AddMember("PenDashPattern", pattern, allocator);
outlineObject.AddMember("PenDashOffset", m_pen.dashOffset(), allocator);
}
}
outlineObject.AddMember("BackgrdR", m_clrBackgrd.red(), allocator);
outlineObject.AddMember("BackgrdG", m_clrBackgrd.green(), allocator);
outlineObject.AddMember("BackgrdB", m_clrBackgrd.blue(), allocator);
outlineObject.AddMember("BackgrdAlpha", m_nBackgrdAlpha, allocator);
return outlineObject; // 返回序列化后的 RapidJSON Value
}
// 从 RapidJSON Value 反序列化数据到 nmDataOutline
void nmDataOutline::FromJsonValue(const rapidjson::Value& jsonValue)
{
// 名称
if (jsonValue.HasMember("OutlineName") && jsonValue["OutlineName"].IsString()) {
m_sName = QString::fromUtf8(jsonValue["OutlineName"].GetString());
}
// 反序列化点
if (jsonValue.HasMember("OutlinePoints") && jsonValue["OutlinePoints"].IsArray()) {
m_vecPts.clear(); // 清空当前点列表,使用m_vecPts
const rapidjson::Value& pointsArray = jsonValue["OutlinePoints"]; // 获取点数组节点
// 遍历数组,将每个点对象反序列化到 m_vecPts
for (rapidjson::SizeType i = 0; i < pointsArray.Size(); ++i) {
if (pointsArray[i].IsObject() &&
pointsArray[i].HasMember("x") && pointsArray[i]["x"].IsDouble() &&
pointsArray[i].HasMember("y") && pointsArray[i]["y"].IsDouble()) {
m_vecPts.append(QPointF(pointsArray[i]["x"].GetDouble(), pointsArray[i]["y"].GetDouble()));
}
}
}
// 缺失或无法读取的类型按封闭处理,保留后续类型对应的边序号。
m_vFlowTypeList.clear();
if (jsonValue.HasMember("FlowTypeList") && jsonValue["FlowTypeList"].IsArray()) {
const rapidjson::Value& flowArr = jsonValue["FlowTypeList"];
for (rapidjson::SizeType i = 0; i < flowArr.Size(); ++i) {
m_vFlowTypeList.append(flowArr[i].IsInt() ? flowArr[i].GetInt() : OIL_Reservoir_Outline_No_Flow);
}
}
// 枚举类型
if (jsonValue.HasMember("OutlineType") && jsonValue["OutlineType"].IsInt()) {
m_eOutlineType = static_cast<NM_Data_Outline_Type>(jsonValue["OutlineType"].GetInt());
}
// 圆形专属字段
if (m_eOutlineType == NM_Data_Outline_Type::NM_Round_Outline_Type) {
if (jsonValue.HasMember("Center") && jsonValue["Center"].IsObject()) {
const rapidjson::Value& c = jsonValue["Center"];
if (c.HasMember("x") && c["x"].IsDouble() && c.HasMember("y") && c["y"].IsDouble()) {
m_ptCenter = QPointF( c["x"].GetDouble(), c["y"].GetDouble());
}
}
if (jsonValue.HasMember("Radius") && jsonValue["Radius"].IsNumber()) {
m_dRadius = jsonValue["Radius"].GetDouble();
}
}
// 反序列化图元可见性 (m_bPlotVisible)
if (jsonValue.HasMember("PlotVisible") && jsonValue["PlotVisible"].IsBool()) {
m_bPlotVisible = jsonValue["PlotVisible"].GetBool(); // 使用m_bPlotVisible
}
m_pen = QPen(QBrush(Qt::black), 0.0);
m_bHasPenStyle = false;
bool bVisualFieldsComplete = jsonValue.HasMember("PenWidth") &&
jsonValue["PenWidth"].IsNumber() && jsonValue["PenWidth"].GetDouble() >= 0.0;
if (bVisualFieldsComplete) {
m_pen.setWidthF(jsonValue["PenWidth"].GetDouble());
}
if (jsonValue.HasMember("PenColor") && jsonValue["PenColor"].IsUint() &&
jsonValue.HasMember("PenStyle") && jsonValue["PenStyle"].IsInt()) {
const int style = jsonValue["PenStyle"].GetInt();
if (style >= Qt::NoPen && style <= Qt::CustomDashLine) {
m_pen.setColor(QColor::fromRgba(jsonValue["PenColor"].GetUint()));
m_pen.setStyle(static_cast<Qt::PenStyle>(style));
m_bHasPenStyle = true;
}
if (style == Qt::CustomDashLine && jsonValue.HasMember("PenDashPattern") &&
jsonValue["PenDashPattern"].IsArray()) {
const rapidjson::Value& pattern = jsonValue["PenDashPattern"];
QVector<qreal> lengths;
for (rapidjson::SizeType i = 0; i < pattern.Size(); ++i) {
if (!pattern[i].IsNumber() || pattern[i].GetDouble() <= 0.0) break;
lengths.append(pattern[i].GetDouble());
}
if (!lengths.isEmpty() && lengths.size() % 2 == 0 &&
static_cast<rapidjson::SizeType>(lengths.size()) == pattern.Size()) {
m_pen.setDashPattern(lengths);
if (jsonValue.HasMember("PenDashOffset") && jsonValue["PenDashOffset"].IsNumber()) {
m_pen.setDashOffset(jsonValue["PenDashOffset"].GetDouble());
}
}
}
}
int r = 0, g = 0, b = 255;
if (jsonValue.HasMember("BackgrdR") && jsonValue["BackgrdR"].IsInt()) {
r = jsonValue["BackgrdR"].GetInt();
} else {
bVisualFieldsComplete = false;
}
if (jsonValue.HasMember("BackgrdG") && jsonValue["BackgrdG"].IsInt()) {
g = jsonValue["BackgrdG"].GetInt();
} else {
bVisualFieldsComplete = false;
}
if (jsonValue.HasMember("BackgrdB") && jsonValue["BackgrdB"].IsInt()) {
b = jsonValue["BackgrdB"].GetInt();
} else {
bVisualFieldsComplete = false;
}
m_clrBackgrd = QColor(r, g, b);
if (jsonValue.HasMember("BackgrdAlpha") && jsonValue["BackgrdAlpha"].IsInt()) {
m_nBackgrdAlpha = jsonValue["BackgrdAlpha"].GetInt();
} else {
bVisualFieldsComplete = false;
}
const bool bSavedVisualProps =
jsonValue.HasMember("HasVisualProps") &&
jsonValue["HasVisualProps"].IsBool()
? jsonValue["HasVisualProps"].GetBool()
: true;
// 旧成果的零线宽代表样式尚未初始化。新成果有画笔样式时允许零线宽。
m_bHasVisualProps = bSavedVisualProps && bVisualFieldsComplete &&
(m_bHasPenStyle || m_pen.widthF() > 0.0);
syncGeometryAttributes();
}
// Getter and Setter for m_sName
QString nmDataOutline::getName() {
return m_sName;
}
void nmDataOutline::setName(const QString& sName) {
m_sName = sName;
}
// Getter and Setter for m_vecPts
QVector<QPointF> nmDataOutline::getOutlinePoints() {
return m_vecPts;
}
void nmDataOutline::setOutlinePoints(const QVector<QPointF>& vecPts) {
m_vecPts = vecPts;
syncPointAttributes();
if (m_eOutlineType == NM_Rect_Outline_Type) {
syncRectAttributes();
} else if (m_eOutlineType == NM_Round_Outline_Type) {
syncRoundAttributes();
}
}
int nmDataOutline::getOutlinePointCount() const {
return m_vecPts.count();
}
nmDataAttribute* nmDataOutline::getPointXAttribute(int nIndex) const {
if (nIndex < 0 || nIndex >= m_vPointXAttributes.count()) return nullptr;
return m_vPointXAttributes[nIndex];
}
nmDataAttribute* nmDataOutline::getPointYAttribute(int nIndex) const {
if (nIndex < 0 || nIndex >= m_vPointYAttributes.count()) return nullptr;
return m_vPointYAttributes[nIndex];
}
nmDataAttribute& nmDataOutline::getLeftAttribute() {
return m_left;
}
nmDataAttribute& nmDataOutline::getRightAttribute() {
return m_right;
}
nmDataAttribute& nmDataOutline::getTopAttribute() {
return m_top;
}
nmDataAttribute& nmDataOutline::getBottomAttribute() {
return m_bottom;
}
nmDataAttribute& nmDataOutline::getCenterXAttribute() {
return m_centerX;
}
nmDataAttribute& nmDataOutline::getCenterYAttribute() {
return m_centerY;
}
nmDataAttribute& nmDataOutline::getRadiusAttribute() {
return m_radius;
}
QVector<int> nmDataOutline::getFlowTypeList() const {
// 旧成果可能未记录所有边的类型。读取和保存使用相同的默认值。
int edgeCount = 0;
switch (m_eOutlineType) {
case NM_Rect_Outline_Type:
edgeCount = 4;
break;
case NM_Round_Outline_Type:
edgeCount = 1;
break;
case NM_Polygon_Outline_Type:
edgeCount = m_vecPts.size();
break;
default:
break;
}
QVector<int> types = m_vFlowTypeList;
while (types.size() < edgeCount) {
types.append(OIL_Reservoir_Outline_No_Flow);
}
return types;
}
void nmDataOutline::setFlowTypeList(const QVector<int>& vecFlowTypeList) {
m_vFlowTypeList = vecFlowTypeList;
}
// Getter for m_eOutlineType
NM_Data_Outline_Type nmDataOutline::getOutlineType() const
{
return m_eOutlineType;
}
// Setter for m_eOutlineType
void nmDataOutline::setOutlineType(NM_Data_Outline_Type eOutlineType)
{
m_eOutlineType = eOutlineType;
syncGeometryAttributes();
}
// Getter for m_ptCenter
QPointF nmDataOutline::getCenter() const
{
return m_ptCenter;
}
// Setter for m_ptCenter
void nmDataOutline::setCenter(const QPointF& ptCenter)
{
m_ptCenter = ptCenter;
syncRoundAttributes();
}
// Getter for m_dRadius
double nmDataOutline::getRadius() const
{
return m_dRadius;
}
// Setter for m_dRadius
void nmDataOutline::setRadius(double dRadius)
{
m_dRadius = dRadius;
syncRoundAttributes();
}
bool nmDataOutline::getPlotVisible() const{
return m_bPlotVisible;
}
void nmDataOutline::setPlotVisible(const bool newState) {
m_bPlotVisible = newState;
}
// 视觉属性 getter/setter
QPen nmDataOutline::getPen(const QPen& defaultPen) const
{
if (m_bHasPenStyle) return m_pen;
QPen pen = defaultPen;
pen.setWidthF(m_pen.widthF());
return pen;
}
void nmDataOutline::setPen(const QPen& pen)
{
m_pen = pen;
m_bHasPenStyle = true;
}
QColor nmDataOutline::getBackgrdColor() const {
return m_clrBackgrd;
}
void nmDataOutline::setBackgrdColor(const QColor& clr) {
m_clrBackgrd = clr;
}
int nmDataOutline::getBackgrdAlpha() const {
return m_nBackgrdAlpha;
}
void nmDataOutline::setBackgrdAlpha(int nAlpha) {
m_nBackgrdAlpha = nAlpha;
}
bool nmDataOutline::hasVisualProps() const {
return m_bHasVisualProps;
}
void nmDataOutline::setVisualPropsValid(bool bValid) {
m_bHasVisualProps = bValid;
}
// 固定几何属性作为参数面板适配层,实际持久化数据仍保存在原有字段中。
void nmDataOutline::initGeometryAttributes()
{
QStringList lengthUnits;
lengthUnits << "m" << "cm" << "mm" << "in" << "0.1 in" << "ft" << "mile" << "km";
m_left = nmDataAttribute("Boundary left X", 0.0, "m", UNIT_TYPE_LENGTH, QStringList(), lengthUnits);
m_right = nmDataAttribute("Boundary right X", 0.0, "m", UNIT_TYPE_LENGTH, QStringList(), lengthUnits);
m_top = nmDataAttribute("Boundary top Y", 0.0, "m", UNIT_TYPE_LENGTH, QStringList(), lengthUnits);
m_bottom = nmDataAttribute("Boundary bottom Y", 0.0, "m", UNIT_TYPE_LENGTH, QStringList(), lengthUnits);
m_centerX = nmDataAttribute("Boundary center X", 0.0, "m", UNIT_TYPE_LENGTH, QStringList(), lengthUnits);
m_centerY = nmDataAttribute("Boundary center Y", 0.0, "m", UNIT_TYPE_LENGTH, QStringList(), lengthUnits);
m_radius = nmDataAttribute("Boundary radius", 0.0, "m", UNIT_TYPE_LENGTH, QStringList(), lengthUnits);
connect(&m_left, SIGNAL(sigValueChanged()), this, SLOT(onGeometryAttributeChanged()), Qt::UniqueConnection);
connect(&m_right, SIGNAL(sigValueChanged()), this, SLOT(onGeometryAttributeChanged()), Qt::UniqueConnection);
connect(&m_top, SIGNAL(sigValueChanged()), this, SLOT(onGeometryAttributeChanged()), Qt::UniqueConnection);
connect(&m_bottom, SIGNAL(sigValueChanged()), this, SLOT(onGeometryAttributeChanged()), Qt::UniqueConnection);
connect(&m_centerX, SIGNAL(sigValueChanged()), this, SLOT(onGeometryAttributeChanged()), Qt::UniqueConnection);
connect(&m_centerY, SIGNAL(sigValueChanged()), this, SLOT(onGeometryAttributeChanged()), Qt::UniqueConnection);
connect(&m_radius, SIGNAL(sigValueChanged()), this, SLOT(onGeometryAttributeChanged()), Qt::UniqueConnection);
}
void nmDataOutline::clearPointAttributes()
{
qDeleteAll(m_vPointXAttributes);
qDeleteAll(m_vPointYAttributes);
m_vPointXAttributes.clear();
m_vPointYAttributes.clear();
}
// 根据边界类型,将原有几何数据同步到对应的面板绑定属性。
void nmDataOutline::syncGeometryAttributes()
{
syncPointAttributes();
if (m_eOutlineType == NM_Rect_Outline_Type) {
syncRectAttributes();
} else if (m_eOutlineType == NM_Round_Outline_Type) {
syncRoundAttributes();
}
}
void nmDataOutline::syncPointAttributes()
{
// 矩形和圆形使用固定几何属性,只有多边形需要逐点属性。
if (m_eOutlineType != NM_Polygon_Outline_Type) {
if (!m_vPointXAttributes.isEmpty() || !m_vPointYAttributes.isEmpty()) {
clearPointAttributes();
}
return;
}
if (m_vPointXAttributes.count() != m_vecPts.count() ||
m_vPointYAttributes.count() != m_vecPts.count()) {
clearPointAttributes();
QStringList lengthUnits;
lengthUnits << "m" << "cm" << "mm" << "in" << "0.1 in" << "ft" << "mile" << "km";
for (int i = 0; i < m_vecPts.count(); ++i) {
nmDataAttribute* pX = new nmDataAttribute(QString("Boundary point %1 X").arg(i + 1),
m_vecPts[i].x(), "m", UNIT_TYPE_LENGTH,
QStringList(), lengthUnits);
nmDataAttribute* pY = new nmDataAttribute(QString("Boundary point %1 Y").arg(i + 1),
m_vecPts[i].y(), "m", UNIT_TYPE_LENGTH,
QStringList(), lengthUnits);
pX->setParent(this);
pY->setParent(this);
connect(pX, SIGNAL(sigValueChanged()), this, SLOT(onGeometryAttributeChanged()), Qt::UniqueConnection);
connect(pY, SIGNAL(sigValueChanged()), this, SLOT(onGeometryAttributeChanged()), Qt::UniqueConnection);
m_vPointXAttributes.append(pX);
m_vPointYAttributes.append(pY);
}
}
m_bSyncingGeometryAttributes = true;
for (int i = 0; i < m_vecPts.count(); ++i) {
if (m_vPointXAttributes[i]->getValue().toDouble() != m_vecPts[i].x()) {
m_vPointXAttributes[i]->setValue(m_vecPts[i].x());
}
if (m_vPointYAttributes[i]->getValue().toDouble() != m_vecPts[i].y()) {
m_vPointYAttributes[i]->setValue(m_vecPts[i].y());
}
}
m_bSyncingGeometryAttributes = false;
}
void nmDataOutline::syncRectAttributes()
{
if (m_vecPts.isEmpty()) return;
double left = m_vecPts.first().x();
double right = left;
double top = m_vecPts.first().y();
double bottom = top;
foreach (const QPointF& pt, m_vecPts) {
left = qMin(left, pt.x());
right = qMax(right, pt.x());
top = qMax(top, pt.y());
bottom = qMin(bottom, pt.y());
}
m_bSyncingGeometryAttributes = true;
if (m_left.getValue().toDouble() != left) m_left.setValue(left);
if (m_right.getValue().toDouble() != right) m_right.setValue(right);
if (m_top.getValue().toDouble() != top) m_top.setValue(top);
if (m_bottom.getValue().toDouble() != bottom) m_bottom.setValue(bottom);
m_bSyncingGeometryAttributes = false;
}
void nmDataOutline::syncRoundAttributes()
{
m_bSyncingGeometryAttributes = true;
if (m_centerX.getValue().toDouble() != m_ptCenter.x()) m_centerX.setValue(m_ptCenter.x());
if (m_centerY.getValue().toDouble() != m_ptCenter.y()) m_centerY.setValue(m_ptCenter.y());
if (m_radius.getValue().toDouble() != m_dRadius) m_radius.setValue(m_dRadius);
m_bSyncingGeometryAttributes = false;
}
void nmDataOutline::updateRectPointsFromAttributes()
{
const double left = m_left.getValue().toDouble();
const double right = m_right.getValue().toDouble();
const double top = m_top.getValue().toDouble();
const double bottom = m_bottom.getValue().toDouble();
m_vecPts.clear();
m_vecPts << QPointF(left, top)
<< QPointF(right, top)
<< QPointF(right, bottom)
<< QPointF(left, bottom);
syncPointAttributes();
}
void nmDataOutline::updateRoundPointsFromAttributes()
{
const QPointF center(m_centerX.getValue().toDouble(), m_centerY.getValue().toDouble());
const double radius = m_radius.getValue().toDouble();
m_ptCenter = center;
m_dRadius = radius;
m_vecPts.clear();
m_vecPts << center
<< QPointF(radius, radius)
<< QPointF(center.x(), center.y() - radius)
<< QPointF(center.x() + radius, center.y())
<< QPointF(center.x(), center.y() + radius)
<< QPointF(center.x() - radius, center.y());
syncPointAttributes();
}
// 参数面板修改几何属性后,回写边界数据并通知 Binder 更新图元。
void nmDataOutline::onGeometryAttributeChanged()
{
if (m_bSyncingGeometryAttributes) return;
QObject* pSender = sender();
if (pSender == &m_left || pSender == &m_right ||
pSender == &m_top || pSender == &m_bottom) {
updateRectPointsFromAttributes();
} else if (pSender == &m_centerX || pSender == &m_centerY || pSender == &m_radius) {
updateRoundPointsFromAttributes();
} else {
for (int i = 0; i < m_vecPts.count(); ++i) {
if (pSender == m_vPointXAttributes.value(i, nullptr)) {
m_vecPts[i].setX(m_vPointXAttributes[i]->getValue().toDouble());
break;
}
if (pSender == m_vPointYAttributes.value(i, nullptr)) {
m_vecPts[i].setY(m_vPointYAttributes[i]->getValue().toDouble());
break;
}
}
}
emit sigOutlineDataChanged();
}