f4ef9b32a5
- 流程图视图:泳道 = 轨道、FS/SS 依赖连线、关键路径高亮、相位阶段栏 (每个动作行归属覆盖占比最大的分区,重叠分区台阶错位) - 无前置依赖的动作从「开始」并列引出(并行分支),无后继动作汇入「结束」 - 流程图只读:AI 生成 / 撤销重做 / 动作增删 / 属性与依赖编辑等入口自动置灰, 左栏视图模式只保留条形 / 圆周(流程图入口在顶部工具栏) - 导出升级:导出按钮合并为下拉菜单,SVG / PNG 三种视图通用; 流程图额外支持 drawio(diagrams.net 可继续编辑泳道 / 相位 / 依赖) - 连线走线统一收敛到 FlowLayout.routes,画布 / SVG / drawio 共用同一几何 - 默认示例升级为完整「装配线节拍」(8 动作 / 3 工位 / 4 相位分区) - 版本号 1.3.0+16,更新说明 release/RELEASE_NOTES_v1.3.0.md
449 lines
16 KiB
Dart
449 lines
16 KiB
Dart
/// 流程图布局:把时序图中的动作与依赖转换为纵向流程图(泳道 = 轨道)。
|
||
///
|
||
/// 产品规则(1.3 大版本,经需求确认):
|
||
/// - 每个动作 = 一个工序节点,节点内显示「序号. 名称」与时长(含工位);
|
||
/// - 泳道 = 动作的轨道(layer),横向并列,一眼看出哪些步骤在并行;
|
||
/// - 依赖:FS 实线、SS 虚线(带偏移角标注);关键路径加粗高亮;
|
||
/// - 自动补「开始 / 结束」节点:开始 → 时间最早的动作(动作1),
|
||
/// 其余无前置的动作作为并行起点同样从开始引出;无后继的动作 → 结束;
|
||
/// - 只读视图:交互仅支持点选联动与平移缩放,内容修改请回时序图。
|
||
library;
|
||
|
||
import 'dart:math' as math;
|
||
import 'dart:ui';
|
||
|
||
import '../model/models.dart';
|
||
import '../model/units.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);
|
||
|
||
/// 对应的连线。
|
||
final FlowEdge edge;
|
||
/// 折线顶点序列(含起点与箭头端点)。
|
||
final List<Offset> points;
|
||
/// 箭头端点。
|
||
final Offset tip;
|
||
/// 箭头方向(分量式单位方向)。
|
||
final Offset dir;
|
||
/// FS / SS 标签位置。
|
||
final Offset labelAt;
|
||
|
||
/// 是否虚线(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;
|
||
|
||
/// 按 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 = (<int>{for (final Action a in project.actions) a.layer}.toList())..sort();
|
||
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>{};
|
||
final double lanePitch = nodeW + 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 = marginX + li * lanePitch + nodeW / 2 +
|
||
(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);
|
||
}
|
||
|
||
// 6) 连线走线:画布绘制 / SVG / drawio 导出共用(同源同层分支扇形错位)。
|
||
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;
|
||
if (b.rank > a.rank) {
|
||
final String key = '${e.fromId}:${b.rank}';
|
||
final int idx = fanIndex[key] ?? 0;
|
||
fanIndex[key] = idx + 1;
|
||
final double lane = math.min(
|
||
(a.rect.bottom + b.rect.top) / 2 + idx * 14,
|
||
b.rect.top - 8,
|
||
);
|
||
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),
|
||
));
|
||
} else {
|
||
// 同层 / 回连:右侧绕行
|
||
final double x = math.max(a.rect.right, b.rect.right) + 22;
|
||
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),
|
||
));
|
||
}
|
||
}
|
||
|
||
// 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;
|
||
}
|