mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-01 06:41:42 -05:00
Nest > Edit opens Edit Nest as the WinForms dialog has it: the Info tab (name, dates, thickness with the standard gauges, material and grade from the posts and machines, customer, units, status and author), the Defaults tab (plate size, part and edge spacing, quadrant, and "Use these defaults for new nests") and the Notes tab. Done is enabled while the name, thickness and size read and the edge spacing leaves room; it is one "Nest Info" undo step through the shared NestCommands, a typed author is remembered for new nests, and the name stays the file's. A click on the status bar's material label opens it too (a right-click offers it in a menu), with the WinForms tooltip. Nest > Clear Nest... asks before leaving one empty plate, as one "Clear Nest" step; it is refused while Auto Nest or a save runs or the plate tool is busy. Tools > Nest Defaults... edits the machine, units, plate size, quadrant, spacing and common-line usage new nests start from, saved where they are read (the nest server once it holds them, read-only while it cannot be reached), and Save Current Plate as Defaults saves the shown plate. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
251 lines
12 KiB
C#
251 lines
12 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Avalonia;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Controls.Templates;
|
|
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>
|
|
/// Tools > Nest Defaults, as the WinForms dialog: the machine, units, plate size,
|
|
/// quadrant, part and edge spacing and Auto Nest's common-line usage new nests start
|
|
/// from. Switching units converts the values shown. The line under the values says where
|
|
/// they are saved; when that is the nest server and it cannot be reached they are shown
|
|
/// read-only and cannot be saved. Save returns the defaults, Cancel or Esc returns null.
|
|
/// </summary>
|
|
internal sealed class NestDefaultsDialog : Window
|
|
{
|
|
private readonly Guid? savedMachineId;
|
|
private readonly ComboBox machine = new() { Name = "Machine", MinWidth = 236 };
|
|
private readonly RadioButton inches = new() { Name = "Inches", Content = "Inches", GroupName = "DefaultUnits" };
|
|
private readonly RadioButton millimeters = new() { Name = "Millimeters", Content = "Millimeters", GroupName = "DefaultUnits" };
|
|
private readonly NumericUpDown width = Box("Width", NestDefaultsChoices.SizeMinimum);
|
|
private readonly NumericUpDown length = Box("Length", NestDefaultsChoices.SizeMinimum);
|
|
private readonly NumericUpDown partSpacing = Box("PartSpacing", NestDefaultsChoices.SpacingMinimum);
|
|
private readonly NumericUpDown edgeLeft = Box("EdgeLeft", NestDefaultsChoices.SpacingMinimum);
|
|
private readonly NumericUpDown edgeBottom = Box("EdgeBottom", NestDefaultsChoices.SpacingMinimum);
|
|
private readonly NumericUpDown edgeRight = Box("EdgeRight", NestDefaultsChoices.SpacingMinimum);
|
|
private readonly NumericUpDown edgeTop = Box("EdgeTop", NestDefaultsChoices.SpacingMinimum);
|
|
private readonly QuadrantPicker quadrant = new() { Name = "Quadrant", Width = 200, Height = 126 };
|
|
private readonly ComboBox commonLines = new() { Name = "CommonLines", MinWidth = 180 };
|
|
private readonly List<TextBlock> unitLabels = new();
|
|
private Units shownUnits;
|
|
|
|
private NestDefaultsDialog(NestDefaults defaults, IEnumerable<MachineConfig> machines, (string Text, bool ReadOnly) source)
|
|
{
|
|
Title = "Nest Defaults";
|
|
SizeToContent = SizeToContent.WidthAndHeight;
|
|
CanResize = false;
|
|
ShowInTaskbar = false;
|
|
WindowStartupLocation = WindowStartupLocation.CenterOwner;
|
|
|
|
savedMachineId = defaults.MachineId;
|
|
var sorted = NestDefaultsChoices.SortedMachines(machines);
|
|
machine.ItemsSource = sorted;
|
|
machine.ItemTemplate = new FuncDataTemplate<MachineConfig>((m, _) => new TextBlock { Text = m?.Name });
|
|
machine.SelectedItem = MachineSettingsSource.DefaultMachine(defaults, sorted);
|
|
machine.IsEnabled = sorted.Count > 0;
|
|
|
|
shownUnits = defaults.Units;
|
|
(defaults.Units == Units.Millimeters ? millimeters : inches).IsChecked = true;
|
|
ShowValues(defaults);
|
|
commonLines.ItemsSource = NestDefaultsChoices.CommonLineUsages.Select(c => c.Text).ToList();
|
|
commonLines.SelectedIndex = NestDefaultsChoices.CommonLineIndex(defaults.CommonLineUsage);
|
|
ToolTip.SetTip(commonLines, NestDefaultsChoices.CommonLineToolTip);
|
|
inches.IsCheckedChanged += (_, _) => UnitsChanged();
|
|
|
|
var save = new Button { Name = "Save", Content = "Save", IsDefault = true, MinWidth = 90, IsEnabled = !source.ReadOnly };
|
|
save.Click += (_, _) => Close(GetDefaults());
|
|
var cancel = new Button { Content = "Cancel", IsCancel = true, MinWidth = 90 };
|
|
cancel.Click += (_, _) => Close(null);
|
|
|
|
var general = Grid2(("Machine:", machine),
|
|
("Units:", new StackPanel { Orientation = Orientation.Horizontal, Spacing = 18, Children = { inches, millimeters } }));
|
|
var plate = Grid2(("Width:", WithUnit(width)), ("Length:", WithUnit(length)));
|
|
var quadrantPanel = new StackPanel
|
|
{
|
|
Spacing = 6,
|
|
Children = { quadrant, new TextBlock { Text = NestDefaultsChoices.QuadrantHint, Foreground = Brushes.Gray } },
|
|
};
|
|
var spacing = new Grid
|
|
{
|
|
ColumnDefinitions = new ColumnDefinitions("Auto,Auto,24,Auto,Auto,Auto,Auto,Auto"),
|
|
RowDefinitions = new RowDefinitions("Auto,Auto,Auto"),
|
|
ColumnSpacing = 6,
|
|
RowSpacing = 8,
|
|
};
|
|
Place(spacing, new TextBlock { Text = "Between parts:" }, 0, 0);
|
|
Place(spacing, WithUnit(partSpacing), 1, 0);
|
|
Place(spacing, new TextBlock { Text = "Plate edges:" }, 0, 3);
|
|
Place(spacing, WithUnit(edgeTop), 0, 5);
|
|
Place(spacing, WithUnit(edgeLeft), 1, 4);
|
|
Place(spacing, new TextBlock { Text = "Plate", HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center }, 1, 5);
|
|
Place(spacing, WithUnit(edgeRight), 1, 6);
|
|
Place(spacing, WithUnit(edgeBottom), 2, 5);
|
|
var nesting = Grid2(("Common lines:", commonLines));
|
|
|
|
var groups = new[]
|
|
{
|
|
Group("General", general), Group("Plate Size", plate), Group("Quadrant", quadrantPanel),
|
|
Group("Spacing", spacing), Group("Auto Nest", nesting),
|
|
};
|
|
// Nothing typed here could be saved, so show the values without inviting edits.
|
|
foreach (var group in groups)
|
|
group.IsEnabled = !source.ReadOnly;
|
|
var top = new StackPanel
|
|
{
|
|
Orientation = Orientation.Horizontal,
|
|
Spacing = 12,
|
|
Children = { new StackPanel { Spacing = 12, Width = 360, Children = { groups[0], groups[1] } }, groups[2] },
|
|
};
|
|
var buttons = new Grid { ColumnDefinitions = new ColumnDefinitions("*,Auto,Auto"), ColumnSpacing = 8 };
|
|
buttons.Children.Add(new TextBlock
|
|
{
|
|
Name = "Source",
|
|
Text = source.Text,
|
|
Foreground = Brushes.Gray,
|
|
VerticalAlignment = VerticalAlignment.Center,
|
|
TextWrapping = TextWrapping.Wrap,
|
|
});
|
|
Grid.SetColumn(save, 1);
|
|
buttons.Children.Add(save);
|
|
Grid.SetColumn(cancel, 2);
|
|
buttons.Children.Add(cancel);
|
|
Content = new StackPanel
|
|
{
|
|
Margin = new Thickness(12),
|
|
Spacing = 12,
|
|
Children = { top, groups[3], groups[4], buttons },
|
|
};
|
|
|
|
AddHandler(KeyDownEvent, (_, e) =>
|
|
{
|
|
if (e.Key != Key.Escape)
|
|
return;
|
|
e.Handled = true;
|
|
Close(null);
|
|
}, RoutingStrategies.Tunnel);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Shows <paramref name="defaults"/> with <paramref name="machines"/> to choose from and
|
|
/// <paramref name="source"/> (where they save, and whether they can now). Returns the
|
|
/// defaults to save, or null when the user cancelled.
|
|
/// </summary>
|
|
public static Task<NestDefaults> ShowAsync(Window owner, NestDefaults defaults, IEnumerable<MachineConfig> machines,
|
|
(string Text, bool ReadOnly) source) =>
|
|
new NestDefaultsDialog(defaults, machines, source).ShowDialog<NestDefaults>(owner);
|
|
|
|
private Units SelectedUnits => millimeters.IsChecked == true ? Units.Millimeters : Units.Inches;
|
|
|
|
private NestDefaults GetDefaults() => ReadValues(SelectedUnits);
|
|
|
|
private NestDefaults ReadValues(Units units) => new()
|
|
{
|
|
Units = units,
|
|
Size = new Geometry.Size(Number(width), Number(length)),
|
|
Quadrant = quadrant.Quadrant,
|
|
PartSpacing = Number(partSpacing),
|
|
EdgeSpacing = new Geometry.Spacing(Number(edgeLeft), Number(edgeBottom), Number(edgeRight), Number(edgeTop)),
|
|
// With no machines on file there is nothing to choose, so keep the saved choice.
|
|
MachineId = NestDefaultsChoices.MachineIdToSave(machine.SelectedItem as MachineConfig, savedMachineId),
|
|
CommonLineUsage = NestDefaultsChoices.CommonLineAt(commonLines.SelectedIndex),
|
|
};
|
|
|
|
private void ShowValues(NestDefaults defaults)
|
|
{
|
|
Show(width, defaults.Size.Width);
|
|
Show(length, defaults.Size.Length);
|
|
Show(partSpacing, defaults.PartSpacing);
|
|
Show(edgeLeft, defaults.EdgeSpacing.Left);
|
|
Show(edgeBottom, defaults.EdgeSpacing.Bottom);
|
|
Show(edgeRight, defaults.EdgeSpacing.Right);
|
|
Show(edgeTop, defaults.EdgeSpacing.Top);
|
|
quadrant.Quadrant = defaults.Quadrant;
|
|
var suffix = defaults.Units == Units.Millimeters ? "mm" : "in";
|
|
foreach (var label in unitLabels)
|
|
label.Text = suffix;
|
|
}
|
|
|
|
// Switching units converts the values shown, so a 48 in plate stays 1219.2 mm.
|
|
private void UnitsChanged()
|
|
{
|
|
var units = SelectedUnits;
|
|
if (units == shownUnits)
|
|
return;
|
|
ShowValues(ReadValues(shownUnits).ConvertedTo(units));
|
|
shownUnits = units;
|
|
}
|
|
|
|
private static void Show(NumericUpDown box, double value) =>
|
|
box.Value = (decimal)NestDefaultsChoices.Clamp(value, (double)box.Minimum, (double)box.Maximum);
|
|
|
|
private static double Number(NumericUpDown box) => (double)(box.Value ?? box.Minimum);
|
|
|
|
private static NumericUpDown Box(string name, double minimum) => new()
|
|
{
|
|
Name = name,
|
|
Minimum = (decimal)minimum,
|
|
Maximum = (decimal)NestDefaultsChoices.Maximum,
|
|
Increment = 1,
|
|
ClipValueToMinMax = true,
|
|
FormatString = "0." + new string('0', NestDefaultsChoices.Decimals),
|
|
Width = 160,
|
|
};
|
|
|
|
private Control WithUnit(NumericUpDown box)
|
|
{
|
|
var unit = new TextBlock { VerticalAlignment = VerticalAlignment.Center, Text = shownUnits == Units.Millimeters ? "mm" : "in" };
|
|
unitLabels.Add(unit);
|
|
return new StackPanel { Orientation = Orientation.Horizontal, Spacing = 4, Children = { box, unit } };
|
|
}
|
|
|
|
private static void Place(Grid grid, Control control, int row, int column)
|
|
{
|
|
if (control is TextBlock)
|
|
control.VerticalAlignment = VerticalAlignment.Center;
|
|
Grid.SetRow(control, row);
|
|
Grid.SetColumn(control, column);
|
|
grid.Children.Add(control);
|
|
}
|
|
|
|
private static Grid Grid2(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));
|
|
Place(grid, new TextBlock { Text = rows[i].Label }, i, 0);
|
|
Place(grid, rows[i].Field, i, 1);
|
|
}
|
|
return grid;
|
|
}
|
|
|
|
// 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,
|
|
},
|
|
},
|
|
};
|
|
}
|
|
}
|