From bdf3e8f2a571502baee36347f67dfc1fcfbf9fab Mon Sep 17 00:00:00 2001 From: Matt Moening Date: Wed, 10 Aug 2016 16:38:58 -0700 Subject: [PATCH] Importing a printer/settings from a file will now notify the user if the file contains no valid user settings - ImportFomEisting now returns a bool of whether or not it succeeded - PrinterSettings now has a Contains method - merging or importing quality/material settings check to make sure at least one setting was importable and notifies user if not --- SetupWizard/ImportSettingsPage.cs | 89 +++++++++++++------ .../Settings/PrinterSettings.cs | 17 ++++ .../Settings/ProfileManager.cs | 35 ++++---- StaticData/Translations/Master.txt | 3 + 4 files changed, 104 insertions(+), 40 deletions(-) diff --git a/SetupWizard/ImportSettingsPage.cs b/SetupWizard/ImportSettingsPage.cs index 80d17f0d7..a1d9b94de 100644 --- a/SetupWizard/ImportSettingsPage.cs +++ b/SetupWizard/ImportSettingsPage.cs @@ -431,11 +431,18 @@ namespace MatterHackers.MatterControl { if(newPrinterButton.Checked) { - ProfileManager.ImportFromExisting(settingsFilePath); - WizardWindow.ChangeToPage(new ImportSucceeded(importPrinterSuccessMessage.FormatWith(Path.GetFileNameWithoutExtension(settingsFilePath))) + if(ProfileManager.ImportFromExisting(settingsFilePath)) { - WizardWindow = this.WizardWindow, - }); + WizardWindow.ChangeToPage(new ImportSucceeded(importPrinterSuccessMessage.FormatWith(Path.GetFileNameWithoutExtension(settingsFilePath))) + { + WizardWindow = this.WizardWindow, + }); + } + else + { + displayFailedToImportMessage(settingsFilePath); + } + } else if(mergeButton.Checked) { @@ -487,6 +494,7 @@ namespace MatterHackers.MatterControl var settingsToImport = PrinterSettingsLayer.LoadFromIni(settingsFilePath); string layerHeight; + bool containsValidSetting = false; bool isSlic3r = importType == ".slice" || settingsToImport.TryGetValue(SettingsKey.layer_height, out layerHeight); if (isSlic3r) { @@ -502,35 +510,49 @@ namespace MatterHackers.MatterControl foreach (var item in settingsToImport) { - string currentValue = ActiveSliceSettings.Instance.GetValue(item.Key, baseAndOEMCascade).Trim(); - // Compare the value to import to the layer cascade value and only set if different - if (currentValue != item.Value) + if(ActiveSliceSettings.Instance.Contains(item.Key)) { - newLayer[item.Key] = item.Value; + containsValidSetting = true; + string currentValue = ActiveSliceSettings.Instance.GetValue(item.Key, baseAndOEMCascade).Trim(); + // Compare the value to import to the layer cascade value and only set if different + if (currentValue != item.Value) + { + newLayer[item.Key] = item.Value; + } } + } - if (newMaterialPresetButton.Checked) + if(containsValidSetting) { - ActiveSliceSettings.Instance.MaterialLayers.Add(newLayer); + if (newMaterialPresetButton.Checked) + { + ActiveSliceSettings.Instance.MaterialLayers.Add(newLayer); + } + else + { + ActiveSliceSettings.Instance.QualityLayers.Add(newLayer); + } + + ActiveSliceSettings.Instance.Save(); + + WizardWindow.ChangeToPage(new ImportSucceeded(importSettingSuccessMessage.FormatWith(Path.GetFileNameWithoutExtension(settingsFilePath), sectionName)) + { + WizardWindow = this.WizardWindow, + }); } else { - ActiveSliceSettings.Instance.QualityLayers.Add(newLayer); + displayFailedToImportMessage(settingsFilePath); } - ActiveSliceSettings.Instance.Save(); - - WizardWindow.ChangeToPage(new ImportSucceeded(importSettingSuccessMessage.FormatWith(Path.GetFileNameWithoutExtension(settingsFilePath), sectionName)) - { - WizardWindow = this.WizardWindow, - }); } else { + displayFailedToImportMessage(settingsFilePath); // looks like a cura file #if DEBUG - throw new NotImplementedException("need to import from 'cure.ini' files"); + //throw new NotImplementedException("need to import from 'cure.ini' files"); #endif } break; @@ -562,23 +584,35 @@ namespace MatterHackers.MatterControl string layerHeight; bool isSlic3r = settingsToImport.TryGetValue(SettingsKey.layer_height, out layerHeight); + bool containsValidSetting = false; //if (isSlic3r) { var activeSettings = ActiveSliceSettings.Instance; foreach (var item in settingsToImport) { - // Compare the value to import to the layer cascade value and only set if different - string currentValue = activeSettings.GetValue(item.Key, null).Trim(); - if (currentValue != item.Value) + if(activeSettings.Contains(item.Key)) { - activeSettings.UserLayer[item.Key] = item.Value; + containsValidSetting = true; + string currentValue = activeSettings.GetValue(item.Key, null).Trim(); + // Compare the value to import to the layer cascade value and only set if different + if (currentValue != item.Value) + { + activeSettings.UserLayer[item.Key] = item.Value; + } } + } + if(containsValidSetting) + { + activeSettings.Save(); - activeSettings.Save(); - - UiThread.RunOnIdle(ApplicationController.Instance.ReloadAdvancedControlsPanel); + UiThread.RunOnIdle(ApplicationController.Instance.ReloadAdvancedControlsPanel); + } + else + { + displayFailedToImportMessage(settingsFilePath); + } } //else { @@ -598,5 +632,10 @@ namespace MatterHackers.MatterControl } Invalidate(); } + + private void displayFailedToImportMessage(string settingsFilePath) + { + StyledMessageBox.ShowMessageBox(null, "Oops! Settings file '{0}' did not contain any settings we could import.".Localize().FormatWith(Path.GetFileName(settingsFilePath)), "Unable to Import".Localize()); + } } } diff --git a/SlicerConfiguration/Settings/PrinterSettings.cs b/SlicerConfiguration/Settings/PrinterSettings.cs index aef26ffb0..d72dfdad7 100644 --- a/SlicerConfiguration/Settings/PrinterSettings.cs +++ b/SlicerConfiguration/Settings/PrinterSettings.cs @@ -371,6 +371,23 @@ namespace MatterHackers.MatterControl.SlicerConfiguration return ""; } + public bool Contains(string sliceSetting, IEnumerable layerCascade = null) + { + if (layerCascade == null) + { + layerCascade = defaultLayerCascade; + } + foreach (PrinterSettingsLayer layer in layerCascade) + { + string value; + if (layer.TryGetValue(sliceSetting, out value)) + { + return true; + } + } + return false; + } + [JsonIgnore] public PrinterSettingsLayer BaseLayer { diff --git a/SlicerConfiguration/Settings/ProfileManager.cs b/SlicerConfiguration/Settings/ProfileManager.cs index ab706050f..bc12d7d82 100644 --- a/SlicerConfiguration/Settings/ProfileManager.cs +++ b/SlicerConfiguration/Settings/ProfileManager.cs @@ -262,11 +262,11 @@ namespace MatterHackers.MatterControl.SlicerConfiguration } } - internal static void ImportFromExisting(string settingsFilePath) + internal static bool ImportFromExisting(string settingsFilePath) { if (string.IsNullOrEmpty(settingsFilePath) || !File.Exists(settingsFilePath)) { - return; + return false; } var printerInfo = new PrinterInfo @@ -274,7 +274,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration Name = Path.GetFileNameWithoutExtension(settingsFilePath), ID = Guid.NewGuid().ToString() }; - + bool importSuccessful = false; string importType = Path.GetExtension(settingsFilePath).ToLower(); switch (importType) { @@ -290,28 +290,33 @@ namespace MatterHackers.MatterControl.SlicerConfiguration Instance.Profiles.Add(printerInfo); profile.Save(); + importSuccessful = true; break; case ".ini": var settingsToImport = PrinterSettingsLayer.LoadFromIni(settingsFilePath); - - var layeredProfile = new PrinterSettings() + //Other import paths validate that this is a slicer or MC ini file via checking if layer_height is a setting + if(settingsToImport.ContainsKey(SettingsKey.layer_height)) { - ID = printerInfo.ID, - OemLayer = settingsToImport - }; + var layeredProfile = new PrinterSettings() + { + ID = printerInfo.ID, + OemLayer = settingsToImport + }; - // TODO: Resolve name conflicts - layeredProfile.UserLayer[SettingsKey.printer_name.ToString()] = printerInfo.Name; + // TODO: Resolve name conflicts + layeredProfile.UserLayer[SettingsKey.printer_name.ToString()] = printerInfo.Name; - layeredProfile.ClearValue(SettingsKey.device_token); - printerInfo.DeviceToken = ""; - Instance.Profiles.Add(printerInfo); - - layeredProfile.Save(); + layeredProfile.ClearValue(SettingsKey.device_token); + printerInfo.DeviceToken = ""; + Instance.Profiles.Add(printerInfo); + layeredProfile.Save(); + importSuccessful = true; + } break; } + return importSuccessful; } internal static async void AcquireNewProfile(string make, string model, string printerName) diff --git a/StaticData/Translations/Master.txt b/StaticData/Translations/Master.txt index 484c45c86..b07986f33 100644 --- a/StaticData/Translations/Master.txt +++ b/StaticData/Translations/Master.txt @@ -5311,3 +5311,6 @@ Translated:There is a required update available. English:Import Printer Translated:Import Printer +English:Oops! Settings file '{0}' did not contain any settings we could import. +Translated:Oops! Settings file '{0}' did not contain any settings we could import. +