diff --git a/OpenNest.Desktop.Tests/Drawings/DrawingCommandsTests.cs b/OpenNest.Desktop.Tests/Drawings/DrawingCommandsTests.cs new file mode 100644 index 00000000..fa711d5d --- /dev/null +++ b/OpenNest.Desktop.Tests/Drawings/DrawingCommandsTests.cs @@ -0,0 +1,135 @@ +using System.Drawing; +using OpenNest.Undo; + +namespace OpenNest.Desktop.Tests.Drawings; + +public class DrawingCommandsTests +{ + /// + /// A nest window's state: "square" (two required) with one part on each of two plates, + /// "unused" with none, the trailing empty plate and a history started on it. + /// + private sealed class Env + { + public Env() + { + var nest = TestNests.OneSquare(); + Square = nest.Drawings.First(); + Square.Quantity.Required = 2; + Unused = new Drawing("unused", (CNC.Program)Square.Program.Clone()) { Color = Color.Coral }; + nest.Drawings.Add(Unused); + nest.Plates[0].Quantity = 1; + var second = new Plate(new Geometry.Size(48, 96)) { Quantity = 1 }; + second.Parts.Add(new Part(Square)); + nest.Plates.Add(second); + nest.UpdateDrawingQuantities(); + Document = new Document { Nest = nest }; + Plates = new PlateManager(nest); + Plates.EnsureSentinel(); + Undo = new NestUndoRecorder(Document); + Undo.Reset(); + } + + public Drawing Square { get; } + public Drawing Unused { get; } + public Document Document { get; } + public Nest Nest => Document.Nest; + public PlateManager Plates { get; } + public NestUndoRecorder Undo { get; } + + public ChangeScope BeginChange(string label) => Undo.BeginChange(label); + + public int Steps => Undo.History.UndoCount; + + public int SquareParts => Nest.Plates.Sum(p => p.Parts.Count(part => part.BaseDrawing.Name == "square")); + } + + [Fact] + public void DeleteDrawing_RemovesItsPartsAndTheDrawing_AsOneStep() + { + var env = new Env(); + Assert.Equal(2, env.SquareParts); + + DrawingCommands.DeleteDrawing(env.Nest, env.Square, env.BeginChange); + + Assert.Equal(0, env.SquareParts); + Assert.DoesNotContain(env.Square, env.Nest.Drawings); + Assert.Single(env.Nest.Drawings); + Assert.Equal(1, env.Steps); + Assert.Equal("Undo Delete Drawing", env.Undo.UndoText); + + env.Undo.Undo(); + Assert.Equal(2, env.SquareParts); + Assert.Equal(2, env.Nest.Drawings.Count); + Assert.Equal(2, env.Nest.Drawings.Single(d => d.Name == "square").Quantity.Nested); + } + + [Fact] + public void DeletePrompt_NamesTheDrawing() => + Assert.Equal("Delete drawing 'square' and all its parts from every plate?", + DrawingCommands.DeletePrompt(new Drawing("square"))); + + [Fact] + public void CleanUnusedDrawings_RemovesOnlyDrawingsWithNoParts_AsOneStep() + { + var env = new Env(); + Assert.True(DrawingCommands.HasUnusedDrawings(env.Nest)); + + Assert.True(DrawingCommands.CleanUnusedDrawings(env.Nest, env.BeginChange)); + + Assert.Equal(new[] { "square" }, env.Nest.Drawings.Select(d => d.Name)); + Assert.Equal(1, env.Steps); + Assert.Equal("Undo Clean Unused Drawings", env.Undo.UndoText); + + env.Undo.Undo(); + Assert.Equal(2, env.Nest.Drawings.Count); + } + + [Fact] + public void CleanUnusedDrawings_WithNoneUnused_RecordsNothing() + { + var env = new Env(); + DrawingCommands.CleanUnusedDrawings(env.Nest, env.BeginChange); + + Assert.False(DrawingCommands.CleanUnusedDrawings(env.Nest, env.BeginChange)); + + Assert.False(DrawingCommands.HasUnusedDrawings(env.Nest)); + Assert.Equal(1, env.Steps); + } + + [Fact] + public void ApplyProperties_SavesTheValues_AsOneStep() + { + var env = new Env(); + var properties = new DrawingProperties(env.Square, env.Nest) + { + Name = "bracket", + Customer = "Acme", + Quantity = 7, + Priority = -3, + CutFirst = true, + AllowCommonLine = true, + Formed = true, + }; + + DrawingCommands.ApplyProperties(properties, env.BeginChange); + + Assert.Equal("bracket", env.Square.Name); + Assert.Equal("Acme", env.Square.Customer); + Assert.Equal(7, env.Square.Quantity.Required); + Assert.Equal(2, env.Square.Quantity.Nested); + Assert.Equal(-3, env.Square.Priority); + Assert.True(env.Square.CutFirst); + Assert.True(env.Square.AllowCommonLine); + Assert.True(env.Square.IsFormed); + Assert.False(env.Square.HasCustomColor); + Assert.Equal(1, env.Steps); + Assert.Equal("Undo Drawing Properties", env.Undo.UndoText); + + env.Undo.Undo(); + var restored = env.Nest.Drawings.Single(d => d.Quantity.Nested == 2); + Assert.Equal("square", restored.Name); + Assert.Equal(2, restored.Quantity.Required); + Assert.False(restored.CutFirst); + } +} diff --git a/OpenNest.Desktop.Tests/Drawings/DrawingListRowTests.cs b/OpenNest.Desktop.Tests/Drawings/DrawingListRowTests.cs new file mode 100644 index 00000000..a671123a --- /dev/null +++ b/OpenNest.Desktop.Tests/Drawings/DrawingListRowTests.cs @@ -0,0 +1,81 @@ +namespace OpenNest.Desktop.Tests.Drawings; + +public class DrawingListRowTests +{ + private static (Nest Nest, Drawing Drawing) Nested(int required) + { + var nest = TestNests.OneSquare(); + var drawing = nest.Drawings.First(); + drawing.Quantity.Required = required; + nest.Plates[0].Quantity = 1; + nest.UpdateDrawingQuantities(); + return (nest, drawing); + } + + [Fact] + public void ShowsTheNameQuantitySizeAndArea() + { + var (_, drawing) = Nested(3); + + var row = DrawingListRow.For(drawing, plate: null, Units.Inches); + + Assert.Same(drawing, row.Drawing); + Assert.Equal("square", row.Name); + Assert.Equal("1 of 3 nested", row.Quantity); + Assert.Equal("10 x 10", row.Size); + Assert.Equal("100 sq/in", row.Area); + Assert.False(row.IsHighlighted); + Assert.Equal(DrawingQuantityBar.Short, row.Bar); + } + + [Fact] + public void PrefixesTheCountOnTheShownPlate() + { + var (nest, drawing) = Nested(3); + + Assert.Equal("[1] 1 of 3 nested", DrawingListRow.For(drawing, nest.Plates[0], Units.Inches).Quantity); + Assert.Equal("1 of 3 nested", DrawingListRow.For(drawing, new Plate(), Units.Inches).Quantity); + } + + [Fact] + public void AllNested_IsHighlightedWithACompleteBar() + { + var (_, drawing) = Nested(1); + + var row = DrawingListRow.For(drawing, null, Units.Millimeters); + + Assert.True(row.IsHighlighted); + Assert.Equal(DrawingQuantityBar.Complete, row.Bar); + Assert.Equal("100 sq/mm", row.Area); + } + + [Fact] + public void NoRequiredQuantity_HasNoBar_ButNestedPartsHighlight() + { + var (_, drawing) = Nested(0); + + var row = DrawingListRow.For(drawing, null, Units.Inches); + + Assert.Equal(DrawingQuantityBar.None, row.Bar); + Assert.True(row.IsHighlighted); + } + + [Fact] + public void HiddenQuantity_LeavesOutTheQuantityHighlightAndBar() + { + var (_, drawing) = Nested(1); + + var row = DrawingListRow.For(drawing, null, Units.Inches, hideQuantity: true); + + Assert.Null(row.Quantity); + Assert.False(row.IsHighlighted); + Assert.Equal(DrawingQuantityBar.None, row.Bar); + } + + [Theory] + [InlineData(true, 2, DrawingListRow.AllHiddenText)] + [InlineData(true, 0, null)] + [InlineData(false, 2, null)] + public void EmptyText_ExplainsOnlyAListTheFilterEmptied(bool hide, int drawings, string expected) => + Assert.Equal(expected, DrawingListRow.EmptyText(hide, drawings)); +} diff --git a/OpenNest.Desktop.Tests/Drawings/DrawingPropertiesTests.cs b/OpenNest.Desktop.Tests/Drawings/DrawingPropertiesTests.cs new file mode 100644 index 00000000..cf8723db --- /dev/null +++ b/OpenNest.Desktop.Tests/Drawings/DrawingPropertiesTests.cs @@ -0,0 +1,134 @@ +using System.Drawing; + +namespace OpenNest.Desktop.Tests.Drawings; + +[Collection(ColorSchemeState.Collection)] +public class DrawingPropertiesTests +{ + private static Drawing Square(string name = "square") + { + var drawing = TestNests.OneSquare().Drawings.First(); + drawing.Name = name; + return drawing; + } + + [Fact] + public void ReadsTheDrawingsValues() + { + var drawing = Square("bracket"); + drawing.Customer = "Acme"; + drawing.Quantity.Required = 5; + drawing.Priority = 2; + drawing.CutFirst = true; + drawing.Source = new SourceInfo { Path = "/parts/bracket.dxf" }; + + var properties = new DrawingProperties(drawing); + + Assert.Equal("bracket", properties.Name); + Assert.Equal("Acme", properties.Customer); + Assert.Equal(5, properties.Quantity); + Assert.Equal(2, properties.Priority); + Assert.True(properties.CutFirst); + Assert.False(properties.AllowCommonLine); + Assert.False(properties.Formed); + Assert.Equal("Drawing Properties - bracket", properties.Title); + Assert.Equal("10 x 10", properties.SizeText); + Assert.Equal("100", properties.AreaText); + Assert.Equal("bracket.dxf", properties.SourceText); + Assert.Equal("/parts/bracket.dxf", properties.SourcePath); + } + + [Fact] + public void WithoutASource_ShowsADash() + { + var properties = new DrawingProperties(Square()); + + Assert.Null(properties.SourcePath); + Assert.Equal("-", properties.SourceText); + } + + [Fact] + public void NestedText_CountsRemainingAgainstTheEditedQuantity() + { + var nest = TestNests.OneSquare(); + nest.Plates[0].Quantity = 1; + nest.UpdateDrawingQuantities(); + var properties = new DrawingProperties(nest.Drawings.First(), nest); + Assert.Equal("1 nested, 0 remaining", properties.NestedText); + + properties.Quantity = 4; + + Assert.Equal("1 nested, 3 remaining", properties.NestedText); + } + + [Fact] + public void OnlyAPickedColorBecomesCustom() + { + var drawing = Square(); + drawing.Color = Color.Coral; + var unchanged = new DrawingProperties(drawing); + Assert.Equal(DrawingProperties.AutomaticColorText, unchanged.ColorModeText); + Assert.False(unchanged.CanUseAutomaticColor); + unchanged.ApplyTo(drawing); + Assert.False(drawing.HasCustomColor); + + var picked = new DrawingProperties(drawing); + picked.PickColor(Color.Red); + Assert.Equal(DrawingProperties.CustomColorText, picked.ColorModeText); + Assert.True(picked.CanUseAutomaticColor); + picked.ApplyTo(drawing); + + Assert.True(drawing.HasCustomColor); + Assert.Equal(Color.Red.ToArgb(), drawing.Color.ToArgb()); + } + + [Fact] + public void AutomaticColor_ClearsTheCustomColor_WithTheDrawingsPaletteSlot() + { + var state = new ColorSchemeState(); + try + { + ColorSchemeRegistry.Apply(ColorSchemeRegistry.Get("Classic")); + var nest = new Nest("Automatic"); + nest.Drawings.Add(new Drawing("First")); + nest.Drawings.Add(new Drawing("Cutoff") { IsCutOff = true }); + var drawing = new Drawing("Bracket") { Color = Color.Red, HasCustomColor = true }; + nest.Drawings.Add(drawing); + + var properties = new DrawingProperties(drawing, nest); + Assert.Equal(DrawingProperties.CustomColorText, properties.ColorModeText); + + properties.UseAutomaticColor(); + + Assert.Equal(DrawingProperties.AutomaticColorText, properties.ColorModeText); + Assert.False(properties.CanUseAutomaticColor); + Assert.True(drawing.HasCustomColor); + + properties.ApplyTo(drawing); + + Assert.False(drawing.HasCustomColor); + Assert.Equal(Drawing.GetPartColor(1).ToArgb(), drawing.Color.ToArgb()); + Assert.Equal(DrawingProperties.AutomaticColorText, new DrawingProperties(drawing, nest).ColorModeText); + } + finally + { + state.Restore(); + } + } + + [Fact] + public void APickedColorCancelsAPendingAutomatic() + { + var drawing = Square(); + drawing.Color = Color.Red; + drawing.HasCustomColor = true; + var properties = new DrawingProperties(drawing); + properties.UseAutomaticColor(); + + properties.PickColor(Color.Blue); + properties.ApplyTo(drawing); + + Assert.True(drawing.HasCustomColor); + Assert.Equal(Color.Blue.ToArgb(), drawing.Color.ToArgb()); + } +} diff --git a/OpenNest.Desktop/DrawingCommands.cs b/OpenNest.Desktop/DrawingCommands.cs new file mode 100644 index 00000000..07d938e5 --- /dev/null +++ b/OpenNest.Desktop/DrawingCommands.cs @@ -0,0 +1,100 @@ +using System; +using System.Linq; +using OpenNest.Undo; + +namespace OpenNest.Desktop; + +/// +/// The Drawings tab's nest edits, toolkit-neutral: Delete on a drawing's menu, Remove Unused +/// Drawings and the Drawing Properties dialog's OK. Each one is one undo step under the WinForms +/// label, opened with (the window's BeginChange), with the +/// WinForms confirmation texts for the front ends to ask first. +/// +public static class DrawingCommands +{ + public const string DeleteLabel = "Delete Drawing"; + + /// The title of Delete's confirmation. + public const string DeleteTitle = "Delete Drawing"; + + public const string CleanUnusedLabel = "Clean Unused Drawings"; + + /// The title of Remove Unused Drawings' confirmation. + public const string CleanUnusedTitle = "Clean Drawings"; + + /// Remove Unused Drawings' confirmation. + public const string CleanUnusedPrompt = "Remove unused drawings?"; + + public const string PropertiesLabel = "Drawing Properties"; + + /// Delete's confirmation for . + public static string DeletePrompt(Drawing drawing) + { + ArgumentNullException.ThrowIfNull(drawing); + return $"Delete drawing '{drawing.Name}' and all its parts from every plate?"; + } + + /// Whether the nest has a drawing with no part on any plate. + public static bool HasUnusedDrawings(Nest nest) + { + ArgumentNullException.ThrowIfNull(nest); + return nest.Drawings.Any(d => d.Quantity.Nested == 0); + } + + /// + /// Removes the drawings with no part on any plate, as one "Clean Unused Drawings" step. + /// + /// Whether a drawing was removed; when none would be, nothing is recorded. + public static bool CleanUnusedDrawings(Nest nest, Func beginChange) + { + ArgumentNullException.ThrowIfNull(nest); + ArgumentNullException.ThrowIfNull(beginChange); + if (!HasUnusedDrawings(nest)) + return false; + + using var change = beginChange(CleanUnusedLabel); + change.MarkChanged(); + nest.Drawings.RemoveWhere(d => d.Quantity.Nested == 0); + change.Complete(); + return true; + } + + /// + /// Deletes and its parts from every plate, as one "Delete Drawing" + /// step. The plates' part lists keep the drawings' nested quantities. + /// + public static void DeleteDrawing(Nest nest, Drawing drawing, Func beginChange) + { + ArgumentNullException.ThrowIfNull(nest); + ArgumentNullException.ThrowIfNull(drawing); + ArgumentNullException.ThrowIfNull(beginChange); + using var change = beginChange(DeleteLabel); + change.MarkChanged(); + // Emptying the last plates drops trailing empty ones, so walk a copy. + foreach (var plate in nest.Plates.ToList()) + { + for (var i = plate.Parts.Count - 1; i >= 0; i--) + { + if (plate.Parts[i].BaseDrawing == drawing) + plate.Parts.RemoveAt(i); + } + } + + nest.Drawings.Remove(drawing); + change.Complete(); + } + + /// + /// The Drawing Properties dialog's OK: saved to its drawing as + /// one "Drawing Properties" step. A cancelled dialog calls nothing, so nothing is recorded. + /// + public static void ApplyProperties(DrawingProperties properties, Func beginChange) + { + ArgumentNullException.ThrowIfNull(properties); + ArgumentNullException.ThrowIfNull(beginChange); + using var change = beginChange(PropertiesLabel); + change.MarkChanged(); + properties.ApplyTo(properties.Drawing); + change.Complete(); + } +} diff --git a/OpenNest.Desktop/DrawingListRow.cs b/OpenNest.Desktop/DrawingListRow.cs new file mode 100644 index 00000000..1cdb1a29 --- /dev/null +++ b/OpenNest.Desktop/DrawingListRow.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace OpenNest.Desktop; + +/// The bar down a Drawings tab row's left edge, for a drawing with a required quantity. +public enum DrawingQuantityBar +{ + /// No quantity is required (or quantities are hidden): no bar. + None, + + /// All of the required quantity is nested: a green bar. + Complete, + + /// Some of the required quantity is still to nest: an orange bar. + Short, +} + +/// +/// One row of the Drawings tab list, as the WinForms list draws it beside the drawing's +/// thumbnail: its name, "[on this plate] nested of required nested", its size and its area in +/// square units. A drawing whose nested parts left none remaining is highlighted, and a drawing +/// with a required quantity carries a bar saying whether all of it is nested. +/// +public sealed record DrawingListRow( + Drawing Drawing, + string Name, + string Quantity, + string Size, + string Area, + bool IsHighlighted, + DrawingQuantityBar Bar) +{ + /// The note an emptied list shows when Hide Fully Nested Drawings hid every drawing. + public const string AllHiddenText = "All drawings are fully nested and hidden.\n\nClick here to show them."; + + /// + /// The row of . Its count on (the plate + /// shown beside the list; null omits it) prefixes the quantity when there is one. With + /// the row has no quantity, highlight or bar. + /// + public static DrawingListRow For(Drawing drawing, Plate plate, Units units, bool hideQuantity = false) + { + ArgumentNullException.ThrowIfNull(drawing); + var quantity = drawing.Quantity; + string quantityText = null; + var bar = DrawingQuantityBar.None; + if (!hideQuantity) + { + quantityText = $"{quantity.Nested} of {quantity.Required} nested"; + var onPlate = plate?.Parts.Count(part => part.BaseDrawing == drawing) ?? 0; + if (onPlate > 0) + quantityText = $"[{onPlate}] {quantityText}"; + if (quantity.Required > 0) + bar = quantity.Nested >= quantity.Required ? DrawingQuantityBar.Complete : DrawingQuantityBar.Short; + } + + return new DrawingListRow( + drawing, + drawing.Name, + quantityText, + drawing.Program.BoundingBox().Size.ToString(4), + $"{System.Math.Round(drawing.Area, 4)} sq/{UnitsHelper.GetShortString(units)}", + !hideQuantity && quantity.Nested > 0 && quantity.Remaining == 0, + bar); + } + + /// The rows of , in their order. + public static IReadOnlyList For(IEnumerable drawings, Plate plate, Units units) + { + ArgumentNullException.ThrowIfNull(drawings); + return drawings.Select(drawing => For(drawing, plate, units)).ToList(); + } + + /// + /// The note the list shows in place of rows: when Hide Fully + /// Nested Drawings () emptied a list of a nest that has + /// drawings, so they don't look lost; otherwise null. The filter stays the user's choice. + /// + public static string EmptyText(bool hideFullyNested, int drawingCount) => + hideFullyNested && drawingCount > 0 ? AllHiddenText : null; +} diff --git a/OpenNest.Desktop/DrawingProperties.cs b/OpenNest.Desktop/DrawingProperties.cs new file mode 100644 index 00000000..d35c7951 --- /dev/null +++ b/OpenNest.Desktop/DrawingProperties.cs @@ -0,0 +1,171 @@ +using System; +using System.Drawing; + +namespace OpenNest.Desktop; + +/// +/// The Drawing Properties dialog's values and rules, toolkit-neutral: read from a drawing, edited +/// by the dialog, then saved back with . 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. +/// +public sealed class DrawingProperties +{ + /// The quantity and priority boxes' limits. + 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; + + /// Reads 's values; holds it (or null). + 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; } + + /// Formed (bent or rolled) after cutting. + public bool Formed { get; set; } + + /// The color shown; set it directly to keep a pending Automatic when it is unchanged. + public Color Color { get; set; } + + /// The dialog's title. + public string Title => string.IsNullOrEmpty(Drawing.Name) + ? "Drawing Properties" + : $"Drawing Properties - {Drawing.Name}"; + + /// A color picked in the dialog: it cancels a pending Automatic. + public void PickColor(Color color) + { + automaticColor = null; + Color = color; + } + + /// Automatic: the palette color for the drawing's slot, saved as not custom. + public void UseAutomaticColor() + { + automaticColor = AutomaticColor(); + Color = automaticColor.Value; + } + + public bool IsAutomaticColorRequested => automaticColor?.ToArgb() == Color.ToArgb(); + + /// Whether saving makes (or keeps) the color custom. + public bool IsCustomColor => + !IsAutomaticColorRequested + && (Drawing.HasCustomColor || Color.ToArgb() != Drawing.Color.ToArgb()); + + /// The note beside the color swatch. + public string ColorModeText => IsCustomColor ? CustomColorText : AutomaticColorText; + + /// Whether Automatic can be chosen: only for a custom color. + public bool CanUseAutomaticColor => IsCustomColor; + + /// The palette color for the drawing's slot among the nest's non-cutoff drawings. + 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); + } + + /// "n nested, m remaining", the remaining counted against the quantity being edited. + 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.###}"; + + /// The full path of the file the drawing came from, or null. + public string SourcePath => string.IsNullOrEmpty(Drawing.Source?.Path) ? null : Drawing.Source.Path; + + /// The source file's name, or "-" when there is none. + public string SourceText => SourcePath == null ? "-" : System.IO.Path.GetFileName(SourcePath); + + /// Saves the values to ; Automatic recolors the nest's drawings. + 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; + } + } +} diff --git a/OpenNest/Controls/DrawingListBox.cs b/OpenNest/Controls/DrawingListBox.cs index 9349d865..fe0a47db 100644 --- a/OpenNest/Controls/DrawingListBox.cs +++ b/OpenNest/Controls/DrawingListBox.cs @@ -3,6 +3,7 @@ using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Windows.Forms; +using OpenNest.Desktop; using Size = System.Drawing.Size; namespace OpenNest.Controls @@ -204,12 +205,8 @@ namespace OpenNest.Controls return; var isSelected = (e.State & DrawItemState.Selected) != 0; - Brush bgBrush; - - if (!HideQuantity && dwg.Quantity.Nested > 0 && dwg.Quantity.Remaining == 0) - bgBrush = SystemBrushes.Info; - else - bgBrush = Brushes.White; + var row = DrawingListRow.For(dwg, Plate, Units, HideQuantity); + var bgBrush = row.IsHighlighted ? SystemBrushes.Info : Brushes.White; e.Graphics.FillRectangle(bgBrush, e.Bounds); @@ -225,11 +222,11 @@ namespace OpenNest.Controls e.Graphics.DrawRectangle(borderPen, borderRect); } - if (!HideQuantity && dwg.Quantity.Required > 0) + if (row.Bar != DrawingQuantityBar.None) { var barWidth = 4; var barColor = - dwg.Quantity.Nested >= dwg.Quantity.Required + row.Bar == DrawingQuantityBar.Complete ? Color.FromArgb(76, 175, 80) : Color.FromArgb(255, 152, 0); using var barBrush = new SolidBrush(barColor); @@ -259,43 +256,23 @@ namespace OpenNest.Controls var textBrush = Brushes.Black; var detailBrush = Brushes.Gray; - e.Graphics.DrawString(dwg.Name, nameFont, textBrush, pt); + e.Graphics.DrawString(row.Name, nameFont, textBrush, pt); - var bounds = dwg.Program.BoundingBox(); - var text2 = bounds.Size.ToString(4); - var text3 = string.Format( - "{0} sq/{1}", - System.Math.Round(dwg.Area, 4), - UnitsHelper.GetShortString(Units) - ); - - if (HideQuantity) + if (row.Quantity != null) { pt.Y += 22; - e.Graphics.DrawString(text2, Font, detailBrush, pt); + e.Graphics.DrawString(row.Quantity, Font, detailBrush, pt); pt.Y += 18; - e.Graphics.DrawString(text3, Font, detailBrush, pt); } else { - var text1 = string.Format( - "{0} of {1} nested", - dwg.Quantity.Nested, - dwg.Quantity.Required - ); - var onPlate = Plate?.Parts.Count(part => part.BaseDrawing == dwg) ?? 0; - - if (onPlate > 0) - text1 = $"[{onPlate}] {text1}"; - pt.Y += 22; - e.Graphics.DrawString(text1, Font, detailBrush, pt); - pt.Y += 18; - e.Graphics.DrawString(text2, Font, detailBrush, pt); - pt.Y += 18; - e.Graphics.DrawString(text3, Font, detailBrush, pt); } + e.Graphics.DrawString(row.Size, Font, detailBrush, pt); + pt.Y += 18; + e.Graphics.DrawString(row.Area, Font, detailBrush, pt); + using var separatorPen = new Pen(Color.LightGray); var separatorY = e.Bounds.Bottom - 1; e.Graphics.DrawLine(separatorPen, e.Bounds.X, separatorY, e.Bounds.Right, separatorY); diff --git a/OpenNest/Forms/EditDrawingForm.cs b/OpenNest/Forms/EditDrawingForm.cs index f2572c5b..51f7b419 100644 --- a/OpenNest/Forms/EditDrawingForm.cs +++ b/OpenNest/Forms/EditDrawingForm.cs @@ -1,6 +1,7 @@ using System; using System.Drawing; using System.Windows.Forms; +using OpenNest.Desktop; namespace OpenNest.Forms { @@ -9,8 +10,7 @@ namespace OpenNest.Forms private const int PreviewInset = 24; private Drawing drawing; - private Nest nest; - private Color? automaticColor; + private DrawingProperties properties; public EditDrawingForm() { @@ -89,9 +89,9 @@ namespace OpenNest.Forms private void UpdateColor() { colorSwatch.BackColor = colorDialog1.Color; - var customColor = IsCustomColor; - colorModeLabel.Text = customColor ? "Custom color" : "Automatic color from the color scheme"; - automaticColorButton.Enabled = customColor; + properties.Color = colorDialog1.Color; + colorModeLabel.Text = properties.ColorModeText; + automaticColorButton.Enabled = properties.CanUseAutomaticColor; UpdateImage(); } @@ -100,74 +100,32 @@ namespace OpenNest.Forms if (drawing == null) return; - var nested = drawing.Quantity.Nested; - var remaining = System.Math.Max(Quantity - nested, 0); - nestedLabel.Text = $"{nested} nested, {remaining} remaining"; + properties.Quantity = Quantity; + nestedLabel.Text = properties.NestedText; } 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); - } + sizeLabel.Text = properties.SizeText; + areaLabel.Text = properties.AreaText; + sourceLabel.Text = properties.SourceText; + toolTip.SetToolTip(sourceLabel, properties.SourcePath); } - /// - /// The palette color for the drawing's slot among the nest's non-cutoff drawings. - /// - 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; + properties = new DrawingProperties(drawing, nest); - colorDialog1.Color = drawing.Color; - DrawingName = drawing.Name; - Customer = drawing.Customer; - Quantity = drawing.Quantity.Required; - Priority = drawing.Priority; - CutFirst = drawing.CutFirst; - AllowCommonLine = drawing.AllowCommonLine; - Formed = drawing.IsFormed; - Text = string.IsNullOrEmpty(drawing.Name) - ? "Drawing Properties" - : $"Drawing Properties - {drawing.Name}"; + colorDialog1.Color = properties.Color; + DrawingName = properties.Name; + Customer = properties.Customer; + Quantity = properties.Quantity; + Priority = properties.Priority; + CutFirst = properties.CutFirst; + AllowCommonLine = properties.AllowCommonLine; + Formed = properties.Formed; + Text = properties.Title; LoadDetails(); UpdateNestedLabel(); UpdateColor(); @@ -175,29 +133,16 @@ namespace OpenNest.Forms public void SaveDrawing(Drawing drawing) { - drawing.Name = DrawingName; - 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 = colorDialog1.Color; - drawing.HasCustomColor = true; - } + properties.Name = DrawingName; + properties.Customer = Customer; + properties.Quantity = Quantity; + properties.Priority = Priority; + properties.CutFirst = CutFirst; + properties.AllowCommonLine = AllowCommonLine; + properties.Formed = Formed; + // The dialog's color is the one shown; a pending Automatic holds while it is unchanged. + properties.Color = colorDialog1.Color; + properties.ApplyTo(drawing); } protected override void OnShown(EventArgs e) @@ -215,14 +160,14 @@ namespace OpenNest.Forms return; // A picked color cancels a pending Automatic. - automaticColor = null; + properties.PickColor(colorDialog1.Color); UpdateColor(); } private void AutomaticColor_Click(object sender, EventArgs e) { - automaticColor = AutomaticColor(); - colorDialog1.Color = automaticColor.Value; + properties.UseAutomaticColor(); + colorDialog1.Color = properties.Color; UpdateColor(); } } diff --git a/OpenNest/Forms/EditNestForm.cs b/OpenNest/Forms/EditNestForm.cs index 78381648..b47f1b21 100644 --- a/OpenNest/Forms/EditNestForm.cs +++ b/OpenNest/Forms/EditNestForm.cs @@ -674,10 +674,7 @@ namespace OpenNest.Forms /// private void UpdateDrawingListEmptyText() { - drawingListBox1.EmptyText = - hideNestedButton.Checked && Nest.Drawings.Count > 0 - ? "All drawings are fully nested and hidden.\n\nClick here to show them." - : null; + drawingListBox1.EmptyText = DrawingListRow.EmptyText(hideNestedButton.Checked, Nest.Drawings.Count); } public void Save() @@ -1774,8 +1771,8 @@ namespace OpenNest.Forms private void drawingListBox1_RemoveUnusedRequested(object sender, EventArgs e) { var result = MessageBox.Show( - "Remove unused drawings?", - "Clean Drawings", + DrawingCommands.CleanUnusedPrompt, + DrawingCommands.CleanUnusedTitle, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1 @@ -1788,10 +1785,9 @@ namespace OpenNest.Forms /// Removes the drawings with no part on any plate. public void CleanUnusedDrawings() { - if (!Nest.Drawings.Any(d => d.Quantity.Nested == 0)) + if (!DrawingCommands.CleanUnusedDrawings(Nest, label => BeginChange(label))) return; - RecordChange("Clean Unused Drawings", () => Nest.Drawings.RemoveWhere(d => d.Quantity.Nested == 0)); UpdateDrawingList(); } @@ -1911,7 +1907,7 @@ namespace OpenNest.Forms if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK) { - RecordChange("Drawing Properties", () => form.SaveDrawing(drawing)); + RecordChange(DrawingCommands.PropertiesLabel, () => form.SaveDrawing(drawing)); RefreshDrawingColor(drawing); @@ -1931,8 +1927,8 @@ namespace OpenNest.Forms private void drawingListBox1_DeleteRequested(object sender, Drawing drawing) { var result = MessageBox.Show( - $"Delete drawing '{drawing.Name}' and all its parts from every plate?", - "Delete Drawing", + DrawingCommands.DeletePrompt(drawing), + DrawingCommands.DeleteTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2 @@ -1947,20 +1943,7 @@ namespace OpenNest.Forms /// Deletes and its parts from every plate. public void DeleteDrawing(Drawing drawing) { - RecordChange("Delete Drawing", () => - { - // Emptying the last plates drops trailing empty ones, so walk a copy. - foreach (var plate in Nest.Plates.ToList()) - { - for (var i = plate.Parts.Count - 1; i >= 0; i--) - { - if (plate.Parts[i].BaseDrawing == drawing) - plate.Parts.RemoveAt(i); - } - } - - Nest.Drawings.Remove(drawing); - }); + DrawingCommands.DeleteDrawing(Nest, drawing, label => BeginChange(label)); UpdateDrawingList(); UpdatePlateList(); PlateView.Invalidate();