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
3 changes: 3 additions & 0 deletions include/QtNodes/internal/GroupGraphicsObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ class NODE_EDITOR_PUBLIC GroupGraphicsObject
/** @copydoc QGraphicsItem::hoverLeaveEvent() */
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;

/** @copydoc QGraphicsItem::mousePressEvent() */
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;

/** @copydoc QGraphicsItem::mouseMoveEvent() */
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;

Expand Down
11 changes: 10 additions & 1 deletion include/QtNodes/internal/UndoCommands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ class NODE_EDITOR_PUBLIC ConnectCommand : public QUndoCommand
class NODE_EDITOR_PUBLIC MoveNodeCommand : public QUndoCommand
{
public:
/// Starts a new move, for example when a drag begins. Every command that is created
/// afterwards belongs to this move. It does not merge with the commands of earlier moves.
static void beginMove();

/// Moves the currently selected nodes.
MoveNodeCommand(BasicGraphicsScene *scene, QPointF const &diff);

Expand All @@ -117,14 +121,19 @@ class NODE_EDITOR_PUBLIC MoveNodeCommand : public QUndoCommand
int id() const override;

/**
* Several sequential movements could be merged into one command.
* Several sequential movements of one move are merged into one command.
* Movements from different moves, see beginMove(), are not merged.
*/
bool mergeWith(QUndoCommand const *c) override;

private:
BasicGraphicsScene *_scene;
std::unordered_set<NodeId> _nodeIds;
QPointF _diff;
/// QUndoStack merges commands that follow each other and have the same id(). It does not
/// know where a drag ends. Without this id, two drags of the same nodes merged into one
/// undo step. The commands of one move have the same _moveId. See beginMove().
unsigned int _moveId;
};

} // namespace QtNodes
8 changes: 8 additions & 0 deletions src/GroupGraphicsObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ void GroupGraphicsObject::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
setHovered(false);
}

void GroupGraphicsObject::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
// A new press starts a new drag. Its moves must not merge into an earlier undo step.
MoveNodeCommand::beginMove();

QGraphicsRectItem::mousePressEvent(event);
}

void GroupGraphicsObject::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if (event->lastPos() != event->pos()) {
Expand Down
3 changes: 3 additions & 0 deletions src/NodeGraphicsObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ void NodeGraphicsObject::mousePressEvent(QGraphicsSceneMouseEvent *event)
// Position updates from setup or undo must not count as a drag.
nodeScene()->_nodeDrag = false;

// A new press starts a new drag. Its moves must not merge into an earlier undo step.
MoveNodeCommand::beginMove();

if (_graphModel.nodeFlags(_nodeId) & NodeFlag::Locked) {
return;
}
Expand Down
16 changes: 15 additions & 1 deletion src/UndoCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -568,9 +568,22 @@ void ConnectCommand::redo()

//------

namespace {

// Only the equality of two ids matters, so one counter for all scenes is enough.
unsigned int currentMoveId = 0;

} // namespace

void MoveNodeCommand::beginMove()
{
++currentMoveId;
}

MoveNodeCommand::MoveNodeCommand(BasicGraphicsScene *scene, QPointF const &diff)
: _scene(scene)
, _diff(diff)
, _moveId(currentMoveId)
{
for (QGraphicsItem *item : _scene->selectedItems()) {
if (auto n = qgraphicsitem_cast<NodeGraphicsObject *>(item)) {
Expand All @@ -585,6 +598,7 @@ MoveNodeCommand::MoveNodeCommand(BasicGraphicsScene *scene,
: _scene(scene)
, _nodeIds(nodeIds)
, _diff(diff)
, _moveId(currentMoveId)
{}

void MoveNodeCommand::undo()
Expand Down Expand Up @@ -626,7 +640,7 @@ bool MoveNodeCommand::mergeWith(QUndoCommand const *c)
{
auto mc = static_cast<MoveNodeCommand const *>(c);

if (_nodeIds == mc->_nodeIds) {
if (_nodeIds == mc->_nodeIds && _moveId == mc->_moveId) {
_diff += mc->_diff;
return true;
}
Expand Down
32 changes: 32 additions & 0 deletions test/src/TestNodeDrag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,38 @@ TEST_CASE("Interactive node drag syncs the model", "[ui][visual][drag]")
CHECK(item->pos() == delta);
}

SECTION("Two separate drags of the same node are two undo steps")
{
UITestHelper::simulateMouseDrag(&view, start, start + delta);
QPointF const second = start + delta;
UITestHelper::simulateMouseDrag(&view, second, second + delta);

REQUIRE(scene.undoStack().count() == 2);
CHECK(item->pos() == delta + delta);

scene.undoStack().undo();
CHECK(item->pos() == delta);
CHECK(model->nodeData(nodeId, NodeRole::Position).value<QPointF>() == delta);

scene.undoStack().undo();
CHECK(item->pos() == QPointF(0, 0));
}

SECTION("One drag with many mouse moves is one undo step")
{
UITestHelper::simulateMousePress(&view, start);
for (int i = 1; i <= 10; ++i) {
UITestHelper::simulateMouseMove(&view, start + delta * (i / 10.0));
}
UITestHelper::simulateMouseRelease(&view, start + delta);

CHECK(scene.undoStack().count() == 1);
CHECK(item->pos() == delta);

scene.undoStack().undo();
CHECK(item->pos() == QPointF(0, 0));
}

SECTION("A click without movement leaves model and undo stack untouched")
{
UITestHelper::simulateMousePress(&view, start);
Expand Down
Loading