mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-02 11:01:37 -05:00
feat(ui): select a selected part's drawing in the drawing list
Selecting parts on the plate selects their drawing in the nest window's drawing list when they all share one; a mixed or empty selection, or a drawing the list hides, leaves the list's selection as it was. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1f7fd248fe
commit
6ad65a7091
2 files changed
+92
No files matched your search
@@ -0,0 +1,61 @@
|
||||
using OpenNest.Controls;
|
||||
using OpenNest.Geometry;
|
||||
using Size = OpenNest.Geometry.Size;
|
||||
|
||||
namespace OpenNest.WinForms.Tests.Forms;
|
||||
|
||||
/// <summary>Selecting parts on the plate selects their drawing in the drawing list.</summary>
|
||||
public class DrawingListSelectionSyncTests
|
||||
{
|
||||
private static Nest TwoDrawingNest()
|
||||
{
|
||||
var nest = new Nest("sync");
|
||||
var a = new Drawing("a", UndoTestEnv.Square(10));
|
||||
var b = new Drawing("b", UndoTestEnv.Square(12));
|
||||
nest.Drawings.Add(a);
|
||||
nest.Drawings.Add(b);
|
||||
var plate = nest.CreatePlate();
|
||||
plate.Size = new Size(100, 100);
|
||||
plate.Parts.Add(new Part(a, new Vector(5, 5)));
|
||||
plate.Parts.Add(new Part(b, new Vector(40, 5)));
|
||||
return nest;
|
||||
}
|
||||
|
||||
private static DrawingListBox List(UndoTestEnv env) =>
|
||||
(DrawingListBox)Assert.Single(env.Editor.Controls.Find("drawingListBox1", true));
|
||||
|
||||
private static void Select(UndoTestEnv env, params string[] drawings)
|
||||
{
|
||||
env.View.DeselectAll();
|
||||
foreach (var layout in env.View.LayoutParts.Where(p => drawings.Contains(p.BasePart.BaseDrawing.Name)))
|
||||
env.View.SelectedParts.Add(layout);
|
||||
env.View.NotifySelectionChanged();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectingAPartSelectsItsDrawing() => UndoTestEnv.Run(env =>
|
||||
{
|
||||
env.Editor.PlateManager.LoadAt(0);
|
||||
var list = List(env);
|
||||
|
||||
Select(env, "b");
|
||||
Assert.Equal("b", ((Drawing)list.SelectedItem!).Name);
|
||||
|
||||
Select(env, "a");
|
||||
Assert.Equal("a", ((Drawing)list.SelectedItem!).Name);
|
||||
}, TwoDrawingNest);
|
||||
|
||||
[Fact]
|
||||
public void MixedOrEmptySelectionKeepsTheListSelection() => UndoTestEnv.Run(env =>
|
||||
{
|
||||
env.Editor.PlateManager.LoadAt(0);
|
||||
var list = List(env);
|
||||
|
||||
Select(env, "b");
|
||||
Select(env, "a", "b");
|
||||
Assert.Equal("b", ((Drawing)list.SelectedItem!).Name);
|
||||
|
||||
Select(env);
|
||||
Assert.Equal("b", ((Drawing)list.SelectedItem!).Name);
|
||||
}, TwoDrawingNest);
|
||||
}
|
||||
@@ -94,6 +94,7 @@ namespace OpenNest.Forms
|
||||
PlateView.PartAdded += PlateView_PartAdded;
|
||||
PlateView.PartRemoved += PlateView_PartRemoved;
|
||||
PlateView.PartsReordered += PlateView_PartsReordered;
|
||||
PlateView.SelectionChanged += PlateView_SelectionChanged;
|
||||
PlateView.Dock = DockStyle.Fill;
|
||||
PlateView.ClampNest = () => Nest;
|
||||
|
||||
@@ -1857,6 +1858,36 @@ namespace OpenNest.Forms
|
||||
updateDrawingListTimer.Start();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Selects the selected parts' drawing in the drawing list when they all share one, so
|
||||
/// picking a part on the plate shows its drawing. A mixed or empty selection, or a
|
||||
/// drawing the list hides, leaves the list's selection as it was.
|
||||
/// </summary>
|
||||
private void PlateView_SelectionChanged(object sender, EventArgs e)
|
||||
{
|
||||
var drawing = SelectedPartsDrawing(PlateView.SelectedParts.Select(p => p.BasePart));
|
||||
if (drawing == null || ReferenceEquals(drawingListBox1.SelectedItem, drawing)
|
||||
|| !drawingListBox1.Items.Contains(drawing))
|
||||
return;
|
||||
|
||||
drawingListBox1.SelectedItem = drawing;
|
||||
}
|
||||
|
||||
/// <summary>The one drawing every part in <paramref name="parts"/> is made from, or null.</summary>
|
||||
internal static Drawing SelectedPartsDrawing(IEnumerable<Part> parts)
|
||||
{
|
||||
Drawing drawing = null;
|
||||
foreach (var part in parts)
|
||||
{
|
||||
if (drawing == null)
|
||||
drawing = part.BaseDrawing;
|
||||
else if (!ReferenceEquals(drawing, part.BaseDrawing))
|
||||
return null;
|
||||
}
|
||||
|
||||
return drawing;
|
||||
}
|
||||
|
||||
private void PlateView_PartsReordered(object sender, EventArgs e)
|
||||
{
|
||||
updateDrawingListTimer.Stop();
|
||||
|
||||
Reference in new issue
Block a user