diff --git a/app/src/ui/logic_editor_widget.cpp b/app/src/ui/logic_editor_widget.cpp index f054f0a..1c601f9 100644 --- a/app/src/ui/logic_editor_widget.cpp +++ b/app/src/ui/logic_editor_widget.cpp @@ -1124,6 +1124,17 @@ void LogicEditorWidget::reloadLogic() scene_->setSceneRect(0, 0, kMinimumSceneWidth, 400); return; } + const bool current_rung_exists = std::any_of( + logic->rungs.cbegin(), logic->rungs.cend(), + [this](const LadderRung &rung) + { + return rung.id == current_rung_id_; + }); + if (!current_rung_id_.empty() && !current_rung_exists) + { + // 撤销或重做可能移除当前网络,不能把过期 ID 继续传给编辑服务 + current_rung_id_.clear(); + } if (current_rung_id_.empty() && !logic->rungs.empty()) { current_rung_id_ = logic->rungs.front().id; diff --git a/app/tests/main_window_tests.cpp b/app/tests/main_window_tests.cpp index a117d68..cf4965f 100644 --- a/app/tests/main_window_tests.cpp +++ b/app/tests/main_window_tests.cpp @@ -1023,6 +1023,39 @@ void testLogicContinuousInsertionConsumesFullWire() "the toolbar must reject only the eleventh condition after all wires are consumed"); } +void testLogicHorizontalWireCanBeInsertedAgainAfterUndoingFirstAutoRung() +{ + TestProjectStorage storage; + ProjectService project_service(storage); + LogicEditorService service(project_service); + const std::string logic_id = service.ensureDefaultLogic().id; + LogicEditorWidget editor(service); + editor.setLogicId(logic_id); + editor.resize(1400, 360); + editor.show(); + QApplication::processEvents(); + + require(editor.addHorizontalWire().succeeded, + "the first horizontal wire must create the initial ladder network"); + const ControlLogic *logic = service.findLogic(logic_id); + require(logic != nullptr && logic->rungs.size() == 1U, + "the first horizontal wire must leave one network in the model"); + + require(service.undo().succeeded, + "undo must remove the automatically created network"); + editor.reloadLogic(); + logic = service.findLogic(logic_id); + require(logic != nullptr && logic->rungs.empty(), + "undo must restore the initially empty ladder logic"); + + require(editor.addHorizontalWire().succeeded, + "a horizontal wire must be insertable again after undoing the first one"); + logic = service.findLogic(logic_id); + require(logic != nullptr && logic->rungs.size() == 1U + && logic->rungs.front().condition.has_value(), + "reinserting the horizontal wire must create a fresh network"); +} + void testWindowTitleTracksUnsavedProjectChanges() { TestProjectStorage storage; @@ -2446,6 +2479,7 @@ int main(int argc, char *argv[]) testLogicParallelPaddingGridInsertion(); testLogicParallelBranchConnectsShortBranchToRightJoin(); testLogicContinuousInsertionConsumesFullWire(); + testLogicHorizontalWireCanBeInsertedAgainAfterUndoingFirstAutoRung(); testWindowTitleTracksUnsavedProjectChanges(); testModeActionsControlEditingAvailability(); testRuntimeMonitorWindowLifecycle();