mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-06 17:19:25 -05:00
The Plates tab lists each plate as the WinForms list does: its number
("New plate" for the trailing empty one), size, part count, utilization
and, beside it, how many sheets are cut from it. A click shows the
plate, a double-click edits it, and its menu duplicates or deletes it.
Dragging a row moves the plate, Ctrl+drag drops a copy, a line showing
where; Alt+Up and Alt+Down move the current plate. The empty plate at
the end stays last.
The Plate menu gains Edit, Set as Default (the nest's default plate and
the defaults new nests start from), Rotate 90 degrees either way or 180
(fitting the turned plate), and Resize to Fit (Ctrl+P, by the auto-size
factor in Options); the Nest menu gains Remove Empty Plates. Each is
one undo step under its WinForms label, through the shared
PlateCommands, and none runs while Auto Nest works on the nest.
Edit Plate edits the quantity, size, part and edge spacing, the
quadrant with a picker (click or arrow keys) and the sheet: rectangular,
or an outline imported from a DXF, DWG or SVG file (an SVG that does not
state its size asks what its units are first), with Clear to make it a
rectangle again. Done is enabled only when the size parses and the edge
spacing leaves room on the sheet; an empty plate's new settings become
the nest's defaults.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
342 lines
14 KiB
C#
342 lines
14 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using Avalonia;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Input;
|
|
using Avalonia.Interactivity;
|
|
using Avalonia.VisualTree;
|
|
using OpenNest.Desktop;
|
|
using OpenNest.Undo;
|
|
|
|
namespace OpenNest.Avalonia
|
|
{
|
|
/// <summary>
|
|
/// The Plates tab list and the plate commands of the WinForms nest window, through the shared
|
|
/// <see cref="PlateCommands"/>: a click shows a plate, a double-click edits it, its menu
|
|
/// duplicates or deletes it, dragging a row moves the plate (Ctrl+drag copies it), and
|
|
/// Alt+Up and Alt+Down move the current plate. Each is one undo step of the nest; none runs
|
|
/// while Auto Nest works on it. The trailing empty plate stays last and cannot be dragged.
|
|
/// </summary>
|
|
public partial class NestView
|
|
{
|
|
/// <summary>The drag format carrying a <see cref="PlateDrag"/> within one nest's list.</summary>
|
|
public const string PlateFormat = "OpenNest.Plate";
|
|
|
|
/// <summary>A plate row dragged in a nest's list.</summary>
|
|
private sealed record PlateDrag(NestView Owner, int Index);
|
|
|
|
// While a plate command runs: the list rebuilds once it is done, on the plate it leaves current.
|
|
private bool runningPlateCommand;
|
|
private int pressedPlate = -1;
|
|
private Point platePressPoint;
|
|
|
|
private ChangeScope BeginChange(string label) => Tab.Undo.BeginChange(label);
|
|
|
|
private void InitializePlateList()
|
|
{
|
|
PlateList.DoubleTapped += async (_, e) =>
|
|
{
|
|
if (RowUnder(e.Source) >= 0)
|
|
await EditCurrentPlateAsync();
|
|
};
|
|
PlateList.AddHandler(KeyDownEvent, OnPlateListKeyDown, RoutingStrategies.Tunnel);
|
|
PlateList.AddHandler(PointerPressedEvent, OnPlatePressed, RoutingStrategies.Tunnel,
|
|
handledEventsToo: true);
|
|
PlateList.AddHandler(PointerMovedEvent, OnPlateDragged, RoutingStrategies.Tunnel,
|
|
handledEventsToo: true);
|
|
PlateList.AddHandler(PointerReleasedEvent, (_, _) => pressedPlate = -1,
|
|
RoutingStrategies.Tunnel, handledEventsToo: true);
|
|
DragDrop.SetAllowDrop(PlateList, true);
|
|
PlateList.AddHandler(DragDrop.DragEnterEvent, OnPlateDragOver);
|
|
PlateList.AddHandler(DragDrop.DragOverEvent, OnPlateDragOver);
|
|
PlateList.AddHandler(DragDrop.DragLeaveEvent, (_, _) => HidePlateDropIndicator());
|
|
PlateList.AddHandler(DragDrop.DropEvent, OnPlateDrop);
|
|
|
|
DuplicatePlateMenuItem.Click += (_, _) => DuplicateCurrentPlate();
|
|
DeletePlateMenuItem.Click += (_, _) => RemoveCurrentPlate();
|
|
PlateMenu.Opening += (_, _) =>
|
|
{
|
|
SyncCurrentPlate();
|
|
var index = plates.CurrentIndex;
|
|
DuplicatePlateMenuItem.IsEnabled = !Canvas.EditsLocked && plates.CanDuplicatePlate(index, index + 1);
|
|
DeletePlateMenuItem.IsEnabled = !Canvas.EditsLocked && plates.CanRemoveCurrent;
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Whether the plate commands can run: not while Auto Nest works on the nest, as the
|
|
/// WinForms progress window prevents them.
|
|
/// </summary>
|
|
public bool CanEditPlates => !Canvas.EditsLocked;
|
|
|
|
// The manager's current plate follows the list, which other edits do not move.
|
|
private void SyncCurrentPlate()
|
|
{
|
|
var index = PlateList.SelectedIndex;
|
|
if (index >= 0 && index < plates.Count && index != plates.CurrentIndex)
|
|
plates.LoadAt(index);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Runs a plate command of the current plate, then shows the plate the manager leaves
|
|
/// current, keeping the zoom, pan and selection when <paramref name="keepView"/>.
|
|
/// </summary>
|
|
private bool RunPlateCommand(Action command, bool keepView)
|
|
{
|
|
if (!CanEditPlates)
|
|
return false;
|
|
SyncCurrentPlate();
|
|
runningPlateCommand = true;
|
|
try
|
|
{
|
|
command();
|
|
}
|
|
finally
|
|
{
|
|
runningPlateCommand = false;
|
|
}
|
|
ShowPlates(plates.CurrentIndex, keepView);
|
|
return true;
|
|
}
|
|
|
|
private void ShowPlates(int index, bool keep)
|
|
{
|
|
var shown = Canvas.Plate;
|
|
keptSelection = keep && shown != null
|
|
? (Canvas.Selection.PartIndices, shown.Parts.Count)
|
|
: (Array.Empty<int>(), -1);
|
|
keepView = keep;
|
|
try
|
|
{
|
|
Refresh(index);
|
|
}
|
|
finally
|
|
{
|
|
keepView = false;
|
|
}
|
|
}
|
|
|
|
/// <summary>Delete Plate: removes the current plate.</summary>
|
|
public void RemoveCurrentPlate() =>
|
|
RunPlateCommand(() => PlateCommands.RemoveCurrentPlate(plates, BeginChange), keepView: false);
|
|
|
|
/// <summary>Duplicate Plate: a copy of the current plate right after it, which becomes current.</summary>
|
|
public void DuplicateCurrentPlate()
|
|
{
|
|
SyncCurrentPlate();
|
|
var index = plates.CurrentIndex;
|
|
DuplicatePlate(index, index + 1);
|
|
}
|
|
|
|
private void DuplicatePlate(int fromIndex, int toIndex)
|
|
{
|
|
if (plates.CanDuplicatePlate(fromIndex, toIndex))
|
|
RunPlateCommand(() => PlateCommands.DuplicatePlate(plates, fromIndex, toIndex, BeginChange),
|
|
keepView: false);
|
|
}
|
|
|
|
private void MovePlate(int fromIndex, int toIndex)
|
|
{
|
|
if (plates.CanMovePlate(fromIndex, toIndex))
|
|
RunPlateCommand(() => PlateCommands.MovePlate(plates, fromIndex, toIndex, BeginChange),
|
|
keepView: true);
|
|
}
|
|
|
|
/// <summary>Nest > Remove Empty Plates.</summary>
|
|
public void RemoveEmptyPlates() =>
|
|
RunPlateCommand(() => PlateCommands.RemoveEmptyPlates(Tab.Document.Nest, BeginChange), keepView: false);
|
|
|
|
/// <summary>Plate > Rotate, fitting the turned plate in the view.</summary>
|
|
public void RotatePlate(PlateRotation rotation)
|
|
{
|
|
if (Canvas.Plate is { } plate)
|
|
RunPlateCommand(() => PlateCommands.Rotate(plate, rotation, BeginChange), keepView: false);
|
|
}
|
|
|
|
/// <summary>Plate > Resize to Fit, rounding up to <paramref name="roundingFactor"/>.</summary>
|
|
public void ResizePlateToFit(double roundingFactor)
|
|
{
|
|
if (Canvas.Plate is { } plate)
|
|
RunPlateCommand(() => PlateCommands.ResizeToFit(plate, roundingFactor, BeginChange), keepView: false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Plate > Set as Default, the nest's part: the shown plate becomes the nest's default
|
|
/// plate. Returns the plate, for the caller to save as the new-nest defaults; null when
|
|
/// nothing ran.
|
|
/// </summary>
|
|
public Plate SetPlateAsNestDefault()
|
|
{
|
|
var plate = Canvas.Plate;
|
|
return plate != null
|
|
&& RunPlateCommand(() => PlateCommands.SetAsNestDefault(Tab.Document.Nest, plates, plate, BeginChange),
|
|
keepView: true)
|
|
? plate
|
|
: null;
|
|
}
|
|
|
|
/// <summary>Plate > Edit (or a double-click in the list): the Edit Plate dialog for the shown plate.</summary>
|
|
public async Task EditCurrentPlateAsync()
|
|
{
|
|
var plate = Canvas.Plate;
|
|
if (plate == null || !CanEditPlates || TopLevel.GetTopLevel(this) is not Window owner)
|
|
return;
|
|
var values = await EditPlateDialog.ShowAsync(owner, plate, Tab.Document.Nest.Units);
|
|
// The dialog is modal, but Auto Nest may have started on the nest before it opened.
|
|
if (values == null || !Tab.Document.Nest.Plates.Contains(plate))
|
|
return;
|
|
RunPlateCommand(() => PlateCommands.EditPlate(Tab.Document.Nest, plates, plate, values, BeginChange),
|
|
keepView: true);
|
|
}
|
|
|
|
private void OnPlateListKeyDown(object sender, KeyEventArgs e)
|
|
{
|
|
if (e.KeyModifiers != KeyModifiers.Alt || e.Key is not (Key.Up or Key.Down))
|
|
return;
|
|
SyncCurrentPlate();
|
|
var from = plates.CurrentIndex;
|
|
MovePlate(from, PlateCommands.StepTarget(from, e.Key == Key.Up));
|
|
e.Handled = true;
|
|
}
|
|
|
|
private int RowUnder(object source) =>
|
|
(source as Visual)?.FindAncestorOfType<ListBoxItem>(includeSelf: true) is { } item
|
|
? PlateList.IndexFromContainer(item)
|
|
: -1;
|
|
|
|
private void OnPlatePressed(object sender, PointerPressedEventArgs e)
|
|
{
|
|
var point = e.GetCurrentPoint(PlateList);
|
|
pressedPlate = point.Properties.IsLeftButtonPressed && e.ClickCount == 1 ? RowUnder(e.Source) : -1;
|
|
platePressPoint = point.Position;
|
|
}
|
|
|
|
// A press moved more than 3 pixels drags the plate; the trailing empty plate stays put.
|
|
private async void OnPlateDragged(object sender, PointerEventArgs e)
|
|
{
|
|
var index = pressedPlate;
|
|
var point = e.GetCurrentPoint(PlateList);
|
|
if (index < 0 || !point.Properties.IsLeftButtonPressed)
|
|
return;
|
|
var dx = point.Position.X - platePressPoint.X;
|
|
var dy = point.Position.Y - platePressPoint.Y;
|
|
if (dx * dx + dy * dy <= 9)
|
|
return;
|
|
pressedPlate = -1;
|
|
if (!CanEditPlates || index >= plates.MovableCount)
|
|
return;
|
|
|
|
var effects = DragDropEffects.None;
|
|
if (plates.MovableCount >= 2)
|
|
effects |= DragDropEffects.Move;
|
|
if (plates.CanDuplicatePlate(index, index + 1))
|
|
effects |= DragDropEffects.Copy;
|
|
if (effects == DragDropEffects.None)
|
|
return;
|
|
|
|
var data = new DataObject();
|
|
data.Set(PlateFormat, new PlateDrag(this, index));
|
|
try
|
|
{
|
|
await DragDrop.DoDragDrop(e, data, effects);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
System.Diagnostics.Debug.WriteLine($"Plate drag failed: {ex}");
|
|
}
|
|
finally
|
|
{
|
|
HidePlateDropIndicator();
|
|
}
|
|
}
|
|
|
|
private PlateDrag DraggedPlate(DragEventArgs e) =>
|
|
e.Data.Get(PlateFormat) is PlateDrag drag && ReferenceEquals(drag.Owner, this) ? drag : null;
|
|
|
|
// The gap the pointer is over: before the row under it in its upper half, after it
|
|
// otherwise; the new-plate row and below drop after the last plate.
|
|
private int DropSlot(DragEventArgs e)
|
|
{
|
|
var row = RowUnder(e.Source);
|
|
var upperHalf = false;
|
|
if (row >= 0 && PlateList.ContainerFromIndex(row) is { } item)
|
|
upperHalf = e.GetPosition(item).Y < item.Bounds.Height / 2;
|
|
return PlateListRow.DropSlot(row, upperHalf, plates.MovableCount);
|
|
}
|
|
|
|
// Ctrl during the drag drops a copy where the plate allows one.
|
|
private static bool IsCopyDrag(DragEventArgs e) =>
|
|
e.KeyModifiers.HasFlag(KeyModifiers.Control);
|
|
|
|
private bool CanDrop(PlateDrag drag, int slot, bool copying) =>
|
|
CanEditPlates && (copying
|
|
? plates.CanDuplicatePlate(drag.Index, slot)
|
|
: plates.CanMovePlate(drag.Index, PlateListRow.DropTarget(drag.Index, slot)));
|
|
|
|
private void OnPlateDragOver(object sender, DragEventArgs e)
|
|
{
|
|
if (!e.Data.Contains(PlateFormat))
|
|
return;
|
|
e.Handled = true;
|
|
var drag = DraggedPlate(e);
|
|
var slot = drag != null ? DropSlot(e) : -1;
|
|
var copying = IsCopyDrag(e);
|
|
if (drag == null || !CanDrop(drag, slot, copying))
|
|
{
|
|
e.DragEffects = DragDropEffects.None;
|
|
HidePlateDropIndicator();
|
|
return;
|
|
}
|
|
e.DragEffects = copying ? DragDropEffects.Copy : DragDropEffects.Move;
|
|
ShowPlateDropIndicator(slot);
|
|
}
|
|
|
|
private void OnPlateDrop(object sender, DragEventArgs e)
|
|
{
|
|
if (!e.Data.Contains(PlateFormat))
|
|
return;
|
|
e.Handled = true;
|
|
HidePlateDropIndicator();
|
|
var drag = DraggedPlate(e);
|
|
if (drag == null)
|
|
return;
|
|
var slot = DropSlot(e);
|
|
var copying = IsCopyDrag(e);
|
|
if (!CanDrop(drag, slot, copying))
|
|
{
|
|
e.DragEffects = DragDropEffects.None;
|
|
return;
|
|
}
|
|
e.DragEffects = copying ? DragDropEffects.Copy : DragDropEffects.Move;
|
|
if (copying)
|
|
DuplicatePlate(drag.Index, slot);
|
|
else
|
|
MovePlate(drag.Index, PlateListRow.DropTarget(drag.Index, slot));
|
|
}
|
|
|
|
// A line along the top of the row at the slot, or the bottom of the last row before it.
|
|
private void ShowPlateDropIndicator(int slot)
|
|
{
|
|
var count = PlateList.ItemCount;
|
|
if (count == 0)
|
|
return;
|
|
var below = slot >= count;
|
|
if (PlateList.ContainerFromIndex(below ? slot - 1 : slot) is not { } item)
|
|
{
|
|
HidePlateDropIndicator();
|
|
return;
|
|
}
|
|
var edge = item.TranslatePoint(new Point(0, below ? item.Bounds.Height : 0), PlateListHost);
|
|
if (edge is not { } at)
|
|
{
|
|
HidePlateDropIndicator();
|
|
return;
|
|
}
|
|
PlateDropIndicator.Margin = new Thickness(0, System.Math.Max(0, at.Y - 1), 0, 0);
|
|
PlateDropIndicator.IsVisible = true;
|
|
}
|
|
|
|
private void HidePlateDropIndicator() => PlateDropIndicator.IsVisible = false;
|
|
}
|
|
}
|