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
heatconfigurationundo.h
Go to the documentation of this file.
1
10#pragma once
11
12#include <openswmm/engine/openswmm_heat.h>
13#include <QPointer>
14#include <QObject>
15#include <QUndoCommand>
16#include <QString>
17#include <functional>
18#include <memory>
19
20namespace OpenSWMMVis
21{
22
28 void operator()(void *snapshot) const { swmm_heat_snapshot_destroy(snapshot); }
29};
30using HeatSnapshot = std::unique_ptr<void, HeatSnapshotDeleter>;
31
37 void operator()(void *snapshot) const { swmm_config_snapshot_destroy(snapshot); }
38};
41using ConfigSnapshot = std::unique_ptr<void, ConfigSnapshotDeleter>;
42
43// Exact values and authoring flags are restored together. Refuse undo rather
44// than overwriting edits made outside this command's history.
45template <class Snapshot, int (*Restore)(SWMM_Engine, void *)>
46class EngineSnapshotUndo : public QUndoCommand
47{
48public:
64 EngineSnapshotUndo(const QString &text, QObject *owner, SWMM_Engine engine, Snapshot before,
65 Snapshot after, QString beforeIdentity, QString afterIdentity,
66 std::function<SWMM_Engine()> current,
67 std::function<QString(SWMM_Engine)> identity, std::function<void()> changed,
68 std::function<void(QString)> failed)
69 : owner_(owner), engine_(engine), before_(std::move(before)), after_(std::move(after)),
70 beforeIdentity_(std::move(beforeIdentity)), afterIdentity_(std::move(afterIdentity)),
71 current_(std::move(current)), identity_(std::move(identity)),
72 changed_(std::move(changed)), failed_(std::move(failed))
73 {
74 setText(text);
75 }
76
81 void undo() override { restore(false); }
85 void redo() override
86 {
87 if (first_) {
88 first_ = false;
89 return;
90 }
91 restore(true);
92 }
93
94private:
100 void restore(bool after)
101 {
102 if (!owner_ || !current_ || current_() != engine_ ||
103 identity_(engine_) != (after ? beforeIdentity_ : afterIdentity_) ||
104 Restore(engine_, after ? after_.get() : before_.get()) != SWMM_OK) {
105 setObsolete(true);
106 if (failed_)
107 failed_(
108 QObject::tr(
109 "%1: the settings changed outside Undo, or the simulation is initialized. The saved edit was not restored.")
110 .arg(text()));
111 return;
112 }
113 if (changed_)
114 changed_();
115 }
116 QPointer<QObject> owner_;
117 SWMM_Engine engine_;
118 Snapshot before_, after_;
119 QString beforeIdentity_, afterIdentity_;
120 std::function<SWMM_Engine()> current_;
121 std::function<QString(SWMM_Engine)> identity_;
122 std::function<void()> changed_;
123 std::function<void(QString)> failed_;
124 bool first_ = true;
125};
126
128 : public EngineSnapshotUndo<HeatSnapshot, swmm_heat_snapshot_restore>
129{
130public:
145 HeatSnapshot after, QString beforeIdentity, QString afterIdentity,
146 std::function<SWMM_Engine()> current,
147 std::function<QString(SWMM_Engine)> identity,
148 std::function<void()> changed, std::function<void(QString)> failed)
149 : EngineSnapshotUndo(QObject::tr("Edit heat configuration"), owner, engine,
150 std::move(before), std::move(after), std::move(beforeIdentity),
151 std::move(afterIdentity), std::move(current), std::move(identity),
152 std::move(changed), std::move(failed))
153 {
154 }
155};
156
158
159} // namespace OpenSWMMVis
const QList< QColor > &(* get)()
Definition categoricalpalette.cpp:177
Definition heatconfigurationundo.h:47
void undo() override
Restore the prior snapshot only when owner/engine/current identity still match the command's applied ...
Definition heatconfigurationundo.h:81
EngineSnapshotUndo(const QString &text, QObject *owner, SWMM_Engine engine, Snapshot before, Snapshot after, QString beforeIdentity, QString afterIdentity, std::function< SWMM_Engine()> current, std::function< QString(SWMM_Engine)> identity, std::function< void()> changed, std::function< void(QString)> failed)
Own exact before/after snapshots for an already-applied engine edit.
Definition heatconfigurationundo.h:64
void redo() override
Skip the initial QUndoStack push, otherwise restore the applied snapshot under the same conflict chec...
Definition heatconfigurationundo.h:85
Definition heatconfigurationundo.h:129
HeatConfigurationUndo(QObject *owner, SWMM_Engine engine, HeatSnapshot before, HeatSnapshot after, QString beforeIdentity, QString afterIdentity, std::function< SWMM_Engine()> current, std::function< QString(SWMM_Engine)> identity, std::function< void()> changed, std::function< void(QString)> failed)
Specialize exact engine snapshot undo for an already-applied heat edit with a translated default labe...
Definition heatconfigurationundo.h:144
bool failed
Suppress repeated exceptions until an explicit refresh.
Definition extensionattributes.cpp:38
std::function< void(bool)> changed
Guarded layer refresh callback.
Definition extensionattributes.cpp:36
std::vector< int > owner
containing cell per pixel (-1 = outside)
Definition mesh2dresultsexport.cpp:766
Definition heatassignment.h:15
std::unique_ptr< void, ConfigSnapshotDeleter > ConfigSnapshot
Definition heatconfigurationundo.h:41
std::unique_ptr< void, HeatSnapshotDeleter > HeatSnapshot
Definition heatconfigurationundo.h:30
void * SWMM_Engine
Definition objectdefaultsapplier.h:23
QString text
Definition queryparser.cpp:54
Definition heatconfigurationundo.h:32
void operator()(void *snapshot) const
Release a uniquely owned configuration snapshot through the engine API.
Definition heatconfigurationundo.h:37
Definition heatconfigurationundo.h:23
void operator()(void *snapshot) const
Release a uniquely owned heat snapshot through the engine API.
Definition heatconfigurationundo.h:28