From 765865a7bd3232e5401da0b89867ebfef776f5f7 Mon Sep 17 00:00:00 2001 From: Dmitry Pinaev Date: Sun, 29 Nov 2026 19:23:58 +0100 Subject: [PATCH] Keep the graphics objects when the scene orientation changes The scene reset itself and deleted every graphics object. The node proxies deleted the embedded widgets, and the groups and the selection were lost. The models kept pointers to the deleted widgets. Only the geometry changes, so the scene now updates each node. Fixes #466 --- src/BasicGraphicsScene.cpp | 6 +- test/CMakeLists.txt | 1 + test/src/TestNodeGroup.cpp | 3 +- test/src/TestOrientationWidgets.cpp | 98 +++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 test/src/TestOrientationWidgets.cpp diff --git a/src/BasicGraphicsScene.cpp b/src/BasicGraphicsScene.cpp index 8f7e1929..6dee5710 100644 --- a/src/BasicGraphicsScene.cpp +++ b/src/BasicGraphicsScene.cpp @@ -300,7 +300,11 @@ void BasicGraphicsScene::setOrientation(Qt::Orientation const orientation) break; } - onModelReset(); + // Only the geometry changes. Keep the graphics objects, so that the embedded widgets, + // the groups and the selection stay as they are. + for (NodeId const nodeId : _graphModel.allNodeIds()) { + onNodeUpdated(nodeId); + } } } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8a36fd1b..d05284d7 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -31,6 +31,7 @@ add_executable(test_nodes src/TestZoomFeatures.cpp src/TestLoopDetection.cpp src/TestEmbeddedWidgetKeys.cpp + src/TestOrientationWidgets.cpp src/TestViewDragMode.cpp include/ApplicationSetup.hpp include/TestGraphModel.hpp diff --git a/test/src/TestNodeGroup.cpp b/test/src/TestNodeGroup.cpp index cdaf2938..3674a080 100644 --- a/test/src/TestNodeGroup.cpp +++ b/test/src/TestNodeGroup.cpp @@ -574,8 +574,7 @@ TEST_CASE("A model reset with a group does not leave dangling group items", "[no scene->nodeGraphicsObject(b)}; REQUIRE(scene->createGroup(nodes, QStringLiteral("Reset")).lock()); - // Changing the orientation resets the model view of the scene. - scene->setOrientation(Qt::Vertical); + Q_EMIT model.modelReset(); CHECK(scene->groups().empty()); diff --git a/test/src/TestOrientationWidgets.cpp b/test/src/TestOrientationWidgets.cpp new file mode 100644 index 00000000..533a702e --- /dev/null +++ b/test/src/TestOrientationWidgets.cpp @@ -0,0 +1,98 @@ +#include "ApplicationSetup.hpp" +#include "UITestHelper.hpp" + +#include +#include +#include +#include +#include +#include + +#include + +#include +#include + +#include + +using namespace QtNodes; + +namespace { + +class EditModel : public NodeDelegateModel +{ +public: + EditModel() + : _edit(new QLineEdit(QStringLiteral("abc"))) + {} + ~EditModel() override { delete _edit; } + + QString name() const override { return QStringLiteral("EditModel"); } + QString caption() const override { return QStringLiteral("Edit"); } + 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("The embedded widget survives a change of the scene orientation", "[ui][orientation]") +{ + auto app = applicationSetup(); + + auto registry = std::make_shared(); + registry->registerModel(); + DataFlowGraphModel model(registry); + DataFlowGraphicsScene scene(model); + GraphicsView view(&scene); + view.show(); + REQUIRE(QTest::qWaitForWindowExposed(&view)); + + NodeId const id = model.addNode(QStringLiteral("EditModel")); + UITestHelper::waitForUI(); + + auto *model0 = model.delegateModel(id); + QWidget *edit = model0->embeddedWidget(); + REQUIRE(edit != nullptr); + static_cast(edit)->setText(QStringLiteral("typed")); + + scene.setOrientation(Qt::Vertical); + UITestHelper::waitForUI(); + + QWidget *after = model0->embeddedWidget(); + REQUIRE(after != nullptr); + CHECK(static_cast(after)->text() == QStringLiteral("typed")); + CHECK(after->graphicsProxyWidget() != nullptr); +} + +TEST_CASE("A change of the scene orientation keeps the groups and the selected nodes", + "[ui][orientation]") +{ + auto app = applicationSetup(); + + auto registry = std::make_shared(); + registry->registerModel(); + DataFlowGraphModel model(registry); + DataFlowGraphicsScene scene(model); + scene.setGroupingEnabled(true); + + NodeId const a = model.addNode(QStringLiteral("EditModel")); + NodeId const b = model.addNode(QStringLiteral("EditModel")); + std::vector nodes{scene.nodeGraphicsObject(a), + scene.nodeGraphicsObject(b)}; + REQUIRE(scene.createGroup(nodes, QStringLiteral("Pair")).lock()); + NodeId const c = model.addNode(QStringLiteral("EditModel")); + scene.nodeGraphicsObject(c)->setSelected(true); + REQUIRE(scene.nodeGraphicsObject(c)->isSelected()); + + scene.setOrientation(Qt::Vertical); + + CHECK(scene.groups().size() == 1); + CHECK(scene.nodeGraphicsObject(a) == nodes[0]); + CHECK(scene.nodeGraphicsObject(c)->isSelected()); +}