diff --git a/src/NodeGraphicsObject.cpp b/src/NodeGraphicsObject.cpp index 2d2ddb21..d060c249 100644 --- a/src/NodeGraphicsObject.cpp +++ b/src/NodeGraphicsObject.cpp @@ -314,13 +314,13 @@ void NodeGraphicsObject::mouseMoveEvent(QGraphicsSceneMouseEvent *event) auto diff = event->pos() - event->lastPos(); // The model moves the nodes, the scene follows its signals. + // itemChange() then moves the connections of each moved node. nodeScene()->undoStack().push(new MoveNodeCommand(nodeScene(), diff)); if (nodeScene()->groupingEnabled()) { if (auto nodeGroup = _nodeGroup.lock(); nodeGroup) { nodeGroup->groupGraphicsObject().moveConnections(); } else { - moveConnections(); // if it intersects with a group, expand group QList overlapItems = collidingItems(); for (auto &item : overlapItems) { @@ -348,8 +348,6 @@ void NodeGraphicsObject::mouseMoveEvent(QGraphicsSceneMouseEvent *event) } } } - } else { - moveConnections(); } } event->ignore(); @@ -386,17 +384,19 @@ void NodeGraphicsObject::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) void NodeGraphicsObject::hoverEnterEvent(QGraphicsSceneHoverEvent *event) { // bring all the colliding nodes to background - QList overlapItems = collidingItems(); + // Only raised items change. Check the z value before the collision test. + // A collision test with a connection builds its stroke, and connections are not raised. + QList const items = scene()->items(); - for (QGraphicsItem *item : overlapItems) { - if (auto group = qgraphicsitem_cast(item)) { - Q_UNUSED(group); + for (QGraphicsItem *item : items) { + if (item == this || item->zValue() <= 0.0) + continue; + + if (qgraphicsitem_cast(item)) continue; - } - if (item->zValue() > 0.0) { + if (collidesWithItem(item)) item->setZValue(0.0); - } } // bring this node forward diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a84d440b..73ffe75f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -34,12 +34,14 @@ add_executable(test_nodes src/TestWidgetPosition.cpp src/TestOrientationWidgets.cpp src/TestViewDragMode.cpp + src/TestNodeHover.cpp include/ApplicationSetup.hpp include/TestGraphModel.hpp include/UITestHelper.hpp include/TestDataFlowNodes.hpp include/TestDelegateModels.hpp include/LockableModel.hpp + include/StrokeCountingPainter.hpp ) target_include_directories(test_nodes diff --git a/test/include/StrokeCountingPainter.hpp b/test/include/StrokeCountingPainter.hpp new file mode 100644 index 00000000..87235bcb --- /dev/null +++ b/test/include/StrokeCountingPainter.hpp @@ -0,0 +1,34 @@ +#pragma once + +#include +#include + +#include +#include +#include + +/// A connection painter that counts its strokes. +/// Qt asks for the stroke each time it tests the shape of a connection, +/// for example in collidingItems() or in a hit test. +class StrokeCountingPainter : public QtNodes::AbstractConnectionPainter +{ +public: + void paint(QPainter *painter, QtNodes::ConnectionGraphicsObject const &cgo) const override + { + painter->drawLine(cgo.endPoint(QtNodes::PortType::Out), cgo.endPoint(QtNodes::PortType::In)); + } + + QPainterPath getPainterStroke(QtNodes::ConnectionGraphicsObject const &cgo) const override + { + ++strokes; + + QPainterPath path(cgo.endPoint(QtNodes::PortType::Out)); + path.lineTo(cgo.endPoint(QtNodes::PortType::In)); + + QPainterPathStroker stroker; + stroker.setWidth(10.0); + return stroker.createStroke(path); + } + + mutable int strokes = 0; +}; diff --git a/test/src/TestNodeDrag.cpp b/test/src/TestNodeDrag.cpp index a9f3227f..6792d02d 100644 --- a/test/src/TestNodeDrag.cpp +++ b/test/src/TestNodeDrag.cpp @@ -2,7 +2,9 @@ #include "TestGraphModel.hpp" #include "UITestHelper.hpp" +#include #include +#include #include #include #include @@ -12,10 +14,32 @@ #include using QtNodes::BasicGraphicsScene; +using QtNodes::ConnectionId; using QtNodes::GraphicsView; using QtNodes::NodeGraphicsObject; using QtNodes::NodeId; using QtNodes::NodeRole; +using QtNodes::PortType; + +namespace { + +/// Counts how often the scene asks for the connections of one node. +class ConnectionLookupCountingModel : public TestGraphModel +{ +public: + std::unordered_set allConnectionIds(NodeId const nodeId) const override + { + if (nodeId == countedNodeId) + ++lookups; + + return TestGraphModel::allConnectionIds(nodeId); + } + + NodeId countedNodeId = QtNodes::InvalidNodeId; + mutable int lookups = 0; +}; + +} // namespace TEST_CASE("Interactive node drag syncs the model", "[ui][visual][drag]") { @@ -108,3 +132,73 @@ TEST_CASE("Interactive node drag syncs the model", "[ui][visual][drag]") CHECK(scene.undoStack().count() == 1); } } + +TEST_CASE("A node drag moves the connections of the node once per mouse move", "[ui][visual][drag]") +{ + auto app = applicationSetup(); + + ConnectionLookupCountingModel model; + BasicGraphicsScene scene(model); + GraphicsView view(&scene); + view.resize(800, 600); + view.show(); + REQUIRE(QTest::qWaitForWindowExposed(&view)); + + NodeId const source = model.addNode("TestNode"); + NodeId const target = model.addNode("TestNode"); + model.setNodeData(target, NodeRole::Position, QPointF(300, 0)); + model.addConnection(ConnectionId{source, 0, target, 0}); + UITestHelper::waitForUI(); + + auto *item = scene.nodeGraphicsObject(source); + REQUIRE(item != nullptr); + item->setOpacity(1.0); + + auto *connection = scene.connectionGraphicsObject(ConnectionId{source, 0, target, 0}); + REQUIRE(connection != nullptr); + + QPointF const start = item->mapToScene(item->boundingRect().center()); + QPointF const delta(0, 40); + + int lookups = -1; + bool connectionFollows = false; + + // The release moves the connections again. Check them before the release. + auto dragOneMove = [&] { + UITestHelper::simulateMousePress(&view, start); + + model.countedNodeId = source; + model.lookups = 0; + UITestHelper::simulateMouseMove(&view, start + delta); + lookups = model.lookups; + model.countedNodeId = QtNodes::InvalidNodeId; + + QPointF const port = scene.nodeGeometry().portScenePosition(source, + PortType::Out, + 0, + item->sceneTransform()); + connectionFollows = connection->mapToScene(connection->endPoint(PortType::Out)) == port; + + UITestHelper::simulateMouseRelease(&view, start + delta); + }; + + SECTION("With grouping enabled") + { + scene.setGroupingEnabled(true); + dragOneMove(); + + CHECK(item->pos() == delta); + CHECK(connectionFollows); + CHECK(lookups == 1); + } + + SECTION("With grouping disabled") + { + scene.setGroupingEnabled(false); + dragOneMove(); + + CHECK(item->pos() == delta); + CHECK(connectionFollows); + CHECK(lookups == 1); + } +} diff --git a/test/src/TestNodeHover.cpp b/test/src/TestNodeHover.cpp new file mode 100644 index 00000000..8181b0d9 --- /dev/null +++ b/test/src/TestNodeHover.cpp @@ -0,0 +1,102 @@ +#include "ApplicationSetup.hpp" +#include "StrokeCountingPainter.hpp" +#include "TestGraphModel.hpp" + +#include +#include +#include +#include +#include + +#include + +#include + +#include +#include + +using QtNodes::BasicGraphicsScene; +using QtNodes::ConnectionId; +using QtNodes::NodeGraphicsObject; +using QtNodes::NodeId; +using QtNodes::NodeRole; + +namespace { +void sendHoverEnter(BasicGraphicsScene &scene, NodeGraphicsObject *item) +{ + QGraphicsSceneHoverEvent enter(QEvent::GraphicsSceneHoverEnter); + enter.setPos(item->boundingRect().center()); + enter.setScenePos(item->mapToScene(enter.pos())); + scene.sendEvent(item, &enter); +} +} // namespace + +TEST_CASE("Hovering a node builds no connection strokes", "[ui][hover]") +{ + auto app = applicationSetup(); + + TestGraphModel model; + BasicGraphicsScene scene(model); + + auto painter = std::make_unique(); + auto *counter = painter.get(); + scene.setConnectionPainter(std::move(painter)); + + NodeId const source = model.addNode("TestNode"); + NodeId const target = model.addNode("TestNode"); + model.setNodeData(target, NodeRole::Position, QPointF(300, 0)); + ConnectionId const connectionId{source, 0, target, 0}; + model.addConnection(connectionId); + REQUIRE(scene.connectionGraphicsObject(connectionId) != nullptr); + + auto *item = scene.nodeGraphicsObject(source); + REQUIRE(item != nullptr); + + counter->strokes = 0; + sendHoverEnter(scene, item); + + CHECK(item->zValue() == 1.0); + CHECK(counter->strokes == 0); +} + +TEST_CASE("Hovering a node lowers the raised items that overlap it", "[ui][hover]") +{ + auto app = applicationSetup(); + + TestGraphModel model; + BasicGraphicsScene scene(model); + + NodeId const hovered = model.addNode("TestNode"); + NodeId const overlapping = model.addNode("TestNode"); + NodeId const distant = model.addNode("TestNode"); + model.setNodeData(overlapping, NodeRole::Position, QPointF(40, 20)); + model.setNodeData(distant, NodeRole::Position, QPointF(600, 600)); + ConnectionId const connectionId{hovered, 0, distant, 0}; + model.addConnection(connectionId); + + auto *hoveredItem = scene.nodeGraphicsObject(hovered); + auto *overlappingItem = scene.nodeGraphicsObject(overlapping); + auto *distantItem = scene.nodeGraphicsObject(distant); + auto *connection = scene.connectionGraphicsObject(connectionId); + REQUIRE(connection != nullptr); + + // A new group is locked and drawn above the nodes. + std::vector groupNodes{overlappingItem}; + auto group = scene.createGroup(groupNodes, QStringLiteral("Raised")).lock(); + REQUIRE(group); + auto &groupItem = group->groupGraphicsObject(); + qreal const groupZ = groupItem.zValue(); + REQUIRE(groupZ > 0.0); + + overlappingItem->setZValue(1.0); + distantItem->setZValue(1.0); + connection->setZValue(1.0); + + sendHoverEnter(scene, hoveredItem); + + CHECK(hoveredItem->zValue() == 1.0); + CHECK(overlappingItem->zValue() == 0.0); + CHECK(connection->zValue() == 0.0); + CHECK(distantItem->zValue() == 1.0); + CHECK(groupItem.zValue() == groupZ); +}