mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-06 17:19:25 -05:00
The Drawings tab now shows the WinForms rows: each drawing's picture, name, "[on this plate] nested of required nested", size and area, with the fully-nested highlight and quantity bar, refreshed 50 ms after parts change on the shown plate or the nest is edited. Its toolbar sorts the list (the choice shared with the WinForms app) and hides the fully nested drawings, with the note to show them again. Its menu deletes a drawing or removes the unused ones after the WinForms confirmations, and a double-click opens Drawing Properties; each edit is one undo step through the shared commands. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
289 lines
13 KiB
C#
289 lines
13 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Avalonia;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Controls.Primitives;
|
|
using Avalonia.Input;
|
|
using Avalonia.Interactivity;
|
|
using Avalonia.Threading;
|
|
using Avalonia.VisualTree;
|
|
using OpenNest.Collections;
|
|
using OpenNest.Data;
|
|
using OpenNest.Desktop;
|
|
|
|
namespace OpenNest.Avalonia
|
|
{
|
|
/// <summary>
|
|
/// The Drawings tab, as the WinForms nest window's drawing list: each drawing's picture,
|
|
/// name, "[on this plate] nested of required nested", size and area, refreshed shortly after
|
|
/// parts come and go on the shown plate or the nest is edited. Its toolbar sorts the list
|
|
/// (the choice shared with the WinForms app) and hides the fully nested drawings; its menu
|
|
/// deletes a drawing or removes the unused ones, and a double-click opens Drawing Properties.
|
|
/// Each edit is one undo step of the nest; none runs while Auto Nest works on it.
|
|
/// </summary>
|
|
public partial class NestView
|
|
{
|
|
// As the WinForms list's timer: a fill's many per-part events refresh the list once.
|
|
private static readonly TimeSpan DrawingListDelay = TimeSpan.FromMilliseconds(50);
|
|
|
|
private readonly ObservableCollection<DrawingListItem> drawingItems = new();
|
|
private DispatcherTimer drawingListTimer;
|
|
private readonly MenuFlyout sortDrawingsMenu = new() { Placement = PlacementMode.BottomEdgeAlignedLeft };
|
|
private DrawingSortOrder drawingSortOrder;
|
|
private Plate watchedPlate;
|
|
|
|
private void InitializeDrawingList()
|
|
{
|
|
DrawingList.ItemsSource = drawingItems;
|
|
drawingListTimer = new DispatcherTimer { Interval = DrawingListDelay };
|
|
drawingListTimer.Tick += (_, _) =>
|
|
{
|
|
drawingListTimer.Stop();
|
|
RefreshDrawingList();
|
|
};
|
|
// Recorded edits (Drawing Properties included) can change any row.
|
|
Tab.Undo.Changed += (_, _) => ScheduleDrawingListRefresh();
|
|
|
|
drawingSortOrder = DrawingSort.Parse(UserSettingText.ReadString(
|
|
UserSettingsFile.Read(UserSettingsFile.DefaultPath), DrawingSort.SettingName, null));
|
|
foreach (var (order, text) in DrawingSort.Choices)
|
|
{
|
|
var item = new MenuItem { Header = text, Tag = order, ToggleType = MenuItemToggleType.Radio };
|
|
item.Click += (_, _) => SetDrawingSortOrder(order);
|
|
sortDrawingsMenu.Items.Add(item);
|
|
}
|
|
SortDrawingsButton.Flyout = sortDrawingsMenu;
|
|
UpdateSortDrawingsChoice();
|
|
|
|
HideNestedButton.IsCheckedChanged += (_, _) => RefreshDrawingList();
|
|
DrawingEmptyText.Text = DrawingListRow.AllHiddenText;
|
|
DrawingEmptyText.PointerReleased += (_, e) =>
|
|
{
|
|
if (e.InitialPressMouseButton == MouseButton.Left)
|
|
HideNestedButton.IsChecked = false;
|
|
};
|
|
|
|
DrawingList.DoubleTapped += async (_, e) =>
|
|
{
|
|
if (DrawingAt(e.Source) is { } drawing)
|
|
await EditDrawingPropertiesAsync(drawing);
|
|
};
|
|
DeleteDrawingMenuItem.Click += async (_, _) => await DeleteSelectedDrawingAsync();
|
|
RemoveUnusedDrawingsMenuItem.Click += async (_, _) => await RemoveUnusedDrawingsAsync();
|
|
DrawingMenu.Opening += (_, _) =>
|
|
{
|
|
// As WinForms: Delete needs a selected drawing; Remove Unused acts on the nest.
|
|
DeleteDrawingMenuItem.IsVisible = SelectedDrawing != null;
|
|
DrawingMenuSeparator.IsVisible = SelectedDrawing != null;
|
|
DeleteDrawingMenuItem.IsEnabled = CanEditDrawings;
|
|
RemoveUnusedDrawingsMenuItem.IsEnabled = CanEditDrawings;
|
|
};
|
|
}
|
|
|
|
/// <summary>The drawing selected in the list, or null.</summary>
|
|
public Drawing SelectedDrawing => (DrawingList.SelectedItem as DrawingListItem)?.Drawing;
|
|
|
|
/// <summary>Whether the drawing commands can run: not while Auto Nest works on the nest.</summary>
|
|
public bool CanEditDrawings => !Canvas.EditsLocked;
|
|
|
|
/// <summary>Whether Hide Fully Nested Drawings is on.</summary>
|
|
public bool HidesFullyNestedDrawings
|
|
{
|
|
get => HideNestedButton.IsChecked == true;
|
|
set => HideNestedButton.IsChecked = value;
|
|
}
|
|
|
|
private static Drawing DrawingAt(object source) =>
|
|
(source as Visual)?.FindAncestorOfType<ListBoxItem>(includeSelf: true)?.DataContext is DrawingListItem item
|
|
? item.Drawing
|
|
: null;
|
|
|
|
/// <summary>Refreshes the list shortly, once however many parts change before then.</summary>
|
|
private void ScheduleDrawingListRefresh()
|
|
{
|
|
if (!Dispatcher.UIThread.CheckAccess())
|
|
{
|
|
Dispatcher.UIThread.Post(ScheduleDrawingListRefresh);
|
|
return;
|
|
}
|
|
drawingListTimer.Stop();
|
|
drawingListTimer.Start();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Shows the nest's drawings in the chosen order, less the fully nested ones when hidden.
|
|
/// Rows update in place while the drawings and their order are unchanged; otherwise the
|
|
/// list is rebuilt keeping the selected drawing (by reference, or by name for the new
|
|
/// objects an Undo brings) and the scroll position.
|
|
/// </summary>
|
|
private void RefreshDrawingList()
|
|
{
|
|
drawingListTimer?.Stop();
|
|
var nest = Tab.Document.Nest;
|
|
var units = nest.Units;
|
|
var plate = Canvas.Plate;
|
|
var scaling = TopLevel.GetTopLevel(this)?.RenderScaling ?? 1;
|
|
var arranged = DrawingSort.Arrange(nest.Drawings.Where(d => !d.IsCutOff), drawingSortOrder,
|
|
HidesFullyNestedDrawings);
|
|
|
|
var unchanged = arranged.Count == drawingItems.Count;
|
|
for (var i = 0; unchanged && i < arranged.Count; i++)
|
|
unchanged = ReferenceEquals(arranged[i], drawingItems[i].Drawing);
|
|
|
|
if (unchanged)
|
|
{
|
|
for (var i = 0; i < arranged.Count; i++)
|
|
drawingItems[i].Update(DrawingListRow.For(arranged[i], plate, units), scaling);
|
|
}
|
|
else
|
|
{
|
|
var selected = SelectedDrawing;
|
|
var scroller = DrawingList.FindDescendantOfType<ScrollViewer>();
|
|
var offset = scroller?.Offset ?? default;
|
|
drawingItems.Clear();
|
|
foreach (var drawing in arranged)
|
|
drawingItems.Add(new DrawingListItem(DrawingListRow.For(drawing, plate, units), scaling));
|
|
DrawingList.SelectedItem = selected == null
|
|
? null
|
|
: drawingItems.FirstOrDefault(item => ReferenceEquals(item.Drawing, selected))
|
|
?? drawingItems.FirstOrDefault(item => item.Drawing.Name == selected.Name);
|
|
if (scroller != null)
|
|
Dispatcher.UIThread.Post(() => scroller.Offset = offset, DispatcherPriority.Background);
|
|
}
|
|
|
|
DrawingEmptyText.IsVisible = drawingItems.Count == 0
|
|
&& DrawingListRow.EmptyText(HidesFullyNestedDrawings, nest.Drawings.Count) != null;
|
|
}
|
|
|
|
// The shown plate's parts change the "[n]" counts and the nested quantities.
|
|
private void WatchShownPlate(Plate plate)
|
|
{
|
|
if (ReferenceEquals(watchedPlate, plate))
|
|
return;
|
|
if (watchedPlate != null)
|
|
{
|
|
watchedPlate.PartAdded -= OnShownPlatePartAdded;
|
|
watchedPlate.PartRemoved -= OnShownPlatePartRemoved;
|
|
watchedPlate.PartsReordered -= OnShownPlatePartsReordered;
|
|
}
|
|
watchedPlate = plate;
|
|
if (plate != null)
|
|
{
|
|
plate.PartAdded += OnShownPlatePartAdded;
|
|
plate.PartRemoved += OnShownPlatePartRemoved;
|
|
plate.PartsReordered += OnShownPlatePartsReordered;
|
|
}
|
|
// The counts on the newly shown plate.
|
|
if (drawingListTimer != null)
|
|
ScheduleDrawingListRefresh();
|
|
}
|
|
|
|
private void OnShownPlatePartAdded(object sender, ItemAddedEventArgs<Part> e) => ScheduleDrawingListRefresh();
|
|
|
|
private void OnShownPlatePartRemoved(object sender, ItemRemovedEventArgs<Part> e) => ScheduleDrawingListRefresh();
|
|
|
|
private void OnShownPlatePartsReordered(object sender, EventArgs e) => ScheduleDrawingListRefresh();
|
|
|
|
private void SetDrawingSortOrder(DrawingSortOrder order)
|
|
{
|
|
if (order != drawingSortOrder)
|
|
{
|
|
drawingSortOrder = order;
|
|
// As WinForms' DrawingSortOrder setting, which the WinForms app reads too.
|
|
UserSettingsFile.Update(UserSettingsFile.DefaultPath,
|
|
values => values[DrawingSort.SettingName] = order.ToString());
|
|
RefreshDrawingList();
|
|
}
|
|
UpdateSortDrawingsChoice();
|
|
}
|
|
|
|
private void UpdateSortDrawingsChoice()
|
|
{
|
|
foreach (var item in sortDrawingsMenu.Items.OfType<MenuItem>())
|
|
item.IsChecked = item.Tag is DrawingSortOrder order && order == drawingSortOrder;
|
|
var text = DrawingSort.Choices.First(c => c.Order == drawingSortOrder).Text;
|
|
ToolTip.SetTip(SortDrawingsButton, $"Sort drawings: {text}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Runs a drawing command, then rebuilds both lists and redraws the plate (a drawing's
|
|
/// color may have changed), keeping its zoom, and its selection while its parts are
|
|
/// unchanged. A command that removes drawings first ends the plate tool
|
|
/// (<paramref name="endTool"/>), which may be placing one of them.
|
|
/// </summary>
|
|
private void RunDrawingCommand(Action command, bool endTool)
|
|
{
|
|
if (endTool)
|
|
{
|
|
Canvas.Tools.EndTool();
|
|
pickup.PutDown();
|
|
pressedDrawing = null;
|
|
}
|
|
runningPlateCommand = true;
|
|
try
|
|
{
|
|
command();
|
|
}
|
|
finally
|
|
{
|
|
runningPlateCommand = false;
|
|
}
|
|
ShowPlates(System.Math.Max(0, PlateList.SelectedIndex), keep: true);
|
|
}
|
|
|
|
/// <summary>The drawing menu's Delete: confirms, then deletes the drawing and its parts.</summary>
|
|
public async Task DeleteSelectedDrawingAsync()
|
|
{
|
|
var drawing = SelectedDrawing;
|
|
if (drawing == null || !CanEditDrawings || TopLevel.GetTopLevel(this) is not Window owner)
|
|
return;
|
|
if (!await ConfirmDialog.AskAsync(owner, DrawingCommands.DeleteTitle,
|
|
DrawingCommands.DeletePrompt(drawing), "Delete"))
|
|
return;
|
|
// Auto Nest may have started, or an Undo swapped the nest, while the question was open.
|
|
if (!CanEditDrawings || !Tab.Document.Nest.Drawings.Contains(drawing))
|
|
return;
|
|
RunDrawingCommand(() => DrawingCommands.DeleteDrawing(Tab.Document.Nest, drawing,
|
|
label => Tab.Undo.BeginChange(label)), endTool: true);
|
|
}
|
|
|
|
/// <summary>The drawing menu's Remove Unused Drawings: confirms, then removes them.</summary>
|
|
public async Task RemoveUnusedDrawingsAsync()
|
|
{
|
|
if (!CanEditDrawings || TopLevel.GetTopLevel(this) is not Window owner)
|
|
return;
|
|
if (!await ConfirmDialog.AskAsync(owner, DrawingCommands.CleanUnusedTitle,
|
|
DrawingCommands.CleanUnusedPrompt, "Yes"))
|
|
return;
|
|
if (!CanEditDrawings)
|
|
return;
|
|
if (DrawingCommands.HasUnusedDrawings(Tab.Document.Nest))
|
|
RunDrawingCommand(() => DrawingCommands.CleanUnusedDrawings(Tab.Document.Nest,
|
|
label => Tab.Undo.BeginChange(label)), endTool: true);
|
|
}
|
|
|
|
/// <summary>
|
|
/// A double-click on a drawing: the Drawing Properties dialog; OK saves the values as one
|
|
/// undo step and redraws the drawing's parts in their color.
|
|
/// </summary>
|
|
public async Task EditDrawingPropertiesAsync(Drawing drawing)
|
|
{
|
|
// As WinForms: the double-click puts a picked-up drawing down.
|
|
pickup.PutDown();
|
|
pressedDrawing = null;
|
|
if (drawing == null || !CanEditDrawings || TopLevel.GetTopLevel(this) is not Window owner)
|
|
return;
|
|
var nest = Tab.Document.Nest;
|
|
var properties = await DrawingPropertiesDialog.ShowAsync(owner, drawing, nest);
|
|
if (properties == null || !CanEditDrawings || !ReferenceEquals(nest, Tab.Document.Nest)
|
|
|| !nest.Drawings.Contains(drawing))
|
|
return;
|
|
RunDrawingCommand(() => DrawingCommands.ApplyProperties(properties,
|
|
label => Tab.Undo.BeginChange(label)), endTool: false);
|
|
}
|
|
}
|
|
}
|