This commit is contained in:
Lars Brubaker 2016-07-14 11:19:13 -07:00
parent 49bc3d8724
commit d681a01cf6
13 changed files with 164 additions and 79 deletions

View file

@ -57,7 +57,7 @@ namespace MatterHackers.MatterControl
AddChild(advancedTab);
AnchorAll();
ApplicationController.Instance.ReloadAdvancedControlsPanelTrigger.RegisterEvent((s, e) => UiThread.RunOnIdle(ReloadSliceSettings), ref unregisterEvents);
ApplicationController.Instance.AdvancedControlsPanelReloading.RegisterEvent((s, e) => UiThread.RunOnIdle(ReloadSliceSettings), ref unregisterEvents);
}
public static string SliceSettingsTabName { get; } = "Slice Settings Tab";

View file

@ -158,7 +158,7 @@ namespace MatterHackers.MatterControl
public class ApplicationController
{
private static ApplicationController globalInstance;
public RootedObjectEventHandler ReloadAdvancedControlsPanelTrigger = new RootedObjectEventHandler();
public RootedObjectEventHandler AdvancedControlsPanelReloading = new RootedObjectEventHandler();
public RootedObjectEventHandler CloudSyncStatusChanged = new RootedObjectEventHandler();
public RootedObjectEventHandler DoneReloadingAll = new RootedObjectEventHandler();
public RootedObjectEventHandler PluginsLoaded = new RootedObjectEventHandler();
@ -185,12 +185,7 @@ namespace MatterHackers.MatterControl
public ApplicationController()
{
//Name = "MainSlidePanel";
ActiveTheme.ThemeChanged.RegisterEvent(ThemeChanged, ref unregisterEvents);
}
public void ThemeChanged(object sender, EventArgs e)
{
ReloadAll(null, null);
ActiveTheme.ThemeChanged.RegisterEvent(ReloadAll, ref unregisterEvents);
}
public void StartLogin()
@ -322,7 +317,7 @@ namespace MatterHackers.MatterControl
public void ReloadAdvancedControlsPanel()
{
ReloadAdvancedControlsPanelTrigger.CallEvents(this, null);
AdvancedControlsPanelReloading.CallEvents(this, null);
}
public LibraryDataView CurrentLibraryDataView = null;
@ -360,7 +355,33 @@ namespace MatterHackers.MatterControl
string activeUserName = ApplicationController.Instance.GetSessionUsernameForFileSystem();
UserSettings.Instance.set("ActiveUserName", activeUserName);
UserChanged();
}
// Called after every startup and at the completion of every authentication change
public void UserChanged()
{
ProfileManager.Reload();
var profileManager = ProfileManager.Instance;
// Ensure SQLite printers are imported
profileManager.EnsurePrintersImported();
// If profiles.json was created, run the import wizard to pull in any SQLite printers
if (!profileManager.IsGuestProfile && !profileManager.PrintersImported)
{
var wizardPage = new CopyGuestProfilesToUser(() =>
{
// On success, set state indicating import has been run and update ProfileManager state
profileManager.PrintersImported = true;
profileManager.Save();
});
// Show the import printers wizard
WizardWindow.Show("/CopyGuestProfiles", "Copy Existing Profiles", wizardPage);
}
}
public class CloudSyncEventArgs : EventArgs

View file

@ -219,7 +219,7 @@ namespace MatterHackers.MatterControl
}, ref unregisterEvents);
ApplicationController.Instance.ReloadAdvancedControlsPanelTrigger.RegisterEvent((s, e) => UiThread.RunOnIdle(ReloadAdvancedControls), ref unregisterEvents);
ApplicationController.Instance.AdvancedControlsPanelReloading.RegisterEvent((s, e) => UiThread.RunOnIdle(ReloadAdvancedControls), ref unregisterEvents);
UpdateControlData.Instance.UpdateStatusChanged.RegisterEvent(SetUpdateNotification, ref unregisterEvents);
// Make sure we are on the right tab when we create this view

View file

@ -69,7 +69,7 @@ namespace MatterHackers.MatterControl
Padding = new BorderDouble(4);
PrinterConnectionAndCommunication.Instance.ActivePrintItemChanged.RegisterEvent(onActivePrintItemChanged, ref unregisterEvents);
ApplicationController.Instance.ReloadAdvancedControlsPanelTrigger.RegisterEvent((s, e) => UiThread.RunOnIdle(ReloadAdvancedControlsPanel), ref unregisterEvents);
ApplicationController.Instance.AdvancedControlsPanelReloading.RegisterEvent((s, e) => UiThread.RunOnIdle(ReloadAdvancedControlsPanel), ref unregisterEvents);
this.BoundsChanged += onBoundsChanges;
}

View file

@ -137,8 +137,12 @@ namespace MatterHackers.MatterControl.DataStorage.ClassicDB
//layeredProfile.SetActiveValue(""calibration_files"", ???);
layeredProfile.ID = printer.Id.ToString();
string fullProfilePath = Path.Combine(profilePath, printer.Id + ".json");
File.WriteAllText(fullProfilePath, JsonConvert.SerializeObject(layeredProfile, Formatting.Indented));
layeredProfile.DocumentVersion = PrinterSettings.LatestVersion;
var settingsProfile = new SettingsProfile(layeredProfile);
settingsProfile.SaveChanges();
}
private static void LoadMaterialSettings(PrinterSettings layeredProfile, Printer printer)

View file

@ -451,8 +451,8 @@ namespace MatterHackers.MatterControl
}
#if DEBUG
//public static string MCWSBaseUri { get; } = "http://192.168.2.129:9206";
public static string MCWSBaseUri { get; } = "https://mattercontrol-test.appspot.com";
public static string MCWSBaseUri { get; } = "http://192.168.2.129:9206";
//public static string MCWSBaseUri { get; } = "https://mattercontrol-test.appspot.com";
#else
public static string MCWSBaseUri { get;} = "https://mattercontrol.appspot.com";
#endif
@ -694,6 +694,8 @@ namespace MatterHackers.MatterControl
TerminalWindow.ShowIfLeftOpen();
ApplicationController.Instance.UserChanged();
#if false
{
SystemWindow releaseNotes = new SystemWindow(640, 480);

View file

@ -64,7 +64,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
public PartPreview3DWidget()
{
SliceSettingsWidget.SettingChanged.RegisterEvent(CheckSettingChanged, ref unregisterEvents);
ApplicationController.Instance.ReloadAdvancedControlsPanelTrigger.RegisterEvent(CheckSettingChanged, ref unregisterEvents);
ApplicationController.Instance.AdvancedControlsPanelReloading.RegisterEvent(CheckSettingChanged, ref unregisterEvents);
#if false
"extruder_offset",
#endif

View file

@ -110,7 +110,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
CreateAndAddChildren();
SliceSettingsWidget.SettingChanged.RegisterEvent(CheckSettingChanged, ref unregisterEvents);
ApplicationController.Instance.ReloadAdvancedControlsPanelTrigger.RegisterEvent((s, e) => ClearGCode(), ref unregisterEvents);
ApplicationController.Instance.AdvancedControlsPanelReloading.RegisterEvent((s, e) => ClearGCode(), ref unregisterEvents);
ActiveSliceSettings.ActivePrinterChanged.RegisterEvent(CheckSettingChanged, ref unregisterEvents);
}

View file

@ -35,18 +35,18 @@ using MatterHackers.MatterControl;
using MatterHackers.MatterControl.CustomWidgets;
using MatterHackers.Agg;
using System.Collections.Generic;
using MatterHackers.MatterControl.SlicerConfiguration;
namespace MatterHackers.MatterControl
{
public class CopyGuestProfilesToUser : WizardPage
{
static string importMessage = "Select which printers you would like to copy into the user account '{0}'.";
static string importMessage = "Select what you would like to merge into your current profile.".Localize();
List<string> guestProfiles;
List<CheckBox> checkBoxes = new List<CheckBox>();
public CopyGuestProfilesToUser()
: base("Cancel", "Copy Printers to User")
public CopyGuestProfilesToUser(Action afterProfilesImported)
: base("Cancel", "Copy Guest Printers")
{
var scrollWindow = new ScrollableWidget()
{
@ -63,35 +63,31 @@ namespace MatterHackers.MatterControl
};
scrollWindow.AddChild(container);
container.AddChild(new WrappedTextWidget(importMessage.FormatWith(ApplicationController.Instance.GetSessionUsername()),
10, textColor: ActiveTheme.Instance.PrimaryTextColor));
container.AddChild(new WrappedTextWidget(importMessage, 10, textColor: ActiveTheme.Instance.PrimaryTextColor));
guestProfiles = new List<string>()
{
"TAZ6",
"JumpStart",
"Emulator",
"Other test printer",
};
var byCheckbox = new Dictionary<CheckBox, PrinterInfo>();
if (guestProfiles.Count > 0)
var guestProfileManager = ProfileManager.LoadGuestDB();
if (guestProfileManager.Profiles.Count > 0)
{
container.AddChild(new TextWidget("")
container.AddChild(new TextWidget("Existing Printers:")
{
TextColor = ActiveTheme.Instance.PrimaryTextColor,
Margin = new BorderDouble(0, 3, 0, 3),
Margin = new BorderDouble(0, 3, 0, 15),
});
foreach (var profileName in guestProfiles)
foreach (var printerInfo in guestProfileManager.Profiles)
{
CheckBox importButton = new CheckBox(profileName)
var checkBox = new CheckBox(printerInfo.Name)
{
TextColor = ActiveTheme.Instance.PrimaryTextColor,
Margin = new BorderDouble(5, 0, 0, 0),
HAnchor = HAnchor.ParentLeft,
};
checkBoxes.Add(importButton);
container.AddChild(importButton);
checkBoxes.Add(checkBox);
container.AddChild(checkBox);
byCheckbox[checkBox] = printerInfo;
}
}
@ -99,18 +95,28 @@ namespace MatterHackers.MatterControl
copyButton.Click += (s, e) =>
{
// do the import
for(int i=0; i< checkBoxes.Count; i++)
foreach(var checkBox in checkBoxes)
{
var checkBox = checkBoxes[i];
if (checkBox.Checked)
{
// import the printer
var printerInfo = byCheckbox[checkBox];
ProfileManager.Instance.Profiles.Add(printerInfo);
guestProfileManager.Profiles.Remove(printerInfo);
}
}
guestProfileManager.Save();
// close the window
UiThread.RunOnIdle(WizardWindow.Close);
UiThread.RunOnIdle(() =>
{
WizardWindow.Close();
// Call back into the original source
afterProfilesImported();
});
};
copyButton.Visible = true;

View file

@ -80,21 +80,16 @@ namespace MatterHackers.MatterControl
public static void Show<PanelType>(string uri, string title) where PanelType : WizardPage, new()
{
WizardWindow existingWindow;
if (allWindows.TryGetValue(uri, out existingWindow))
{
existingWindow.BringToFront();
}
else
{
existingWindow = new WizardWindow();
existingWindow.Closed += (s, e) => allWindows.Remove(uri);
allWindows[uri] = existingWindow;
WizardWindow wizardWindow = GetWindow(uri);
wizardWindow.Title = title;
wizardWindow.ChangeToPage<PanelType>();
}
existingWindow.Title = title;
existingWindow.ChangeToPage<PanelType>();
public static void Show(string uri, string title, WizardPage wizardPage)
{
WizardWindow wizardWindow = GetWindow(uri);
wizardWindow.Title = title;
wizardWindow.ChangeToPage(wizardPage);
}
public static void Show(bool openToHome = false)
@ -110,6 +105,24 @@ namespace MatterHackers.MatterControl
}
}
private static WizardWindow GetWindow(string uri)
{
WizardWindow wizardWindow;
if (allWindows.TryGetValue(uri, out wizardWindow))
{
wizardWindow.BringToFront();
}
else
{
wizardWindow = new WizardWindow();
wizardWindow.Closed += (s, e) => allWindows.Remove(uri);
allWindows[uri] = wizardWindow;
}
return wizardWindow;
}
public override void OnClosed(EventArgs e)
{
base.OnClosed(e);

View file

@ -168,7 +168,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
}
}
private string DocumentPath => Path.Combine(ProfileManager.ProfilesPath, this.ID + ".json");
private string DocumentPath => ProfileManager.Instance.ProfilePath(this.ID);
internal void Save()
{
@ -191,8 +191,11 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
File.WriteAllText(DocumentPath, json);
if (ActiveSliceSettings.Instance.ID == this.ID)
{
ActiveSliceSettings.ActiveProfileModified.CallEvents(null, null);
}
}
/// <summary>
/// User settings overrides

View file

@ -50,16 +50,21 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
public static ProfileManager Instance { get; set; }
private static EventHandler unregisterEvents;
private static readonly string userDataPath = DataStorage.ApplicationDataStorage.ApplicationUserDataPath;
internal static readonly string ProfilesPath = Path.Combine(userDataPath, "Profiles");
public static string ProfileExtension { get; } = ".json";
private static string ProfilesDBPath
private static EventHandler unregisterEvents;
private static readonly string userDataPath = ApplicationDataStorage.ApplicationUserDataPath;
private static readonly string ProfilesPath = Path.Combine(userDataPath, "Profiles");
private const string guestDBFileName = "guest.profiles";
private static string GuestDBPath => Path.Combine(ProfilesPath, guestDBFileName);
internal static string ProfilesDBPath
{
get
{
string username = UserSettings.Instance.get("ActiveUserName");
return Path.Combine(ProfilesPath, string.IsNullOrEmpty(username) ? "profiles.json" : username + ".json");
return string.IsNullOrEmpty(username) ? GuestDBPath : Path.Combine(ProfilesPath, $"{username}.profiles");
}
}
@ -77,8 +82,17 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
{
}
[JsonIgnore]
public bool IsGuestProfile => Path.GetFileName(ProfilesDBPath) == guestDBFileName;
public static void Reload()
{
if (Instance?.Profiles != null)
{
// Release event registration
Instance.Profiles.CollectionChanged -= Profiles_CollectionChanged;
}
// Load the profiles document
if (File.Exists(ProfilesDBPath))
{
@ -88,28 +102,28 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
else
{
Instance = new ProfileManager();
if (Path.GetFileName(ProfilesDBPath) == "profiles.json")
{
// Import classic db based profiles into local json files
DataStorage.ClassicDB.ClassicSqlitePrinterProfiles.ImportPrinters(Instance, ProfilesPath);
}
}
// Load the last profile or an empty profile
// Load the last selected printer profile or an empty profile
ActiveSliceSettings.Instance = Instance.LoadLastProfile() ?? LoadEmptyProfile();
if (Instance != null)
{
// Release event registration
Instance.Profiles.CollectionChanged -= Profiles_CollectionChanged;
}
// In either case, wire up the CollectionChanged event
Instance.Profiles.CollectionChanged += Profiles_CollectionChanged;
}
internal static ProfileManager LoadGuestDB()
{
if (File.Exists(GuestDBPath))
{
string json = File.ReadAllText(GuestDBPath);
return JsonConvert.DeserializeObject<ProfileManager>(json);
}
return null;
}
internal static void SettingsChanged(object sender, EventArgs e)
{
string settingsKey = ((StringEventArgs)e).Data;
@ -161,6 +175,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
return new SettingsProfile(printerSettings);
}
[JsonIgnore]
public string LastProfileID
{
get
@ -172,6 +187,8 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
}
}
public bool PrintersImported { get; set; } = false;
public SettingsProfile LoadLastProfile()
{
return LoadProfile(this.LastProfileID);
@ -185,6 +202,16 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
UserSettings.Instance.set(settingsKey, printerID);
}
public string ProfilePath(PrinterInfo printer)
{
return Path.Combine(ProfileManager.ProfilesPath, printer.ID + ProfileExtension);
}
public string ProfilePath(string printerID)
{
return Path.Combine(ProfileManager.ProfilesPath, printerID + ProfileExtension);
}
/// <summary>
/// Loads the specified SettingsProfile
/// </summary>
@ -193,9 +220,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
/// <returns></returns>
public static SettingsProfile LoadProfile(string profileID, bool useActiveInstance = true)
{
//return LoadProfileFromMCWS(profileID);
// Only load profiles by ID that are defined in the profiles.json document
// Only load profiles by ID that are defined in the profiles document
if (ProfileManager.Instance[profileID] == null)
{
return null;
@ -358,6 +383,17 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
});
}
public void EnsurePrintersImported()
{
if (IsGuestProfile && !PrintersImported)
{
// Import Sqlite printer profiles into local json files
DataStorage.ClassicDB.ClassicSqlitePrinterProfiles.ImportPrinters(Instance, ProfilesPath);
PrintersImported = true;
Save();
}
}
private static void Profiles_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
// Any time the list changes, persist the updates to disk

View file

@ -966,6 +966,6 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
}
[JsonIgnore]
public string ProfilePath => Path.Combine(ProfileManager.ProfilesPath, ID + ".json");
public string ProfilePath => ProfileManager.Instance.ProfilePath(this);
}
}