Skip to content

Commit

Permalink
Merge pull request #227 from cneben/b/#223-edges-selection
Browse files Browse the repository at this point in the history
#223 Add full support for notifications onNodesMoved() and so on in e…
  • Loading branch information
cneben committed Feb 21, 2024
2 parents de076f9 + 5907b3e commit c912bbe
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
18 changes: 15 additions & 3 deletions src/qanDraggableCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,17 @@ void DraggableCtrl::beginDragMove(const QPointF& sceneDragPos, bool dragSelec
// Note 20231029:
// Notification is disabled when a multiple selection is dragged.
// Use nodesAboutToBeMoved() route on multiple selection, nodeAboutToBeMoved() on single selection.
if (graph != nullptr && notify) {
if (notify) {
std::vector<qan::Node*> nodes;
const auto& selectedNodes = graph->getSelectedNodes();
std::copy(selectedNodes.begin(), selectedNodes.end(), std::back_inserter(nodes));
std::copy(graph->getSelectedGroups().begin(), graph->getSelectedGroups().end(), std::back_inserter(nodes));
for (const auto& selectedEdge: graph->getSelectedEdges()) { // Add selected edges control points
if (selectedEdge->getSource() != nullptr)
nodes.push_back(selectedEdge->getSource());
if (selectedEdge->getDestination() != nullptr)
nodes.push_back(selectedEdge->getDestination());
}
if (selectedNodes.size() == 0)
nodes.push_back(getTarget());
if (dragSelection && graph->hasMultipleSelection()) {
Expand Down Expand Up @@ -398,6 +404,7 @@ void DraggableCtrl::endDragMove(bool dragSelection, bool notify)
if (graphContainerItem == nullptr)
return;


//qWarning() << "qan::DraggableCtrl::endDragMove(): dragSelection=" << dragSelection;
//qWarning() << " graph->hasMultipleSelection()=" << graph->hasMultipleSelection();
//qWarning() << " notify=" << notify;
Expand Down Expand Up @@ -436,8 +443,7 @@ void DraggableCtrl::endDragMove(bool dragSelection, bool notify)
// Notification is disabled when a multiple selection is dragged.
// Use nodesAboutToBeMoved() route on multiple selection, nodeAboutToBeMoved() on single selection.
if (notify) {
if (/*!dragSelection &&*/
!graph->hasMultipleSelection() &&
if (!graph->hasMultipleSelection() &&
!nodeGrouped) // Do not emit nodeMoved() if it has been grouped
emit graph->nodeMoved(_target);
else if (dragSelection &&
Expand All @@ -446,6 +452,12 @@ void DraggableCtrl::endDragMove(bool dragSelection, bool notify)
const auto& selectedNodes = graph->getSelectedNodes();
std::copy(selectedNodes.begin(), selectedNodes.end(), std::back_inserter(nodes));
std::copy(graph->getSelectedGroups().begin(), graph->getSelectedGroups().end(), std::back_inserter(nodes));
for (const auto& selectedEdge: graph->getSelectedEdges()) { // Add selected edges control points
if (selectedEdge->getSource() != nullptr)
nodes.push_back(selectedEdge->getSource());
if (selectedEdge->getDestination() != nullptr)
nodes.push_back(selectedEdge->getDestination());
}
emit graph->nodesMoved(nodes);
}
}
Expand Down
48 changes: 34 additions & 14 deletions src/qanEdgeDraggableCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,29 +119,39 @@ void EdgeDraggableCtrl::beginDragMove(const QPointF& sceneDragPos, bool dragS
_target->getLocked())
return;
//qWarning() << "EdgeDraggableCtrl::beginDragMove(): target=" << getTargetItem() << " dragSelection=" << dragSelection << " notify=" << notify;
const auto graph = getGraph();
if (graph == nullptr)
return;

// Get target edge adjacent nodes
auto src = _targetItem->getSourceItem() != nullptr &&
_targetItem->getSourceItem()->getNode() != nullptr ? _targetItem->getSourceItem()->getNode()->getItem() :
nullptr;
auto dst = _targetItem->getDestinationItem() != nullptr &&
_targetItem->getDestinationItem()->getNode() != nullptr ? _targetItem->getDestinationItem()->getNode()->getItem() :
nullptr;

if (notify) {
std::vector<qan::Node*> nodes;
const auto& selectedNodes = graph->getSelectedNodes();
std::copy(selectedNodes.begin(), selectedNodes.end(), std::back_inserter(nodes));
std::copy(graph->getSelectedGroups().begin(), graph->getSelectedGroups().end(), std::back_inserter(nodes));
nodes.push_back(src->getNode());
nodes.push_back(dst->getNode());
emit graph->nodesAboutToBeMoved(nodes);
}
_targetItem->setDragged(true);
_initialDragPos = sceneDragPos;
const auto rootItem = getGraph()->getContainerItem();
if (rootItem != nullptr) // Project in scene rect (for example is a node is part of a group)
_initialTargetPos = rootItem->mapFromItem(_targetItem, QPointF{0,0});

// Get target edge adjacent nodes
auto src = _targetItem->getSourceItem() != nullptr &&
_targetItem->getSourceItem()->getNode() != nullptr ? _targetItem->getSourceItem()->getNode()->getItem() :
nullptr;
auto dst = _targetItem->getDestinationItem() != nullptr &&
_targetItem->getDestinationItem()->getNode() != nullptr ? _targetItem->getDestinationItem()->getNode()->getItem() :
nullptr;
if (src != nullptr)
src->draggableCtrl().beginDragMove(sceneDragPos, /*dragSelection=*/false);
src->draggableCtrl().beginDragMove(sceneDragPos, /*dragSelection=*/false, /*notify=*/false);
if (dst != nullptr)
dst->draggableCtrl().beginDragMove(sceneDragPos, /*dragSelection=*/false);
dst->draggableCtrl().beginDragMove(sceneDragPos, /*dragSelection=*/false, /*notify=*/false);

// If there is a selection, keep start position for all selected nodes.
const auto graph = getGraph();
if (graph == nullptr)
return;
if (dragSelection &&
graph->hasMultipleSelection()) {
auto beginDragMoveSelected = [this, &sceneDragPos] (auto primitive) { // Call beginDragMove() on a given node or group
Expand Down Expand Up @@ -241,9 +251,9 @@ void EdgeDraggableCtrl::endDragMove(bool dragSelection, bool notify)
_targetItem->getDestinationItem()->getNode() != nullptr ? _targetItem->getDestinationItem()->getNode()->getItem() :
nullptr;
if (src != nullptr)
src->draggableCtrl().endDragMove(/*dragSelection=*/false);
src->draggableCtrl().endDragMove(/*dragSelection=*/false, /*notify=*/false);
if (dst != nullptr)
dst->draggableCtrl().endDragMove(/*dragSelection=*/false);
dst->draggableCtrl().endDragMove(/*dragSelection=*/false, /*notify=*/false);

const auto graph = getGraph();
if (graph == nullptr)
Expand All @@ -260,6 +270,16 @@ void EdgeDraggableCtrl::endDragMove(bool dragSelection, bool notify)
std::for_each(graph->getSelectedEdges().begin(), graph->getSelectedEdges().end(), enDragMoveSelected);
std::for_each(graph->getSelectedGroups().begin(), graph->getSelectedGroups().end(), enDragMoveSelected);
}

if (notify) { // With edge we _always_ move multiple nodes since there is at least src/dst
std::vector<qan::Node*> nodes;
nodes.push_back(src->getNode());
nodes.push_back(dst->getNode());
const auto& selectedNodes = graph->getSelectedNodes();
std::copy(selectedNodes.begin(), selectedNodes.end(), std::back_inserter(nodes));
std::copy(graph->getSelectedGroups().begin(), graph->getSelectedGroups().end(), std::back_inserter(nodes));
emit graph->nodesMoved(nodes);
}
}
//-----------------------------------------------------------------------------

Expand Down

0 comments on commit c912bbe

Please sign in to comment.