From ce948c105f6095788abb06d292c2f11416895e01 Mon Sep 17 00:00:00 2001 From: John Lewin Date: Fri, 18 Jan 2019 18:16:21 -0800 Subject: [PATCH] Initial prototype for revised validation reporting --- MatterControl.Printing/NamedAction.cs | 75 +++++++++++++++++++ .../Settings/ValidationError.cs | 1 + .../ApplicationView/SettingsValidation.cs | 31 +++++++- MatterControlLib/CustomWidgets/NamedAction.cs | 27 ------- .../Library/Export/GCodeExport.cs | 6 +- .../View3D/PrinterBar/PrintPopupMenu.cs | 27 ++----- 6 files changed, 115 insertions(+), 52 deletions(-) create mode 100644 MatterControl.Printing/NamedAction.cs diff --git a/MatterControl.Printing/NamedAction.cs b/MatterControl.Printing/NamedAction.cs new file mode 100644 index 000000000..3188d503e --- /dev/null +++ b/MatterControl.Printing/NamedAction.cs @@ -0,0 +1,75 @@ +/* +Copyright (c) 2019, John Lewin + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +The views and conclusions contained in the software and documentation are those +of the authors and should not be interpreted as representing official policies, +either expressed or implied, of the FreeBSD Project. +*/ + +using System; +using System.Collections.Generic; +using MatterHackers.Agg.Image; + +namespace MatterHackers.MatterControl +{ + public class NamedAction + { + public string Title { get; set; } + public string Shortcut { get; set; } + public Action Action { get; set; } + public ImageBuffer Icon { get; set; } + public Func IsEnabled { get; set; } + public string ID { get; set; } + } + + public class ActionSeparator : NamedAction + { + } + + public class NamedBoolAction : NamedAction + { + public Func GetIsActive { get; set; } + public Action SetIsActive { get; set; } + } + + public abstract class LocalizedAction + { + public Func TitleResolver { get; set; } + public string Title => this.TitleResolver?.Invoke(); + public ImageBuffer Icon { get; set; } + } + + public static class NamedActionExtensions + { + public static void Add(this List list, string title, Action action) + { + list.Add(new NamedAction() + { + Title = title, + Action = action + }); + } + } +} \ No newline at end of file diff --git a/MatterControl.Printing/Settings/ValidationError.cs b/MatterControl.Printing/Settings/ValidationError.cs index f6e284d29..6be60d900 100644 --- a/MatterControl.Printing/Settings/ValidationError.cs +++ b/MatterControl.Printing/Settings/ValidationError.cs @@ -44,5 +44,6 @@ namespace MatterHackers.MatterControl public string Details { get; set; } public ValidationErrorLevel ErrorLevel { get; set; } = ValidationErrorLevel.Error; + public NamedAction FixAction { get; set; } } } \ No newline at end of file diff --git a/MatterControlLib/ApplicationView/SettingsValidation.cs b/MatterControlLib/ApplicationView/SettingsValidation.cs index e3c56fd11..909ed2cf8 100644 --- a/MatterControlLib/ApplicationView/SettingsValidation.cs +++ b/MatterControlLib/ApplicationView/SettingsValidation.cs @@ -30,6 +30,7 @@ either expressed or implied, of the FreeBSD Project. using System; using System.Collections.Generic; using MatterHackers.Agg; +using MatterHackers.Agg.UI; using MatterHackers.Localizations; using MatterHackers.MatterControl.PartPreviewWindow; using MatterHackers.MatterControl.SlicerConfiguration; @@ -44,6 +45,25 @@ namespace MatterHackers.MatterControl var errors = new List(); + var printerIsConnected = printer.Connection.CommunicationState != PrinterCommunication.CommunicationStates.Disconnected; + if (!printerIsConnected) + { + errors.Add(new ValidationError() + { + Error = "Printer Disconnected".Localize(), + Details = "Connect to your printer to continue".Localize() + }); + } + + if (ApplicationController.PrinterNeedsToRunSetup(printer)) + { + errors.Add(new ValidationError() + { + Error = "Setup Required".Localize(), + Details = "Setup needs to be run before printing".Localize() + }); + } + // last let's check if there is any support in the scene and if it looks like it is needed if (GenerateSupportPanel.RequiresSupport(printer.Bed.Scene)) { @@ -51,7 +71,16 @@ namespace MatterHackers.MatterControl { Error = "Support Recommended".Localize(), Details = "Some of the parts appear to require support. Consider canceling this print then adding support to get the best results possible.".Localize(), - ErrorLevel = ValidationErrorLevel.Warning + ErrorLevel = ValidationErrorLevel.Warning, + FixAction = new NamedAction() + { + Title = "Add Supports".Localize(), + Action = () => + { + // Do something like popup the supports panel or directly show the popup + System.Diagnostics.Debugger.Break(); + } + } }); } diff --git a/MatterControlLib/CustomWidgets/NamedAction.cs b/MatterControlLib/CustomWidgets/NamedAction.cs index b336d3518..e436cbaf9 100644 --- a/MatterControlLib/CustomWidgets/NamedAction.cs +++ b/MatterControlLib/CustomWidgets/NamedAction.cs @@ -36,33 +36,6 @@ using MatterHackers.MatterControl; namespace MatterHackers.Agg.UI { - public class NamedAction - { - public string Title { get; set; } - public string Shortcut { get; set; } - public Action Action { get; set; } - public ImageBuffer Icon { get; set; } - public Func IsEnabled { get; set; } - public string ID { get; internal set; } - } - - public class ActionSeparator : NamedAction - { - } - - public class NamedBoolAction : NamedAction - { - public Func GetIsActive { get; set; } - public Action SetIsActive { get; set; } - } - - public abstract class LocalizedAction - { - public Func TitleResolver { get; set; } - public string Title => this.TitleResolver?.Invoke(); - public ImageBuffer Icon { get; set; } - } - public class SceneSelectionOperation : LocalizedAction { public Action Action { get; set; } diff --git a/MatterControlLib/Library/Export/GCodeExport.cs b/MatterControlLib/Library/Export/GCodeExport.cs index 93728d26b..b4612622b 100644 --- a/MatterControlLib/Library/Export/GCodeExport.cs +++ b/MatterControlLib/Library/Export/GCodeExport.cs @@ -195,6 +195,8 @@ namespace MatterHackers.MatterControl.Library.Export // TODO: Prior code bypassed GCodeOverridePath mechanisms in EditContext. Consolidating into a single pathway string gcodePath = printer.Bed.EditContext.GCodeFilePath(printer); + List errors = new List(); + if (ApplicationSettings.ValidFileExtensions.IndexOf(sourceExtension, StringComparison.OrdinalIgnoreCase) >= 0 || string.Equals(sourceExtension, ".mcx", StringComparison.OrdinalIgnoreCase)) { @@ -218,7 +220,7 @@ namespace MatterHackers.MatterControl.Library.Export printer.Settings.SetValue(SettingsKey.spiral_vase, "1"); } - var errors = printer.ValidateSettings(); + errors = printer.ValidateSettings(); if(errors.Any(e => e.ErrorLevel == ValidationErrorLevel.Error)) { @@ -257,7 +259,7 @@ namespace MatterHackers.MatterControl.Library.Export }); } - return null; + return errors; } } catch diff --git a/MatterControlLib/PartPreviewWindow/View3D/PrinterBar/PrintPopupMenu.cs b/MatterControlLib/PartPreviewWindow/View3D/PrinterBar/PrintPopupMenu.cs index e2d31701b..1ba98b35a 100644 --- a/MatterControlLib/PartPreviewWindow/View3D/PrinterBar/PrintPopupMenu.cs +++ b/MatterControlLib/PartPreviewWindow/View3D/PrinterBar/PrintPopupMenu.cs @@ -154,39 +154,20 @@ namespace MatterHackers.MatterControl.PartPreviewWindow var printerReadyToTakeCommands = printer.Connection.CommunicationState == PrinterCommunication.CommunicationStates.FinishedPrint || printer.Connection.CommunicationState == PrinterCommunication.CommunicationStates.Connected; - var printerIsConnected = printer.Connection.CommunicationState != PrinterCommunication.CommunicationStates.Disconnected; - var printerNeedsToRunSetup = ApplicationController.PrinterNeedsToRunSetup(printer); - // add the start print button var setupRow = new FlowLayoutWidget() { HAnchor = HAnchor.Stretch }; - var printingMessage = ""; + var errors = printer.ValidateSettings(); - if (!printerIsConnected) - { - printingMessage = "Need to connect before printing".Localize(); - } - else if(printerNeedsToRunSetup) - { - printingMessage = "Setup needs to be run before printing".Localize(); - } - - if (!string.IsNullOrEmpty(printingMessage)) - { - setupRow.AddChild(new TextWidget(printingMessage, textColor: menuTheme.TextColor) - { - VAnchor = VAnchor.Center, - AutoExpandBoundsToText = true, - }); - } + var printEnabled = !errors.Any(err => err.ErrorLevel == ValidationErrorLevel.Error); var startPrintButton = new TextButton("Start Print".Localize(), menuTheme) { Name = "Start Print Button", - Enabled = printerIsConnected && !printerNeedsToRunSetup + Enabled = printEnabled }; startPrintButton.Click += (s, e) => @@ -217,6 +198,8 @@ namespace MatterHackers.MatterControl.PartPreviewWindow column.AddChild(setupRow); + var printerNeedsToRunSetup = ApplicationController.PrinterNeedsToRunSetup(printer); + if (!printerNeedsToRunSetup) { theme.ApplyPrimaryActionStyle(startPrintButton);