Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/NodeGraphicsObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<QGraphicsItem *> overlapItems = collidingItems();
for (auto &item : overlapItems) {
Expand Down Expand Up @@ -348,8 +348,6 @@ void NodeGraphicsObject::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
}
}
}
} else {
moveConnections();
}
}
event->ignore();
Expand Down Expand Up @@ -386,17 +384,19 @@ void NodeGraphicsObject::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
void NodeGraphicsObject::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
// bring all the colliding nodes to background
QList<QGraphicsItem *> 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<QGraphicsItem *> const items = scene()->items();

for (QGraphicsItem *item : overlapItems) {
if (auto group = qgraphicsitem_cast<GroupGraphicsObject *>(item)) {
Q_UNUSED(group);
for (QGraphicsItem *item : items) {
if (item == this || item->zValue() <= 0.0)
continue;

if (qgraphicsitem_cast<GroupGraphicsObject *>(item))
continue;
}

if (item->zValue() > 0.0) {
if (collidesWithItem(item))
item->setZValue(0.0);
}
}

// bring this node forward
Expand Down
2 changes: 2 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions test/include/StrokeCountingPainter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include <QtNodes/internal/AbstractConnectionPainter.hpp>
#include <QtNodes/internal/ConnectionGraphicsObject.hpp>

#include <QPainter>
#include <QPainterPath>
#include <QPainterPathStroker>

/// 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;
};
94 changes: 94 additions & 0 deletions test/src/TestNodeDrag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#include "TestGraphModel.hpp"
#include "UITestHelper.hpp"

#include <QtNodes/internal/AbstractNodeGeometry.hpp>
#include <QtNodes/internal/BasicGraphicsScene.hpp>
#include <QtNodes/internal/ConnectionGraphicsObject.hpp>
#include <QtNodes/internal/GraphicsView.hpp>
#include <QtNodes/internal/NodeGraphicsObject.hpp>
#include <QtNodes/Definitions>
Expand All @@ -12,10 +14,32 @@
#include <QUndoStack>

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<ConnectionId> 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]")
{
Expand Down Expand Up @@ -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);
}
}
102 changes: 102 additions & 0 deletions test/src/TestNodeHover.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include "ApplicationSetup.hpp"
#include "StrokeCountingPainter.hpp"
#include "TestGraphModel.hpp"

#include <QtNodes/internal/BasicGraphicsScene.hpp>
#include <QtNodes/internal/ConnectionGraphicsObject.hpp>
#include <QtNodes/internal/GroupGraphicsObject.hpp>
#include <QtNodes/internal/NodeGraphicsObject.hpp>
#include <QtNodes/internal/NodeGroup.hpp>

#include <catch2/catch.hpp>

#include <QGraphicsSceneHoverEvent>

#include <memory>
#include <vector>

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<StrokeCountingPainter>();
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<NodeGraphicsObject *> 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);
}
Loading