Files
OpenNest/OpenNest/Forms/EditDrawingForm.cs
T
ajandClaude Opus 5.5 f26c75ada5 feat(ui): let Drawing Properties return a custom color to automatic
Drawing Properties now says whether the drawing's color is custom or comes
from the color scheme, and an Automatic button clears the custom color. On
save the drawing gets its palette slot back through RecolorDrawings when the
nest is known, otherwise by its non-cutoff index.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 20:30:18 -04:00

214 lines
6.4 KiB
C#

using System;
using System.Drawing;
using System.Windows.Forms;
namespace OpenNest.Forms
{
public partial class EditDrawingForm : Form
{
private const int PreviewInset = 24;
private Drawing drawing;
private Nest nest;
private Color? automaticColor;
public EditDrawingForm()
{
InitializeComponent();
pictureBox1.SizeChanged += (_, _) => UpdateImage();
}
private string DrawingName
{
get { return nameBox.Text; }
set { nameBox.Text = value; }
}
private string Customer
{
get { return customerBox.Text; }
set { customerBox.Text = value; }
}
private int Quantity
{
get { return (int)qtyBox.Value; }
set { qtyBox.Value = value; }
}
private int Priority
{
get { return (int)priorityBox.Value; }
set { priorityBox.Value = value; }
}
private bool CutFirst
{
get { return cutFirstBox.Checked; }
set { cutFirstBox.Checked = value; }
}
private Image DrawingImage
{
get { return pictureBox1.Image; }
set { pictureBox1.Image = value; }
}
private void UpdateImage()
{
if (drawing == null)
return;
var size = pictureBox1.ClientSize;
if (size.Width <= PreviewInset || size.Height <= PreviewInset)
return;
var brush = new SolidBrush(colorDialog1.Color);
var pen = new Pen(ColorScheme.Default.GetPartOutlineColor(colorDialog1.Color));
var previous = DrawingImage;
DrawingImage = drawing.Program.GetImage(
new Size(size.Width - PreviewInset, size.Height - PreviewInset), pen, brush);
previous?.Dispose();
pen.Dispose();
brush.Dispose();
}
private void UpdateColor()
{
colorSwatch.BackColor = colorDialog1.Color;
var customColor = IsCustomColor;
colorModeLabel.Text = customColor ? "Custom color" : "Automatic color from the color scheme";
automaticColorButton.Enabled = customColor;
UpdateImage();
}
private void UpdateNestedLabel()
{
if (drawing == null)
return;
var nested = drawing.Quantity.Nested;
var remaining = System.Math.Max(Quantity - nested, 0);
nestedLabel.Text = $"{nested} nested, {remaining} remaining";
}
private void LoadDetails()
{
var box = drawing.Program.BoundingBox();
sizeLabel.Text = $"{box.Length:0.###} x {box.Width:0.###}";
areaLabel.Text = $"{drawing.Area:0.###}";
var path = drawing.Source?.Path;
if (string.IsNullOrEmpty(path))
{
sourceLabel.Text = "-";
toolTip.SetToolTip(sourceLabel, null);
}
else
{
sourceLabel.Text = System.IO.Path.GetFileName(path);
toolTip.SetToolTip(sourceLabel, path);
}
}
/// <summary>
/// The palette color for the drawing's slot among the nest's non-cutoff drawings.
/// </summary>
private 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);
}
private bool IsAutomaticColorRequested =>
automaticColor?.ToArgb() == colorDialog1.Color.ToArgb();
// Only a color actually picked here becomes custom; reopening and saving keeps the palette color.
private bool IsCustomColor =>
!IsAutomaticColorRequested
&& (drawing.HasCustomColor || colorDialog1.Color.ToArgb() != drawing.Color.ToArgb());
public void LoadDrawing(Drawing drawing, Nest nest = null)
{
this.drawing = drawing;
this.nest = nest;
automaticColor = null;
colorDialog1.Color = drawing.Color;
DrawingName = drawing.Name;
Customer = drawing.Customer;
Quantity = drawing.Quantity.Required;
Priority = drawing.Priority;
CutFirst = drawing.CutFirst;
Text = string.IsNullOrEmpty(drawing.Name)
? "Drawing Properties"
: $"Drawing Properties - {drawing.Name}";
LoadDetails();
UpdateNestedLabel();
UpdateColor();
}
public void SaveDrawing(Drawing drawing)
{
drawing.Name = DrawingName;
drawing.Customer = Customer;
drawing.Quantity.Required = Quantity;
drawing.Priority = Priority;
drawing.CutFirst = CutFirst;
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 = colorDialog1.Color;
drawing.HasCustomColor = true;
}
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
nameBox.SelectAll();
nameBox.Focus();
}
private void QtyBox_ValueChanged(object sender, EventArgs e) => UpdateNestedLabel();
private void ChangeColor_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog(this) != DialogResult.OK)
return;
// A picked color cancels a pending Automatic.
automaticColor = null;
UpdateColor();
}
private void AutomaticColor_Click(object sender, EventArgs e)
{
automaticColor = AutomaticColor();
colorDialog1.Color = automaticColor.Value;
UpdateColor();
}
}
}