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
This commit is contained in:
Matt Moening 2016-08-10 16:38:58 -07:00
parent c78be029c6
commit bdf3e8f2a5
4 changed files with 104 additions and 40 deletions

View file

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

View file

@ -371,6 +371,23 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
return "";
}
public bool Contains(string sliceSetting, IEnumerable<PrinterSettingsLayer> 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
{

View file

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

View file

@ -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.