mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-06 11:39:25 -05:00
Job numbers were going into Customer ("GM / 4980-88R"), which breaks
sorting, searching and grouping by customer. A nest now has its own
Job # field: one or several jobs separated by commas, normalized to
"a, b". It is saved in nest.json and the server (schema v21), edited
beside Customer in Edit Nest (WinForms and Avalonia), shown as a
sortable, searchable column in the Open list, and printed on the nest
report and in MCP output.
Deploy the server before the desktop: the open overlay takes the
server's job number, and an older server has none.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
382 lines
17 KiB
C#
382 lines
17 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Avalonia;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Input;
|
|
using Avalonia.Interactivity;
|
|
using Avalonia.Layout;
|
|
using Avalonia.Media;
|
|
using OpenNest.Avalonia.Controls;
|
|
using OpenNest.Data;
|
|
using OpenNest.Desktop;
|
|
|
|
namespace OpenNest.Avalonia
|
|
{
|
|
/// <summary>What Edit Nest's Done returns: the values to write and whether new nests start from them.</summary>
|
|
internal sealed record EditNestResult(NestInfoValues Values, bool UseForNewNests);
|
|
|
|
/// <summary>
|
|
/// Nest > Edit, as the WinForms Edit Nest dialog: the Info tab (name, dates, thickness,
|
|
/// material and grade, customer, job number, units, status and author), the Defaults tab (the plate
|
|
/// size, part and edge spacing and quadrant new plates start from, and whether new nests
|
|
/// start from them too) and the Notes tab. The name is read-only unless renaming is
|
|
/// allowed (in File mode the name is the file's, set by Save As). Done is enabled while
|
|
/// <see cref="NestInfoValues.CanApply"/> holds; it returns the values (the caller writes
|
|
/// the nest as one undo step), Cancel or Esc returns null.
|
|
/// </summary>
|
|
internal sealed class EditNestDialog : Window
|
|
{
|
|
private readonly TextBox name = new() { Name = "NestName" };
|
|
private readonly TextBox created = new() { Name = "DateCreated", IsReadOnly = true };
|
|
private readonly TextBox modified = new() { Name = "DateLastModified", IsReadOnly = true };
|
|
private readonly EditableCombo thickness = new() { Name = "Thickness" };
|
|
private readonly EditableCombo material = new() { Name = "Material" };
|
|
private readonly EditableCombo grade = new() { Name = "Grade" };
|
|
private readonly TextBox customer = new() { Name = "Customer" };
|
|
private readonly TextBox jobNumber = new() { Name = "JobNumber", Watermark = "Separate several with commas" };
|
|
private readonly RadioButton inches = new() { Name = "Inches", Content = "Inches", GroupName = "NestUnits" };
|
|
private readonly RadioButton millimeters = new() { Name = "Millimeters", Content = "Millimeters", GroupName = "NestUnits" };
|
|
private readonly ComboBox status = new() { Name = "Status", HorizontalAlignment = HorizontalAlignment.Stretch };
|
|
private readonly TextBox madeBy = new() { Name = "MadeBy" };
|
|
private readonly TextBox size = new() { Name = "Size" };
|
|
private readonly NumericUpDown partSpacing;
|
|
private readonly NumericUpDown left;
|
|
private readonly NumericUpDown top;
|
|
private readonly NumericUpDown right;
|
|
private readonly NumericUpDown bottom;
|
|
private readonly List<TextBlock> unitLabels = new();
|
|
private readonly QuadrantPicker quadrant = new() { Name = "Quadrant", Width = 200, Height = 126 };
|
|
private readonly CheckBox useForNewNests = new() { Name = "UseForNewNests", Content = "Use these defaults for new nests" };
|
|
private readonly TextBox notes = new()
|
|
{
|
|
Name = "Notes",
|
|
AcceptsReturn = true,
|
|
TextWrapping = TextWrapping.Wrap,
|
|
MinHeight = 300,
|
|
};
|
|
private readonly TextBlock releaseOrigin = new() { Name = "ReleaseOrigin", VerticalAlignment = VerticalAlignment.Center };
|
|
private readonly Button done = new() { Name = "Done", Content = "Done", IsDefault = true, MinWidth = 90 };
|
|
private readonly NestInfoValues shown;
|
|
private readonly List<MaterialConfig> catalog;
|
|
private Units thicknessUnits;
|
|
private string gradeMaterialKey = "";
|
|
|
|
private EditNestDialog(NestInfoValues values, IEnumerable<MaterialConfig> materials, bool allowRename,
|
|
bool offerNewNestDefaults, string releaseOriginText)
|
|
{
|
|
shown = values;
|
|
catalog = (materials ?? Enumerable.Empty<MaterialConfig>()).ToList();
|
|
Title = "Edit Nest";
|
|
Width = 620;
|
|
SizeToContent = SizeToContent.Height;
|
|
CanResize = false;
|
|
ShowInTaskbar = false;
|
|
WindowStartupLocation = WindowStartupLocation.CenterOwner;
|
|
|
|
partSpacing = Box("PartSpacing", values.PartSpacing);
|
|
left = Box("Left", values.LeftSpacing);
|
|
top = Box("Top", values.TopSpacing);
|
|
right = Box("Right", values.RightSpacing);
|
|
bottom = Box("Bottom", values.BottomSpacing);
|
|
|
|
name.Text = values.Name;
|
|
name.IsReadOnly = !allowRename;
|
|
created.Text = values.DateCreated.ToShortDateString();
|
|
modified.Text = values.DateLastModified.ToShortDateString();
|
|
customer.Text = values.Customer;
|
|
jobNumber.Text = values.JobNumber;
|
|
madeBy.Text = values.MadeBy;
|
|
notes.Text = values.Notes;
|
|
size.Text = values.SizeText;
|
|
quadrant.Quadrant = values.Quadrant;
|
|
useForNewNests.IsVisible = offerNewNestDefaults;
|
|
releaseOrigin.Text = releaseOriginText ?? "";
|
|
|
|
status.ItemsSource = NestStatuses.WorkflowOrder;
|
|
status.ItemTemplate = new global::Avalonia.Controls.Templates.FuncDataTemplate<NestStatus>(
|
|
(value, _) => new TextBlock { Text = NestStatuses.DisplayName(value) });
|
|
status.SelectedItem = values.Status;
|
|
|
|
// The material list: the posts' materials and the machines' catalog; the grades on
|
|
// file follow the material.
|
|
material.Items = NestInfoValues.MaterialNames(PostProcessorMaterials.Names, catalog);
|
|
material.Text = values.MaterialName;
|
|
grade.Text = values.MaterialGrade;
|
|
gradeMaterialKey = SharedListNames.Key(values.MaterialName);
|
|
grade.Items = NestInfoValues.Grades(catalog, values.MaterialName);
|
|
material.TextChanged += (_, _) => RefreshGrades();
|
|
|
|
thicknessUnits = values.Units;
|
|
thickness.Items = ThicknessItems(values.Units);
|
|
thickness.Text = StandardThicknesses.Format(values.Thickness, values.Units);
|
|
// A typed gauge or matching decimal shows as its listed label, as WinForms does.
|
|
thickness.LostFocusText += (_, _) =>
|
|
{
|
|
if (StandardThicknesses.TryParse(thickness.Text, thicknessUnits, out var value)
|
|
&& thickness.Text.Trim().Length > 0)
|
|
thickness.Text = StandardThicknesses.Format(value, thicknessUnits);
|
|
};
|
|
(values.Units == Units.Millimeters ? millimeters : inches).IsChecked = true;
|
|
inches.IsCheckedChanged += (_, _) => UnitsChanged();
|
|
|
|
name.TextChanged += (_, _) => EnableCheck();
|
|
thickness.TextChanged += (_, _) => EnableCheck();
|
|
size.TextChanged += (_, _) => EnableCheck();
|
|
foreach (var box in new[] { partSpacing, left, top, right, bottom })
|
|
box.ValueChanged += (_, _) => EnableCheck();
|
|
|
|
done.Click += (_, _) => Close(new EditNestResult(Values(), useForNewNests.IsVisible && useForNewNests.IsChecked == true));
|
|
var cancel = new Button { Content = "Cancel", IsCancel = true, MinWidth = 90 };
|
|
cancel.Click += (_, _) => Close(null);
|
|
|
|
var tabs = new TabControl
|
|
{
|
|
Items =
|
|
{
|
|
new TabItem { Header = "Info", Content = InfoTab() },
|
|
new TabItem { Header = "Defaults", Content = DefaultsTab() },
|
|
new TabItem { Header = "Notes", Content = notes },
|
|
},
|
|
};
|
|
var buttons = new Grid { ColumnDefinitions = new ColumnDefinitions("*,Auto,Auto"), ColumnSpacing = 8 };
|
|
buttons.Children.Add(releaseOrigin);
|
|
Grid.SetColumn(done, 1);
|
|
buttons.Children.Add(done);
|
|
Grid.SetColumn(cancel, 2);
|
|
buttons.Children.Add(cancel);
|
|
Content = new StackPanel
|
|
{
|
|
Margin = new Thickness(14),
|
|
Spacing = 14,
|
|
Children = { tabs, buttons },
|
|
};
|
|
|
|
AddHandler(KeyDownEvent, (_, e) =>
|
|
{
|
|
if (e.Key != Key.Escape)
|
|
return;
|
|
e.Handled = true;
|
|
Close(null);
|
|
}, RoutingStrategies.Tunnel);
|
|
Opened += (_, _) =>
|
|
{
|
|
if (allowRename)
|
|
name.Focus();
|
|
else
|
|
thickness.FocusText();
|
|
};
|
|
UpdateUnitLabels();
|
|
EnableCheck();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Shows the dialog on <paramref name="nest"/>'s info. Returns what Done applies, or null
|
|
/// when the user cancelled.
|
|
/// </summary>
|
|
public static Task<EditNestResult> ShowAsync(Window owner, Nest nest, IEnumerable<MaterialConfig> materials,
|
|
bool allowRename = false, bool offerNewNestDefaults = true, string releaseOrigin = null) =>
|
|
new EditNestDialog(NestInfoValues.FromNest(nest), materials, allowRename, offerNewNestDefaults, releaseOrigin)
|
|
.ShowDialog<EditNestResult>(owner);
|
|
|
|
private Control InfoTab()
|
|
{
|
|
var unitsRow = new StackPanel { Orientation = Orientation.Horizontal, Spacing = 18, Children = { inches, millimeters } };
|
|
var grid = Rows(
|
|
("Name :", name),
|
|
("Date Created :", created),
|
|
("Date Last Modified :", modified),
|
|
("Thickness :", thickness),
|
|
("Material :", material),
|
|
("Grade :", grade),
|
|
("Customer :", customer),
|
|
("Job # :", jobNumber),
|
|
("Units :", unitsRow),
|
|
("Status :", status),
|
|
("Made By :", madeBy));
|
|
grid.Margin = new Thickness(6, 10);
|
|
return grid;
|
|
}
|
|
|
|
private Control DefaultsTab()
|
|
{
|
|
var plate = new Grid
|
|
{
|
|
ColumnDefinitions = new ColumnDefinitions("Auto,*,Auto"),
|
|
RowDefinitions = new RowDefinitions("Auto,Auto"),
|
|
ColumnSpacing = 8,
|
|
RowSpacing = 8,
|
|
};
|
|
AddRow(plate, 0, "Size :", size, null);
|
|
AddRow(plate, 1, "Part Spacing :", partSpacing, UnitLabel());
|
|
|
|
var edges = new Grid
|
|
{
|
|
ColumnDefinitions = new ColumnDefinitions("Auto,*,Auto"),
|
|
RowDefinitions = new RowDefinitions("Auto,Auto,Auto,Auto"),
|
|
ColumnSpacing = 8,
|
|
RowSpacing = 8,
|
|
};
|
|
AddRow(edges, 0, "Left :", left, UnitLabel());
|
|
AddRow(edges, 1, "Top :", top, UnitLabel());
|
|
AddRow(edges, 2, "Right :", right, UnitLabel());
|
|
AddRow(edges, 3, "Bottom :", bottom, UnitLabel());
|
|
|
|
var leftColumn = new StackPanel { Spacing = 14, Width = 280, Children = { plate, Group("Edge Spacing", edges) } };
|
|
var columns = new StackPanel
|
|
{
|
|
Orientation = Orientation.Horizontal,
|
|
Spacing = 16,
|
|
Children = { leftColumn, Group("Quadrant", quadrant) },
|
|
};
|
|
return new StackPanel { Margin = new Thickness(6, 10), Spacing = 14, Children = { columns, useForNewNests } };
|
|
}
|
|
|
|
private TextBlock UnitLabel()
|
|
{
|
|
var label = new TextBlock { VerticalAlignment = VerticalAlignment.Center };
|
|
unitLabels.Add(label);
|
|
return label;
|
|
}
|
|
|
|
// As the WinForms boxes: 0 to 999999, four decimals, a quarter step.
|
|
private static NumericUpDown Box(string name, double value) => new()
|
|
{
|
|
Name = name,
|
|
Minimum = (decimal)NestInfoValues.SpacingMinimum,
|
|
Maximum = (decimal)System.Math.Max(NestInfoValues.SpacingMaximum, value),
|
|
Increment = (decimal)NestInfoValues.SpacingIncrement,
|
|
ClipValueToMinMax = true,
|
|
FormatString = "0." + new string('0', NestInfoValues.SpacingDecimals),
|
|
Value = (decimal)value,
|
|
MinWidth = 150,
|
|
};
|
|
|
|
private static Grid Rows(params (string Label, Control Field)[] rows)
|
|
{
|
|
var grid = new Grid
|
|
{
|
|
ColumnDefinitions = new ColumnDefinitions("Auto,*"),
|
|
ColumnSpacing = 10,
|
|
RowSpacing = 8,
|
|
};
|
|
for (var i = 0; i < rows.Length; i++)
|
|
{
|
|
grid.RowDefinitions.Add(new RowDefinition(GridLength.Auto));
|
|
AddRow(grid, i, rows[i].Label, rows[i].Field, null);
|
|
}
|
|
return grid;
|
|
}
|
|
|
|
private static void AddRow(Grid grid, int row, string label, Control field, TextBlock suffix)
|
|
{
|
|
var text = new TextBlock
|
|
{
|
|
Text = label,
|
|
VerticalAlignment = VerticalAlignment.Center,
|
|
HorizontalAlignment = HorizontalAlignment.Right,
|
|
};
|
|
Grid.SetRow(text, row);
|
|
grid.Children.Add(text);
|
|
Grid.SetRow(field, row);
|
|
Grid.SetColumn(field, 1);
|
|
grid.Children.Add(field);
|
|
if (suffix == null)
|
|
return;
|
|
Grid.SetRow(suffix, row);
|
|
Grid.SetColumn(suffix, 2);
|
|
grid.Children.Add(suffix);
|
|
}
|
|
|
|
// A titled box, as the WinForms group boxes.
|
|
private static Control Group(string header, Control content) => new StackPanel
|
|
{
|
|
Spacing = 4,
|
|
Children =
|
|
{
|
|
new TextBlock { Text = header, FontWeight = FontWeight.SemiBold },
|
|
new Border
|
|
{
|
|
BorderBrush = Brush.Parse("#C8CDD2"),
|
|
BorderThickness = new Thickness(1),
|
|
Padding = new Thickness(10),
|
|
Child = content,
|
|
},
|
|
},
|
|
};
|
|
|
|
private static IReadOnlyList<string> ThicknessItems(Units units) =>
|
|
StandardThicknesses.Entries.Select(entry => StandardThicknesses.Label(entry, units)).ToList();
|
|
|
|
private Units SelectedUnits => millimeters.IsChecked == true ? Units.Millimeters : Units.Inches;
|
|
|
|
// The spacing boxes' unit follows the choice, and the thickness keeps its number while
|
|
// its list changes units, as the WinForms thickness box does.
|
|
private void UnitsChanged()
|
|
{
|
|
var units = SelectedUnits;
|
|
if (units == thicknessUnits)
|
|
return;
|
|
var valid = StandardThicknesses.TryParse(thickness.Text, thicknessUnits, out var value);
|
|
thicknessUnits = units;
|
|
thickness.Items = ThicknessItems(units);
|
|
if (valid)
|
|
thickness.Text = StandardThicknesses.Format(value, units);
|
|
UpdateUnitLabels();
|
|
EnableCheck();
|
|
}
|
|
|
|
private void UpdateUnitLabels()
|
|
{
|
|
var unit = UnitsHelper.GetShortString(SelectedUnits);
|
|
foreach (var label in unitLabels)
|
|
label.Text = unit;
|
|
}
|
|
|
|
// A grade belongs to its material, so a new material clears a grade it does not have.
|
|
private void RefreshGrades()
|
|
{
|
|
var grades = NestInfoValues.Grades(catalog, material.Text);
|
|
var text = NestInfoValues.GradeAfterMaterialChange(gradeMaterialKey, material.Text, grade.Text, grades);
|
|
gradeMaterialKey = SharedListNames.Key(material.Text);
|
|
grade.Items = grades;
|
|
grade.Text = text;
|
|
}
|
|
|
|
private static double Number(NumericUpDown box) => (double)(box.Value ?? 0);
|
|
|
|
private bool ThicknessValid(out double value) =>
|
|
StandardThicknesses.TryParse(thickness.Text, thicknessUnits, out value);
|
|
|
|
private NestInfoValues Values()
|
|
{
|
|
ThicknessValid(out var thicknessValue);
|
|
return shown with
|
|
{
|
|
Name = (name.Text ?? "").Trim(),
|
|
Thickness = thicknessValue,
|
|
// No box edits the name here yet: it stays with its thickness and goes with it.
|
|
ThicknessName = thicknessValue == shown.Thickness ? shown.ThicknessName : "",
|
|
MaterialName = material.Text,
|
|
MaterialGrade = grade.Text,
|
|
Customer = customer.Text ?? "",
|
|
JobNumber = jobNumber.Text ?? "",
|
|
Units = SelectedUnits,
|
|
Status = status.SelectedItem is NestStatus value ? value : NestStatus.Quote,
|
|
MadeBy = madeBy.Text ?? "",
|
|
Notes = notes.Text ?? "",
|
|
SizeText = size.Text ?? "",
|
|
PartSpacing = Number(partSpacing),
|
|
LeftSpacing = Number(left),
|
|
TopSpacing = Number(top),
|
|
RightSpacing = Number(right),
|
|
BottomSpacing = Number(bottom),
|
|
Quadrant = quadrant.Quadrant,
|
|
};
|
|
}
|
|
|
|
private void EnableCheck() => done.IsEnabled = Values().CanApply(ThicknessValid(out _));
|
|
}
|
|
}
|