SILICON
Loading...
Searching...
No Matches
undoCommands.hpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include <QByteArray>
21#include <QPointF>
22#include <QUndoCommand>
23#include <functional>
24#include <map>
25#include <string>
26#include <vector>
27
28#include <nlohmann/json.hpp>
29
30#include <core/component.hpp>
35
36namespace SILICON::ui {
37 using namespace SILICON::core;
38
39 class GraphicalItem;
42 class DiagramScene;
43
44 class WireTopologyCommand : public QUndoCommand {
45 public:
46 enum class Operation { Add, Remove };
56 const std::vector<GraphicalWireSegment*>& segments,
58 QUndoCommand* parent = nullptr);
60 void undo() override;
62 void redo() override;
63
64 private:
65 struct Snapshot {
66 uint64_t uiId;
67 uint64_t routeId;
69 QPointF position;
70 std::vector<QPointF> points;
71 };
72
76 void apply(bool restore);
78 std::string documentPath;
79 std::vector<Snapshot> snapshots;
82 };
83
85 class CallbackUndoCommand : public QUndoCommand {
86 public:
87 using Callback = std::function<void()>;
88
90 QUndoCommand* parent = nullptr);
91
92 void undo() override;
93 void redo() override;
94
95 private:
98 };
99
100 class MetadataEditCommand : public QUndoCommand {
101 public:
102 using ApplyFn = std::function<void(const std::string&)>;
103
104 MetadataEditCommand(QString text, std::string oldValue, std::string newValue,
105 ApplyFn apply, QUndoCommand* parent = nullptr);
106 void undo() override;
107 void redo() override;
108
109 private:
110 std::string oldValue;
111 std::string newValue;
113 };
114
115 class MoveItemCommand : public QUndoCommand {
116 public:
117 explicit MoveItemCommand(QUndoCommand* parent = nullptr) : QUndoCommand(parent)
118 {
119 setText("Move Item(s)");
120 }
121
122 void addItemMove(GraphicalItem* item, const QPointF& oldPos, const QPointF& newPos);
124 void setWireMoves(std::vector<WireRouteChange> changes)
125 {
126 wireMoves = std::move(changes);
127 }
128
129 void setTopologyEdit(WireEndpointEdit edit) { topologyEdit = std::move(edit); }
130
131 void undo() override;
132 void redo() override;
133
134 private:
135 DiagramScene* scene = nullptr;
136 std::vector<ItemPositionChange> moves;
137 std::vector<WireRouteChange> wireMoves;
139 std::string documentPath;
140 bool skipInitialRedo = true;
141 };
142
143 class EditWireRouteCommand : public QUndoCommand {
144 public:
155 std::vector<WireRouteChange> changes,
157 QUndoCommand* parent = nullptr);
158
160 void undo() override;
162 void redo() override;
163
164 private:
166 std::string documentPath;
167 std::vector<WireRouteChange> changes;
169 bool skipInitialRedo = true;
170 };
171
172 class RotateItemCommand : public QUndoCommand {
173 public:
174 explicit RotateItemCommand(GraphicalComponent* component, qreal oldRotation,
175 qreal newRotation, QUndoCommand* parent = nullptr);
176
177 void undo() override;
178 void redo() override;
179
180 private:
182 std::string documentPath;
183 uint64_t uiId;
186 bool skipInitialRedo = true;
187 };
188
189 class ResizeComponentCommand : public QUndoCommand {
190 public:
193 void undo() override;
194 void redo() override;
195
196 private:
197 void apply(const ComponentResizeState& state);
199 std::string documentPath;
200 uint64_t uiId;
203 bool skipInitialRedo = true;
204 };
205
206 class ModifyPropertyCommand : public QUndoCommand {
207 public:
208 explicit ModifyPropertyCommand(std::string key, QUndoCommand* parent = nullptr);
209
210 void addPropertyChange(GraphicalComponent* component, const PropertyValue& oldValue,
211 const PropertyValue& newValue);
212 [[nodiscard]] bool isEmpty() const { return changes.empty(); }
213
214 void undo() override;
215 void redo() override;
216
217 private:
224
225 void apply(bool useNewValue);
226
227 std::string key;
228 std::string documentPath;
229 std::vector<PropertyChange> changes;
230 };
231
232 class SceneSelectionCommand : public QUndoCommand {
233 public:
234 enum class Operation { Add, Remove };
235
236 SceneSelectionCommand(DiagramScene* scene, const nlohmann::json& payload,
237 Operation operation, bool skipInitialRedo = false,
238 QUndoCommand* parent = nullptr);
239
240 void undo() override;
241 void redo() override;
242
243 private:
244 [[nodiscard]] nlohmann::json payload() const;
245 static QByteArray encodePayload(const nlohmann::json& payload);
246 static nlohmann::json decodePayload(const QByteArray& payload);
247 static QPointF payloadOrigin(const nlohmann::json& payload);
248
250 std::string documentPath;
251 QByteArray bsonPayload;
254 };
255
256} // namespace SILICON::ui
CallbackUndoCommand(QString text, Callback undoCallback, Callback redoCallback, QUndoCommand *parent=nullptr)
std::function< void()> Callback
Graphics scene for the circuit diagram editor.
void undo() override
Restores the wire routes from before the gesture.
void redo() override
Restores the gesture's final routes, skipping the first invocation.
std::vector< WireRouteChange > changes
EditWireRouteCommand(GraphicalWireSegment *segment, std::vector< WireRouteChange > changes, WireDragKind kind, WireEndpointEdit endpointEdit={}, QUndoCommand *parent=nullptr)
Captures an already applied wire gesture for undo and redo.
Base class for graphical circuit components.
Base class for all graphical items in the diagram.
Base class for logic-gate graphical components.
The visible, selectable polyline wire on the diagram scene.
std::function< void(const std::string &)> ApplyFn
MetadataEditCommand(QString text, std::string oldValue, std::string newValue, ApplyFn apply, QUndoCommand *parent=nullptr)
ModifyPropertyCommand(std::string key, QUndoCommand *parent=nullptr)
std::vector< PropertyChange > changes
void addPropertyChange(GraphicalComponent *component, const PropertyValue &oldValue, const PropertyValue &newValue)
std::vector< WireRouteChange > wireMoves
MoveItemCommand(QUndoCommand *parent=nullptr)
void addItemMove(GraphicalItem *item, const QPointF &oldPos, const QPointF &newPos)
void setTopologyEdit(WireEndpointEdit edit)
Stores topology and net snapshots accompanying the item moves.
void setWireMoves(std::vector< WireRouteChange > changes)
Stores before and after wire routes accompanying the item moves.
std::vector< ItemPositionChange > moves
ResizeComponentCommand(GraphicalComponent *component, ComponentResizeState before, ComponentResizeState after)
void apply(const ComponentResizeState &state)
RotateItemCommand(GraphicalComponent *component, qreal oldRotation, qreal newRotation, QUndoCommand *parent=nullptr)
static QByteArray encodePayload(const nlohmann::json &payload)
SceneSelectionCommand(DiagramScene *scene, const nlohmann::json &payload, Operation operation, bool skipInitialRedo=false, QUndoCommand *parent=nullptr)
static nlohmann::json decodePayload(const QByteArray &payload)
static QPointF payloadOrigin(const nlohmann::json &payload)
WireTopologyCommand(DiagramScene *scene, const std::vector< GraphicalWireSegment * > &segments, Operation operation, bool skipInitialRedo=true, QUndoCommand *parent=nullptr)
Captures segments for undoing or redoing wire addition or removal.
void undo() override
Reverses the captured wire addition or removal.
std::vector< Snapshot > snapshots
void apply(bool restore)
Restores captured segments or removes them from their original document.
void redo() override
Reapplies the operation, optionally skipping the first invocation.
std::variant< int, bool, std::string, BusValue > PropertyValue
Variant type for component properties.