mattercontrol/SlicerConfiguration/SliceSettingsOrganizer.cs

297 lines
8.5 KiB
C#
Raw Normal View History

/*
Copyright (c) 2016, Kevin Pope, John Lewin
2014-04-05 15:51:13 -07:00
All rights reserved.
Redistribution and use in source and binary forms, with or without
2015-04-08 15:20:10 -07:00
modification, are permitted provided that the following conditions are met:
2014-04-05 15:51:13 -07:00
1. Redistributions of source code must retain the above copyright notice, this
2015-04-08 15:20:10 -07:00
list of conditions and the following disclaimer.
2014-04-05 15:51:13 -07:00
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
2015-04-08 15:20:10 -07:00
and/or other materials provided with the distribution.
2014-04-05 15:51:13 -07:00
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
2015-04-08 15:20:10 -07:00
of the authors and should not be interpreted as representing official policies,
2014-04-05 15:51:13 -07:00
either expressed or implied, of the FreeBSD Project.
*/
2015-04-08 15:20:10 -07:00
2014-04-05 15:51:13 -07:00
using System;
2014-01-29 19:09:30 -08:00
using System.Collections.Generic;
using System.IO;
using MatterHackers.Agg.Platform;
using MatterHackers.Localizations;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
2014-01-29 19:09:30 -08:00
namespace MatterHackers.MatterControl.SlicerConfiguration
2014-01-29 19:09:30 -08:00
{
2015-04-08 15:20:10 -07:00
public class QuickMenuNameValue
{
public string MenuName;
public string Value;
}
public class SliceSettingData
2015-04-08 15:20:10 -07:00
{
[JsonConverter(typeof(StringEnumConverter))]
public enum DataEditTypes { STRING, INT, INT_OR_MM, DOUBLE, POSITIVE_DOUBLE, OFFSET, DOUBLE_OR_PERCENT, VECTOR2, OFFSET2, CHECK_BOX, LIST, MULTI_LINE_TEXT, HARDWARE_PRESENT, COM_PORT, IP_LIST };
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
public string SlicerConfigName { get; set; }
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
public string PresentationName { get; set; }
2014-01-29 19:09:30 -08:00
public string ShowIfSet { get; set; }
public string EnableIfSet { get; set; }
public string DefaultValue { get; set; }
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
public DataEditTypes DataEditType { get; set; }
2014-01-29 19:09:30 -08:00
public string HelpText { get; set; } = "";
public string Units { get; set; } = "";
2014-01-29 19:09:30 -08:00
public string ListValues { get; set; } = "";
public bool ShowAsOverride { get; set; } = true;
2015-04-08 15:20:10 -07:00
public List<QuickMenuNameValue> QuickMenuSettings = new List<QuickMenuNameValue>();
public List<Dictionary<string, string>> SetSettingsOnChange = new List<Dictionary<string,string>>();
public bool ResetAtEndOfPrint { get; set; } = false;
public bool RebuildGCodeOnChange { get; set; } = true;
public bool ReloadUiWhenChanged { get; set; } = false;
2014-01-29 19:09:30 -08:00
2018-01-13 18:15:23 -08:00
public SubGroup OrganizerSubGroup { get; set; }
public SliceSettingData(string slicerConfigName, string presentationName, DataEditTypes dataEditType, string helpText = "")
2015-04-08 15:20:10 -07:00
{
// During deserialization Json.net has to call this constructor but may fail to find the optional ExtraSettings
// value. When this occurs, it passes null overriding the default empty string. To ensure empty string instead
// of null, we conditionally reassign "" if null
2015-04-08 15:20:10 -07:00
this.SlicerConfigName = slicerConfigName;
this.PresentationName = presentationName;
this.DataEditType = dataEditType;
this.HelpText = helpText.Localize();
2015-04-08 15:20:10 -07:00
}
}
2018-01-13 18:15:23 -08:00
public class SubGroup
2015-04-08 15:20:10 -07:00
{
public string Name { get; }
2015-04-08 15:20:10 -07:00
2018-01-13 18:13:29 -08:00
public List<SliceSettingData> Settings { get; private set; } = new List<SliceSettingData>();
2015-04-08 15:20:10 -07:00
2018-01-13 18:15:23 -08:00
public SubGroup(string groupName, Group group)
2015-04-08 15:20:10 -07:00
{
this.Name = groupName;
2018-01-13 18:13:29 -08:00
this.Group = group;
2015-04-08 15:20:10 -07:00
}
2018-01-13 18:15:23 -08:00
public Group Group { get; }
2015-04-08 15:20:10 -07:00
}
2018-01-13 18:15:23 -08:00
public class Group
2015-04-08 15:20:10 -07:00
{
public string Name { get; }
2015-04-08 15:20:10 -07:00
2018-01-13 18:15:23 -08:00
public List<SubGroup> SubGroups { get; set; } = new List<SubGroup>();
2018-01-13 18:15:23 -08:00
public Group(string displayName, Category organizerCategory)
2015-04-08 15:20:10 -07:00
{
this.Name = displayName;
2018-01-13 18:13:29 -08:00
this.Category = organizerCategory;
2015-04-08 15:20:10 -07:00
}
2018-01-13 18:15:23 -08:00
public Category Category { get; }
2015-04-08 15:20:10 -07:00
}
2018-01-13 18:15:23 -08:00
public class Category
2015-04-08 15:20:10 -07:00
{
public string Name { get; set; }
2018-01-13 18:15:23 -08:00
public List<Group> Groups { get; set; } = new List<Group>();
2015-04-08 15:20:10 -07:00
2018-01-13 18:15:23 -08:00
public Category(string categoryName, UserLevel userLevel)
2015-04-08 15:20:10 -07:00
{
this.Name = categoryName;
2018-01-13 18:13:29 -08:00
this.UserLevel = userLevel;
2015-04-08 15:20:10 -07:00
}
2018-01-13 18:15:23 -08:00
private UserLevel UserLevel { get; }
2015-04-08 15:20:10 -07:00
}
2018-01-13 18:15:23 -08:00
public class UserLevel
2015-04-08 15:20:10 -07:00
{
public string Name { get; set; }
2018-01-13 18:15:23 -08:00
public List<Category> Categories = new List<Category>();
2015-04-08 15:20:10 -07:00
2018-01-13 18:15:23 -08:00
private Dictionary<string, SubGroup> mappedSettings = new Dictionary<string, SubGroup>();
2018-01-13 18:15:23 -08:00
public UserLevel(string userLevelName)
2015-04-08 15:20:10 -07:00
{
this.Name = userLevelName;
2015-04-08 15:20:10 -07:00
}
2018-01-13 18:15:23 -08:00
internal void AddSetting(string slicerConfigName, SubGroup organizerSubGroup)
{
mappedSettings.Add(slicerConfigName, organizerSubGroup);
}
public bool ContainsKey(string settingsKey) => mappedSettings.ContainsKey(settingsKey);
2018-01-13 18:15:23 -08:00
public SubGroup GetContainerForSetting(string slicerConfigName)
{
return mappedSettings[slicerConfigName];
}
2015-04-08 15:20:10 -07:00
}
public class SliceSettingsOrganizer
{
2018-01-13 18:15:23 -08:00
public Dictionary<string, UserLevel> UserLevels { get; set; } = new Dictionary<string, UserLevel>();
2015-04-08 15:20:10 -07:00
private static SliceSettingsOrganizer instance = null;
public static Dictionary<string, SliceSettingData> SettingsData { get; }
static SliceSettingsOrganizer()
{
string propertiesFileContents = AggContext.StaticData.ReadAllText(Path.Combine("SliceSettings", "Properties.json"));
var propertiesJsonData = JsonConvert.DeserializeObject<List<SliceSettingData>>(propertiesFileContents);
SettingsData = new Dictionary<string, SliceSettingData>();
foreach (var settingsData in propertiesJsonData)
{
SettingsData.Add(settingsData.SlicerConfigName, settingsData);
}
}
2015-04-08 15:20:10 -07:00
public static SliceSettingsOrganizer Instance
{
get
{
if (instance == null)
{
instance = new SliceSettingsOrganizer();
}
return instance;
}
}
private SliceSettingsOrganizer()
{
LoadAndParseSettingsFiles();
2014-01-29 19:09:30 -08:00
#if false
Categories.Add(CreatePrintSettings());
SettingsCategory filamentSettingsCategory = new SettingsCategory("Filament Settings");
Categories.Add(filamentSettingsCategory);
SettingsCategory printerSettingsCategory = new SettingsCategory("Printer Settings");
Categories.Add(printerSettingsCategory);
#endif
2015-04-08 15:20:10 -07:00
}
public bool Contains(string userLevelKey, string slicerConfigName)
2015-04-08 15:20:10 -07:00
{
2018-01-13 18:15:23 -08:00
if (this.UserLevels.TryGetValue(userLevelKey, out UserLevel userLevel))
2015-04-08 15:20:10 -07:00
{
return userLevel.ContainsKey(slicerConfigName);
2015-04-08 15:20:10 -07:00
}
return false;
}
public SliceSettingData GetSettingsData(string slicerConfigName)
2015-04-08 15:20:10 -07:00
{
if (SliceSettingsOrganizer.SettingsData.TryGetValue(slicerConfigName, out SliceSettingData settingsData))
2015-04-08 15:20:10 -07:00
{
return settingsData;
2015-04-08 15:20:10 -07:00
}
2015-04-08 15:20:10 -07:00
return null;
}
private void LoadAndParseSettingsFiles()
{
2018-01-13 18:15:23 -08:00
UserLevel userLevelToAddTo = null;
Category categoryToAddTo = null;
Group groupToAddTo = null;
SubGroup subGroupToAddTo = null;
2015-04-08 15:20:10 -07:00
foreach (string line in AggContext.StaticData.ReadAllLines(Path.Combine("SliceSettings", "Layouts.txt")))
2015-04-08 15:20:10 -07:00
{
if (line.Length > 0)
{
string sanitizedLine = line.Replace('"', ' ').Trim();
2015-04-08 15:20:10 -07:00
switch (CountLeadingSpaces(line))
{
case 0:
2018-01-13 18:15:23 -08:00
userLevelToAddTo = new UserLevel(sanitizedLine);
UserLevels.Add(sanitizedLine, userLevelToAddTo);
2015-04-08 15:20:10 -07:00
break;
case 2:
2018-01-13 18:15:23 -08:00
categoryToAddTo = new Category(sanitizedLine, userLevelToAddTo);
2018-01-13 18:13:29 -08:00
userLevelToAddTo.Categories.Add(categoryToAddTo);
2015-04-08 15:20:10 -07:00
break;
case 4:
2018-01-13 18:15:23 -08:00
groupToAddTo = new Group(sanitizedLine, categoryToAddTo);
2018-01-13 18:13:29 -08:00
categoryToAddTo.Groups.Add(groupToAddTo);
2015-04-08 15:20:10 -07:00
break;
case 6:
2018-01-13 18:15:23 -08:00
subGroupToAddTo = new SubGroup(sanitizedLine, groupToAddTo);
2018-01-13 18:13:29 -08:00
groupToAddTo.SubGroups.Add(subGroupToAddTo);
2015-04-08 15:20:10 -07:00
break;
case 8:
SliceSettingData data = GetSettingsData(sanitizedLine);
2015-04-08 15:20:10 -07:00
if (data != null)
{
2018-01-13 18:13:29 -08:00
subGroupToAddTo.Settings.Add(data);
data.OrganizerSubGroup = subGroupToAddTo;
userLevelToAddTo.AddSetting(data.SlicerConfigName, subGroupToAddTo);
2015-04-08 15:20:10 -07:00
}
break;
default:
throw new Exception("Bad file, too many spaces (must be 0, 2, 4 or 6).");
}
}
}
}
private static int CountLeadingSpaces(string line)
{
int numSpaces = 0;
while (line[numSpaces] == ' ' && numSpaces < line.Length)
{
numSpaces++;
}
return numSpaces;
}
}
}