From 80a75a40fe332acda905b92bf4025ee4e2c951d8 Mon Sep 17 00:00:00 2001 From: John Lewin Date: Sat, 23 Sep 2017 14:44:43 -0700 Subject: [PATCH] Continue consolidating Printer load operations --- ActionBar/PrinterSelector.cs | 5 +- ApplicationView/ApplicationController.cs | 72 +++++++++++++++++- ApplicationView/PrinterModels.cs | 11 ++- SetupWizard/PrinterProfileHistoryPage.cs | 8 +- SetupWizard/SyncingPrintersPage.cs | 7 +- .../Settings/ActiveSliceSettings.cs | 76 +------------------ .../Settings/PrinterSettings.cs | 3 - .../Settings/ProfileManager.cs | 43 +++++------ .../Settings/SettingsHelpers.cs | 2 +- .../SliceSettingsTests.cs | 10 +-- 10 files changed, 117 insertions(+), 120 deletions(-) diff --git a/ActionBar/PrinterSelector.cs b/ActionBar/PrinterSelector.cs index e2ed748f8..e5bb4298d 100644 --- a/ActionBar/PrinterSelector.cs +++ b/ActionBar/PrinterSelector.cs @@ -74,7 +74,10 @@ namespace MatterHackers.MatterControl else { lastSelectedIndex = this.SelectedIndex; - UiThread.RunOnIdle(() => ActiveSliceSettings.SwitchToProfile(printerID)); + UiThread.RunOnIdle(() => + { + ProfileManager.SwitchToProfile(printerID).ConfigureAwait(false); + }); } } }; diff --git a/ApplicationView/ApplicationController.cs b/ApplicationView/ApplicationController.cs index d659182cb..c4642c6d6 100644 --- a/ApplicationView/ApplicationController.cs +++ b/ApplicationView/ApplicationController.cs @@ -111,14 +111,80 @@ namespace MatterHackers.MatterControl private Queue> queuedThumbCallbacks = new Queue>(); - public void SetActivePrinter(PrinterConfig printer) + public void SetActivePrinter(PrinterConfig printer, bool allowChangedEvent = true) { - (this.ActivePrinters as List).Add(printer); - this.ActivePrinter = printer; + var initialPrinter = this.ActivePrinter; + if (initialPrinter?.Settings.ID != printer.Settings.ID) + { + // If we have an active printer, run Disable + if (initialPrinter.Settings != PrinterSettings.Empty) + { + initialPrinter?.Connection?.Disable(); + } + + // ActivePrinters is IEnumerable to force us to use SetActivePrinter until it's ingrained in our pattern - cast to list since it is and we need to add + (this.ActivePrinters as List).Add(printer); + this.ActivePrinter = printer; + + // TODO: Decide if non-printer contexts should prompt for a printer, if we should have a default printer, or get "ActiveTab printer" working + // HACK: short term solution to resolve printer reference for non-printer related contexts + DragDropData.Printer = printer; + + if (!MatterControlApplication.IsLoading) + { + // Fire printer changed event + } + + BedSettings.SetMakeAndModel( + printer.Settings.GetValue(SettingsKey.make), + printer.Settings.GetValue(SettingsKey.model)); + + ActiveSliceSettings.SwitchToPrinterTheme(); + + if (allowChangedEvent) + { + ActiveSliceSettings.OnActivePrinterChanged(null); + } + + if (!MatterControlApplication.IsLoading + && printer.Settings.PrinterSelected + && printer.Settings.GetValue(SettingsKey.auto_connect)) + { + UiThread.RunOnIdle(() => + { + printer.Settings.printer.Connection.Connect(false); + }, 2); + } + + } + } + + internal void ClearActivePrinter() + { + this.ActivePrinter = emptyPrinter; + + } + + public void RefreshActiveInstance(PrinterSettings updatedPrinterSettings) + { + ActivePrinter.SwapToSettings(updatedPrinterSettings); + + /* + // TODO: Should we rebroadcast settings changed events for each settings? + bool themeChanged = ActivePrinter.Settings.GetValue(SettingsKey.active_theme_name) != updatedProfile.GetValue(SettingsKey.active_theme_name); + ActiveSliceSettings.SettingChanged.CallEvents(null, new StringEventArgs(SettingsKey.printer_name)); // TODO: Decide if non-printer contexts should prompt for a printer, if we should have a default printer, or get "ActiveTab printer" working // HACK: short term solution to resolve printer reference for non-printer related contexts DragDropData.Printer = printer; + if (themeChanged) + { + UiThread.RunOnIdle(ActiveSliceSettings.SwitchToPrinterTheme); + } + else + { + UiThread.RunOnIdle(ApplicationController.Instance.ReloadAdvancedControlsPanel); + }*/ } private AutoResetEvent thumbGenResetEvent = new AutoResetEvent(false); diff --git a/ApplicationView/PrinterModels.cs b/ApplicationView/PrinterModels.cs index d0c49af28..d197a9b99 100644 --- a/ApplicationView/PrinterModels.cs +++ b/ApplicationView/PrinterModels.cs @@ -380,15 +380,20 @@ namespace MatterHackers.MatterControl { this.Bed = new BedConfig(this, loadLastBedplate); + this.Connection = new PrinterConnection(printer: this); + this.Settings = settings; this.Settings.printer = this; - this.Connection = new PrinterConnection(printer: this); - - ActiveSliceSettings.SettingChanged.RegisterEvent(Printer_SettingChanged, ref unregisterEvents); } + internal void SwapToSettings(PrinterSettings printerSettings) + { + _settings = printerSettings; + ApplicationController.Instance.ReloadAll(); + } + private void ReloadSettings() { this.Bed.BuildHeight = this.Settings.GetValue(SettingsKey.build_height); diff --git a/SetupWizard/PrinterProfileHistoryPage.cs b/SetupWizard/PrinterProfileHistoryPage.cs index 0cf402497..8eaff14e0 100644 --- a/SetupWizard/PrinterProfileHistoryPage.cs +++ b/SetupWizard/PrinterProfileHistoryPage.cs @@ -46,14 +46,14 @@ namespace MatterHackers.MatterControl.SetupWizard var activeProfile = ProfileManager.Instance.ActiveProfile; // Download the specified json profile - var jsonProfile = await ApplicationController.GetPrinterProfileAsync(activeProfile, profileToken); - if (jsonProfile != null) + var printerSettings = await ApplicationController.GetPrinterProfileAsync(activeProfile, profileToken); + if (printerSettings != null) { // Persist downloaded profile - jsonProfile.Save(); + printerSettings.Save(); // Update active instance without calling ReloadAll - ActiveSliceSettings.RefreshActiveInstance(jsonProfile); + ApplicationController.Instance.RefreshActiveInstance(printerSettings); } UiThread.RunOnIdle(WizardWindow.Close); diff --git a/SetupWizard/SyncingPrintersPage.cs b/SetupWizard/SyncingPrintersPage.cs index f8162643f..a70d3d082 100644 --- a/SetupWizard/SyncingPrintersPage.cs +++ b/SetupWizard/SyncingPrintersPage.cs @@ -34,9 +34,12 @@ namespace MatterHackers.MatterControl.SetupWizard } else if (ProfileManager.Instance.ActiveProfiles.Count() == 1) { - ActiveSliceSettings.ShowComPortConnectionHelp(); + // TODO: Investigate what this was doing and re-implement + //ActiveSliceSettings.ShowComPortConnectionHelp(); + //Set as active printer - ActiveSliceSettings.SwitchToProfile(ProfileManager.Instance.ActiveProfiles.First().ID); + ProfileManager.SwitchToProfile(ProfileManager.Instance.ActiveProfiles.First().ID).ConfigureAwait(false); + // only close the window if we are not switching to the setup printer form UiThread.RunOnIdle(WizardWindow.Close); } diff --git a/SlicerConfiguration/Settings/ActiveSliceSettings.cs b/SlicerConfiguration/Settings/ActiveSliceSettings.cs index 4d34fdbb0..dd035dfed 100644 --- a/SlicerConfiguration/Settings/ActiveSliceSettings.cs +++ b/SlicerConfiguration/Settings/ActiveSliceSettings.cs @@ -49,79 +49,13 @@ namespace MatterHackers.MatterControl.SlicerConfiguration public static event EventHandler MaterialPresetChanged; - static bool showConnectionHelp = false; - public static void ShowComPortConnectionHelp() { showConnectionHelp = true; } - - private static PrinterSettings activeInstance; - public static PrinterSettings Instance - { - get - { - return activeInstance; - } - set - { - if (activeInstance != value - && value != null) - { - // If we have an active printer, run Disable - if (activeInstance != PrinterSettings.Empty) - { - activeInstance?.printer.Connection.Disable(); - } - - activeInstance = value; - - BedSettings.SetMakeAndModel(activeInstance.GetValue(SettingsKey.make), activeInstance.GetValue(SettingsKey.model)); - - SwitchToPrinterTheme(); - - OnActivePrinterChanged(null); - - if (!MatterControlApplication.IsLoading) - { - if (activeInstance.PrinterSelected - && activeInstance.GetValue(SettingsKey.auto_connect)) - { - UiThread.RunOnIdle(() => - { - activeInstance.printer.Connection.Connect(showConnectionHelp); - showConnectionHelp = false; - }, 2); - } - } - } - } - } - - static ActiveSliceSettings() - { - activeInstance = PrinterSettings.Empty; - } + public static PrinterSettings Instance => ApplicationController.Instance.ActivePrinter.Settings; public static void OnSettingChanged(string slicerConfigName) { SettingChanged.CallEvents(null, new StringEventArgs(slicerConfigName)); } - public static void RefreshActiveInstance(PrinterSettings updatedProfile) - { - bool themeChanged = activeInstance.GetValue(SettingsKey.active_theme_name) != updatedProfile.GetValue(SettingsKey.active_theme_name); - - activeInstance = updatedProfile; - - ActiveSliceSettings.SettingChanged.CallEvents(null, new StringEventArgs(SettingsKey.printer_name)); - - if (themeChanged) - { - UiThread.RunOnIdle(SwitchToPrinterTheme); - } - else - { - UiThread.RunOnIdle(ApplicationController.Instance.ReloadAdvancedControlsPanel); - } - } - /// /// Switches to the ActivePrinter theme without firing the ThemeChanged event. This is useful when changing printers and /// allows the theme state to be updated before the ActivePrinterChanged event fires, resulting in a single ReloadAll @@ -140,13 +74,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration } } - internal static async Task SwitchToProfile(string printerID) - { - ProfileManager.Instance.LastProfileID = printerID; - Instance = (await ProfileManager.LoadProfileAsync(printerID)) ?? PrinterSettings.Empty; - } - - private static void OnActivePrinterChanged(EventArgs e) + public static void OnActivePrinterChanged(EventArgs e) { ActivePrinterChanged.CallEvents(null, e); } diff --git a/SlicerConfiguration/Settings/PrinterSettings.cs b/SlicerConfiguration/Settings/PrinterSettings.cs index 97b9d203c..3bc42443b 100644 --- a/SlicerConfiguration/Settings/PrinterSettings.cs +++ b/SlicerConfiguration/Settings/PrinterSettings.cs @@ -483,9 +483,6 @@ namespace MatterHackers.MatterControl.SlicerConfiguration // Persist any profile recovered above as the current printerSettings.Save(); - // Update active instance without calling ReloadAll - ActiveSliceSettings.RefreshActiveInstance(printerSettings); - WarnAboutRevert(printerInfo); } diff --git a/SlicerConfiguration/Settings/ProfileManager.cs b/SlicerConfiguration/Settings/ProfileManager.cs index 76bdaae33..130027005 100644 --- a/SlicerConfiguration/Settings/ProfileManager.cs +++ b/SlicerConfiguration/Settings/ProfileManager.cs @@ -48,34 +48,21 @@ namespace MatterHackers.MatterControl.SlicerConfiguration private static ProfileManager activeInstance = null; public static ProfileManager Instance { - get - { - return activeInstance; - } + get => activeInstance; private set { activeInstance = value; - // If profile is not loaded, load the loaded slice settings do not match the last active settings for this profile, change to the last active - if (!ApplicationController.Instance.ActivePrinters.Where(p => p.Settings.ID == activeInstance.LastProfileID).Any()) + // Select a 'LastProfile' exists and it is missing from ActivePrinters, load it + var lastProfile = activeInstance[activeInstance.LastProfileID]; + if (lastProfile != null + && !ApplicationController.Instance.ActivePrinters.Where(p => p.Settings.ID == lastProfile.ID).Any()) { // Load or download on a background thread the last loaded settings - var printerSettings = LoadProfileAsync(activeInstance.LastProfileID).Result ?? PrinterSettings.Empty; - - if (MatterControlApplication.IsLoading) - { - ActiveSliceSettings.Instance = printerSettings; - } - else - { - UiThread.RunOnIdle(() => - { - // Assign on the UI thread - ActiveSliceSettings.Instance = printerSettings ; - }); - } - - ApplicationController.Instance.SetActivePrinter(new PrinterConfig(true, printerSettings)); + ApplicationController.Instance.SetActivePrinter( + new PrinterConfig( + true, + LoadProfileAsync(activeInstance.LastProfileID).Result)); } } } @@ -151,6 +138,16 @@ namespace MatterHackers.MatterControl.SlicerConfiguration Instance.Profiles.CollectionChanged += Profiles_CollectionChanged; } + public static async Task SwitchToProfile(string printerID) + { + ProfileManager.Instance.LastProfileID = printerID; + + ApplicationController.Instance.SetActivePrinter( + new PrinterConfig( + false, + await ProfileManager.LoadProfileAsync(printerID))); + } + /// /// Loads a ProfileManager for the given user /// @@ -479,8 +476,6 @@ namespace MatterHackers.MatterControl.SlicerConfiguration ApplicationController.Instance.SetActivePrinter(printer); - ActiveSliceSettings.Instance = printerSettings; - return printer; } diff --git a/SlicerConfiguration/Settings/SettingsHelpers.cs b/SlicerConfiguration/Settings/SettingsHelpers.cs index a85629c61..3aa10c249 100644 --- a/SlicerConfiguration/Settings/SettingsHelpers.cs +++ b/SlicerConfiguration/Settings/SettingsHelpers.cs @@ -249,7 +249,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration UiThread.RunOnIdle(() => { - ActiveSliceSettings.Instance = PrinterSettings.Empty; + ApplicationController.Instance.ClearActivePrinter(); // Notify listeners of a ProfileListChange event due to this printers removal ProfileManager.ProfilesListChanged.CallEvents(this, null); diff --git a/Tests/MatterControl.AutomationTests/SliceSettingsTests.cs b/Tests/MatterControl.AutomationTests/SliceSettingsTests.cs index 2210a4acc..db1f438c6 100644 --- a/Tests/MatterControl.AutomationTests/SliceSettingsTests.cs +++ b/Tests/MatterControl.AutomationTests/SliceSettingsTests.cs @@ -436,7 +436,7 @@ namespace MatterHackers.MatterControl.Tests.Automation return Task.CompletedTask; }, overrideWidth: 1300); } - + [Test] public async Task QualitySettingsStayAsOverrides() { @@ -451,12 +451,12 @@ namespace MatterHackers.MatterControl.Tests.Automation testRunner.ClickByName("Layer Thickness Field"); testRunner.Type(".5\n"); testRunner.Delay(.5); - Assert.AreEqual(ActiveSliceSettings.Instance.GetValue(SettingsKey.layer_height), .5, "Layer height is what we set it to"); + Assert.AreEqual(ActiveSliceSettings.Instance.GetValue(SettingsKey.layer_height).ToString(), "0.5", "Layer height is what we set it to"); testRunner.ClickByName("Quality"); testRunner.ClickByName("Fine Menu"); testRunner.Delay(.5); - Assert.AreEqual(ActiveSliceSettings.Instance.GetValue(SettingsKey.layer_height), .1, "Layer height is the fine override"); + Assert.AreEqual(ActiveSliceSettings.Instance.GetValue(SettingsKey.layer_height).ToString(), "0.1", "Layer height is the fine override"); testRunner.AddAndSelectPrinter("BCN", "Sigma"); @@ -468,14 +468,14 @@ namespace MatterHackers.MatterControl.Tests.Automation testRunner.ClickByName("Airwolf 3D HD Menu Item"); testRunner.Delay(1); - Assert.AreEqual(ActiveSliceSettings.Instance.GetValue(SettingsKey.layer_height), .1, "Layer height is the fine override"); + Assert.AreEqual(ActiveSliceSettings.Instance.GetValue(SettingsKey.layer_height).ToString(), "0.1", "Layer height is the fine override"); // Switch to Slice Settings Tab testRunner.ClickByName("Slice Settings Tab"); testRunner.ClickByName("Quality"); testRunner.ClickByName("- none - Menu Item", delayBeforeReturn: .5); - Assert.AreEqual(ActiveSliceSettings.Instance.GetValue(SettingsKey.layer_height), .5, "Layer height is what we set it to"); + Assert.AreEqual(ActiveSliceSettings.Instance.GetValue(SettingsKey.layer_height).ToString(), "0.5", "Layer height is what we set it to"); return Task.CompletedTask; }, maxTimeToRun: 120);