SWMMVis  6.0.0-alpha.4
Qt6/C++ GIS-based graphical user interface for the SWMMVis engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
heatpropertyedit.h
Go to the documentation of this file.
1
10#pragma once
11#include "ui/heatproperties.h"
16#include <QUndoStack>
17#include <QMessageBox>
18#include <QVariant>
19#include <QVector>
20
21namespace OpenSWMMVis
22{
24 int attr;
25 int scope;
26 QString name;
27 QVariant value;
28};
37inline QVariant authoredHeatProperty(SWMM_Engine engine, int attr, int scope, const QString &name)
38{
39 int count = 0;
40 if (swmm_heat_element_override_count(engine, &count) != SWMM_OK)
41 return {};
42 for (int i = 0; i < count; ++i) {
43 int a = -1, s = 0;
44 double value = 0;
45 char id[1024]{};
46 if (swmm_heat_get_element_override(engine, i, &a, &s, id, sizeof id, &value) == SWMM_OK &&
47 a == attr && s == scope && QString::fromUtf8(id) == name)
48 return value;
49 }
50 return {};
51}
52// The raster route, attribute table and feature import share the engine row
53// store, persistence registration and exact undo contract.
64inline bool applyHeatPropertyEdits(SWMMModelLayer *layer, const QVector<HeatPropertyEdit> &edits,
65 QUndoStack *stack, const QString &description, QString *error)
66{
67 if (!layer || !layer->engine()) {
68 if (error)
69 *error = QObject::tr("The model was closed.");
70 return false;
71 }
72 const auto engine = layer->engine();
73 QVector<HeatPropertyEdit> changed;
74 for (const auto &edit : edits) {
75 const QString text = edit.value.toString().trimmed();
76 bool ok = false;
77 const double value = text.toDouble(&ok);
78 if (!text.isEmpty() && (!ok || !heatPropertyValueValid(edit.attr, value))) {
79 if (error)
80 *error =
81 QObject::tr("Invalid %1 for %2.").arg(heatPropertyLabel(edit.attr), edit.name);
82 return false;
83 }
84 const auto before = authoredHeatProperty(engine, edit.attr, edit.scope, edit.name);
85 if ((text.isEmpty() && !before.isValid()) ||
86 (!text.isEmpty() && before.isValid() && before.toDouble() == value))
87 continue;
88 changed.append(edit);
89 }
90 if (changed.isEmpty())
91 return true;
92 HeatSnapshot before(swmm_heat_snapshot_create(engine));
93 if (!before) {
94 if (error)
95 *error = QObject::tr("Cannot snapshot heat settings before editing.");
96 return false;
97 }
98 const auto identity = HeatConfigDialog::configurationSignature(engine);
99 struct Context {
100 SWMM_Engine engine;
101 const QVector<HeatPropertyEdit> *edits;
102 } context{engine, &changed};
103 const int result = swmm_hydrothermal_edit_atomic(
104 engine,
105 [](SWMM_Engine, void *ptr) -> int {
106 auto &ctx = *static_cast<Context *>(ptr);
107 for (const auto &edit : *ctx.edits) {
108 const auto name = edit.name.toUtf8();
109 const auto text = edit.value.toString().trimmed();
110 if (text.isEmpty()) {
111 int count = 0;
112 int rc = swmm_heat_element_override_count(ctx.engine, &count);
113 if (rc != SWMM_OK)
114 return rc;
115 for (int i = count - 1; i >= 0; --i) {
116 int a = -1, s = 0;
117 char id[1024]{};
118 rc = swmm_heat_get_element_override(ctx.engine, i, &a, &s, id, sizeof id,
119 nullptr);
120 if (rc != SWMM_OK)
121 return rc;
122 if (a == edit.attr && s == edit.scope &&
123 edit.name == QString::fromUtf8(id)) {
124 rc = swmm_heat_remove_element_override(ctx.engine, i);
125 if (rc != SWMM_OK)
126 return rc;
127 }
128 }
129 } else {
130 const int rc = swmm_heat_set_element_override(
131 ctx.engine, edit.attr, edit.scope, name.constData(), text.toDouble());
132 if (rc != SWMM_OK)
133 return rc;
134 }
135 }
137 },
138 &context);
139 if (result != SWMM_OK) {
140 if (error)
141 *error =
142 QObject::tr("Heat properties were refused (%1). Previous settings were restored.")
143 .arg(result);
144 return false;
145 }
146 HeatSnapshot after(swmm_heat_snapshot_create(engine));
147 if (!after) {
148 swmm_heat_snapshot_restore(engine, before.get());
149 if (error)
150 *error = QObject::tr("Cannot record the edit. Previous settings were restored.");
151 return false;
152 }
153 QPointer<SWMMModelLayer> model(layer);
154 auto notify = [model] {
155 if (model) {
156 model->markEdited();
157 emit model->optionsChanged({"HEAT_PROPERTIES"});
158 }
159 };
160 if (stack) {
161 auto *command = new HeatConfigurationUndo(
162 layer, engine, std::move(before), std::move(after), identity,
164 [model] { return model ? model->engine() : nullptr; },
166 [](const QString &why) {
167 QMessageBox::warning(nullptr, QObject::tr("Heat properties"), why);
168 });
169 command->setText(description);
170 stack->push(command);
171 }
172 notify();
173 return true;
174}
175}
static QString configurationSignature(SWMM_Engine engine)
Serialize the saved heat configuration and selected model identities for stale-editor detection.
Definition heatconfigdialog.cpp:2352
Definition heatconfigurationundo.h:129
Renders the SWMM network elements (nodes, links, subcatchments, rain gages) for one OpenSWMMEngine mo...
Definition swmmmodellayer.h:137
size_t i
Definition contourjob.cpp:30
QPointer< QObject > context
Required provider lifetime guard.
Definition extensionattributes.cpp:31
std::function< void(bool)> changed
Guarded layer refresh callback.
Definition extensionattributes.cpp:36
ModelFacade * model
Definition extensionmodelaccess.cpp:625
Editor for the heat-transport configuration — [HEAT_SOURCES] inlet temperatures, [HEAT_FLUXES] module...
Undoable edits to heat, climate, snow, and component configuration.
Shared thermal attribute identifiers for tables and import mapping.
Persistent configuration of heat components and their owners.
bool ok
Definition importplanning.cpp:143
int scope
Definition initialbedqualitydialog.cpp:50
double s
Scene px per model length unit.
Definition inletdrawingview.cpp:95
int ensureHeatConfigurationFile(SWMM_Engine engine)
Ensure configured heat authoring has a registered persistent component asset name.
Definition hydrothermalconfiguration.h:28
Definition heatassignment.h:15
bool heatPropertyValueValid(int attr, double value)
Apply shared refuse-instead-of-clamp validation for thermal overrides.
Definition heatproperties.h:87
QString heatPropertyLabel(int attr)
Translate a thermal property's shared field caption.
Definition heatproperties.h:65
bool applyHeatPropertyEdits(SWMMModelLayer *layer, const QVector< HeatPropertyEdit > &edits, QUndoStack *stack, const QString &description, QString *error)
Validate and atomically apply explicit thermal overrides, register persistence and optionally retain ...
Definition heatpropertyedit.h:64
QVariant authoredHeatProperty(SWMM_Engine engine, int attr, int scope, const QString &name)
Find the first explicit thermal override matching attribute/scope/exact name.
Definition heatpropertyedit.h:37
std::unique_ptr< void, HeatSnapshotDeleter > HeatSnapshot
Definition heatconfigurationundo.h:30
void * SWMM_Engine
Definition objectdefaultsapplier.h:23
QString text
Definition queryparser.cpp:54
double value
Definition rasterfluxintegration.cpp:27
Definition heatpropertyedit.h:23
int attr
Definition heatpropertyedit.h:24
int scope
Definition heatpropertyedit.h:25
QVariant value
Definition heatpropertyedit.h:27
QString name
Definition heatpropertyedit.h:26
Map layer that renders an OpenSWMMEngine network (nodes, links, subcatchments, rain gages) and provid...
double a
Definition terrainerrorfield.cpp:42
double error
Definition terrainerrorfield.cpp:42
quint64 count
Definition terrainerrorfield.cpp:43
TraceAnalysisLayer * layer
Definition traceanalysislayer.cpp:162