From a82123e7a878cfebb8d92eeb717226503b1c0eb0 Mon Sep 17 00:00:00 2001 From: Dmitry Pinaev Date: Sun, 29 Nov 2026 17:31:17 +0100 Subject: [PATCH 1/2] Give Backspace and Delete to a focused embedded widget first GraphicsView handled Backspace (on macOS) and Delete before the scene saw the key. A focused line edit in a node lost the key and the selected nodes were deleted instead. Fixes #562 --- src/GraphicsView.cpp | 8 +++ test/CMakeLists.txt | 1 + test/src/TestEmbeddedWidgetKeys.cpp | 103 ++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 test/src/TestEmbeddedWidgetKeys.cpp diff --git a/src/GraphicsView.cpp b/src/GraphicsView.cpp index 7f72c4401..e0b4092ab 100644 --- a/src/GraphicsView.cpp +++ b/src/GraphicsView.cpp @@ -370,6 +370,14 @@ void GraphicsView::onPasteObjects() void GraphicsView::keyPressEvent(QKeyEvent *event) { + // An embedded widget with the focus gets the key first. Backspace and Delete edit its text. + if (auto *focusItem = scene() ? scene()->focusItem() : nullptr) { + if (qgraphicsitem_cast(focusItem)) { + QGraphicsView::keyPressEvent(event); + return; + } + } + switch (event->key()) { #ifdef Q_OS_MACOS case Qt::Key_Backspace: diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 82dc60b58..921d02c3c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -30,6 +30,7 @@ add_executable(test_nodes src/TestVersion.cpp src/TestZoomFeatures.cpp src/TestLoopDetection.cpp + src/TestEmbeddedWidgetKeys.cpp include/ApplicationSetup.hpp include/TestGraphModel.hpp include/UITestHelper.hpp diff --git a/test/src/TestEmbeddedWidgetKeys.cpp b/test/src/TestEmbeddedWidgetKeys.cpp new file mode 100644 index 000000000..12f169646 --- /dev/null +++ b/test/src/TestEmbeddedWidgetKeys.cpp @@ -0,0 +1,103 @@ +#include "ApplicationSetup.hpp" +#include "UITestHelper.hpp" + +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include + +#include + +using QtNodes::DataFlowGraphicsScene; +using QtNodes::DataFlowGraphModel; +using QtNodes::GraphicsView; +using QtNodes::NodeData; +using QtNodes::NodeDataType; +using QtNodes::NodeDelegateModel; +using QtNodes::NodeDelegateModelRegistry; +using QtNodes::NodeId; +using QtNodes::NodeRole; +using QtNodes::PortIndex; +using QtNodes::PortType; + +namespace { + +class LineEditModel : public NodeDelegateModel +{ +public: + LineEditModel() + : _edit(new QLineEdit(QStringLiteral("abc"))) + {} + + ~LineEditModel() override { delete _edit; } + + QString name() const override { return QStringLiteral("LineEditModel"); } + QString caption() const override { return QStringLiteral("Line"); } + + unsigned int nPorts(PortType) const override { return 0; } + NodeDataType dataType(PortType, PortIndex) const override { return {}; } + void setInData(std::shared_ptr, PortIndex) override {} + std::shared_ptr outData(PortIndex) override { return nullptr; } + + QWidget *embeddedWidget() override { return _edit; } + +private: + QPointer _edit; +}; + +} // namespace + +TEST_CASE("Backspace in an embedded line edit erases text and does not delete a selected node", + "[ui][widget-keys]") +{ + auto app = applicationSetup(); + + auto registry = std::make_shared(); + registry->registerModel(); + + DataFlowGraphModel model(registry); + DataFlowGraphicsScene scene(model); + GraphicsView view(&scene); + view.resize(800, 600); + view.show(); + REQUIRE(QTest::qWaitForWindowExposed(&view)); + + NodeId const other = model.addNode(QStringLiteral("LineEditModel")); + NodeId const edited = model.addNode(QStringLiteral("LineEditModel")); + model.setNodeData(other, NodeRole::Position, QPointF(-300, 0)); + model.setNodeData(edited, NodeRole::Position, QPointF(100, 0)); + UITestHelper::waitForUI(); + + // No style is loaded in tests, so the default opacity is 0 and Qt would + // skip the nodes when hit-testing the simulated mouse events. + scene.nodeGraphicsObject(other)->setOpacity(1.0); + scene.nodeGraphicsObject(edited)->setOpacity(1.0); + + auto *edit = qobject_cast(model.delegateModel(edited) + ->embeddedWidget()); + REQUIRE(edit != nullptr); + + scene.nodeGraphicsObject(other)->setSelected(true); + + QGraphicsProxyWidget *proxy = edit->graphicsProxyWidget(); + REQUIRE(proxy != nullptr); + QPointF const target = proxy->mapToScene(proxy->boundingRect().center()); + + UITestHelper::simulateMousePress(&view, target); + UITestHelper::simulateMouseRelease(&view, target); + UITestHelper::waitForUI(); + + QTest::keyClick(&view, Qt::Key_Backspace); + UITestHelper::waitForUI(); + + CHECK(edit->text() == QStringLiteral("ab")); + CHECK(model.allNodeIds().size() == 2); +} From 51ed642793c42f1326e5ca3415d2b4a8ca73e5c1 Mon Sep 17 00:00:00 2001 From: Dmitry Pinaev Date: Sun, 29 Nov 2026 17:31:17 +0100 Subject: [PATCH 2/2] Do not select the connections restored by the undo of a delete Undo of a delete selected every restored node and connection. The restore function is shared with paste, where this is wanted. Before the delete, selecting a node did not select its connections. Select only the nodes in the undo. Fixes #563 --- src/UndoCommands.cpp | 18 +++++--- test/src/TestSerializedItemCommands.cpp | 58 +++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 6 deletions(-) diff --git a/src/UndoCommands.cpp b/src/UndoCommands.cpp index c655d19a6..dda9e853a 100644 --- a/src/UndoCommands.cpp +++ b/src/UndoCommands.cpp @@ -112,7 +112,9 @@ static QJsonObject serializeSelectedItems(BasicGraphicsScene *scene) return serializedScene; } -static void insertSerializedItems(QJsonObject const &json, BasicGraphicsScene *scene) +static void insertSerializedItems(QJsonObject const &json, + BasicGraphicsScene *scene, + bool const selectConnections) { AbstractGraphModel &graphModel = scene->graphModel(); @@ -141,8 +143,10 @@ static void insertSerializedItems(QJsonObject const &json, BasicGraphicsScene *s // Restore the connection graphModel.addConnection(connId); - if (auto *cgo = scene->connectionGraphicsObject(connId)) - cgo->setSelected(true); + if (selectConnections) { + if (auto *cgo = scene->connectionGraphicsObject(connId)) + cgo->setSelected(true); + } } if (json.contains("groups")) { @@ -236,7 +240,7 @@ void CreateCommand::redo() if (_sceneJson.empty() || _sceneJson["nodes"].toArray().empty()) return; - insertSerializedItems(_sceneJson, _scene); + insertSerializedItems(_sceneJson, _scene, true); } //------------------------------------- @@ -324,7 +328,9 @@ DeleteCommand::DeleteCommand(BasicGraphicsScene *scene) void DeleteCommand::undo() { - insertSerializedItems(_sceneJson, _scene); + // The deleted nodes were selected, so they are selected again. Selecting a node does not + // select its connections, so the restored connections stay unselected. + insertSerializedItems(_sceneJson, _scene, false); } void DeleteCommand::redo() @@ -416,7 +422,7 @@ void PasteCommand::redo() // Ignore if pasted in content does not generate nodes. try { - insertSerializedItems(_newSceneJson, _scene); + insertSerializedItems(_newSceneJson, _scene, true); } catch (...) { // If the paste does not work, delete all selected nodes and connections // `deleteNode(...)` implicitly removed connections diff --git a/test/src/TestSerializedItemCommands.cpp b/test/src/TestSerializedItemCommands.cpp index 25f0f8c18..6bd661015 100644 --- a/test/src/TestSerializedItemCommands.cpp +++ b/test/src/TestSerializedItemCommands.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -231,6 +232,63 @@ TEST_CASE("Deleting a node removes its connections and undo restores them", CHECK(model.connectionExists(ac)); } +TEST_CASE("Undo of a delete selects the restored nodes and not their connections", + "[undo][serialized]") +{ + auto app = applicationSetup(); + + RecordingGraphModel model; + BasicGraphicsScene scene(model); + + NodeId const a = addNodeAt(model, QPointF(0, 0)); + NodeId const b = addNodeAt(model, QPointF(100, 0)); + NodeId const c = addNodeAt(model, QPointF(200, 0)); + + ConnectionId const ab{a, 0, b, 0}; + ConnectionId const bc{b, 0, c, 0}; + model.addConnection(ab); + model.addConnection(bc); + + selectOnly(scene, {b}); + scene.undoStack().push(new DeleteCommand(&scene)); + scene.undoStack().undo(); + + CHECK(scene.nodeGraphicsObject(b)->isSelected()); + CHECK_FALSE(scene.nodeGraphicsObject(a)->isSelected()); + REQUIRE(scene.connectionGraphicsObject(ab) != nullptr); + REQUIRE(scene.connectionGraphicsObject(bc) != nullptr); + CHECK_FALSE(scene.connectionGraphicsObject(ab)->isSelected()); + CHECK_FALSE(scene.connectionGraphicsObject(bc)->isSelected()); +} + +TEST_CASE("Paste selects the pasted nodes and their connections", "[undo][serialized]") +{ + auto app = applicationSetup(); + + RecordingGraphModel model; + BasicGraphicsScene scene(model); + + NodeId const a = addNodeAt(model, QPointF(0, 0)); + NodeId const b = addNodeAt(model, QPointF(100, 0)); + model.addConnection(ConnectionId{a, 0, b, 0}); + + selectOnly(scene, {a, b}); + scene.undoStack().push(new CopyCommand(&scene)); + scene.undoStack().push(new PasteCommand(&scene, QPointF(500, 500))); + + REQUIRE(model.allNodeIds().size() == 4); + + for (NodeId id : model.allNodeIds()) { + bool const pasted = id != a && id != b; + CHECK(scene.nodeGraphicsObject(id)->isSelected() == pasted); + + for (ConnectionId const &connectionId : model.allConnectionIds(id)) { + REQUIRE(scene.connectionGraphicsObject(connectionId) != nullptr); + CHECK(scene.connectionGraphicsObject(connectionId)->isSelected() == pasted); + } + } +} + TEST_CASE("CreateCommand adds a node, undo removes it and redo restores it", "[undo][serialized]") {