diff --git a/src/DefaultHorizontalNodeGeometry.cpp b/src/DefaultHorizontalNodeGeometry.cpp index e739161a..ff2e8955 100644 --- a/src/DefaultHorizontalNodeGeometry.cpp +++ b/src/DefaultHorizontalNodeGeometry.cpp @@ -222,7 +222,8 @@ QPointF DefaultHorizontalNodeGeometry::widgetPosition(NodeId const nodeId) const { QSize size = _graphModel.nodeData(nodeId, NodeRole::Size); - unsigned int captionHeight = captionRect(nodeId).height(); + // Signed, because the subtraction below can be negative. An unsigned value would wrap. + int captionHeight = captionRect(nodeId).height(); if (_graphModel.nodeData(nodeId, NodeRole::LabelVisible)) captionHeight += labelRect(nodeId).height() + _portSpacing / 2; diff --git a/src/DefaultVerticalNodeGeometry.cpp b/src/DefaultVerticalNodeGeometry.cpp index 589f5cdc..8af3466e 100644 --- a/src/DefaultVerticalNodeGeometry.cpp +++ b/src/DefaultVerticalNodeGeometry.cpp @@ -224,7 +224,8 @@ QPointF DefaultVerticalNodeGeometry::widgetPosition(NodeId const nodeId) const { QSize size = _graphModel.nodeData(nodeId, NodeRole::Size); - unsigned int captionHeight = captionRect(nodeId).height(); + // Signed, because the subtraction below can be negative. An unsigned value would wrap. + int captionHeight = captionRect(nodeId).height(); if (_graphModel.nodeData(nodeId, NodeRole::LabelVisible)) captionHeight += labelRect(nodeId).height() + _portSpacing / 2; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d05284d7..a84d440b 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/TestWidgetPosition.cpp src/TestOrientationWidgets.cpp src/TestViewDragMode.cpp include/ApplicationSetup.hpp diff --git a/test/src/TestWidgetPosition.cpp b/test/src/TestWidgetPosition.cpp new file mode 100644 index 00000000..ccfd3fad --- /dev/null +++ b/test/src/TestWidgetPosition.cpp @@ -0,0 +1,71 @@ +#include "ApplicationSetup.hpp" +#include "UITestHelper.hpp" + +#include +#include +#include +#include +#include + +#include + +#include +#include + +#include + +using namespace QtNodes; + +namespace { + +class BoxModel : public NodeDelegateModel +{ +public: + BoxModel() + : _box(new QWidget()) + { + _box->setFixedSize(60, 40); + } + ~BoxModel() override { delete _box; } + + QString name() const override { return QStringLiteral("BoxModel"); } + QString caption() const override { return QStringLiteral("Box"); } + 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 _box; } + +private: + QPointer _box; +}; + +} // namespace + +TEST_CASE("A widget taller than its node is not placed far below the node", + "[ui][widget-position]") +{ + auto app = applicationSetup(); + + auto registry = std::make_shared(); + registry->registerModel(); + DataFlowGraphModel model(registry); + DataFlowGraphicsScene scene(model); + + NodeId const id = model.addNode(QStringLiteral("BoxModel")); + UITestHelper::waitForUI(); + + QWidget *box = model.delegateModel(id)->embeddedWidget(); + REQUIRE(box != nullptr); + + for (Qt::Orientation orientation : {Qt::Horizontal, Qt::Vertical}) { + scene.setOrientation(orientation); + + // The widget grows and the node is not recomputed yet. + box->setFixedHeight(box->height() + 400); + + QPointF const pos = scene.nodeGeometry().widgetPosition(id); + CAPTURE(orientation, pos.y()); + CHECK(std::abs(pos.y()) < 1000.0); + } +}