Files
EasyDesignCollection/lib/modules/sequence/layout/flow_layout.dart
T
admin 0a3f2c5cc6 v1.3.13: 模块化注册架构 + PDS流程设计套件更名 + 依赖/圆周交互修复
- 模块化:EdcModule 契约 + registry + 模块独立目录;发布包增加 modules/registry.json
- 更名:PDS流程设计套件 / ProcessDesignSuite;同步构建、发布脚本与文档
- 时序图:依赖偏移以时间显示/编辑;紧贴依赖隐藏箭头并在接缝显示标签;动作条磁吸
- 圆周:依赖标签沿圆弧方向、环间空隙;动作旋转磁吸增强;短弧标签优先动作名
- 清理:release 移至源码同级(本地不入库);tool 合并到 tools;发布说明迁移 docs/release-notes
2026-11-22 10:02:54 +08:00

604 lines
23 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/// 流程图布局:把时序图中的动作与依赖转换为纵向流程图(泳道 = 轨道)。
///
/// 产品规则(1.3 大版本,经需求确认):
/// - 每个动作 = 一个工序节点,节点内显示「序号. 名称」与时长(含工位);
/// - 泳道 = 动作的轨道(layer),横向并列,一眼看出哪些步骤在并行;
/// - 依赖:FS 实线、SS 虚线(带偏移角标注);关键路径加粗高亮;
/// - 自动补「开始 / 结束」节点:开始 → 时间最早的动作(动作1),
/// 其余无前置的动作作为并行起点同样从开始引出;无后继的动作 → 结束;
/// - 只读视图:交互仅支持点选联动与平移缩放,内容修改请回时序图。
library;
import 'dart:math' as math;
import 'dart:ui';
import 'package:sequence_diagram/model/models.dart';
import 'package:sequence_diagram/model/units.dart';
import 'dep_routes.dart';
import 'layout.dart';
/// 流程图节点(动作节点或开始 / 结束节点)。
class FlowNode {
/// 动作节点。
FlowNode.action(Action this.action)
: id = action.id,
isStart = false,
isEnd = false;
/// 开始节点。
FlowNode.start()
: action = null,
id = startId,
isStart = true,
isEnd = false;
/// 结束节点。
FlowNode.end()
: action = null,
id = endId,
isStart = false,
isEnd = true;
/// 开始节点的保留 id。
static const String startId = '__flow_start__';
/// 结束节点的保留 id。
static const String endId = '__flow_end__';
/// 对应动作;开始 / 结束节点为 null。
final Action? action;
/// 节点 id(开始 / 结束使用保留 id)。
final String id;
/// 是否开始节点。
final bool isStart;
/// 是否结束节点。
final bool isEnd;
/// 所属泳道(轨道号 layer)。
int lane = 0;
/// 纵向层级(0 = 开始节点所在层)。
int rank = 0;
/// 布局后的节点矩形(逻辑坐标)。
Rect rect = Rect.zero;
/// 主标题(动作节点为「序号. 名称」)。
String title = '';
/// 副标题(时长 · 工位)。
String subtitle = '';
}
/// 流程图连线。
class FlowEdge {
/// 构造;[type] 为依赖类型(开始 / 结束相关连线为 null)。
FlowEdge(this.fromId, this.toId,
{this.type, this.offset = 0, this.critical = false});
/// 起点节点 id。
final String fromId;
/// 终点节点 id。
final String toId;
/// 依赖类型;null 表示开始 / 结束的流程连线。
final DepType? type;
/// SS 等依赖的偏移角(度)。
final double offset;
/// 是否属于关键路径。
final bool critical;
}
/// 连线走线结果(供画布绘制 / SVG / drawio 导出共用)。
class FlowEdgeRoute {
/// 构造。
FlowEdgeRoute(this.edge, this.points, this.tip, this.dir, this.labelAt, this.axis);
/// 对应的连线。
final FlowEdge edge;
/// 折线顶点序列(含起点与箭头端点)。
final List<Offset> points;
/// 箭头端点。
final Offset tip;
/// 箭头方向(分量式单位方向)。
final Offset dir;
/// FS / SS 标签位置。
final Offset labelAt;
/// 拖拽调整走线的方向:向下流为竖直(上下拖动),同层绕行为水平(左右拖动)。
final RouteAxis axis;
/// 依赖键 `from->to`。
String get key => '${edge.fromId}->${edge.toId}';
/// 是否虚线(SS 依赖)。
bool get dashed => edge.type == DepType.ss;
}
/// 相位分区在流程图中的纵向跨度(左侧阶段栏用)。
class FlowPhaseSpan {
/// 构造。
FlowPhaseSpan(this.zone, this.rankTop, this.rankBottom);
/// 相位分区。
final PhaseZone zone;
/// 覆盖的最上层级。
final int rankTop;
/// 覆盖的最下层级。
final int rankBottom;
/// 与其它阶段纵向重叠时的横向错位档位(0 起,每档向右 [FlowLayout.railStep] px,
/// 仅轻微错开以免重叠处看不清;纵向仍严格对齐流程行)。
int slot = 0;
}
/// 流程图布局结果:节点、连线、泳道与内容范围。
class FlowLayout {
/// 构造并从 [project] 生成流程图。
///
/// [criticalActions] / [criticalDeps] 用于标记关键路径(与其它视图一致)。
FlowLayout(
this.project, {
this.criticalActions = const <String>{},
this.criticalDeps = const <String>{},
}) {
_build();
}
/// 数据源项目。
final Project project;
/// 关键路径上的动作 id。
final Set<String> criticalActions;
/// 关键路径上的依赖(`from->to`)。
final Set<String> criticalDeps;
/// 动作节点尺寸(流程图与条形 / 圆周不同:横向更宽,给连线与箭头留出空间)。
static const double nodeW = 216;
/// 动作节点高度。
static const double nodeH = 58;
/// 开始 / 结束节点尺寸。
static const double capW = 116;
/// 开始 / 结束节点高度。
static const double capH = 46;
/// 层级(纵向)间距。
static const double rankGap = 48;
/// 泳道(横向)间距(加大给扇出连线让位,避免看起来拥挤)。
static const double laneGap = 64;
/// 左边距(左侧留出相位阶段栏 + 泳道间距)。
static const double marginX = 150;
/// 上边距(含泳道标题)。
static const double marginY = 92;
/// 相位阶段栏起始 x。
static const double railX = 24;
/// 相位竖带宽度(窄带,相当于把条形视图的相位色带竖起来)。
static const double railW = 24;
/// 相位台阶错位宽度(重叠的分区逐级向右错开,形成台阶)。
static const double railStep = 24;
/// 全部节点(按创建顺序:动作 → 开始 → 结束)。
final List<FlowNode> nodes = <FlowNode>[];
/// 全部连线。
final List<FlowEdge> edges = <FlowEdge>[];
/// 泳道列表(轨道号,升序)。
List<int> lanes = <int>[];
/// 相位分区跨度(左侧阶段栏,按项目分区顺序)。
final List<FlowPhaseSpan> spans = <FlowPhaseSpan>[];
/// 连线走线结果(画布 / SVG / drawio 导出共用)。
final List<FlowEdgeRoute> routes = <FlowEdgeRoute>[];
/// 内容范围(所有节点矩形并集再外扩,供适应视图使用)。
Rect bounds = Rect.zero;
/// 泳道内两个槽位之间的横向间距(留出依赖连线的走线空间)。
static const double slotGap = 26;
/// 各泳道左边缘 x(放置完成后按内容自适应填充)。
final List<double> laneStarts = <double>[];
/// 各泳道宽度(基础 = 槽位数,放置后按内容自动加宽)。
final List<double> laneWidths = <double>[];
/// 第 [li] 条泳道的宽度(优先取内容自适应结果;未计算时按槽位数)。
double laneWidth(int li) {
if (li >= 0 && li < laneWidths.length) return laneWidths[li];
if (li < 0 || li >= lanes.length) return nodeW;
final int s = project.slotsOf(lanes[li]);
return s * nodeW + (s - 1) * slotGap;
}
/// 第 [li] 条泳道左边缘 x(优先取内容自适应结果,其次按槽位宽度累加)。
double laneStartX(int li) {
if (li >= 0 && li < laneStarts.length) return laneStarts[li];
double x = marginX;
for (int i = 0; i < li && i < lanes.length; i++) {
x += laneWidth(i) + laneGap;
}
return x;
}
/// 第 [li] 条泳道第 [slot] 列的节点中心 x(槽位钳制到有效范围)。
double slotCenterX(int li, int slot) {
final int s = (li >= 0 && li < lanes.length) ? project.slotsOf(lanes[li]) : 1;
final int k = slot < 0 ? 0 : (slot >= s ? s - 1 : slot);
return laneStartX(li) + k * (nodeW + slotGap) + nodeW / 2;
}
/// 按 id 查找节点。
FlowNode? nodeById(String id) {
for (final FlowNode n in nodes) {
if (n.id == id) return n;
}
return null;
}
/// 命中测试:返回坐标 [p] 下的动作(开始 / 结束节点不返回)。
Action? hitTest(Offset p) {
for (final FlowNode n in nodes) {
if (n.action != null && n.rect.contains(p)) return n.action;
}
return null;
}
/// 构建节点、连线并完成布局。
void _build() {
lanes = project.renderLanes;
final Map<String, FlowNode> byId = <String, FlowNode>{};
// 1) 动作节点(序号与条形视图一致:项目动作列表顺序 + 1)。
for (int i = 0; i < project.actions.length; i++) {
final Action a = project.actions[i];
final String dur = tfmt(a.durationAngle / 360 * project.cycle.durationMs, project.units);
final String st = project.stationById(a.stationId)?.name ?? a.stationId;
final FlowNode n = FlowNode.action(a)
..lane = a.layer
..title = '${i + 1}. ${a.name}'
..subtitle = '$dur · $st';
nodes.add(n);
byId[n.id] = n;
}
if (nodes.isEmpty) {
bounds = Rect.zero;
return;
}
// 2) 依赖连线(FS 实线 / SS 虚线等)。
for (final Action to in project.actions) {
for (final Dependency d in to.deps) {
if (d.fromId == to.id || !byId.containsKey(d.fromId)) continue;
edges.add(FlowEdge(d.fromId, to.id,
type: d.type,
offset: d.offsetAngle,
critical: criticalDeps.contains('${d.fromId}->${to.id}')));
}
}
// 3) 开始 / 结束节点。
// 「动作1」= 时间上最早开始的动作(并列时取 id 较小者)。
final List<Action> ordered = List<Action>.of(project.actions)
..sort((Action a, Action b) {
final int c = a.startAngle.compareTo(b.startAngle);
return c != 0 ? c : a.id.compareTo(b.id);
});
final Action first = ordered.first;
Action last = ordered.first;
for (final Action a in ordered) {
if (a.endAngle > last.endAngle) last = a;
}
final FlowNode start = FlowNode.start()..lane = first.layer;
final FlowNode end = FlowNode.end()..lane = last.layer;
start.title = '开始';
end.title = '结束';
nodes.add(start);
nodes.add(end);
byId[start.id] = start;
byId[end.id] = end;
final Set<String> hasPred = <String>{for (final FlowEdge e in edges) e.toId};
final Set<String> hasSucc = <String>{for (final FlowEdge e in edges) e.fromId};
// 开始 → 无前置的动作(含「动作1」;其它无前置动作视为并行起点)。
for (final Action a in project.actions) {
if (!hasPred.contains(a.id)) edges.add(FlowEdge(start.id, a.id));
}
// 无后继的动作 → 结束。
for (final Action a in project.actions) {
if (!hasSucc.contains(a.id)) edges.add(FlowEdge(a.id, end.id));
}
// 4) 层级:从开始节点做最长路径(带迭代上限,兼容异常环)。
final Map<String, int> rank = <String, int>{for (final FlowNode n in nodes) n.id: 0};
bool changed = true;
int guard = 0;
while (changed && guard < nodes.length * 4 + 8) {
changed = false;
guard++;
for (final FlowEdge e in edges) {
final int nr = (rank[e.fromId] ?? 0) + 1;
if (nr > (rank[e.toId] ?? 0)) {
rank[e.toId] = nr;
changed = true;
}
}
}
for (final FlowNode n in nodes) {
n.rank = rank[n.id] ?? 0;
}
// 5) 坐标:泳道 → 横列(居中于列),层级 → 纵行;同格多节点横向错开。
final Map<int, int> laneIndex = <int, int>{
for (int i = 0; i < lanes.length; i++) lanes[i]: i,
};
final Map<String, int> cellTotal = <String, int>{};
for (final FlowNode n in nodes) {
final String key = '${n.lane}:${n.rank}';
cellTotal[key] = (cellTotal[key] ?? 0) + 1;
}
final Map<String, int> cellSeen = <String, int>{};
// 泳道基础几何:宽度按槽位数,左边缘按前一泳道宽度累加。
laneStarts.clear();
laneWidths.clear();
{
double x = marginX;
for (int i = 0; i < lanes.length; i++) {
final int s = project.slotsOf(lanes[i]);
laneStarts.add(x);
laneWidths.add(s * nodeW + (s - 1) * slotGap);
x += laneWidths.last + laneGap;
}
}
for (final FlowNode n in nodes) {
final int li = laneIndex[n.lane] ?? 0;
final String key = '${n.lane}:${n.rank}';
final int total = cellTotal[key] ?? 1;
final int idx = cellSeen[key] ?? 0;
cellSeen[key] = idx + 1;
final double w = (n.isStart || n.isEnd) ? capW : nodeW;
final double h = (n.isStart || n.isEnd) ? capH : nodeH;
// 开始 / 结束节点居中于泳道带;动作节点按(泳道, 槽位)分列,
// 同格多节点再沿横向错开。
final double centerX;
if (n.isStart || n.isEnd) {
centerX = laneStartX(li) + laneWidth(li) / 2;
} else {
final int slot = effectiveSlot(project, n.action!);
centerX = slotCenterX(li, slot) + (idx - (total - 1) / 2) * (nodeW * 0.62);
}
final double top = marginY + n.rank * (nodeH + rankGap) + (nodeH - h) / 2;
n.rect = Rect.fromLTWH(centerX - w / 2, top, w, h);
}
// 同层智能排开:同一 rank 行内若有水平重叠(同格多节点 / 跨列挤出),
// 从左到右依次推开,保证节点之间至少留 10px,避免“堆叠到一起看不清”。
final Map<int, List<FlowNode>> byRank = <int, List<FlowNode>>{};
for (final FlowNode n in nodes) {
byRank.putIfAbsent(n.rank, () => <FlowNode>[]).add(n);
}
for (final List<FlowNode> row in byRank.values) {
row.sort((FlowNode a, FlowNode b) => a.rect.left.compareTo(b.rect.left));
for (int i = 1; i < row.length; i++) {
final FlowNode prev = row[i - 1];
final FlowNode cur = row[i];
final double minLeft = prev.rect.right + 10;
if (cur.rect.left < minLeft) {
cur.rect = cur.rect.shift(Offset(minLeft - cur.rect.left, 0));
}
}
}
// 泳道带按内容**智能加宽**:若某泳道的节点被推到基础宽度之外,
// 先按内容算出需要的加宽量,再把该泳道及其后所有泳道整体右移,
// 保证底部泳道带完整覆盖自己的动作节点(与条形 / 圆周视图的观感一致)。
final List<double> extra = List<double>.filled(lanes.length, 0);
for (int i = 0; i < lanes.length; i++) {
double contentRight = double.negativeInfinity;
for (final FlowNode n in nodes) {
if ((laneIndex[n.lane] ?? 0) == i) {
contentRight = math.max(contentRight, n.rect.right);
}
}
if (contentRight.isFinite) {
final double need = contentRight + 26 - (laneStarts[i] + laneWidths[i]);
if (need > 0) extra[i] = need;
}
}
if (extra.any((double e) => e > 0)) {
double delta = 0;
for (int i = 0; i < lanes.length; i++) {
if (delta != 0) {
for (final FlowNode n in nodes) {
if ((laneIndex[n.lane] ?? 0) == i) {
n.rect = n.rect.shift(Offset(delta, 0));
}
}
laneStarts[i] += delta;
}
laneWidths[i] += extra[i];
// 泳道加宽后,开始 / 结束胶囊重新居中于泳道带。
if (extra[i] > 0) {
for (final FlowNode n in nodes) {
if ((n.isStart || n.isEnd) && (laneIndex[n.lane] ?? 0) == i) {
final double cx = laneStarts[i] + laneWidths[i] / 2;
n.rect = Rect.fromLTWH(
cx - n.rect.width / 2, n.rect.top, n.rect.width, n.rect.height);
}
}
}
delta += extra[i];
}
}
// 6) 连线走线:画布绘制 / SVG / drawio 导出共用(同源同层分支扇形错位)。
// 依赖上保存的手动走线偏移(routeOffset)在此叠加:
// 向下流为竖直偏移(拖动横线),同层绕行为水平偏移(拖动纵向走廊)。
// 同源同层的多条分支预先分组,横线在可用空隙内**均匀铺开**,
// 分支多时不再全部挤在同一条线上(流程图“智能排开”)。
final Map<String, List<FlowEdge>> fanGroups = <String, List<FlowEdge>>{};
for (final FlowEdge e in edges) {
final FlowNode? a = byId[e.fromId];
final FlowNode? b = byId[e.toId];
if (a == null || b == null || b.rank <= a.rank) continue;
fanGroups.putIfAbsent('${e.fromId}:${b.rank}', () => <FlowEdge>[]).add(e);
}
final Map<String, int> fanIndex = <String, int>{};
for (final FlowEdge e in edges) {
final FlowNode? a = byId[e.fromId];
final FlowNode? b = byId[e.toId];
if (a == null || b == null) continue;
double routeOffset = 0;
if (e.type != null) {
final Action? to = project.actionById(e.toId);
for (final Dependency d in to?.deps ?? const <Dependency>[]) {
if (d.fromId == e.fromId) {
routeOffset = d.routeOffset;
break;
}
}
}
if (b.rank > a.rank) {
final String key = '${e.fromId}:${b.rank}';
final List<FlowEdge> group = fanGroups[key] ?? <FlowEdge>[e];
final int idx = fanIndex[key] ?? 0;
fanIndex[key] = idx + 1;
// 横线高度:单条分支沿用“空隙中线”;多条分支在
// [源底 + 10, 目标顶 - 8] 内均匀铺开(第 idx+1 / n+1 份),再叠加手动偏移。
final double gapLow = a.rect.bottom + 10;
double gapHigh = double.infinity;
for (final FlowEdge ge in group) {
final FlowNode? gb = byId[ge.toId];
if (gb != null && gb.rect.top - 8 < gapHigh) gapHigh = gb.rect.top - 8;
}
double lane;
if (group.length <= 1) {
lane = math.min((a.rect.bottom + b.rect.top) / 2 + routeOffset, b.rect.top - 8);
} else {
final double span = math.max(gapHigh - gapLow, 8);
lane = gapLow + span * (idx + 1) / (group.length + 1) + routeOffset;
}
if (lane < gapLow) lane = gapLow;
if (lane > gapHigh) lane = gapHigh;
final double x0 = a.rect.center.dx;
final double x1 = b.rect.center.dx;
routes.add(FlowEdgeRoute(
e,
<Offset>[
Offset(x0, a.rect.bottom),
Offset(x0, lane),
Offset(x1, lane),
Offset(x1, b.rect.top),
],
Offset(x1, b.rect.top),
const Offset(0, 1),
Offset((x0 + x1) / 2, lane - 11),
RouteAxis.vertical,
));
} else {
// 同层 / 回连:右侧绕行;手动偏移作用于绕行走廊的横向位置。
final double x = math.max(a.rect.right, b.rect.right) + 22 + routeOffset;
routes.add(FlowEdgeRoute(
e,
<Offset>[
Offset(a.rect.right, a.rect.center.dy),
Offset(x, a.rect.center.dy),
Offset(x, b.rect.center.dy),
Offset(b.rect.right, b.rect.center.dy),
],
Offset(b.rect.right, b.rect.center.dy),
const Offset(-1, 0),
Offset(x + 16, (a.rect.center.dy + b.rect.center.dy) / 2),
RouteAxis.horizontal,
));
}
}
// 7) 相位分区跨度:每个动作行归属到“覆盖角度占比最大”的分区(并列取靠前分区)。
// 这样阶段栏的每一段都真实对应分区归属,不会出现两个分区同时压住同一行、
// 看起来“对不上号”的情况。
final List<PhaseZone> zones = project.phaseZones;
final Map<String, int> dominant = <String, int>{};
for (final Action a in project.actions) {
int best = -1;
double bestSpan = 0;
for (int i = 0; i < zones.length; i++) {
final double span = _angleOverlapSpan(
a.startAngle, a.endAngle, zones[i].startAngle, zones[i].endAngle);
if (span > bestSpan + 0.001) {
bestSpan = span;
best = i;
}
}
dominant[a.id] = best;
}
for (int i = 0; i < zones.length; i++) {
final PhaseZone z = zones[i];
int top = -1;
int bottom = -1;
for (final FlowNode n in nodes) {
final Action? a = n.action;
if (a == null || dominant[a.id] != i) continue;
if (top < 0 || n.rank < top) top = n.rank;
if (n.rank > bottom) bottom = n.rank;
}
if (top >= 0) spans.add(FlowPhaseSpan(z, top, bottom));
}
// 回退:没有任何主导行的分区,按“与动作行有角度重叠即覆盖”补上,
// 避免分区(如仅少量跨界的下料相位)在阶段栏中缺失。
for (int i = 0; i < zones.length; i++) {
if (spans.any((FlowPhaseSpan s) => identical(s.zone, zones[i]))) continue;
int top = -1;
int bottom = -1;
for (final FlowNode n in nodes) {
final Action? a = n.action;
if (a == null) continue;
if (!angleRangesOverlap(a.startAngle, a.endAngle, zones[i].startAngle, zones[i].endAngle)) {
continue;
}
if (top < 0 || n.rank < top) top = n.rank;
if (n.rank > bottom) bottom = n.rank;
}
if (top >= 0) spans.add(FlowPhaseSpan(zones[i], top, bottom));
}
// 纵向仍重叠的相位:轻微向右错位(每档 [railStep] px)。采用贪心图着色,
// 保证互相重叠的两个相位一定拿到不同档位(同档的必定不重叠)。
for (int i = 0; i < spans.length; i++) {
final Set<int> used = <int>{};
for (int j = 0; j < i; j++) {
final bool overlap = spans[i].rankTop <= spans[j].rankBottom &&
spans[j].rankTop <= spans[i].rankBottom;
if (overlap) used.add(spans[j].slot);
}
int slot = 0;
while (used.contains(slot)) {
slot++;
}
spans[i].slot = slot;
}
// 8) 内容范围:节点 + 连线走线 + 左侧阶段栏(按实际错位档数计算宽度,
// 保证「适应窗口」不裁切)。
Rect b = nodes.first.rect;
for (final FlowNode n in nodes) {
b = b.expandToInclude(n.rect);
}
for (final FlowEdgeRoute r in routes) {
for (final Offset p in r.points) {
b = b.expandToInclude(Rect.fromCenter(center: p, width: 30, height: 30));
}
b = b.expandToInclude(Rect.fromCenter(center: r.labelAt, width: 70, height: 26));
}
bounds = b.inflate(math.max(60, laneGap));
int maxSlot = 0;
for (final FlowPhaseSpan s in spans) {
if (s.slot > maxSlot) maxSlot = s.slot;
}
bounds = bounds.expandToInclude(
Rect.fromLTRB(
railX - 10,
bounds.top,
railX + maxSlot * railStep + railW + 24,
bounds.bottom,
),
);
}
}
/// 两个角度区间(允许超过 360° 表示跨圈)的重叠跨度(度);无重叠返回 0。
double _angleOverlapSpan(double a0, double a1, double b0, double b1) {
double best = 0;
for (final double shift in <double>[-360, 0, 360]) {
final double lo = math.max(a0, b0 + shift);
final double hi = math.min(a1, b1 + shift);
if (hi > lo) best = math.max(best, hi - lo);
}
return best;
}