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; } } }