mattercontrol/SlicerConfiguration/Settings/ActiveSliceSettings.cs
John Lewin 3a9833697d Platform providers
- New Provider model
- Remove WindowsFileDialogs project
- Remove PlatformAbstract assembly, use AggContext
- Rename OsInformation to OperatingSystem
2017-08-22 13:42:34 -07:00

179 lines
No EOL
6.1 KiB
C#

/*
Copyright (c) 2016, Lars Brubaker, John Lewin
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
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
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using MatterHackers.Agg;
using MatterHackers.Agg.Platform;
using MatterHackers.Agg.UI;
using MatterHackers.MatterControl.PrinterCommunication;
using MatterHackers.MatterControl.SettingsManagement;
using Newtonsoft.Json;
namespace MatterHackers.MatterControl.SlicerConfiguration
{
public enum NamedSettingsLayers { MHBaseSettings, OEMSettings, Quality, Material, User, All }
public class ActiveSliceSettings
{
public static RootedObjectEventHandler ActivePrinterChanged = new RootedObjectEventHandler();
public static RootedObjectEventHandler ActiveProfileModified = new RootedObjectEventHandler();
public static RootedObjectEventHandler SettingChanged = new RootedObjectEventHandler();
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)
{
PrinterConnection.Instance.Disable();
}
activeInstance = value;
BedSettings.SetMakeAndModel(activeInstance.GetValue(SettingsKey.make), activeInstance.GetValue(SettingsKey.model));
SwitchToPrinterTheme();
OnActivePrinterChanged(null);
if (!MatterControlApplication.IsLoading)
{
if (ActiveSliceSettings.Instance.PrinterSelected
&& Instance.GetValue<bool>(SettingsKey.auto_connect))
{
UiThread.RunOnIdle(() =>
{
PrinterConnection.Instance.ConnectToActivePrinter(showConnectionHelp);
showConnectionHelp = false;
}, 2);
}
}
}
}
}
public static List<SliceSettingData> SettingsData { get; private set; } = new List<SliceSettingData>();
private static Dictionary<string, SliceSettingData> settingsByName;
static ActiveSliceSettings()
{
string propertiesFileContents = AggContext.StaticData.ReadAllText(Path.Combine("SliceSettings", "Properties.json"));
SettingsData = JsonConvert.DeserializeObject<List<SliceSettingData>>(propertiesFileContents);
activeInstance = PrinterSettings.Empty;
settingsByName = new Dictionary<string, SliceSettingData>();
foreach (var settingsData in ActiveSliceSettings.SettingsData)
{
settingsByName.Add(settingsData.SlicerConfigName, settingsData);
}
activeInstance = PrinterSettings.Empty;
}
public static void OnSettingChanged(string slicerConfigName)
{
SettingChanged.CallEvents(null, new StringEventArgs(slicerConfigName));
SliceSettingData settingsData;
if (settingsByName.TryGetValue(slicerConfigName, out settingsData) && settingsData.ReloadUiWhenChanged)
{
UiThread.RunOnIdle(ApplicationController.Instance.ReloadAll);
}
}
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
/// occurring rather than two
/// </summary>
public static void SwitchToPrinterTheme()
{
if (ActiveSliceSettings.Instance.PrinterSelected)
{
//Attempt to load userSetting theme as default
string activeThemeName = ActiveSliceSettings.Instance.GetValue(SettingsKey.active_theme_name);
if (!string.IsNullOrEmpty(activeThemeName))
{
ActiveTheme.Instance = ActiveTheme.GetThemeColors(activeThemeName);
}
}
}
internal static async Task SwitchToProfile(string printerID)
{
ProfileManager.Instance.LastProfileID = printerID;
Instance = (await ProfileManager.LoadProfileAsync(printerID)) ?? PrinterSettings.Empty;
}
private static void OnActivePrinterChanged(EventArgs e)
{
ActivePrinterChanged.CallEvents(null, e);
}
internal static void OnMaterialPresetChanged()
{
MaterialPresetChanged?.Invoke(null, null);
}
}
}