diff --git a/app/widget/nodeview/nodeview.cpp b/app/widget/nodeview/nodeview.cpp index da7f65da5..92fb6ec4e 100644 --- a/app/widget/nodeview/nodeview.cpp +++ b/app/widget/nodeview/nodeview.cpp @@ -471,7 +471,7 @@ void NodeView::mousePressEvent(QMouseEvent *event) // See if we're dragging the arrow of a node for (NodeViewItem *node_item : scene_.item_map()) { if (node_item->GetOutputTriangle().boundingRect().translated(node_item->pos()).contains(scene_pt)) { - CreateNewEdge(node_item); + CreateNewEdge(node_item, event->pos()); return; } } @@ -496,7 +496,7 @@ void NodeView::mousePressEvent(QMouseEvent *event) if (event->modifiers() & Qt::ControlModifier) { NodeViewItem* node_item = dynamic_cast(item); if (node_item) { - CreateNewEdge(node_item); + CreateNewEdge(node_item, event->pos()); return; } } @@ -509,68 +509,7 @@ void NodeView::mouseMoveEvent(QMouseEvent *event) if (HandMove(event)) return; if (create_edge_) { - // Determine scene coordinate - QPointF scene_pt = mapToScene(event->pos()); - - // Find if the cursor is currently inside an item - NodeViewItem* item_at_cursor = dynamic_cast(itemAt(event->pos())); - - // Filter out connecting to self - if (item_at_cursor == create_edge_src_) { - item_at_cursor = nullptr; - } - - // Filter out connecting to a node that connects to us - if (item_at_cursor && item_at_cursor->GetNode()->OutputsTo(create_edge_src_->GetNode(), true)) { - item_at_cursor = nullptr; - } - - // If the item has changed - if (item_at_cursor != create_edge_dst_) { - // If we had a destination active, disconnect from it since the item has changed - if (create_edge_dst_) { - create_edge_dst_->SetHighlightedIndex(-1); - - if (create_edge_dst_temp_expanded_) { - // We expanded this item, so we can un-expand it - create_edge_dst_->SetExpanded(false); - create_edge_dst_->setZValue(0); - } - } - - // Set destination - create_edge_dst_ = item_at_cursor; - - // If our destination is an item, ensure it's expanded - if (create_edge_dst_) { - if ((create_edge_dst_temp_expanded_ = (!create_edge_dst_->IsExpanded()))) { - create_edge_dst_->SetExpanded(true, true); - create_edge_dst_->setZValue(100); // Ensure item is in front - } - } - } - - // If we have a destination, highlight the appropriate input - int highlight_index = -1; - if (create_edge_dst_) { - highlight_index = create_edge_dst_->GetIndexAt(scene_pt); - create_edge_dst_->SetHighlightedIndex(highlight_index); - } - - if (highlight_index >= 0) { - create_edge_dst_input_ = create_edge_dst_->GetInputAtIndex(highlight_index); - create_edge_->SetPoints(create_edge_src_->GetOutputPoint(Node::kDefaultOutput), - create_edge_dst_->GetInputPoint(create_edge_dst_input_.input(), create_edge_dst_input_.element(), create_edge_src_->pos()), - true); - } else { - create_edge_dst_input_.Reset(); - create_edge_->SetPoints(create_edge_src_->GetOutputPoint(Node::kDefaultOutput), - scene_pt, - false); - } - - // Set connected to whether we have a valid input destination - create_edge_->SetConnected(create_edge_dst_input_.IsValid()); + PositionNewEdge(event->pos()); return; } @@ -1494,7 +1433,7 @@ Menu *NodeView::CreateAddMenu(Menu *parent) return add_menu; } -void NodeView::CreateNewEdge(NodeViewItem *output_item) +void NodeView::CreateNewEdge(NodeViewItem *output_item, const QPoint &mouse_pos) { create_edge_ = new NodeViewEdge(); create_edge_src_ = output_item; @@ -1505,6 +1444,74 @@ void NodeView::CreateNewEdge(NodeViewItem *output_item) create_edge_->SetFlowDirection(scene_.GetFlowDirection()); scene_.addItem(create_edge_); + + PositionNewEdge(mouse_pos); +} + +void NodeView::PositionNewEdge(const QPoint &pos) +{ + // Determine scene coordinate + QPointF scene_pt = mapToScene(pos); + + // Find if the cursor is currently inside an item + NodeViewItem* item_at_cursor = dynamic_cast(itemAt(pos)); + + // Filter out connecting to self + if (item_at_cursor == create_edge_src_) { + item_at_cursor = nullptr; + } + + // Filter out connecting to a node that connects to us + if (item_at_cursor && item_at_cursor->GetNode()->OutputsTo(create_edge_src_->GetNode(), true)) { + item_at_cursor = nullptr; + } + + // If the item has changed + if (item_at_cursor != create_edge_dst_) { + // If we had a destination active, disconnect from it since the item has changed + if (create_edge_dst_) { + create_edge_dst_->SetHighlightedIndex(-1); + + if (create_edge_dst_temp_expanded_) { + // We expanded this item, so we can un-expand it + create_edge_dst_->SetExpanded(false); + create_edge_dst_->setZValue(0); + } + } + + // Set destination + create_edge_dst_ = item_at_cursor; + + // If our destination is an item, ensure it's expanded + if (create_edge_dst_) { + if ((create_edge_dst_temp_expanded_ = (!create_edge_dst_->IsExpanded()))) { + create_edge_dst_->SetExpanded(true, true); + create_edge_dst_->setZValue(100); // Ensure item is in front + } + } + } + + // If we have a destination, highlight the appropriate input + int highlight_index = -1; + if (create_edge_dst_) { + highlight_index = create_edge_dst_->GetIndexAt(scene_pt); + create_edge_dst_->SetHighlightedIndex(highlight_index); + } + + if (highlight_index >= 0) { + create_edge_dst_input_ = create_edge_dst_->GetInputAtIndex(highlight_index); + create_edge_->SetPoints(create_edge_src_->GetOutputPoint(Node::kDefaultOutput), + create_edge_dst_->GetInputPoint(create_edge_dst_input_.input(), create_edge_dst_input_.element(), create_edge_src_->pos()), + true); + } else { + create_edge_dst_input_.Reset(); + create_edge_->SetPoints(create_edge_src_->GetOutputPoint(Node::kDefaultOutput), + scene_pt, + false); + } + + // Set connected to whether we have a valid input destination + create_edge_->SetConnected(create_edge_dst_input_.IsValid()); } void NodeView::RepositionContexts() diff --git a/app/widget/nodeview/nodeview.h b/app/widget/nodeview/nodeview.h index 65d1b0438..c1aa9e095 100644 --- a/app/widget/nodeview/nodeview.h +++ b/app/widget/nodeview/nodeview.h @@ -141,7 +141,9 @@ private: Menu *CreateAddMenu(Menu *parent); - void CreateNewEdge(NodeViewItem *output_item); + void CreateNewEdge(NodeViewItem *output_item, const QPoint &mouse_pos); + + void PositionNewEdge(const QPoint &pos); NodeViewItem *UpdateNodeItem(Node *node, bool ignore_own_context = false);