Skip to content

Commit b3a3e98

Browse files
Quincunx271paceholder
authored andcommitted
* Fix Issue paceholder#127 * add unit test
1 parent 56fbb93 commit b3a3e98

File tree

3 files changed

+85
-16
lines changed

3 files changed

+85
-16
lines changed

src/NodeGraphicsObject.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -233,24 +233,26 @@ mousePressEvent(QGraphicsSceneMouseEvent * event)
233233
}
234234
else // initialize new Connection
235235
{
236-
const auto outPolicy = _node.nodeDataModel()->portOutConnectionPolicy(portIndex);
237-
if (!connections.empty() &&
238-
portToCheck == PortType::Out &&
239-
outPolicy == NodeDataModel::ConnectionPolicy::One)
236+
if (portToCheck == PortType::Out)
240237
{
241-
_scene.deleteConnection( *connections.begin()->second );
238+
const auto outPolicy = _node.nodeDataModel()->portOutConnectionPolicy(portIndex);
239+
if (!connections.empty() &&
240+
outPolicy == NodeDataModel::ConnectionPolicy::One)
241+
{
242+
_scene.deleteConnection( *connections.begin()->second );
243+
}
244+
245+
// todo add to FlowScene
246+
auto connection = _scene.createConnection(portToCheck,
247+
_node,
248+
portIndex);
249+
250+
_node.nodeState().setConnection(portToCheck,
251+
portIndex,
252+
*connection);
253+
254+
connection->getConnectionGraphicsObject().grabMouse();
242255
}
243-
244-
// todo add to FlowScene
245-
auto connection = _scene.createConnection(portToCheck,
246-
_node,
247-
portIndex);
248-
249-
_node.nodeState().setConnection(portToCheck,
250-
portIndex,
251-
*connection);
252-
253-
connection->getConnectionGraphicsObject().grabMouse();
254256
}
255257
}
256258
};

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ add_executable(test_nodes
55
test_main.cpp
66
src/test_dragging.cpp
77
src/TestDataModelRegistry.cpp
8+
src/TestNodeGraphicsObject.cpp
89
)
910

1011
target_include_directories(test_nodes
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <nodes/FlowScene>
2+
#include <nodes/FlowView>
3+
#include <nodes/Node>
4+
#include <nodes/NodeDataModel>
5+
6+
#include <catch.hpp>
7+
8+
#include <QtTest>
9+
10+
#include "ApplicationSetup.hpp"
11+
#include "StubNodeDataModel.hpp"
12+
13+
using QtNodes::FlowScene;
14+
using QtNodes::FlowView;
15+
using QtNodes::Node;
16+
using QtNodes::NodeDataModel;
17+
using QtNodes::NodeGraphicsObject;
18+
using QtNodes::PortType;
19+
20+
TEST_CASE("NodeDataModel::portOutConnectionPolicy(...) isn't called for input "
21+
"connections (issue #127)",
22+
"[gui]")
23+
{
24+
class MockModel : public StubNodeDataModel
25+
{
26+
public:
27+
unsigned int nPorts(PortType) const override { return 1; }
28+
29+
NodeDataModel::ConnectionPolicy
30+
portOutConnectionPolicy(int index) const override
31+
{
32+
portOutConnectionPolicyCalledCount++;
33+
return NodeDataModel::ConnectionPolicy::One;
34+
}
35+
36+
mutable int portOutConnectionPolicyCalledCount = 0;
37+
};
38+
39+
auto setup = applicationSetup();
40+
41+
FlowScene scene;
42+
FlowView view(&scene);
43+
44+
// Ensure we have enough size to contain the node
45+
view.resize(640, 480);
46+
47+
view.show();
48+
REQUIRE(QTest::qWaitForWindowExposed(&view));
49+
50+
auto& node = scene.createNode(std::make_unique<MockModel>());
51+
auto& model = dynamic_cast<MockModel&>(*node.nodeDataModel());
52+
auto& ngo = node.nodeGraphicsObject();
53+
auto& ngeom = node.nodeGeometry();
54+
55+
// Move the node to somewhere in the middle of the screen
56+
ngo.setPos(QPointF(50, 50));
57+
58+
// Compute the on-screen position of the input port
59+
QPointF scInPortPos = ngeom.portScenePosition(0, PortType::In, ngo.sceneTransform());
60+
QPoint vwInPortPos = view.mapFromScene(scInPortPos);
61+
62+
// Create a partial connection by clicking on the input port of the node
63+
QTest::mousePress(view.windowHandle(), Qt::LeftButton, Qt::NoModifier, vwInPortPos);
64+
65+
CHECK(model.portOutConnectionPolicyCalledCount == 0);
66+
}

0 commit comments

Comments
 (0)