Skip to content
Merged
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
8 changes: 7 additions & 1 deletion docs/guide/undo-redo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,16 @@ QtNodes provides these ``QUndoCommand`` implementations in ``UndoCommands.cpp``:
* - ``ConnectCommand``
- Creates a connection
* - ``MoveNodeCommand``
- Moves a node to a new position
- Moves nodes by an offset. The moves of one drag merge into one undo step

Duplicate has no command of its own. It pushes a ``CopyCommand`` and a ``PasteCommand``.

``MoveNodeCommand`` merges with the command before it when both move the same nodes and belong
to the same move. A mouse press on a node or on a group starts a new move, so two separate drags
of the same node are two undo steps. If you push ``MoveNodeCommand`` yourself, for example from
your own view, call ``MoveNodeCommand::beginMove()`` when a new drag starts. Without it, the
moves of the new drag merge with the previous drag of the same nodes.

Serialization Requirement
-------------------------

Expand Down
32 changes: 12 additions & 20 deletions docs/migration/v3-to-v4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,12 @@ The model has no group data, so:
that was deleted and restored loses its name and its id.
- Copy and paste of a group loses its name and lock state.

The graphics items point to each other, so:

- A node that is dragged over a group keeps a raw pointer to it. If the group
is deleted during the drag, the pointer dangles.
- The drag-into-group state is reset only while the node still touches the
group. If you move a node away from a group in one big step and release it,
the node joins the group.
- The group item changes its own geometry inside ``paint()``. That schedules
the next paint, so a view with a group repaints all the time, also when
idle. This costs CPU.

Other problems:

- ``BasicGraphicsScene::loadItems()`` with a paste position moves plain nodes
in the graphics only, and the model keeps the old position. The offset of a
group is wrong, because the group item is always at (0,0). The built-in
paste does not use this path, so only code that calls ``loadItems()`` itself
is affected.
- Two separate drags of the same node are merged into one undo step. This is
the behavior of the upstream code. A fix needs a decision on what one undo
step is.
- ``BasicGraphicsScene::loadItems()`` with a paste position computes the
offset of a group wrongly, because the group item is always at (0,0). The
built-in paste does not use this path, so only code that calls
``loadItems()`` itself is affected.

Why these are not fixed in 3.x
------------------------------
Expand All @@ -68,6 +53,12 @@ users today:
(#536).
- Crashes in the group lifetime are fixed (#540): a model reset with a group,
the double delete of the group item, and a node in more than one group.
- A node that is dragged over a group joins it only if it touches the group
at the release. A group that is deleted during the drag is not used (#537).
- A visible group does not repaint all the time (#537).
- Two separate drags of the same node are two undo steps (#538).
- ``BasicGraphicsScene::loadItems()`` with a paste position moves plain nodes
through the model, so the model and the item agree.
- ``GroupGraphicsObject`` is exported. On a Windows shared build, code that
called its methods did not link.

Expand All @@ -77,7 +68,8 @@ Use of groups in 3.x
Groups are usable. Until v4:

- Do not rely on undo for group changes.
- Do not call ``BasicGraphicsScene::loadItems()`` with a paste position.
- Do not call ``BasicGraphicsScene::loadItems()`` with a paste position for data that has
groups. The offset of a group is wrong.
- Keep the group lock on, if you do not want the drag-into-group behavior.

What changes in v4
Expand Down
5 changes: 3 additions & 2 deletions include/QtNodes/internal/NodeGraphicsObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "NodeGroup.hpp"
#include "NodeState.hpp"
#include <QIcon>
#include <QPointer>
#include <QRectF>
#include <QtCore/QUuid>
#include <QtWidgets/QGraphicsObject>
Expand Down Expand Up @@ -108,8 +109,8 @@ class NODE_EDITOR_PUBLIC NodeGraphicsObject : public QGraphicsObject

bool _locked;

bool _draggingIntoGroup;
GroupGraphicsObject *_possibleGroup;
// The group that the node joins at the release.
QPointer<GroupGraphicsObject> _possibleGroup;
QRectF _originalGroupSize;

// either nullptr or owned by parent QGraphicsItem
Expand Down
48 changes: 19 additions & 29 deletions src/NodeGraphicsObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ NodeGraphicsObject::NodeGraphicsObject(BasicGraphicsScene &scene, NodeId nodeId)
, _graphModel(scene.graphModel())
, _nodeState(*this)
, _locked(false)
, _draggingIntoGroup(false)
, _possibleGroup(nullptr)
, _originalGroupSize()
, _proxyWidget(nullptr)
Expand Down Expand Up @@ -334,29 +333,21 @@ void NodeGraphicsObject::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
if (auto nodeGroup = _nodeGroup.lock(); nodeGroup) {
nodeGroup->groupGraphicsObject().moveConnections();
} else {
// if it intersects with a group, expand group
QList<QGraphicsItem *> overlapItems = collidingItems();
for (auto &item : overlapItems) {
auto ggo = qgraphicsitem_cast<GroupGraphicsObject *>(item);
if (ggo != nullptr) {
if (!ggo->locked()) {
if (!_draggingIntoGroup) {
_draggingIntoGroup = true;
_possibleGroup = ggo;
_originalGroupSize = _possibleGroup->mapRectToScene(ggo->rect());
_possibleGroup->setPossibleChild(this);
break;
} else {
if (ggo == _possibleGroup) {
if (!boundingRect().intersects(
mapRectFromScene(_originalGroupSize))) {
_draggingIntoGroup = false;
_originalGroupSize = QRectF();
_possibleGroup->unsetPossibleChild();
_possibleGroup = nullptr;
}
}
}
// _originalGroupSize is the box before it grew to show this node.
if (_possibleGroup) {
if (!boundingRect().intersects(mapRectFromScene(_originalGroupSize))) {
_possibleGroup->unsetPossibleChild();
_possibleGroup = nullptr;
_originalGroupSize = QRectF();
}
} else {
for (QGraphicsItem *item : collidingItems()) {
auto ggo = qgraphicsitem_cast<GroupGraphicsObject *>(item);
if (ggo && !ggo->locked()) {
_possibleGroup = ggo;
_originalGroupSize = ggo->mapRectToScene(ggo->rect());
ggo->setPossibleChild(this);
break;
}
}
}
Expand All @@ -382,13 +373,12 @@ void NodeGraphicsObject::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
// position connections precisely after fast node move
moveConnections();

if (nodeScene()->groupingEnabled() && _draggingIntoGroup && _possibleGroup
&& _nodeGroup.expired()) {
nodeScene()->addNodeToGroup(_nodeId, _possibleGroup->group().id());
if (_possibleGroup) {
if (nodeScene()->groupingEnabled() && _nodeGroup.expired())
nodeScene()->addNodeToGroup(_nodeId, _possibleGroup->group().id());
_possibleGroup->unsetPossibleChild();
_draggingIntoGroup = false;
_originalGroupSize = QRectF();
_possibleGroup = nullptr;
_originalGroupSize = QRectF();
}

nodeScene()->nodeClicked(_nodeId);
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ add_executable(test_nodes
src/TestSceneSetup.cpp
src/TestLockAndPaste.cpp
src/TestGroupRepaint.cpp
src/TestGroupDrag.cpp
src/TestNotifications.cpp
src/TestDataInvalidated.cpp
src/TestNodeHover.cpp
Expand Down
210 changes: 210 additions & 0 deletions test/src/TestGroupDrag.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
#include "ApplicationSetup.hpp"
#include "TestDelegateModels.hpp"
#include "TestGraphModel.hpp"
#include "UITestHelper.hpp"

#include <QtNodes/BasicGraphicsScene>
#include <QtNodes/DataFlowGraphModel>
#include <QtNodes/GraphicsView>
#include <QtNodes/NodeDelegateModelRegistry>
#include <QtNodes/internal/AbstractNodeGeometry.hpp>
#include <QtNodes/internal/GroupGraphicsObject.hpp>
#include <QtNodes/internal/NodeGraphicsObject.hpp>
#include <QtNodes/internal/NodeGroup.hpp>

#include <catch2/catch.hpp>

#include <QPointer>
#include <QTest>
#include <QWidget>

using QtNodes::BasicGraphicsScene;
using QtNodes::DataFlowGraphModel;
using QtNodes::GraphicsView;
using QtNodes::NodeDelegateModelRegistry;
using QtNodes::NodeGraphicsObject;
using QtNodes::NodeGroup;
using QtNodes::NodeId;
using QtNodes::NodeRole;

namespace {

/// A shown view with one unlocked group that holds node `member`, and node `outsider`
/// below the group.
struct GroupDragFixture
{
TestGraphModel model;
BasicGraphicsScene scene{model};
GraphicsView view{&scene};
NodeId member = QtNodes::InvalidNodeId;
NodeId outsider = QtNodes::InvalidNodeId;
std::shared_ptr<NodeGroup> group;

GroupDragFixture()
{
view.resize(800, 600);
view.show();
REQUIRE(QTest::qWaitForWindowExposed(&view));

member = model.addNode("TestNode");
outsider = model.addNode("TestNode");
model.setNodeData(outsider, NodeRole::Position, QPointF(0, 250));

// No style is loaded in tests, so the default opacity is 0 and Qt would
// skip the nodes when hit-testing the simulated mouse events.
scene.nodeGraphicsObject(member)->setOpacity(1.0);
scene.nodeGraphicsObject(outsider)->setOpacity(1.0);

std::vector<NodeGraphicsObject *> nodes{scene.nodeGraphicsObject(member)};
group = scene.createGroup(nodes, "Group").lock();
REQUIRE(group);
// A locked group does not take new nodes.
group->groupGraphicsObject().lock(false);
UITestHelper::waitForUI();
}

NodeGraphicsObject &outsiderObject() { return *scene.nodeGraphicsObject(outsider); }

QPointF outsiderCenter() { return outsiderObject().sceneBoundingRect().center(); }

/// Presses on the outsider and moves it so that it overlaps the member.
QPointF pressAndMoveOntoGroup()
{
QPointF const start = outsiderCenter();
QPointF const memberCenter = scene.nodeGraphicsObject(member)->sceneBoundingRect().center();
QPointF const overGroup = memberCenter + QPointF(30, 30);

UITestHelper::simulateMousePress(&view, start);
UITestHelper::simulateMouseMove(&view, overGroup);

REQUIRE(outsiderObject().sceneBoundingRect().intersects(
group->groupGraphicsObject().sceneBoundingRect()));
return overGroup;
}
};

/// A resizable node with an embedded widget. The resize handle changes the widget size.
class ResizableWidgetNode : public StubDelegateModel
{
public:
ResizableWidgetNode()
: StubDelegateModel(QStringLiteral("ResizableWidgetNode"), QStringLiteral("Resizable"))
{}

bool resizable() const override { return true; }

QWidget *embeddedWidget() override
{
if (!_widget) {
_widget = new QWidget;
_widget->resize(80, 60);
}
return _widget;
}

private:
QPointer<QWidget> _widget;
};

} // namespace

TEST_CASE("A node dragged over a group and released away from it stays out of the group",
"[node-group][drag]")
{
auto app = applicationSetup();
GroupDragFixture f;

QRectF const boxBefore = f.group->groupGraphicsObject().sceneBoundingRect();

QPointF const overGroup = f.pressAndMoveOntoGroup();

// One move takes the node far from the group.
QPointF const away = overGroup + QPointF(450, 0);
UITestHelper::simulateMouseMove(&f.view, away);
REQUIRE_FALSE(f.outsiderObject().sceneBoundingRect().intersects(boxBefore));

UITestHelper::simulateMouseRelease(&f.view, away);
UITestHelper::waitForUI();

CHECK(f.outsiderObject().nodeGroup().expired());
CHECK(f.group->childNodes().size() == 1);
CHECK(f.group->groupGraphicsObject().sceneBoundingRect() == boxBefore);
}

TEST_CASE("A node dragged over a group and released on it joins the group", "[node-group][drag]")
{
auto app = applicationSetup();
GroupDragFixture f;

QPointF const overGroup = f.pressAndMoveOntoGroup();
UITestHelper::simulateMouseRelease(&f.view, overGroup);
UITestHelper::waitForUI();

CHECK(f.outsiderObject().nodeGroup().lock() == f.group);
}

TEST_CASE("A group deleted while a node is dragged over it is not used at the release",
"[node-group][drag][lifetime]")
{
auto app = applicationSetup();
GroupDragFixture f;

QPointF const overGroup = f.pressAndMoveOntoGroup();

// Removing the last node deletes the group.
f.scene.removeNodeFromGroup(f.member);
f.group.reset();
REQUIRE(f.scene.groups().empty());

UITestHelper::simulateMouseMove(&f.view, overGroup + QPointF(5, 5));
UITestHelper::simulateMouseRelease(&f.view, overGroup + QPointF(5, 5));
UITestHelper::waitForUI();

CHECK(f.outsiderObject().nodeGroup().expired());
CHECK(f.scene.groups().empty());
}

TEST_CASE("The box of a group follows a node resized with its handle", "[node-group][drag]")
{
auto app = applicationSetup();

auto registry = std::make_shared<NodeDelegateModelRegistry>();
registry->registerModel([] { return std::make_unique<ResizableWidgetNode>(); }, "Test");
DataFlowGraphModel model(registry);
BasicGraphicsScene scene(model);
GraphicsView view(&scene);
view.resize(800, 600);
view.show();
REQUIRE(QTest::qWaitForWindowExposed(&view));

NodeId const nodeId = model.addNode(QStringLiteral("ResizableWidgetNode"));
auto *node = scene.nodeGraphicsObject(nodeId);
REQUIRE(node != nullptr);
node->setOpacity(1.0);

std::vector<NodeGraphicsObject *> nodes{node};
auto group = scene.createGroup(nodes, "Group").lock();
REQUIRE(group);
// The nodes of a locked group do not take mouse presses.
group->groupGraphicsObject().lock(false);
UITestHelper::waitForUI();

auto &box = group->groupGraphicsObject();
QRectF const nodeBefore = node->sceneBoundingRect();
REQUIRE(box.sceneBoundingRect().contains(nodeBefore));

QRect const handle = scene.nodeGeometry().resizeHandleRect(nodeId);
QPointF const start = node->mapToScene(QRectF(handle).center());
QPointF const delta(120, 90);

UITestHelper::simulateMousePress(&view, start);
UITestHelper::simulateMouseMove(&view, start + delta);
UITestHelper::simulateMouseRelease(&view, start + delta);
UITestHelper::waitForUI();

QRectF const nodeAfter = node->sceneBoundingRect();
REQUIRE(nodeAfter.topLeft() == nodeBefore.topLeft());
REQUIRE(nodeAfter.width() > nodeBefore.width());
REQUIRE(nodeAfter.height() > nodeBefore.height());
CHECK(box.sceneBoundingRect().contains(nodeAfter));
}
Loading