Add support for renaming and duplicating presets

- Fixes #720 - Changing preset names has no effect
 - Fixes #767 - Presets editor contains user overrides
 - Fixes #768 - Oem presets should be copied...
 - Migrate from int to string based printer IDs
 - Add json document migration capabilities
This commit is contained in:
John Lewin 2016-05-16 16:21:42 -07:00
parent 9102a03dc6
commit cb56e85776
11 changed files with 337 additions and 151 deletions

View file

@ -120,7 +120,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
BaudRate = profile.BaudRate(),
ComPort = profile.ComPort(),
DriverType = profile.DriverType(),
Id = profile.Id(),
Id = profile.ID,
Make = profile.Make,
Model = profile.Model,
Name = profile.Name(),
@ -193,11 +193,11 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
}
}
internal static void SwitchToProfile(int id)
internal static void SwitchToProfile(string printerID)
{
var profile = LoadProfile(id);
var profile = LoadProfile(printerID);
UserSettings.Instance.set("ActiveProfileID", id.ToString());
UserSettings.Instance.set("ActiveProfileID", printerID);
if (profile != null)
{
@ -205,16 +205,6 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
}
}
internal static SettingsProfile LoadProfile(int id)
{
string profileID = ProfileData.Profiles.Where(p => p.Id == id.ToString()).FirstOrDefault()?.Id.ToString();
if (!string.IsNullOrEmpty(profileID))
{
return LoadProfile(profileID);
}
return null;
}
internal static void AcquireNewProfile(string make, string model, string printerName)
{
@ -223,11 +213,11 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
OemProfile printerProfile = LoadHttpOemProfile(make, model);
SettingsLayer baseConfig = LoadMatterHackersBaseLayer();
var layeredProfile = new LayeredProfile(
printerProfile,
baseConfig);
layeredProfile.DocumentPath = Path.Combine(profilesPath, guid + ".json");
layeredProfile.UserLayer["MatterControl.PrinterID"] = guid.ToString();
var layeredProfile = new LayeredProfile(printerProfile, baseConfig)
{
ID = guid,
DocumentPath = Path.Combine(profilesPath, guid + ".json")
};
layeredProfile.UserLayer["MatterControl.PrinterName"] = printerName;
// Import named macros as defined in the following printers: (Airwolf Axiom, HD, HD-R, HD2x, HDL, HDx, Me3D Me2, Robo R1[+])
@ -258,6 +248,17 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
}
}
// Copy OemProfile presets into user layers
foreach(var layer in layeredProfile.OemProfile.MaterialLayers)
{
layeredProfile.MaterialLayers[layer.Key] = layer.Value;
}
foreach (var layer in layeredProfile.OemProfile.QualityLayers)
{
layeredProfile.QualityLayers[layer.Key] = layer.Value;
}
layeredProfile.Save();
ProfileData.Profiles.Add(new PrinterInfo

View file

@ -33,6 +33,7 @@ using System.Linq;
using System.Runtime.Serialization;
using System;
using System.IO;
using Newtonsoft.Json.Linq;
namespace MatterHackers.MatterControl.SlicerConfiguration
{
@ -43,8 +44,73 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
public DateTime LastModified { get; set; }
}
public static class ProfileMigrations
{
public static string MigrateDocument(string filePath, int fromVersion)
{
var jObject = JObject.Parse(File.ReadAllText(filePath));
if (fromVersion < 201605131)
{
var materialLayers = jObject["MaterialLayers"] as JObject;
foreach (JProperty layer in materialLayers.Properties().ToList())
{
layer.Remove();
string layerID = Guid.NewGuid().ToString();
var body = layer.Value as JObject;
body["MatterControl.LayerID"] = layerID;
body["MatterControl.LayerName"] = layer.Name;
materialLayers[layerID] = layer.Value;
}
var qualityLayers = jObject["QualityLayers"] as JObject;
foreach (JProperty layer in qualityLayers.Properties().ToList())
{
layer.Remove();
string layerID = Guid.NewGuid().ToString();
var body = layer.Value as JObject;
body["MatterControl.LayerID"] = layerID;
body["MatterControl.LayerName"] = layer.Name;
qualityLayers[layerID] = layer.Value;
}
jObject["DocumentVersion"] = 201605131;
}
if (fromVersion < 201605132)
{
string printerID = Guid.NewGuid().ToString();
jObject.Remove("DocumentPath");
jObject["ID"] = printerID;
jObject["DocumentVersion"] = 201605132;
File.Delete(filePath);
filePath = Path.Combine(Path.GetDirectoryName(filePath), printerID + ".json");
}
File.WriteAllText(
filePath,
JsonConvert.SerializeObject(jObject, Formatting.Indented));
return filePath;
}
}
public class LayeredProfile
{
public int DocumentVersion { get; set; }
public string ID { get; set; }
public static int LatestVersion { get; } = 201605132;
[JsonIgnore]
internal SettingsLayer QualityLayer { get; private set; }
@ -126,7 +192,6 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
}
}
public string GetMaterialPresetKey(int extruderIndex)
{
if (extruderIndex >= MaterialSettingsKeys.Count)
@ -165,13 +230,14 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
public List<string> MaterialSettingsKeys { get; set; } = new List<string>();
[JsonIgnore]
public string DocumentPath { get; set; }
internal void Save()
{
if (!string.IsNullOrEmpty(DocumentPath))
{
File.WriteAllText(DocumentPath, JsonConvert.SerializeObject(this));
File.WriteAllText(DocumentPath, JsonConvert.SerializeObject(this, Formatting.Indented));
}
}
@ -180,19 +246,17 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
/// </summary>
public SettingsLayer UserLayer { get; } = new SettingsLayer();
public IEnumerable<string> AllMaterialKeys()
{
return MaterialLayers.Keys.Union(this.OemProfile.MaterialLayers.Keys);
}
public IEnumerable<string> AllQualityKeys()
{
return QualityLayers.Keys.Union(this.OemProfile.QualityLayers.Keys);
}
internal static LayeredProfile LoadFile(string printerProfilePath)
{
var layeredProfile = JsonConvert.DeserializeObject<LayeredProfile>(File.ReadAllText(printerProfilePath));
if (layeredProfile.DocumentVersion < LayeredProfile.LatestVersion)
{
printerProfilePath = ProfileMigrations.MigrateDocument(printerProfilePath, layeredProfile.DocumentVersion);
// Reload the document with the new schema
layeredProfile = JsonConvert.DeserializeObject<LayeredProfile>(File.ReadAllText(printerProfilePath));
}
layeredProfile.DocumentPath = printerProfilePath;
return layeredProfile;
@ -210,18 +274,22 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
/// </summary>
public Dictionary<string, SettingsLayer> QualityLayers { get; } = new Dictionary<string, SettingsLayer>();
///<summary>
///Returns the settings value at the 'top' of the stack
///</summary>
public string GetValue(string sliceSetting)
{
return GetValue(sliceSetting, settingsLayers);
return GetValue(sliceSetting, defaultLayerCascade);
}
public string GetValue(string sliceSetting, IEnumerable<SettingsLayer> layers)
public string GetValue(string sliceSetting, IEnumerable<SettingsLayer> layerCascade)
{
foreach (SettingsLayer layer in layers)
if (layerCascade == null)
{
layerCascade = defaultLayerCascade;
}
foreach (SettingsLayer layer in layerCascade)
{
string value;
if (layer.TryGetValue(sliceSetting, out value))
@ -235,7 +303,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
public SettingsLayer BaseLayer { get; set; }
private IEnumerable<SettingsLayer> settingsLayers
private IEnumerable<SettingsLayer> defaultLayerCascade
{
get
{

View file

@ -64,13 +64,25 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
layeredProfile = profile;
}
public string ID => layeredProfile.ID;
public SettingsLayer BaseLayer => layeredProfile.BaseLayer;
public SettingsLayer OemLayer => layeredProfile.OemProfile.OemLayer;
public SettingsLayer UserLayer => layeredProfile.UserLayer;
public string ActiveMaterialKey => layeredProfile.ActiveMaterialKey;
public string ActiveMaterialKey
{
get
{
return layeredProfile.ActiveMaterialKey;
}
set
{
layeredProfile.ActiveMaterialKey = value;
}
}
public string ActiveQualityKey
{
@ -92,15 +104,9 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
// Commit
}
public IEnumerable<string> AllMaterialKeys()
{
return layeredProfile.AllMaterialKeys();
}
public Dictionary<string, SettingsLayer> MaterialLayers => layeredProfile.MaterialLayers;
public IEnumerable<string> AllQualityKeys()
{
return layeredProfile.AllQualityKeys();
}
public Dictionary<string, SettingsLayer> QualityLayers => layeredProfile.QualityLayers;
public class SettingsConverter
{
@ -176,23 +182,6 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
return layeredProfile.GetMaterialLayer(key);
}
internal SettingsLayer CreatePresetsLayer(NamedSettingsLayers layerType)
{
SettingsLayer newLayer = new SettingsLayer();
if (layerType == NamedSettingsLayers.Quality)
{
newLayer.Name = "Quality" + layeredProfile.QualityLayers.Count;
layeredProfile.QualityLayers[newLayer.Name] = newLayer;
}
else
{
newLayer.Name = "Material" + layeredProfile.MaterialLayers.Count;
layeredProfile.MaterialLayers[newLayer.Name] = newLayer;
}
return newLayer;
}
internal SettingsLayer QualityLayer(string key)
{
return layeredProfile.GetQualityLayer(key);
@ -563,9 +552,9 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
return layeredProfile.GetValue(sliceSetting);
}
public string GetActiveValue(string sliceSetting, IEnumerable<SettingsLayer> layers)
public string GetActiveValue(string sliceSetting, IEnumerable<SettingsLayer> layerCascade)
{
return layeredProfile.GetValue(sliceSetting, layers);
return layeredProfile.GetValue(sliceSetting, layerCascade);
}
public void SetActiveValue(string sliceSetting, string sliceValue)
@ -924,16 +913,6 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
layeredProfile.SetActiveValue("MatterControl.PrinterName", name);
}
public string Id()
{
return layeredProfile.GetValue("MatterControl.PrinterID");
}
public void SetId(string id)
{
layeredProfile.SetActiveValue("MatterControl.PrinterID", id);
}
public string Model => layeredProfile.GetValue("MatterControl.Model");
HashSet<string> knownSettings = null;
@ -1047,9 +1026,61 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
}
}
public string Name { get; set; }
public string Source { get; set; }
public string ETag { get; set; }
public string ID
{
get
{
// TODO: Short term hack to silently upgrade existing profiles with missing ID
string layerKey = ValueOrDefault("MatterControl.LayerID");
if (string.IsNullOrEmpty(layerKey))
{
layerKey = Guid.NewGuid().ToString();
ID = layerKey;
}
return layerKey;
}
set
{
this["MatterControl.LayerID"] = value;
}
}
public string Name
{
get
{
return ValueOrDefault("MatterControl.LayerName");
}
set
{
this["MatterControl.LayerName"] = value;
}
}
public string Source
{
get
{
return ValueOrDefault("MatterControl.LayerSource");
}
set
{
this["MatterControl.LayerSource"] = value;
}
}
public string ETag
{
get
{
return ValueOrDefault("MatterControl.LayerETag");
}
set
{
this["MatterControl.LayerETag"] = value;
}
}
public string ValueOrDefault(string key, string defaultValue = "")
{
@ -1104,6 +1135,18 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
return layer;
}
public SettingsLayer Clone()
{
string id = Guid.NewGuid().ToString();
return new SettingsLayer(this as Dictionary<string, string>)
{
ID = id,
Name = this.Name,
ETag = this.ETag,
Source = this.Source
};
}
}
public class ProfileData