mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-01 06:41:42 -05:00
Moves the Drawings tab's row content (quantity, size, area, highlight and quantity bar, the hidden-list note), Delete Drawing, Remove Unused Drawings and the Drawing Properties dialog's values and color rules into OpenNest.Desktop so the Avalonia app can share them. The WinForms list, nest window and Edit Drawing form delegate to them unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
172 lines
5.7 KiB
C#
172 lines
5.7 KiB
C#
using System;
|
|
using System.Drawing;
|
|
|
|
namespace OpenNest.Desktop;
|
|
|
|
/// <summary>
|
|
/// The Drawing Properties dialog's values and rules, toolkit-neutral: read from a drawing, edited
|
|
/// by the dialog, then saved back with <see cref="ApplyTo"/>. Its color is either one picked here
|
|
/// (custom) or the color scheme's palette color for the drawing's slot in the nest (Automatic);
|
|
/// only a color actually picked becomes custom, so reopening and saving keeps the palette color.
|
|
/// </summary>
|
|
public sealed class DrawingProperties
|
|
{
|
|
/// <summary>The quantity and priority boxes' limits.</summary>
|
|
public const int MaxQuantity = 999999;
|
|
|
|
public const int MinPriority = -999999;
|
|
|
|
public const int MaxPriority = 999999;
|
|
|
|
public const string CustomColorText = "Custom color";
|
|
|
|
public const string AutomaticColorText = "Automatic color from the color scheme";
|
|
|
|
// The palette color Automatic asked for, while the color is still that one.
|
|
private Color? automaticColor;
|
|
|
|
/// <summary>Reads <paramref name="drawing"/>'s values; <paramref name="nest"/> holds it (or null).</summary>
|
|
public DrawingProperties(Drawing drawing, Nest nest = null)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(drawing);
|
|
Drawing = drawing;
|
|
Nest = nest;
|
|
Color = drawing.Color;
|
|
Name = drawing.Name;
|
|
Customer = drawing.Customer;
|
|
Quantity = drawing.Quantity.Required;
|
|
Priority = drawing.Priority;
|
|
CutFirst = drawing.CutFirst;
|
|
AllowCommonLine = drawing.AllowCommonLine;
|
|
Formed = drawing.IsFormed;
|
|
}
|
|
|
|
public Drawing Drawing { get; }
|
|
|
|
public Nest Nest { get; }
|
|
|
|
public string Name { get; set; }
|
|
|
|
public string Customer { get; set; }
|
|
|
|
public int Quantity { get; set; }
|
|
|
|
public int Priority { get; set; }
|
|
|
|
public bool CutFirst { get; set; }
|
|
|
|
public bool AllowCommonLine { get; set; }
|
|
|
|
/// <summary>Formed (bent or rolled) after cutting.</summary>
|
|
public bool Formed { get; set; }
|
|
|
|
/// <summary>The color shown; set it directly to keep a pending Automatic when it is unchanged.</summary>
|
|
public Color Color { get; set; }
|
|
|
|
/// <summary>The dialog's title.</summary>
|
|
public string Title => string.IsNullOrEmpty(Drawing.Name)
|
|
? "Drawing Properties"
|
|
: $"Drawing Properties - {Drawing.Name}";
|
|
|
|
/// <summary>A color picked in the dialog: it cancels a pending Automatic.</summary>
|
|
public void PickColor(Color color)
|
|
{
|
|
automaticColor = null;
|
|
Color = color;
|
|
}
|
|
|
|
/// <summary>Automatic: the palette color for the drawing's slot, saved as not custom.</summary>
|
|
public void UseAutomaticColor()
|
|
{
|
|
automaticColor = AutomaticColor();
|
|
Color = automaticColor.Value;
|
|
}
|
|
|
|
public bool IsAutomaticColorRequested => automaticColor?.ToArgb() == Color.ToArgb();
|
|
|
|
/// <summary>Whether saving makes (or keeps) the color custom.</summary>
|
|
public bool IsCustomColor =>
|
|
!IsAutomaticColorRequested
|
|
&& (Drawing.HasCustomColor || Color.ToArgb() != Drawing.Color.ToArgb());
|
|
|
|
/// <summary>The note beside the color swatch.</summary>
|
|
public string ColorModeText => IsCustomColor ? CustomColorText : AutomaticColorText;
|
|
|
|
/// <summary>Whether Automatic can be chosen: only for a custom color.</summary>
|
|
public bool CanUseAutomaticColor => IsCustomColor;
|
|
|
|
/// <summary>The palette color for the drawing's slot among the nest's non-cutoff drawings.</summary>
|
|
public Color AutomaticColor()
|
|
{
|
|
var index = 0;
|
|
if (Nest != null)
|
|
{
|
|
foreach (var d in Nest.Drawings)
|
|
{
|
|
if (ReferenceEquals(d, Drawing))
|
|
break;
|
|
if (!d.IsCutOff)
|
|
index++;
|
|
}
|
|
}
|
|
return Drawing.GetPartColor(index);
|
|
}
|
|
|
|
/// <summary>"n nested, m remaining", the remaining counted against the quantity being edited.</summary>
|
|
public string NestedText
|
|
{
|
|
get
|
|
{
|
|
var nested = Drawing.Quantity.Nested;
|
|
var remaining = System.Math.Max(Quantity - nested, 0);
|
|
return $"{nested} nested, {remaining} remaining";
|
|
}
|
|
}
|
|
|
|
public string SizeText
|
|
{
|
|
get
|
|
{
|
|
var box = Drawing.Program.BoundingBox();
|
|
return $"{box.Length:0.###} x {box.Width:0.###}";
|
|
}
|
|
}
|
|
|
|
public string AreaText => $"{Drawing.Area:0.###}";
|
|
|
|
/// <summary>The full path of the file the drawing came from, or null.</summary>
|
|
public string SourcePath => string.IsNullOrEmpty(Drawing.Source?.Path) ? null : Drawing.Source.Path;
|
|
|
|
/// <summary>The source file's name, or "-" when there is none.</summary>
|
|
public string SourceText => SourcePath == null ? "-" : System.IO.Path.GetFileName(SourcePath);
|
|
|
|
/// <summary>Saves the values to <paramref name="drawing"/>; Automatic recolors the nest's drawings.</summary>
|
|
public void ApplyTo(Drawing drawing)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(drawing);
|
|
drawing.Name = Name;
|
|
drawing.Customer = Customer;
|
|
drawing.Quantity.Required = Quantity;
|
|
drawing.Priority = Priority;
|
|
drawing.CutFirst = CutFirst;
|
|
drawing.AllowCommonLine = AllowCommonLine;
|
|
drawing.SetFormed(Formed);
|
|
if (IsAutomaticColorRequested)
|
|
{
|
|
if (!drawing.HasCustomColor)
|
|
return;
|
|
|
|
drawing.HasCustomColor = false;
|
|
if (Nest != null && Nest.Drawings.Contains(drawing))
|
|
ColorSchemeRegistry.RecolorDrawings(Nest);
|
|
else
|
|
drawing.Color = AutomaticColor();
|
|
}
|
|
else if (IsCustomColor)
|
|
{
|
|
drawing.Color = Color;
|
|
drawing.HasCustomColor = true;
|
|
}
|
|
}
|
|
}
|