35using namespace SILICON::core;
41 if (!item || !item->scene())
43 return qobject_cast<DiagramScene*>(item->scene());
48 if (
auto* navigator = scene ? scene->getDocumentNavigator() :
nullptr)
49 return navigator->currentDocumentPath();
54 bool activateDocument(
DiagramScene* scene,
const std::string& documentPath)
56 if (documentPath.empty())
59 if (
auto* navigator = scene ? scene->getDocumentNavigator() :
nullptr)
60 return navigator->activateDocument(documentPath);
69 return scene->findGraphicalItemByUiId(uiId);
81 DiagramScene* diagram,
const std::vector<GraphicalWireSegment*>& segments,
83 : QUndoCommand(parent),
scene(diagram),
documentPath(activeDocumentPath(diagram)),
87 for (
const auto* segment : segments) {
88 if (!segment || !segment->getGraphicalWire())
91 segment->getGraphicalWire()->getRouteId(),
92 segment->getGraphicalWire()->getBus(),
93 segment->pos(), segment->getPoints()});
101 if (
scene->getInteractionMode() != DiagramScene::InteractionMode::NORMAL_MODE)
102 scene->setInteractionMode(DiagramScene::InteractionMode::NORMAL_MODE);
104 std::vector<QGraphicsItem*> removed;
106 if (
auto* item =
scene->findGraphicalItemByUiId(snapshot.uiId))
107 removed.push_back(item);
108 scene->removeItems(removed);
112 auto& manager =
scene->getWireManager();
114 std::map<uint64_t, std::shared_ptr<GraphicalWire>> groups;
116 for (
const auto& wire : manager.wires())
117 if (wire->getRouteId() == snapshot.routeId)
118 groups[snapshot.routeId] = wire;
120 if (
scene->findGraphicalItemByUiId(snapshot.uiId))
122 auto& wire = groups[snapshot.routeId];
124 wire = manager.createWire(1);
125 wire->setBus(snapshot.bus);
126 wire->restoreRouteId(snapshot.routeId);
129 segment->setUiId(snapshot.uiId);
130 segment->setPos(snapshot.position);
131 segment->setPoints(snapshot.points);
132 scene->addItem(segment);
133 segment->setGraphicalWire(wire.get());
134 manager.addSegment(segment,
false);
136 manager.rebuildFromGeometry();
156 : QUndoCommand(std::move(text), parent),
175 const QPointF& newPos)
180 scene = itemScene(item);
205 std::vector<WireRouteChange> routeChanges,
208 QUndoCommand* parent)
209 : QUndoCommand(parent),
210 scene(itemScene(segment)),
212 changes(std::move(routeChanges)),
249 QUndoCommand* parent)
250 : QUndoCommand(parent),
251 scene(itemScene(component)),
253 uiId(component ? component->getUiId() : 0),
257 setText(
"Rotate Component");
265 if (
auto* component = findComponent(
scene,
uiId)) {
267 component->setInitialRotation();
269 scene->updateSceneAfterEdit();
283 if (
auto* component = findComponent(
scene,
uiId)) {
285 component->setInitialRotation();
287 scene->updateSceneAfterEdit();
296 :
scene(itemScene(component)),
298 uiId(component->getUiId()),
302 setText(
"Resize Component");
309 if (
auto* component = findComponent(
scene,
uiId)) {
310 scene->cancelComponentResize();
311 component->applyResizeState(state);
312 scene->updateSceneAfterEdit();
313 emit
scene->componentResized();
330 : QUndoCommand(parent),
key(std::move(
key))
332 setText(
"Modify Property");
339 if (!component || oldValue == newValue)
342 auto* scene = itemScene(component);
345 changes.push_back({scene, component->
getUiId(), oldValue, newValue});
353 bool applied =
false;
354 for (
const auto& change :
changes) {
355 if (
auto* component = findComponent(change.scene, change.uiId)) {
356 component->applyProperty(
key, useNewValue ? change.newValue : change.oldValue);
361 if (applied &&
changes.front().scene)
362 changes.front().scene->updateSceneAfterEdit();
381 QUndoCommand* parent)
382 : QUndoCommand(parent),
399 const auto bson = nlohmann::json::to_bson(
payload);
400 return {
reinterpret_cast<const char*
>(bson.data()),
401 static_cast<QByteArray::size_type
>(bson.size())};
406 const auto* ptr =
reinterpret_cast<const std::uint8_t*
>(
payload.constData());
407 return nlohmann::json::from_bson(ptr, ptr +
payload.size());
412 if (!
payload.contains(
"origin") || !
payload[
"origin"].is_object())
415 return {
payload[
"origin"].value(
"x", 0.0),
payload[
"origin"].value(
"y", 0.0)};
426 if (
scene->getInteractionMode() != InteractionMode::NORMAL_MODE)
427 scene->setInteractionMode(InteractionMode::NORMAL_MODE);
429 const auto selectionPayload =
payload();
433 scene->removeSelection(selectionPayload);
454 if (
scene->getInteractionMode() != InteractionMode::NORMAL_MODE)
455 scene->setInteractionMode(InteractionMode::NORMAL_MODE);
457 const auto selectionPayload =
payload();
468 scene->removeSelection(selectionPayload);
472 QUndoCommand* parent)
473 : QUndoCommand(std::move(text), parent),
static ComponentRegistry & instance()
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.
WireEndpointEdit endpointEdit
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.
static GUIComponentFactory & instance()
Gets the global singleton factory instance.
Base class for graphical circuit components.
Base class for all graphical items in the diagram.
The visible, selectable polyline wire on the diagram scene.
void apply(bool useNewValue)
ModifyPropertyCommand(std::string key, QUndoCommand *parent=nullptr)
std::vector< PropertyChange > changes
void addPropertyChange(GraphicalComponent *component, const PropertyValue &oldValue, const PropertyValue &newValue)
WireEndpointEdit topologyEdit
std::vector< WireRouteChange > wireMoves
void addItemMove(GraphicalItem *item, const QPointF &oldPos, const QPointF &newPos)
std::vector< ItemPositionChange > moves
ComponentResizeState before
ResizeComponentCommand(GraphicalComponent *component, ComponentResizeState before, ComponentResizeState after)
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)
nlohmann::json payload() const
static QPointF payloadOrigin(const nlohmann::json &payload)
void commit()
Finishes the batch, rebuilding topology and notifying at the outermost level.
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.
TargetType * category_cast(QGraphicsItem *item, ItemCategory expectedCategory)