mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-26 20:37:42 -05:00
feat(desktop): open the nest's material in Machine Configuration from the status bar
Right-clicking the status bar material offers Edit Nest... and Show in Machine Configuration..., which opens the configuration with the nest machine's thickness for its material selected (else the grade, the material or the machine). A left click still edits the nest.
This commit is contained in:
1 parent
197d77a560
commit
0d21e71d24
5 files changed
+215
-7
No files matched your search
@@ -0,0 +1,124 @@
|
||||
using System.Windows.Forms;
|
||||
using OpenNest.Data;
|
||||
using OpenNest.Forms;
|
||||
|
||||
namespace OpenNest.WinForms.Tests.Forms;
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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<TreeView>(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<TreeView>(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<MachineConfigForm> 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<T>(Control root, string name) where T : Control =>
|
||||
Assert.IsType<T>(root.Controls.Find(name, true).Single());
|
||||
|
||||
private static void RunSta(Action action) =>
|
||||
StaTestThread.Run(action, Timeout.InfiniteTimeSpan, "The STA test did not complete.");
|
||||
}
|
||||
@@ -95,6 +95,57 @@ namespace OpenNest.Forms
|
||||
_tree.SelectedNode = _tree.Nodes[0];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public TreeNode SelectNestMaterial(Nest nest)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(nest);
|
||||
var machineNode = _tree.Nodes.Cast<TreeNode>()
|
||||
.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<TreeNode>()
|
||||
.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<TreeNode>()
|
||||
.FirstOrDefault(n => ReferenceEquals(n.Tag, material));
|
||||
if (gradeNode is null)
|
||||
return groupNode;
|
||||
|
||||
var thickness = MachineCuttingDefaults.Find(machine, nest);
|
||||
return gradeNode.Nodes.Cast<TreeNode>()
|
||||
.FirstOrDefault(n => ReferenceEquals(n.Tag, thickness)) ?? gradeNode;
|
||||
}
|
||||
|
||||
private static TreeNode BuildMachineNode(MachineConfig machine)
|
||||
{
|
||||
var machineNode = new TreeNode(machine.Name) { Tag = machine };
|
||||
|
||||
Generated
+2
-2
@@ -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
|
||||
//
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user