diff --git a/OpenNest.WinForms.Tests/Forms/MachineConfigNestMaterialTests.cs b/OpenNest.WinForms.Tests/Forms/MachineConfigNestMaterialTests.cs new file mode 100644 index 00000000..eef032d8 --- /dev/null +++ b/OpenNest.WinForms.Tests/Forms/MachineConfigNestMaterialTests.cs @@ -0,0 +1,124 @@ +using System.Windows.Forms; +using OpenNest.Data; +using OpenNest.Forms; + +namespace OpenNest.WinForms.Tests.Forms; + +/// +/// Opening Machine Configuration from the status bar's material selects the nest machine's +/// settings for the nest's material: the thickness when it has one on file, else the grade, +/// else the material, else the machine. +/// +public class MachineConfigNestMaterialTests +{ + private static readonly Guid LaserId = Guid.NewGuid(); + + [Theory] + [InlineData("mild steel", "a36", 0.5, "0.5")] + [InlineData("Mild Steel", "A36", 0.375, "A36")] + [InlineData("Mild Steel", "", 0.25, "0.25")] + [InlineData("Mild Steel", "A572", 0.25, "Mild Steel")] + [InlineData("Aluminum", "", 0.25, "Laser")] + public void SelectNestMaterial_SelectsTheMostSpecificNodeOnFile( + string material, string grade, double thickness, string expected) + { + RunSta(() => WithForm(form => + { + var nest = new Nest + { + Machine = new NestMachine(LaserId, "Laser"), + Material = new Material(material, grade), + Thickness = thickness, + }; + + var node = form.SelectNestMaterial(nest); + + Assert.NotNull(node); + Assert.Same(node, Find(form, "_tree").SelectedNode); + Assert.StartsWith(expected, node.Text); + Assert.Equal("Laser", Root(node).Text); + })); + } + + [Fact] + public void SelectNestMaterial_LeavesTheSelectionForAMachineNotOnFile() + { + RunSta(() => WithForm(form => + { + var tree = Find(form, "_tree"); + var before = tree.SelectedNode; + var nest = new Nest + { + Machine = new NestMachine(Guid.NewGuid(), "Other"), + Material = new Material("Mild Steel", "A36"), + Thickness = 0.5, + }; + + Assert.Null(form.SelectNestMaterial(nest)); + Assert.Same(before, tree.SelectedNode); + })); + } + + private static void WithForm(Action test) + { + var directory = Path.Combine( + Path.GetTempPath(), "OpenNestTests", Guid.NewGuid().ToString()); + Directory.CreateDirectory(directory); + try + { + var provider = new LocalJsonProvider(directory); + // A machine listed first, so a default selection never passes for the nest's one. + provider.SaveMachine(new MachineConfig + { + Name = "Another", + Materials = + { + new MaterialConfig + { + Name = "Mild Steel", + Grade = "A36", + Thicknesses = { new ThicknessConfig { Value = 0.5 } }, + }, + }, + }); + provider.SaveMachine(new MachineConfig + { + Id = LaserId, + Name = "Laser", + Materials = + { + new MaterialConfig + { + Name = "Mild Steel", + Thicknesses = { new ThicknessConfig { Value = 0.25 } }, + }, + new MaterialConfig + { + Name = "Mild Steel", + Grade = "A36", + Thicknesses = + { + new ThicknessConfig { Value = 0.25 }, + new ThicknessConfig { Value = 0.5 }, + }, + }, + }, + }); + + using var form = new MachineConfigForm(provider); + test(form); + } + finally + { + Directory.Delete(directory, true); + } + } + + private static TreeNode Root(TreeNode node) => node.Parent is null ? node : Root(node.Parent); + + private static T Find(Control root, string name) where T : Control => + Assert.IsType(root.Controls.Find(name, true).Single()); + + private static void RunSta(Action action) => + StaTestThread.Run(action, Timeout.InfiniteTimeSpan, "The STA test did not complete."); +} diff --git a/OpenNest/Forms/MachineConfigForm.cs b/OpenNest/Forms/MachineConfigForm.cs index b909dbd9..46123509 100644 --- a/OpenNest/Forms/MachineConfigForm.cs +++ b/OpenNest/Forms/MachineConfigForm.cs @@ -95,6 +95,57 @@ namespace OpenNest.Forms _tree.SelectedNode = _tree.Nodes[0]; } + /// + /// Selects the most specific node the nest's machine has on file for its material: the + /// thickness the cutting defaults use, else the grade, else the material, else the + /// machine. Returns the selected node, or null (selection unchanged) when the nest's + /// machine is not in the tree. + /// + public TreeNode SelectNestMaterial(Nest nest) + { + ArgumentNullException.ThrowIfNull(nest); + var machineNode = _tree.Nodes.Cast() + .FirstOrDefault(n => n.Tag is MachineConfig m && m.Id == nest.Machine?.Id); + if (machineNode is null) + return null; + + var machine = (MachineConfig)machineNode.Tag; + var node = FindNestMaterialNode(machineNode, machine, nest) ?? machineNode; + _tree.SelectedNode = node; + node.EnsureVisible(); + return node; + } + + private static TreeNode FindNestMaterialNode( + TreeNode machineNode, + MachineConfig machine, + Nest nest + ) + { + var name = nest.Material?.Name; + if (string.IsNullOrWhiteSpace(name)) + return null; + + var key = SharedListNames.Key(name); + var groupNode = machineNode.Nodes.Cast() + .FirstOrDefault(n => n.Tag is string group && SharedListNames.Key(group) == key); + if (groupNode is null) + return null; + + var grade = nest.Material.Grade; + var material = string.IsNullOrWhiteSpace(grade) + ? machine.GetMaterial(name) + : machine.GetMaterial(name, grade); + var gradeNode = groupNode.Nodes.Cast() + .FirstOrDefault(n => ReferenceEquals(n.Tag, material)); + if (gradeNode is null) + return groupNode; + + var thickness = MachineCuttingDefaults.Find(machine, nest); + return gradeNode.Nodes.Cast() + .FirstOrDefault(n => ReferenceEquals(n.Tag, thickness)) ?? gradeNode; + } + private static TreeNode BuildMachineNode(MachineConfig machine) { var machineNode = new TreeNode(machine.Name) { Tag = machine }; diff --git a/OpenNest/Forms/MainForm.Designer.cs b/OpenNest/Forms/MainForm.Designer.cs index b96ac4a5..58ac5315 100644 --- a/OpenNest/Forms/MainForm.Designer.cs +++ b/OpenNest/Forms/MainForm.Designer.cs @@ -1266,8 +1266,8 @@ namespace OpenNest.Forms materialStatusLabel.Padding = new System.Windows.Forms.Padding(5, 0, 5, 0); materialStatusLabel.Size = new System.Drawing.Size(85, 19); materialStatusLabel.Text = "Material: (none)"; - materialStatusLabel.ToolTipText = "Click to edit nest info"; - materialStatusLabel.Click += MaterialStatusLabel_Click; + materialStatusLabel.ToolTipText = "Click to edit nest info; right-click for more"; + materialStatusLabel.MouseUp += MaterialStatusLabel_MouseUp; // // plateUtilStatusLabel // diff --git a/OpenNest/Forms/MainForm.Status.cs b/OpenNest/Forms/MainForm.Status.cs index aea10692..47e8c697 100644 --- a/OpenNest/Forms/MainForm.Status.cs +++ b/OpenNest/Forms/MainForm.Status.cs @@ -99,8 +99,28 @@ namespace OpenNest.Forms private void LocationStatusLabel_Click(object sender, EventArgs e) => statusBar.ToggleLocationMode(); - private void MaterialStatusLabel_Click(object sender, EventArgs e) => - EditNest_Click(sender, e); + // A click edits the nest info; a right-click also offers the machine's settings for the + // nest's material and thickness. + private void MaterialStatusLabel_MouseUp(object sender, MouseEventArgs e) + { + if (activeForm == null) + return; + if (e.Button == MouseButtons.Left) + { + EditNest_Click(sender, e); + return; + } + if (e.Button != MouseButtons.Right) + return; + + var nest = activeForm.Nest; + var menu = new ContextMenuStrip(); + menu.Items.Add("Edit Nest...", null, EditNest_Click); + var showConfig = menu.Items.Add("Show in Machine Configuration...", null, + (_, _) => ShowMachineConfig(nest)); + showConfig.Enabled = nest.Machine != null; + ShowAboveStatusLabel(menu, materialStatusLabel); + } // Holds are read only on open, save and here; the status bar never polls. private void LedgerStatusLabel_Click(object sender, EventArgs e) @@ -117,9 +137,16 @@ namespace OpenNest.Forms var menu = new ContextMenuStrip(); FillMachineItems(menu.Items); + ShowAboveStatusLabel(menu, machineStatusLabel); + } + + // Opens a one-off menu above a status label; it is disposed once closed. + private void ShowAboveStatusLabel(ContextMenuStrip menu, ToolStripItem label) + { menu.Closed += (_, _) => BeginInvoke(menu.Dispose); - var bounds = machineStatusLabel.Bounds; - menu.Show(statusStrip1, new System.Drawing.Point(bounds.Left, bounds.Top), ToolStripDropDownDirection.AboveRight); + var bounds = label.Bounds; + menu.Show(statusStrip1, new System.Drawing.Point(bounds.Left, bounds.Top), + ToolStripDropDownDirection.AboveRight); } #endregion Statusbar Events diff --git a/OpenNest/Forms/MainForm.Tools.cs b/OpenNest/Forms/MainForm.Tools.cs index 0b140b7f..46cca508 100644 --- a/OpenNest/Forms/MainForm.Tools.cs +++ b/OpenNest/Forms/MainForm.Tools.cs @@ -271,7 +271,11 @@ namespace OpenNest.Forms MachineSettings.LoadSavedNestDefaults(out _)), activeForm.Nest.Machine); } - private void MachineConfig_Click(object sender, EventArgs e) + private void MachineConfig_Click(object sender, EventArgs e) => ShowMachineConfig(null); + + // Opens Machine Configuration, at the nest's machine settings for its material and + // thickness when a nest is given. + private void ShowMachineConfig(Nest nest) { var provider = MachineSettings.CreateMachineProvider(refresh: true); SharedSettings.WarnIfOffline(this); @@ -280,6 +284,8 @@ namespace OpenNest.Forms var source = SharedSettings.MachineSource(provider); if (source.Length > 0) form.Text += $" ({source})"; + if (nest != null) + form.SelectNestMaterial(nest); form.ShowDialog(this); } }