Continue consolidating Printer load operations

This commit is contained in:
John Lewin 2017-09-23 14:44:43 -07:00
parent 9694e9775f
commit 80a75a40fe
10 changed files with 117 additions and 120 deletions

View file

@ -74,7 +74,10 @@ namespace MatterHackers.MatterControl
else
{
lastSelectedIndex = this.SelectedIndex;
UiThread.RunOnIdle(() => ActiveSliceSettings.SwitchToProfile(printerID));
UiThread.RunOnIdle(() =>
{
ProfileManager.SwitchToProfile(printerID).ConfigureAwait(false);
});
}
}
};

View file

@ -111,14 +111,80 @@ namespace MatterHackers.MatterControl
private Queue<Func<Task>> queuedThumbCallbacks = new Queue<Func<Task>>();
public void SetActivePrinter(PrinterConfig printer)
public void SetActivePrinter(PrinterConfig printer, bool allowChangedEvent = true)
{
(this.ActivePrinters as List<PrinterConfig>).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<PrinterConfig>).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<bool>(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);

View file

@ -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<double>(SettingsKey.build_height);

View file

@ -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);

View file

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

View file

@ -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<bool>(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);
}
}
/// <summary>
/// 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);
}

View file

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

View file

@ -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)));
}
/// <summary>
/// Loads a ProfileManager for the given user
/// </summary>
@ -479,8 +476,6 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
ApplicationController.Instance.SetActivePrinter(printer);
ActiveSliceSettings.Instance = printerSettings;
return printer;
}

View file

@ -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);

View file

@ -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<double>(SettingsKey.layer_height), .5, "Layer height is what we set it to");
Assert.AreEqual(ActiveSliceSettings.Instance.GetValue<double>(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<double>(SettingsKey.layer_height), .1, "Layer height is the fine override");
Assert.AreEqual(ActiveSliceSettings.Instance.GetValue<double>(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<double>(SettingsKey.layer_height), .1, "Layer height is the fine override");
Assert.AreEqual(ActiveSliceSettings.Instance.GetValue<double>(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<double>(SettingsKey.layer_height), .5, "Layer height is what we set it to");
Assert.AreEqual(ActiveSliceSettings.Instance.GetValue<double>(SettingsKey.layer_height).ToString(), "0.5", "Layer height is what we set it to");
return Task.CompletedTask;
}, maxTimeToRun: 120);