From 83b123c046bab2ff7666f7327f3c23eb77d420e3 Mon Sep 17 00:00:00 2001 From: linbo <1034003879@qq.com> Date: Tue, 13 Oct 2026 10:19:28 +0800 Subject: [PATCH] feat: improve joint flow node insertion --- src/jointfloweditor.cpp | 151 +++++++++++++++++++++++++--------------- src/jointfloweditor.h | 4 ++ src/mainwindow_ui.cpp | 24 ++++--- 3 files changed, 114 insertions(+), 65 deletions(-) diff --git a/src/jointfloweditor.cpp b/src/jointfloweditor.cpp index 00a15f2..a60e512 100644 --- a/src/jointfloweditor.cpp +++ b/src/jointfloweditor.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -319,8 +320,16 @@ JointFlowEditor::JointFlowEditor(QWidget *parent) : QWidget(parent) connect(m_scene, &QGraphicsScene::selectionChanged, this, [this]() { for (QGraphicsItem *item : m_scene->selectedItems()) { const QString id = item->data(0).toString(); - if (!id.isEmpty()) { m_selectedNodeId = id; return; } + if (!id.isEmpty()) { + m_selectedNodeId = id; + if (const Node *selected = nodeById(id)) + m_insertionParentId = selected->parentId; + notifyInsertionContext(); + return; + } } + m_selectedNodeId.clear(); + notifyInsertionContext(); }); } @@ -338,10 +347,12 @@ void JointFlowEditor::addSingleNode(const QString &nodeName, const QString &poin const Node *insertionParent = nodeById(m_insertionParentId); node.parentId = insertionParent && insertionParent->type == LoopNode ? insertionParent->id : QString(); - node.order = childrenOf(node.parentId).size(); + node.order = insertionOrder(node.parentId); + makeRoomForInsertion(node.parentId, node.order); node.position = QPointF(40 + node.order * 220, 40 + (node.parentId.isEmpty() ? 0 : 100)); m_nodes.append(node); m_selectedNodeId = node.id; + layoutSiblings(node.parentId); rebuildScene(); emit changed(); } @@ -360,10 +371,12 @@ void JointFlowEditor::addLoopNode(const QString &nodeName, int loopCount, const Node *insertionParent = nodeById(m_insertionParentId); node.parentId = insertionParent && insertionParent->type == LoopNode ? insertionParent->id : QString(); - node.order = childrenOf(node.parentId).size(); + node.order = insertionOrder(node.parentId); + makeRoomForInsertion(node.parentId, node.order); node.position = QPointF(40 + node.order * 220, 40 + (node.parentId.isEmpty() ? 0 : 100)); m_nodes.append(node); m_selectedNodeId = node.id; + layoutSiblings(node.parentId); rebuildScene(); emit changed(); } @@ -515,7 +528,7 @@ void JointFlowEditor::setAvailablePointNames(const QStringList &pointNames) void JointFlowEditor::setInsertionAtRoot() { m_insertionParentId.clear(); - emit insertionContextChanged(QStringLiteral("根流程")); + notifyInsertionContext(); } bool JointFlowEditor::useSelectedLoopAsInsertionParent() @@ -524,7 +537,8 @@ bool JointFlowEditor::useSelectedLoopAsInsertionParent() if (!selected || selected->type != LoopNode) return false; m_insertionParentId = selected->id; - emit insertionContextChanged(selected->nodeName); + m_selectedNodeId.clear(); + notifyInsertionContext(); return true; } @@ -626,61 +640,87 @@ void JointFlowEditor::moveNode(const QString &id, const QPointF &position) { Node *node = nodeById(id); if (!node) return; - const QString originalParent = node->parentId; - const QPointF center = position + QPointF(kNodeWidth / 2.0, kNodeHeight / 2.0); - if (!originalParent.isEmpty()) { - for (const Node &candidate : m_nodes) { - if (candidate.id == id || candidate.parentId != originalParent) - continue; - if (QRectF(candidate.position, QSizeF(kNodeWidth, kNodeHeight)).contains(center)) { - rebuildScene(); - return; - } - } - } - node->position = position; - QString newParent = originalParent; - // Once a node belongs to a loop, dragging only changes its order inside that loop. - // This prevents an existing child from accidentally becoming a child of a sibling loop. - if (originalParent.isEmpty()) { - for (const Node &candidate : m_nodes) { - if (candidate.id == id || candidate.type != LoopNode) - continue; - if (!QRectF(candidate.position, QSizeF(kNodeWidth, kNodeHeight)).contains(center)) - continue; - bool createsCycle = false; - const Node *ancestor = &candidate; - while (ancestor && !ancestor->parentId.isEmpty()) { - if (ancestor->parentId == id) { createsCycle = true; break; } - ancestor = nodeById(ancestor->parentId); - } - if (!createsCycle) { - newParent = candidate.id; - break; - } - } - } - node = nodeById(id); - if (node) - node->parentId = newParent; - QStringList parents{newParent}; - for (const Node &candidate : m_nodes) - if (!parents.contains(candidate.parentId)) parents.append(candidate.parentId); - for (const QString &parent : parents) { - QList siblings; - for (Node &candidate : m_nodes) - if (candidate.parentId == parent) siblings.append(&candidate); - std::sort(siblings.begin(), siblings.end(), [](const Node *a, const Node *b) { - return a->position.x() == b->position.x() - ? a->position.y() < b->position.y() : a->position.x() < b->position.x(); - }); - for (int order = 0; order < siblings.size(); ++order) - siblings.at(order)->order = order; + const QString parentId = node->parentId; + const qreal droppedCenterX = position.x() + kNodeWidth / 2.0; + QList siblings; + for (Node &candidate : m_nodes) + if (candidate.parentId == parentId && candidate.id != id) + siblings.append(&candidate); + std::sort(siblings.begin(), siblings.end(), [](const Node *a, const Node *b) { + return a->order < b->order; + }); + + int targetOrder = 0; + for (const Node *sibling : siblings) { + if (droppedCenterX > sibling->position.x() + kNodeWidth / 2.0) + ++targetOrder; } + siblings.insert(targetOrder, node); + for (int order = 0; order < siblings.size(); ++order) + siblings.at(order)->order = order; + layoutSiblings(parentId); rebuildScene(); emit changed(); } +int JointFlowEditor::insertionOrder(const QString &parentId) const +{ + const Node *selected = nodeById(selectedNodeId()); + if (selected && selected->parentId == parentId) + return selected->order + 1; + return childrenOf(parentId).size(); +} + +void JointFlowEditor::makeRoomForInsertion(const QString &parentId, int order) +{ + for (Node &candidate : m_nodes) + if (candidate.parentId == parentId && candidate.order >= order) + ++candidate.order; +} + +void JointFlowEditor::layoutSiblings(const QString &parentId) +{ + QList siblings; + for (Node &candidate : m_nodes) + if (candidate.parentId == parentId) + siblings.append(&candidate); + std::sort(siblings.begin(), siblings.end(), [](const Node *a, const Node *b) { + return a->order < b->order; + }); + qreal startX = 40.0; + qreal y = 40.0; + if (!parentId.isEmpty()) { + const Node *parent = nodeById(parentId); + if (parent) { + startX = parent->position.x() + 40.0; + y = parent->position.y() + 112.0; + } else { + y = 140.0; + } + } + for (int order = 0; order < siblings.size(); ++order) { + siblings.at(order)->order = order; + siblings.at(order)->position = QPointF(startX + order * 220.0, y); + } + for (Node *sibling : siblings) + if (sibling->type == LoopNode) + layoutSiblings(sibling->id); +} + +void JointFlowEditor::notifyInsertionContext() +{ + const Node *parent = nodeById(m_insertionParentId); + const QString container = parent && parent->type == LoopNode + ? parent->nodeName : QStringLiteral("根流程"); + const Node *selected = nodeById(selectedNodeId()); + if (selected && selected->parentId == (parent ? parent->id : QString())) { + emit insertionContextChanged(QStringLiteral("%1(插入到“%2”之后)") + .arg(container, selected->nodeName)); + return; + } + emit insertionContextChanged(QStringLiteral("%1(末尾)").arg(container)); +} + void JointFlowEditor::normalizeOrders(const QString &parentId) { int order = 0; @@ -690,6 +730,7 @@ void JointFlowEditor::normalizeOrders(const QString &parentId) void JointFlowEditor::rebuildScene() { + const QSignalBlocker sceneBlocker(m_scene); m_scene->clear(); QHash cards; for (const Node &node : m_nodes) { diff --git a/src/jointfloweditor.h b/src/jointfloweditor.h index 0cd1aeb..2aaa469 100644 --- a/src/jointfloweditor.h +++ b/src/jointfloweditor.h @@ -62,6 +62,10 @@ private: void editNode(const QString &id); void toggleLoopCollapsed(const QString &id); void moveNode(const QString &id, const QPointF &position); + int insertionOrder(const QString &parentId) const; + void makeRoomForInsertion(const QString &parentId, int order); + void layoutSiblings(const QString &parentId); + void notifyInsertionContext(); void normalizeOrders(const QString &parentId); void rebuildScene(); void appendJsonNode(QJsonArray &target, const Node &node) const; diff --git a/src/mainwindow_ui.cpp b/src/mainwindow_ui.cpp index f884539..6f13167 100644 --- a/src/mainwindow_ui.cpp +++ b/src/mainwindow_ui.cpp @@ -651,7 +651,7 @@ QWidget *MainWindow::createArmPage() QLabel *flowTitle = new QLabel(QStringLiteral("自定义执行流程"), flowPanel); flowTitle->setObjectName(QStringLiteral("jointFlowSectionTitle")); QLabel *flowSubtitle = new QLabel( - QStringLiteral("组合单步与循环节点,拖动调整执行顺序"), flowPanel); + QStringLiteral("选中节点后可在其后插入,拖动可调整同级执行顺序"), flowPanel); flowSubtitle->setObjectName(QStringLiteral("jointFlowSectionSubtitle")); QLabel *intervalLabel = new QLabel(QStringLiteral("完成后等待"), flowPanel); m_jointPointInterval = new QDoubleSpinBox(flowPanel); @@ -662,10 +662,12 @@ QWidget *MainWindow::createArmPage() m_jointFlowGroupCount = new QSpinBox(flowPanel); m_jointFlowGroupCount->setRange(1, 10000); m_jointFlowGroupCount->setValue(2); - QLabel *insertionContextLabel = new QLabel(QStringLiteral("添加到:根流程"), flowPanel); + QLabel *insertionContextLabel = new QLabel(QStringLiteral("添加到:根流程(末尾)"), flowPanel); insertionContextLabel->setObjectName(QStringLiteral("jointFlowContextChip")); - QPushButton *insertAtRootButton = new QPushButton(QStringLiteral("返回根流程"), flowPanel); - QPushButton *enterSelectedLoopButton = new QPushButton(QStringLiteral("进入选中循环"), flowPanel); + QPushButton *insertAtRootButton = new QPushButton( + QStringLiteral("切换到根流程末尾"), flowPanel); + QPushButton *enterSelectedLoopButton = new QPushButton( + QStringLiteral("添加到选中循环"), flowPanel); QPushButton *addFlowStepButton = new QPushButton(QStringLiteral("+ 单步节点"), flowPanel); QPushButton *addFlowLoopButton = new QPushButton(QStringLiteral("+ 循环节点"), flowPanel); insertAtRootButton->setObjectName(QStringLiteral("secondaryButton")); @@ -699,9 +701,11 @@ QWidget *MainWindow::createArmPage() flowBuilder->addWidget(addFlowLoopButton); flowLayout->addWidget(flowToolbar); QLabel *flowHint = new QLabel(QStringLiteral( - "默认在根流程添加;选中循环后点击“进入选中循环”,可向循环内添加节点。" - "点击“返回根流程”退出循环;双击卡片编辑参数,左键拖动节点,右键拖动画布。" - "循环卡片右上角可展开或收起子节点;循环内子节点只能在当前循环内调整顺序。"), flowPanel); + "未选中节点时添加到当前流程末尾;选中节点后,新节点将插入到该节点之后。" + "选中循环并点击“添加到选中循环”可添加第一个或末尾子节点;" + "点击“切换到根流程末尾”可退出循环并继续添加根节点。" + "双击卡片编辑参数;左键拖动仅调整同级顺序并自动吸附,右键拖动画布。" + "循环卡片右上角可展开或收起子节点。"), flowPanel); flowHint->setObjectName(QStringLiteral("mutedLabel")); flowLayout->addWidget(flowHint); m_jointFlowEditor = new JointFlowEditor(flowPanel); @@ -779,7 +783,7 @@ QWidget *MainWindow::createArmPage() m_jointFlowEditor, &JointFlowEditor::setInsertionAtRoot); connect(enterSelectedLoopButton, &QPushButton::clicked, this, [this]() { if (!m_jointFlowEditor->useSelectedLoopAsInsertionParent()) { - QMessageBox::information(m_jointFlowDialog, QStringLiteral("进入循环"), + QMessageBox::information(m_jointFlowDialog, QStringLiteral("添加到循环"), QStringLiteral("请先在画布中选择一个循环节点。")); } }); @@ -802,8 +806,8 @@ QWidget *MainWindow::createArmPage() m_jointFlowDialog->setObjectName(QStringLiteral("jointFlowDialog")); m_jointFlowDialog->setWindowTitle(QStringLiteral("机械臂点位与流程编排")); m_jointFlowDialog->setModal(false); - m_jointFlowDialog->resize(1240, 820); - m_jointFlowDialog->setMinimumSize(980, 680); + m_jointFlowDialog->resize(1480, 920); + m_jointFlowDialog->setMinimumSize(1120, 740); QVBoxLayout *jointFlowDialogLayout = new QVBoxLayout(m_jointFlowDialog); jointFlowDialogLayout->setContentsMargins(14, 14, 14, 14); jointFlowDialogLayout->setSpacing(0);