wiRedPanda
Logic Circuit Simulator
Loading...
Searching...
No Matches
Commands.h
Go to the documentation of this file.
1// Copyright 2015 - 2026, GIBIS-UNIFESP and the wiRedPanda contributors
2// SPDX-License-Identifier: GPL-3.0-or-later
3
7
8#pragma once
9
10#include <QCoreApplication>
11
13#include "App/Scene/Scene.h"
14
15class Connection;
16
17namespace CommandUtils {
18 // Element/Connection lookup (scene-scoped)
19 GraphicElement *findElm(Scene *scene, const int id);
20 Connection *findConn(Scene *scene, const int id);
21 const QList<GraphicElement *> findElements(Scene *scene, const QList<int> &ids);
22 const QList<QGraphicsItem *> findItems(Scene *scene, const QList<int> &ids);
23
24 // Serialization helpers
25 const QList<QGraphicsItem *> loadItems(Scene *scene, QByteArray &itemData, const QList<int> &ids, QList<int> &otherIds);
26 const QList<QGraphicsItem *> loadList(const QList<QGraphicsItem *> &items, QList<int> &ids, QList<int> &otherIds);
27 void saveItems(Scene *scene, QByteArray &itemData, const QList<QGraphicsItem *> &items, const QList<int> &otherIds);
28
29 // Scene operations
30 void addItems(Scene *scene, const QList<QGraphicsItem *> &items);
31 void deleteItems(Scene *scene, const QList<QGraphicsItem *> &items);
32
34 void drainPortConnections(GraphicElement *elm, int fromPort, int toPort,
35 bool isInput, QDataStream &stream, Scene *scene);
36
37 // ID management
38 void storeIds(const QList<QGraphicsItem *> &items, QList<int> &ids);
39 void storeOtherIds(const QList<QGraphicsItem *> &connections, const QList<int> &ids, QList<int> &otherIds);
40}
41
50class ElementsCommand : public QUndoCommand
51{
52protected:
53 explicit ElementsCommand(const QList<GraphicElement *> &elements, Scene *scene, QUndoCommand *parent = nullptr);
54
56 QList<GraphicElement *> elements() const;
57
59 QList<int> m_ids;
60};
61
66class AddItemsCommand : public QUndoCommand
67{
68 Q_DECLARE_TR_FUNCTIONS(AddItemsCommand)
69
70public:
77 explicit AddItemsCommand(const QList<QGraphicsItem *> &items, Scene *scene, QUndoCommand *parent = nullptr);
78
80 void redo() override;
81
83 void undo() override;
84
85private:
86 // --- Members ---
87 QByteArray m_itemData;
88 QList<int> m_ids;
89 QList<int> m_otherIds;
90 Scene *m_scene;
91};
92
97class DeleteItemsCommand : public QUndoCommand
98{
99 Q_DECLARE_TR_FUNCTIONS(DeleteItemsCommand)
100
101public:
108 explicit DeleteItemsCommand(const QList<QGraphicsItem *> &items, Scene *scene, QUndoCommand *parent = nullptr);
109
111 void redo() override;
112
114 void undo() override;
115
116private:
117 // --- Members ---
118 QByteArray m_itemData;
119 QList<int> m_ids;
120 QList<int> m_otherIds;
121 Scene *m_scene;
122};
123
129{
130 Q_DECLARE_TR_FUNCTIONS(RotateCommand)
131
132public:
140 explicit RotateCommand(const QList<GraphicElement *> &items, const int angle, Scene *scene, QUndoCommand *parent = nullptr);
141
143 void redo() override;
144
146 void undo() override;
147
148private:
150 int m_angle;
151
152 QList<QPointF> m_positions;
153};
154
160{
161 Q_DECLARE_TR_FUNCTIONS(MoveCommand)
162
163public:
171 explicit MoveCommand(const QList<GraphicElement *> &list, const QList<QPointF> &oldPositions, Scene *scene, QUndoCommand *parent = nullptr);
172
174 void redo() override;
175
177 void undo() override;
178
179private:
180 // --- Members ---
181 QList<QPointF> m_newPositions;
182 QList<QPointF> m_oldPositions;
183};
184
193{
194 Q_DECLARE_TR_FUNCTIONS(UpdateCommand)
195
196public:
204 explicit UpdateCommand(const QList<GraphicElement *> &elements, const QByteArray &oldData, Scene *scene, QUndoCommand *parent = nullptr);
205
207 void redo() override;
208
210 void undo() override;
211
212private:
213 // --- Helpers ---
214 void loadData(QByteArray &itemData);
215
218 struct WirelessState {
219 GraphicElement *element = nullptr;
220 WirelessMode mode = WirelessMode::None;
221 QString label;
222 };
224 QVector<WirelessState> snapshotWirelessState() const;
227 static bool wirelessStateDiffers(const QVector<WirelessState> &before,
228 const QVector<WirelessState> &after);
229
233 void refreshRuntimeState();
234
235 // --- Members ---
236 QByteArray m_newData;
237 QByteArray m_oldData;
242 bool m_wirelessTopologyChange = false;
243};
244
249class SplitCommand : public QUndoCommand
250{
251 Q_DECLARE_TR_FUNCTIONS(SplitCommand)
252
253public:
261 explicit SplitCommand(Connection *conn, QPointF mousePos, Scene *scene, QUndoCommand *parent = nullptr);
262
264 void undo() override;
265
267 void redo() override;
268
269private:
270 // --- Members ---
271 QPointF m_nodePos;
272 Scene *m_scene;
273 int m_c1Id;
274 int m_c2Id;
275 int m_elm1Id;
276 int m_elm2Id;
277 int m_nodeAngle;
278 int m_nodeId;
279};
280
286{
287 Q_DECLARE_TR_FUNCTIONS(MorphCommand)
288
289public:
297 explicit MorphCommand(const QList<GraphicElement *> &elements, ElementType type, Scene *scene, QUndoCommand *parent = nullptr);
298
300 void redo() override;
301
303 void undo() override;
304
305private:
306 // --- Helpers ---
307
309 struct DeletedConnectionInfo {
310 int connectionId;
311 int morphedElementId;
312 int portIndex;
313 bool isInput;
314 int otherElementId;
315 int otherPortIndex;
316 };
317
322 void transferConnections(const QList<GraphicElement *> &from, const QList<GraphicElement *> &to,
323 QList<DeletedConnectionInfo> *deleted = nullptr);
324
332 void transferPortConnections(GraphicElement *oldElm, GraphicElement *newElm,
333 bool isInput, QList<DeletedConnectionInfo> *deleted);
334
338 void restoreDeletedConnections(const QList<DeletedConnectionInfo> &deleted);
339
340 // --- Members ---
341 ElementType m_newType;
342 QList<ElementType> m_types;
343 QList<DeletedConnectionInfo> m_deletedConnections;
344 QList<DeletedConnectionInfo> m_deletedConnectionsOnUndo;
345};
346
352{
353 Q_DECLARE_TR_FUNCTIONS(ChangePortSizeCommand)
354
355public:
364 explicit ChangePortSizeCommand(const QList<GraphicElement *> &elements, const int newPortSize, Scene *scene, const bool isInput, QUndoCommand *parent = nullptr);
365
367 void redo() override;
368
370 void undo() override;
371
372private:
373 // --- Members ---
374 QByteArray m_oldData;
375 QList<int> m_order;
376 int m_newPortSize;
377 bool m_isInput;
378};
379
385{
386 Q_DECLARE_TR_FUNCTIONS(FlipCommand)
387
388public:
396 explicit FlipCommand(const QList<GraphicElement *> &items, const int axis, Scene *scene, QUndoCommand *parent = nullptr);
397
399 void redo() override;
400
402 void undo() override;
403
404private:
405 // --- Members ---
406 QList<QPointF> m_positions;
407 QPointF m_maxPos;
408 QPointF m_minPos;
409 int m_axis;
410};
411
416class ToggleTruthTableOutputCommand : public QUndoCommand
417{
418 Q_DECLARE_TR_FUNCTIONS(ToggleTruthTableOutputCommand)
419
420public:
428 explicit ToggleTruthTableOutputCommand(GraphicElement *element, int pos, Scene *scene, QUndoCommand *parent = nullptr);
429
431 void redo() override;
432
434 void undo() override;
435
436private:
437 Scene *m_scene;
438 int m_id;
439 int m_pos;
440};
441
449class RegisterBlobCommand : public QUndoCommand
450{
451 Q_DECLARE_TR_FUNCTIONS(RegisterBlobCommand)
452
453public:
454 RegisterBlobCommand(const QString &blobName, const QByteArray &data, Scene *scene, QUndoCommand *parent = nullptr);
455
456 void redo() override;
457 void undo() override;
458
459private:
460 QString m_blobName;
461 QByteArray m_data;
462 Scene *m_scene;
463};
464
473class RemoveBlobCommand : public QUndoCommand
474{
475 Q_DECLARE_TR_FUNCTIONS(RemoveBlobCommand)
476
477public:
478 RemoveBlobCommand(const QString &blobName, Scene *scene, QUndoCommand *parent = nullptr);
479
480 void redo() override;
481 void undo() override;
482
483private:
484 QString m_blobName;
485 QByteArray m_data;
486 Scene *m_scene;
487};
488
497class RenameBlobCommand : public QUndoCommand
498{
499 Q_DECLARE_TR_FUNCTIONS(RenameBlobCommand)
500
501public:
502 RenameBlobCommand(const QString &oldName, const QString &newName, Scene *scene, QUndoCommand *parent = nullptr);
503
504 void redo() override;
505 void undo() override;
506
507private:
508 QString m_oldName;
509 QString m_newName;
510 Scene *m_scene;
511};
512
521{
522 Q_DECLARE_TR_FUNCTIONS(UpdateBlobCommand)
523
524public:
533
534 explicit UpdateBlobCommand(const QList<GraphicElement *> &elements, const QByteArray &oldData,
535 const QList<ConnectionInfo> &connections, Scene *scene, QUndoCommand *parent = nullptr);
536
538 static QList<ConnectionInfo> captureConnections(const QList<GraphicElement *> &targets);
539
541 void setOldBlob(const QByteArray &blob) { m_oldBlob = blob; }
542
544 void setBlobName(const QString &name) { m_blobName = name; }
545
546 void redo() override;
547 void undo() override;
548
549private:
550 void loadData(QByteArray &itemData);
551 void reconnectConnections();
552
553 QByteArray m_oldData;
554 QByteArray m_newData;
555 QByteArray m_oldBlob;
556 QByteArray m_newBlob;
557 QString m_blobName;
558 QList<ConnectionInfo> m_connections;
559};
Enums::ElementType ElementType
Definition Enums.h:107
Enums::WirelessMode WirelessMode
Definition Enums.h:109
Abstract base class for all graphical circuit elements.
Main circuit editing scene with undo/redo and user interaction.
void undo() override
Removes the items from the scene.
Definition Commands.cpp:286
void redo() override
Adds the items to the scene.
Definition Commands.cpp:296
AddItemsCommand(const QList< QGraphicsItem * > &items, Scene *scene, QUndoCommand *parent=nullptr)
Constructs the command for adding items.
Definition Commands.cpp:270
void undo() override
Restores the old port count and connections.
void redo() override
Applies the new port count.
Definition Commands.cpp:968
ChangePortSizeCommand(const QList< GraphicElement * > &elements, const int newPortSize, Scene *scene, const bool isInput, QUndoCommand *parent=nullptr)
Constructs the command.
Definition Commands.cpp:959
A bezier-curve wire connecting an output port to an input port in the scene.
Definition Connection.h:38
DeleteItemsCommand(const QList< QGraphicsItem * > &items, Scene *scene, QUndoCommand *parent=nullptr)
Constructs the command for deleting items.
Definition Commands.cpp:304
void undo() override
Restores the items to the scene.
Definition Commands.cpp:312
void redo() override
Removes the items from the scene.
Definition Commands.cpp:320
QList< int > m_ids
Definition Commands.h:59
ElementsCommand(const QList< GraphicElement * > &elements, Scene *scene, QUndoCommand *parent=nullptr)
Definition Commands.cpp:255
Scene * m_scene
Definition Commands.h:58
QList< GraphicElement * > elements() const
Returns the live element pointers for this command's targets, looked up by stored ID.
Definition Commands.cpp:265
FlipCommand(const QList< GraphicElement * > &items, const int axis, Scene *scene, QUndoCommand *parent=nullptr)
Constructs the flip command.
Definition Commands.cpp:897
void redo() override
Applies the flip transformation.
Definition Commands.cpp:935
void undo() override
Reverses the flip transformation (involution: flip twice = identity).
Definition Commands.cpp:927
Abstract base class for all graphical circuit elements in wiRedPanda.
MorphCommand(const QList< GraphicElement * > &elements, ElementType type, Scene *scene, QUndoCommand *parent=nullptr)
Constructs the morph command.
Definition Commands.cpp:693
void undo() override
Restores the original element types.
Definition Commands.cpp:734
void redo() override
Replaces elements with instances of the new type.
Definition Commands.cpp:757
MoveCommand(const QList< GraphicElement * > &list, const QList< QPointF > &oldPositions, Scene *scene, QUndoCommand *parent=nullptr)
Constructs the command capturing old and new positions.
Definition Commands.cpp:392
void undo() override
Restores elements to their old positions.
Definition Commands.cpp:405
void redo() override
Moves elements to their new positions.
Definition Commands.cpp:416
void redo() override
void undo() override
RegisterBlobCommand(const QString &blobName, const QByteArray &data, Scene *scene, QUndoCommand *parent=nullptr)
void redo() override
void undo() override
RemoveBlobCommand(const QString &blobName, Scene *scene, QUndoCommand *parent=nullptr)
void redo() override
void undo() override
RenameBlobCommand(const QString &oldName, const QString &newName, Scene *scene, QUndoCommand *parent=nullptr)
void undo() override
Reverses the rotation.
Definition Commands.cpp:344
void redo() override
Applies the rotation.
Definition Commands.cpp:359
RotateCommand(const QList< GraphicElement * > &items, const int angle, Scene *scene, QUndoCommand *parent=nullptr)
Constructs the command.
Definition Commands.cpp:330
Main circuit editing scene.
Definition Scene.h:56
SplitCommand(Connection *conn, QPointF mousePos, Scene *scene, QUndoCommand *parent=nullptr)
Constructs the split command.
Definition Commands.cpp:551
void undo() override
Removes the node and restores the original connection.
Definition Commands.cpp:663
void redo() override
Inserts the node and splits the connection.
Definition Commands.cpp:610
void undo() override
Toggles the bit again (involution).
void redo() override
Toggles the bit at pos.
ToggleTruthTableOutputCommand(GraphicElement *element, int pos, Scene *scene, QUndoCommand *parent=nullptr)
Constructs the toggle command.
void setBlobName(const QString &name)
Overrides the blob name (needed when elements are already file-backed at construction time).
Definition Commands.h:544
UpdateBlobCommand(const QList< GraphicElement * > &elements, const QByteArray &oldData, const QList< ConnectionInfo > &connections, Scene *scene, QUndoCommand *parent=nullptr)
void undo() override
void setOldBlob(const QByteArray &blob)
Sets the old blob data for registry swap on undo.
Definition Commands.h:541
static QList< ConnectionInfo > captureConnections(const QList< GraphicElement * > &targets)
Captures connection topology for all target elements before a blob operation.
void redo() override
UpdateCommand(const QList< GraphicElement * > &elements, const QByteArray &oldData, Scene *scene, QUndoCommand *parent=nullptr)
Constructs the command.
Definition Commands.cpp:427
void undo() override
Restores the old property values.
Definition Commands.cpp:460
void redo() override
Applies the new property values.
Definition Commands.cpp:471
const QList< GraphicElement * > findElements(Scene *scene, const QList< int > &ids)
Definition Commands.cpp:129
void addItems(Scene *scene, const QList< QGraphicsItem * > &items)
Definition Commands.cpp:172
void saveItems(Scene *scene, QByteArray &itemData, const QList< QGraphicsItem * > &items, const QList< int > &otherIds)
Definition Commands.cpp:157
GraphicElement * findElm(Scene *scene, const int id)
Definition Commands.cpp:152
void storeIds(const QList< QGraphicsItem * > &items, QList< int > &ids)
Definition Commands.cpp:28
const QList< QGraphicsItem * > findItems(Scene *scene, const QList< int > &ids)
Definition Commands.cpp:111
Connection * findConn(Scene *scene, const int id)
Definition Commands.cpp:147
const QList< QGraphicsItem * > loadItems(Scene *scene, QByteArray &itemData, const QList< int > &ids, QList< int > &otherIds)
Definition Commands.cpp:185
void drainPortConnections(GraphicElement *elm, int fromPort, int toPort, bool isInput, QDataStream &stream, Scene *scene)
Saves and deletes connections on ports in range [fromPort, toPort).
Definition Commands.cpp:229
const QList< QGraphicsItem * > loadList(const QList< QGraphicsItem * > &items, QList< int > &ids, QList< int > &otherIds)
Definition Commands.cpp:57
void storeOtherIds(const QList< QGraphicsItem * > &connections, const QList< int > &ids, QList< int > &otherIds)
Definition Commands.cpp:39
void deleteItems(Scene *scene, const QList< QGraphicsItem * > &items)
Definition Commands.cpp:220