From f314731449cb128be04903d2b87f0a1c61a996b8 Mon Sep 17 00:00:00 2001 From: John Lewin Date: Sat, 7 May 2016 17:47:42 -0700 Subject: [PATCH] Better integrate new Presets editor - Fixes #718 - Presets editor is hard-coded to show Material presets - Fixes #719 - Name field in presets editor is incorrect - Fixes #721 - Wire up "Add Preset" --- MatterControl.csproj | 1 - .../Settings/SettingsProfile.cs | 17 + SlicerConfiguration/SettingsControlBar.cs | 7 +- .../SettingsControlSelectors.cs | 142 ++------ .../SlicePresetDetailWidget.cs | 117 ++---- .../SlicePresetListWidget.cs | 334 ------------------ .../SlicePresetsWindow/SlicePresetsWindow.cs | 73 +--- 7 files changed, 88 insertions(+), 603 deletions(-) delete mode 100644 SlicerConfiguration/SlicePresetsWindow/SlicePresetListWidget.cs diff --git a/MatterControl.csproj b/MatterControl.csproj index c59097fb4..a81839bfb 100644 --- a/MatterControl.csproj +++ b/MatterControl.csproj @@ -266,7 +266,6 @@ - diff --git a/SlicerConfiguration/Settings/SettingsProfile.cs b/SlicerConfiguration/Settings/SettingsProfile.cs index 3e49eba82..bd9693d80 100644 --- a/SlicerConfiguration/Settings/SettingsProfile.cs +++ b/SlicerConfiguration/Settings/SettingsProfile.cs @@ -171,6 +171,23 @@ 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); diff --git a/SlicerConfiguration/SettingsControlBar.cs b/SlicerConfiguration/SettingsControlBar.cs index 4104d50d1..123b35f96 100644 --- a/SlicerConfiguration/SettingsControlBar.cs +++ b/SlicerConfiguration/SettingsControlBar.cs @@ -43,7 +43,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration int numberOfHeatedExtruders = ActiveSliceSettings.Instance.ExtruderCount(); - this.AddChild(new PresetSelectorWidget("Quality".Localize(), RGBA_Bytes.Yellow, "quality", 0)); + this.AddChild(new PresetSelectorWidget("Quality".Localize(), RGBA_Bytes.Yellow, NamedSettingsLayers.Quality, 0)); this.AddChild(new GuiWidget(8, 0)); if (numberOfHeatedExtruders > 1) @@ -58,16 +58,15 @@ namespace MatterHackers.MatterControl.SlicerConfiguration } int colorIndex = i % colorList.Count; RGBA_Bytes color = colorList[colorIndex]; - this.AddChild(new PresetSelectorWidget(string.Format("{0} {1}", "Material".Localize(), i + 1), color, "material", i)); + this.AddChild(new PresetSelectorWidget(string.Format("{0} {1}", "Material".Localize(), i + 1), color, NamedSettingsLayers.Material, i)); } } else { - this.AddChild(new PresetSelectorWidget("Material".Localize(), RGBA_Bytes.Orange, "material", 0)); + this.AddChild(new PresetSelectorWidget("Material".Localize(), RGBA_Bytes.Orange, NamedSettingsLayers.Material, 0)); } this.Height = 60 * GuiWidget.DeviceScale; - } } } diff --git a/SlicerConfiguration/SettingsControlSelectors.cs b/SlicerConfiguration/SettingsControlSelectors.cs index eb55e2ba6..8f106390c 100644 --- a/SlicerConfiguration/SettingsControlSelectors.cs +++ b/SlicerConfiguration/SettingsControlSelectors.cs @@ -48,21 +48,16 @@ namespace MatterHackers.MatterControl.SlicerConfiguration public class PresetSelectorWidget : FlowLayoutWidget { private Button editButton; - private ImageButtonFactory imageButtonFactory = new ImageButtonFactory(); - - private string filterTag; - private string filterLabel; - public StyledDropDownList DropDownList; - private TupleList> DropDownMenuItems = new TupleList>(); + private NamedSettingsLayers layerType; + private StyledDropDownList dropDownList; private int extruderIndex; //For multiple materials - public PresetSelectorWidget(string label, RGBA_Bytes accentColor, string tag, int extruderIndex) + public PresetSelectorWidget(string label, RGBA_Bytes accentColor, NamedSettingsLayers layerType, int extruderIndex) : base(FlowDirection.TopToBottom) { this.extruderIndex = extruderIndex; - this.filterLabel = label; - this.filterTag = (tag == null) ? label.ToLower() : tag; + this.layerType = layerType; this.HAnchor = HAnchor.ParentLeftRight; this.VAnchor = Agg.UI.VAnchor.Max_FitToChildren_ParentHeight; @@ -89,7 +84,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration public virtual FlowLayoutWidget GetPulldownContainer() { - DropDownList = CreateDropdown(); + dropDownList = CreateDropdown(); FlowLayoutWidget container = new FlowLayoutWidget(); container.HAnchor = HAnchor.ParentLeftRight; @@ -101,24 +96,12 @@ namespace MatterHackers.MatterControl.SlicerConfiguration editButton.Margin = new BorderDouble(right: 6); editButton.Click += (sender, e) => { -#if DO_IN_PLACE_EDIT - if (filterTag == "quality") - { - SliceSettingsWidget.SettingsIndexBeingEdited = 2; - } - else - { - SliceSettingsWidget.SettingsIndexBeingEdited = 3; - } - // If there is a setting selected then reload the slice setting widget with the presetIndex to edit. - ApplicationController.Instance.ReloadAdvancedControlsPanel(); - // If no setting selected then call onNewItemSelect(object sender, EventArgs e) -#else - if (filterTag == "material") + if (layerType == NamedSettingsLayers.Material) { if (ApplicationController.Instance.EditMaterialPresetsWindow == null) { - ApplicationController.Instance.EditMaterialPresetsWindow = new SlicePresetsWindow(ReloadOptions, filterLabel, filterTag); + string presetsKey = ActiveSliceSettings.Instance.MaterialPresetKey(extruderIndex); + ApplicationController.Instance.EditMaterialPresetsWindow = new SlicePresetsWindow(ActiveSliceSettings.Instance.MaterialLayer(presetsKey), NamedSettingsLayers.Material, presetsKey); ApplicationController.Instance.EditMaterialPresetsWindow.Closed += (popupWindowSender, popupWindowSenderE) => { ApplicationController.Instance.EditMaterialPresetsWindow = null; }; } else @@ -127,11 +110,12 @@ namespace MatterHackers.MatterControl.SlicerConfiguration } } - if (filterTag == "quality") + if (layerType == NamedSettingsLayers.Quality) { if (ApplicationController.Instance.EditQualityPresetsWindow == null) { - ApplicationController.Instance.EditQualityPresetsWindow = new SlicePresetsWindow(ReloadOptions, filterLabel, filterTag); + string presetsKey = ActiveSliceSettings.Instance.ActiveQualityKey; + ApplicationController.Instance.EditQualityPresetsWindow = new SlicePresetsWindow(ActiveSliceSettings.Instance.QualityLayer(presetsKey), NamedSettingsLayers.Quality, presetsKey); ApplicationController.Instance.EditQualityPresetsWindow.Closed += (popupWindowSender, popupWindowSenderE) => { ApplicationController.Instance.EditQualityPresetsWindow = null; }; } else @@ -139,32 +123,26 @@ namespace MatterHackers.MatterControl.SlicerConfiguration ApplicationController.Instance.EditQualityPresetsWindow.BringToFront(); } } -#endif }; container.AddChild(editButton); - container.AddChild(DropDownList); + container.AddChild(dropDownList); return container; } - protected void ReloadOptions(object sender, EventArgs e) - { - ApplicationController.Instance.ReloadAdvancedControlsPanel(); - } - private void MenuItem_Selected(object sender, EventArgs e) { var activeSettings = ActiveSliceSettings.Instance; MenuItem item = (MenuItem)sender; - if (filterTag == "material") + if (layerType == NamedSettingsLayers.Material) { if (activeSettings.MaterialPresetKey(extruderIndex) != item.Value) { activeSettings.SetMaterialPreset(extruderIndex, item.Value); } } - else if (filterTag == "quality") + else if (layerType == NamedSettingsLayers.Quality) { if (activeSettings.ActiveQualityKey != item.Value) { @@ -178,46 +156,6 @@ namespace MatterHackers.MatterControl.SlicerConfiguration }); } - private void onNewItemSelect(object sender, EventArgs e) - { -#if DO_IN_PLACE_EDIT - // pop up a dialog to request a new setting name - // after getting the new name select it and reload the slice setting widget editing the new setting - throw new NotImplementedException(); -#else - UiThread.RunOnIdle(() => - { - ApplicationController.Instance.ReloadAdvancedControlsPanel(); - if (filterTag == "material") - { - if (ApplicationController.Instance.EditMaterialPresetsWindow == null) - { - ApplicationController.Instance.EditMaterialPresetsWindow = new SlicePresetsWindow(ReloadOptions, filterLabel, filterTag, false); - ApplicationController.Instance.EditMaterialPresetsWindow.Closed += (popupWindowSender, popupWindowSenderE) => { ApplicationController.Instance.EditMaterialPresetsWindow = null; }; - } - else - { - ApplicationController.Instance.EditMaterialPresetsWindow.ChangeToSlicePresetFromID(""); - ApplicationController.Instance.EditMaterialPresetsWindow.BringToFront(); - } - } - if (filterTag == "quality") - { - if (ApplicationController.Instance.EditQualityPresetsWindow == null) - { - ApplicationController.Instance.EditQualityPresetsWindow = new SlicePresetsWindow(ReloadOptions, filterLabel, filterTag, false); - ApplicationController.Instance.EditQualityPresetsWindow.Closed += (popupWindowSender, popupWindowSenderE) => { ApplicationController.Instance.EditQualityPresetsWindow = null; }; - } - else - { - ApplicationController.Instance.EditQualityPresetsWindow.ChangeToSlicePresetFromID(""); - ApplicationController.Instance.EditQualityPresetsWindow.BringToFront(); - } - } - }); -#endif - } - private StyledDropDownList CreateDropdown() { var dropDownList = new StyledDropDownList("- default -", maxHeight: 300, useLeftIcons: true) @@ -232,7 +170,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration MenuItem defaultMenuItem = dropDownList.AddItem("- default -", ""); defaultMenuItem.Selected += MenuItem_Selected; - var listSource = (filterTag == "material") ? ActiveSliceSettings.Instance.AllMaterialKeys() : ActiveSliceSettings.Instance.AllQualityKeys(); + var listSource = (layerType == NamedSettingsLayers.Material) ? ActiveSliceSettings.Instance.AllMaterialKeys() : ActiveSliceSettings.Instance.AllQualityKeys(); foreach (var presetName in listSource) { MenuItem menuItem = dropDownList.AddItem(presetName, presetName); @@ -240,43 +178,35 @@ namespace MatterHackers.MatterControl.SlicerConfiguration } MenuItem addNewPreset = dropDownList.AddItem(InvertLightness.DoInvertLightness(StaticData.Instance.LoadIcon("icon_circle_plus.png")), "Add New Setting...", "new"); - addNewPreset.Selected += onNewItemSelect; - - if (false) + addNewPreset.Selected += (s, e) => { - FlowLayoutWidget container = new FlowLayoutWidget(); - container.HAnchor = HAnchor.ParentLeftRight; - - TextImageButtonFactory buttonFactory = new TextImageButtonFactory(); - buttonFactory.normalTextColor = ActiveTheme.Instance.PrimaryTextColor; - buttonFactory.hoverTextColor = ActiveTheme.Instance.PrimaryTextColor; - buttonFactory.disabledTextColor = ActiveTheme.Instance.PrimaryTextColor; - buttonFactory.pressedTextColor = ActiveTheme.Instance.PrimaryTextColor; - buttonFactory.borderWidth = 0; - - Button addPresetButton = buttonFactory.Generate(LocalizedString.Get("Add"), "icon_circle_plus.png"); - addPresetButton.ToolTipText = "Add a new Settings Preset".Localize(); - addPresetButton.Click += (sender, e) => + var newLayer = ActiveSliceSettings.Instance.CreatePresetsLayer(layerType); + if (layerType == NamedSettingsLayers.Quality) { - onNewItemSelect(sender, e); - }; - container.AddChild(addPresetButton); - - Button importPresetButton = buttonFactory.Generate(LocalizedString.Get("Import")); - importPresetButton.ToolTipText = "Import an existing Settings Preset".Localize(); - importPresetButton.Click += (sender, e) => + ActiveSliceSettings.Instance.ActiveQualityKey = newLayer.Name; + } + else if (layerType == NamedSettingsLayers.Material) { - }; - container.AddChild(importPresetButton); + ActiveSliceSettings.Instance.SetMaterialPreset(this.extruderIndex, newLayer.Name); + } - dropDownList.MenuItems.Add(new MenuItem(container)); - } + // TODO: Consider adding a .Replace(existingWidget, newWidget) to GuiWidget + // Replace existing list with updated list + var parent = this.dropDownList.Parent; + parent.RemoveChild(this.dropDownList); + this.dropDownList.Close(); + + this.dropDownList = CreateDropdown(); + parent.AddChild(this.dropDownList); + + editButton.ClickButton(new MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0)); + }; try { string settingsKey; - if (filterTag == "material") + if (layerType == NamedSettingsLayers.Material) { settingsKey = ActiveSliceSettings.Instance.MaterialPresetKey(extruderIndex); } @@ -301,8 +231,6 @@ namespace MatterHackers.MatterControl.SlicerConfiguration public class SliceEngineSelector : StyledDropDownList { - private TupleList> engineOptionsMenuItems = new TupleList>(); - public SliceEngineSelector(string label) : base(label) { diff --git a/SlicerConfiguration/SlicePresetsWindow/SlicePresetDetailWidget.cs b/SlicerConfiguration/SlicePresetsWindow/SlicePresetDetailWidget.cs index 703b231f4..ce4488839 100644 --- a/SlicerConfiguration/SlicePresetsWindow/SlicePresetDetailWidget.cs +++ b/SlicerConfiguration/SlicePresetsWindow/SlicePresetDetailWidget.cs @@ -54,16 +54,17 @@ namespace MatterHackers.MatterControl.SlicerConfiguration private Button importPresetButton; private Button exportPresetButton; - private int tabIndexForItem = 0; + private NamedSettingsLayers layerType; + private SettingsLayer persistenceLayer; + private string presetsKey; - public SlicePresetDetailWidget(SlicePresetsWindow windowController) + public SlicePresetDetailWidget(SlicePresetsWindow windowController, SettingsLayer persistenceLayer, NamedSettingsLayers layerType, string presetsKey) { + this.persistenceLayer = persistenceLayer; + this.layerType = layerType; this.windowController = windowController; + this.presetsKey = presetsKey; this.AnchorAll(); - if (this.windowController.ActivePresetLayer == null) - { - initSlicePreset(); - } linkButtonFactory.fontSize = 8; linkButtonFactory.textColor = ActiveTheme.Instance.SecondaryAccentColor; @@ -108,12 +109,12 @@ namespace MatterHackers.MatterControl.SlicerConfiguration FlowLayoutWidget firstRow = new FlowLayoutWidget(); firstRow.HAnchor = HAnchor.ParentLeftRight; - TextWidget labelText = new TextWidget("Edit Preset:".FormatWith(windowController.filterLabel.Localize()), pointSize: 14); + TextWidget labelText = new TextWidget("Edit Preset:".Localize(), pointSize: 14); labelText.TextColor = ActiveTheme.Instance.PrimaryTextColor; labelText.VAnchor = VAnchor.ParentCenter; labelText.Margin = new BorderDouble(right: 4); - presetNameInput = new MHTextEditWidget(windowController.ActivePresetLayer.settingsCollectionData.Name); + presetNameInput = new MHTextEditWidget(this.presetsKey); presetNameInput.HAnchor = HAnchor.ParentLeftRight; firstRow.AddChild(labelText); @@ -137,38 +138,14 @@ namespace MatterHackers.MatterControl.SlicerConfiguration return metaContainer; } - private SettingsDropDownList categoryDropDownList; - private SettingsDropDownList groupDropDownList; - private SettingsDropDownList settingDropDownList; - private GuiWidget GetMiddleRow() { - NamedSettingsLayers layerFilter = NamedSettingsLayers.Material; - List layerFilters = null; + List layerCascade = null; - if (layerFilter != NamedSettingsLayers.All) - { - var settings = ActiveSliceSettings.Instance; + var settings = ActiveSliceSettings.Instance; + layerCascade = new List { settings.BaseLayer, settings.OemLayer, persistenceLayer }; - // TODO: The editing context needs to provide the key - System.Diagnostics.Debugger.Break(); - string layerKey = settings.ActiveMaterialKey; - - layerFilters = new List { settings.BaseLayer, settings.OemLayer }; - - switch (layerFilter) - { - case NamedSettingsLayers.Material: - layerFilters.Add(settings.MaterialLayer(layerKey)); - break; - - case NamedSettingsLayers.Quality: - layerFilters.Add(settings.QualityLayer(layerKey)); - break; - } - } - - var settingsWidget = new SliceSettingsWidget(layerFilters, NamedSettingsLayers.Material); + var settingsWidget = new SliceSettingsWidget(layerCascade, layerType); settingsWidget.settingsControlBar.Visible = false; return settingsWidget; @@ -180,27 +157,27 @@ namespace MatterHackers.MatterControl.SlicerConfiguration container.HAnchor = HAnchor.ParentLeftRight; container.Margin = new BorderDouble(top: 3); - savePresetButton = buttonFactory.Generate(LocalizedString.Get("Save")); - duplicatePresetButton = buttonFactory.Generate(LocalizedString.Get("Duplicate")); - importPresetButton = buttonFactory.Generate(LocalizedString.Get("Import")); - exportPresetButton = buttonFactory.Generate(LocalizedString.Get("Export")); + savePresetButton = buttonFactory.Generate("Save".Localize()); + duplicatePresetButton = buttonFactory.Generate("Duplicate".Localize()); + importPresetButton = buttonFactory.Generate("Import".Localize()); + exportPresetButton = buttonFactory.Generate("Export".Localize()); - Button cancelButton = buttonFactory.Generate(LocalizedString.Get("Cancel")); - cancelButton.Click += (sender, e) => + Button close = buttonFactory.Generate("Close".Localize()); + close.Click += (sender, e) => { UiThread.RunOnIdle(windowController.Close); }; container.AddChild(savePresetButton); //Only show duplicate/import/export buttons if setting has been saved. - if (windowController.ActivePresetLayer.settingsCollectionData.Id != 0) + if (windowController.ActivePresetLayer != null) { container.AddChild(duplicatePresetButton); container.AddChild(importPresetButton); container.AddChild(exportPresetButton); } container.AddChild(new HorizontalSpacer()); - container.AddChild(cancelButton); + container.AddChild(close); return container; } @@ -328,43 +305,14 @@ namespace MatterHackers.MatterControl.SlicerConfiguration return formIsValid; } - private void initSlicePreset() - { - int noExistingPresets = ExistingPresetsCount() + 1; - - Dictionary settingsDictionary = new Dictionary(); - SliceSettingsCollection collection = new SliceSettingsCollection(); - - if (ActiveSliceSettings.Instance != null) - { - // TODO: Review bindings to int printerID - int printerID; - int.TryParse(ActiveSliceSettings.Instance.Id(), out printerID); - - collection.Name = string.Format("{0} ({1})", windowController.filterLabel, noExistingPresets.ToString()); - collection.Tag = windowController.filterTag; - collection.PrinterId = printerID; - } - - windowController.ActivePresetLayer = new ClassicSqlitePrinterProfiles.ClassicSettingsLayer(collection, settingsDictionary); - } - - public int ExistingPresetsCount() - { - string query = string.Format("SELECT COUNT(*) FROM SliceSettingsCollection WHERE Tag = '{0}';", windowController.filterTag); - string result = Datastore.Instance.dbSQLite.ExecuteScalar(query); - return Convert.ToInt32(result); - } - private void savePresets_Click(object sender, EventArgs mouseEvent) { + throw new NotImplementedException(); UiThread.RunOnIdle(() => { if (ValidatePresetsForm()) { saveActivePresets(); - windowController.functionToCallOnSave(this, null); - windowController.ChangeToSlicePresetList(); } }); } @@ -373,25 +321,8 @@ namespace MatterHackers.MatterControl.SlicerConfiguration { UiThread.RunOnIdle(() => { - SliceSettingsCollection duplicateCollection = new SliceSettingsCollection(); - duplicateCollection.Name = string.Format("{0} (copy)".FormatWith(windowController.ActivePresetLayer.settingsCollectionData.Name)); - duplicateCollection.Tag = windowController.ActivePresetLayer.settingsCollectionData.Tag; - duplicateCollection.PrinterId = windowController.ActivePresetLayer.settingsCollectionData.PrinterId; - - Dictionary settingsDictionary = new Dictionary(); - IEnumerable settingsList = this.windowController.GetCollectionSettings(windowController.ActivePresetLayer.settingsCollectionData.Id); - foreach (SliceSetting s in settingsList) - { - SliceSetting newSetting = new SliceSetting(); - newSetting.Name = s.Name; - newSetting.Value = s.Value; - - settingsDictionary.Add(s.Name, newSetting); - } - - var duplicateLayer = new ClassicSqlitePrinterProfiles.ClassicSettingsLayer(duplicateCollection, settingsDictionary); - windowController.ActivePresetLayer = duplicateLayer; - windowController.ChangeToSlicePresetDetail(); + // TODO: copy existing directionary to new named instance + throw new NotImplementedException(); }); } diff --git a/SlicerConfiguration/SlicePresetsWindow/SlicePresetListWidget.cs b/SlicerConfiguration/SlicePresetsWindow/SlicePresetListWidget.cs deleted file mode 100644 index 1ccd4ab61..000000000 --- a/SlicerConfiguration/SlicePresetsWindow/SlicePresetListWidget.cs +++ /dev/null @@ -1,334 +0,0 @@ -/* -Copyright (c) 2014, Lars Brubaker -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 MatterHackers.Agg; -using MatterHackers.Agg.UI; -using MatterHackers.Localizations; -using MatterHackers.MatterControl.CustomWidgets; -using MatterHackers.MatterControl.DataStorage; -using MatterHackers.VectorMath; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; - -namespace MatterHackers.MatterControl.SlicerConfiguration -{ - public class SlicePresetListWidget : GuiWidget - { - private SlicePresetsWindow windowController; - private TextImageButtonFactory buttonFactory; - private LinkButtonFactory linkButtonFactory; - private PresetListControl presetListControl; - private Button importPresetButton; - - public SlicePresetListWidget(SlicePresetsWindow windowController) - { - this.windowController = windowController; - this.AnchorAll(); - - linkButtonFactory = new LinkButtonFactory(); - - buttonFactory = new TextImageButtonFactory(); - buttonFactory.normalTextColor = ActiveTheme.Instance.PrimaryTextColor; - buttonFactory.hoverTextColor = ActiveTheme.Instance.PrimaryTextColor; - buttonFactory.disabledTextColor = ActiveTheme.Instance.PrimaryTextColor; - buttonFactory.pressedTextColor = ActiveTheme.Instance.PrimaryTextColor; - buttonFactory.borderWidth = 0; - - AddElements(); - AddHandlers(); - } - - private void AddElements() - { - FlowLayoutWidget mainContainer = new FlowLayoutWidget(FlowDirection.TopToBottom); - mainContainer.Padding = new BorderDouble(3); - mainContainer.AnchorAll(); - - mainContainer.AddChild(GetTopRow()); - mainContainer.AddChild(GetMiddleRow()); - mainContainer.AddChild(GetBottomRow()); - - this.AddChild(mainContainer); - } - - private void AddHandlers() - { - importPresetButton.Click += new EventHandler(importPreset_Click); - } - - private FlowLayoutWidget GetTopRow() - { - FlowLayoutWidget container = new FlowLayoutWidget(); - container.HAnchor = HAnchor.ParentLeftRight; - container.Padding = new BorderDouble(0, 6); - TextWidget labelText = new TextWidget("{0} Presets:".FormatWith(windowController.filterLabel.Localize()), pointSize: 14); - labelText.TextColor = ActiveTheme.Instance.PrimaryTextColor; - - container.AddChild(labelText); - container.AddChild(new HorizontalSpacer()); - return container; - } - - private FlowLayoutWidget GetMiddleRow() - { - FlowLayoutWidget container = new FlowLayoutWidget(); - container.HAnchor = HAnchor.ParentLeftRight; - container.VAnchor = Agg.UI.VAnchor.ParentBottomTop; - container.BackgroundColor = ActiveTheme.Instance.SecondaryBackgroundColor; - container.Margin = new BorderDouble(0, 3, 0, 0); - - presetListControl = new PresetListControl(); - - foreach (SliceSettingsCollection collection in GetCollections()) - { - presetListControl.AddChild(new PresetListItem(this.windowController, collection)); - } - container.AddChild(presetListControl); - - return container; - } - - private FlowLayoutWidget GetBottomRow() - { - FlowLayoutWidget container = new FlowLayoutWidget(); - container.HAnchor = HAnchor.ParentLeftRight; - - Button addPresetButton = buttonFactory.Generate(LocalizedString.Get("Add"), "icon_circle_plus.png"); - addPresetButton.ToolTipText = "Add a new Material Preset".Localize(); - addPresetButton.Click += (sender, e) => - { - UiThread.RunOnIdle(() => - { - windowController.ChangeToSlicePresetDetail(); - }); - }; - - importPresetButton = buttonFactory.Generate(LocalizedString.Get("Import")); - importPresetButton.ToolTipText = "Import an existing Material Preset".Localize(); - - Button closeButton = buttonFactory.Generate(LocalizedString.Get("Close")); - closeButton.Click += (sender, e) => - { - UiThread.RunOnIdle(() => - { - windowController.Close(); - }); - }; - - container.AddChild(addPresetButton); - container.AddChild(importPresetButton); - container.AddChild(new HorizontalSpacer()); - container.AddChild(closeButton); - - return container; - } - - private void importPreset_Click(object sender, EventArgs mouseEvent) - { - UiThread.RunOnIdle(importPresetDo); - } - - private void importPresetDo() - { - OpenFileDialogParams openParams = new OpenFileDialogParams("Load Slice Preset|*.slice;*.ini"); - openParams.ActionButtonLabel = "Load Slice Preset"; - openParams.Title = "MatterControl: Select A File"; - - FileDialog.OpenFileDialog(openParams, onPresetLoad); - } - - private void onPresetLoad(OpenFileDialogParams openParams) - { - if (openParams.FileNames != null) - { - SliceSettingsCollection settingsCollection; - try - { - if (File.Exists(openParams.FileName)) - { - // TODO: Review bindings to int printerID - int printerID; - int.TryParse(ActiveSliceSettings.Instance.Id(), out printerID); - - //Create collection to hold preset settings - settingsCollection = new SliceSettingsCollection(); - settingsCollection.Tag = windowController.filterTag; - settingsCollection.PrinterId = printerID; - settingsCollection.Name = System.IO.Path.GetFileNameWithoutExtension(openParams.FileName); - settingsCollection.Commit(); - - string[] lines = System.IO.File.ReadAllLines(openParams.FileName); - foreach (string line in lines) - { - //Ignore commented lines - if (!line.StartsWith("#")) - { - string[] settingLine = line.Split('='); - string keyName = settingLine[0].Trim(); - string settingDefaultValue = settingLine[1].Trim(); - - //To do - validate imported settings as valid (KP) - SliceSetting sliceSetting = new SliceSetting(); - sliceSetting.Name = keyName; - sliceSetting.Value = settingDefaultValue; - sliceSetting.SettingsCollectionId = settingsCollection.Id; - sliceSetting.Commit(); - } - } - windowController.ChangeToSlicePresetList(); - } - } - catch (Exception) - { - // Error loading configuration - } - } - } - - private IEnumerable GetCollections() - { - if (ActiveSliceSettings.Instance != null) - { - //Retrieve a list of collections matching from the Datastore - string query = string.Format("SELECT * FROM SliceSettingsCollection WHERE Tag = '{0}' AND PrinterId = {1} ORDER BY Name;", windowController.filterTag, ActiveSliceSettings.Instance.Id()); - return Datastore.Instance.dbSQLite.Query(query); - } - - return Enumerable.Empty(); - } - - private class PresetListItem : FlowLayoutWidget - { - private SliceSettingsCollection preset; - - private SliceSettingsCollection Preset { get { return preset; } } - - public PresetListItem(SlicePresetsWindow windowController, SliceSettingsCollection preset) - { - this.preset = preset; - this.BackgroundColor = RGBA_Bytes.White; - this.HAnchor = HAnchor.ParentLeftRight; - this.Margin = new BorderDouble(6, 0, 6, 3); - this.Padding = new BorderDouble(3); - - LinkButtonFactory linkButtonFactory = new LinkButtonFactory(); - linkButtonFactory.fontSize = 10; - - int maxLabelWidth = 300; - TextWidget materialLabel = new TextWidget(preset.Name, pointSize: 14); - materialLabel.EllipsisIfClipped = true; - materialLabel.VAnchor = Agg.UI.VAnchor.ParentCenter; - materialLabel.MinimumSize = new Vector2(maxLabelWidth, materialLabel.Height); - materialLabel.Width = maxLabelWidth; - - Button materialEditLink = linkButtonFactory.Generate("edit"); - materialEditLink.VAnchor = Agg.UI.VAnchor.ParentCenter; - materialEditLink.Click += (sender, e) => - { - UiThread.RunOnIdle(() => - { - windowController.ChangeToSlicePresetDetail(preset); - }); - }; - - Button materialRemoveLink = linkButtonFactory.Generate("remove"); - materialRemoveLink.Margin = new BorderDouble(left: 4); - this.DebugShowBounds = true; - materialRemoveLink.VAnchor = Agg.UI.VAnchor.ParentCenter; - materialRemoveLink.Click += (sender, e) => - { - UiThread.RunOnIdle(() => - { - //Unwind this setting if it is currently active - if (ActiveSliceSettings.Instance != null) - { - /* - if (preset.Id == ActivePrinterProfile.Instance.ActiveQualitySettingsID) - { - ActivePrinterProfile.Instance.ActiveQualitySettingsID = 0; - } - - string[] activeMaterialPresets = ActiveSliceSettings.Instance.MaterialCollectionIds.Split(','); - for (int i = 0; i < activeMaterialPresets.Count(); i++) - { - int index = 0; - Int32.TryParse(activeMaterialPresets[i], out index); - if (preset.Id == index) - { - ActiveSliceSettings.Instance.SetMaterialPreset(i, ""); - } - } */ - } - preset.Delete(); - windowController.ChangeToSlicePresetList(); - ApplicationController.Instance.ReloadAdvancedControlsPanel(); - }); - }; - - this.AddChild(materialLabel); - this.AddChild(new HorizontalSpacer()); - this.AddChild(materialEditLink); - this.AddChild(materialRemoveLink); - - this.Height = 35; - } - } - - private class PresetListControl : ScrollableWidget - { - private FlowLayoutWidget topToBottomItemList; - - public PresetListControl() - { - this.AnchorAll(); - this.AutoScroll = true; - this.ScrollArea.HAnchor |= Agg.UI.HAnchor.ParentLeftRight; - - topToBottomItemList = new FlowLayoutWidget(FlowDirection.TopToBottom); - topToBottomItemList.HAnchor = Agg.UI.HAnchor.Max_FitToChildren_ParentWidth; - topToBottomItemList.Margin = new BorderDouble(top: 3); - - base.AddChild(topToBottomItemList); - } - - public override void AddChild(GuiWidget child, int indexInChildrenList = -1) - { - FlowLayoutWidget itemHolder = new FlowLayoutWidget(); - itemHolder.Margin = new BorderDouble(0, 0, 0, 0); - itemHolder.HAnchor = Agg.UI.HAnchor.Max_FitToChildren_ParentWidth; - itemHolder.AddChild(child); - itemHolder.VAnchor = VAnchor.FitToChildren; - - topToBottomItemList.AddChild(itemHolder, indexInChildrenList); - } - } - } -} \ No newline at end of file diff --git a/SlicerConfiguration/SlicePresetsWindow/SlicePresetsWindow.cs b/SlicerConfiguration/SlicePresetsWindow/SlicePresetsWindow.cs index b94020b1b..8c2eaed3e 100644 --- a/SlicerConfiguration/SlicePresetsWindow/SlicePresetsWindow.cs +++ b/SlicerConfiguration/SlicePresetsWindow/SlicePresetsWindow.cs @@ -39,9 +39,9 @@ namespace MatterHackers.MatterControl.SlicerConfiguration { public class SlicePresetsWindow : SystemWindow { - public EventHandler functionToCallOnSave; - public string filterTag; - public string filterLabel; + private string presetsKey; + private SettingsLayer persistenceLayer; + private NamedSettingsLayers layerType; // TODO: Short term compile hack public ClassicSqlitePrinterProfiles.ClassicSettingsLayer ActivePresetLayer @@ -50,81 +50,26 @@ namespace MatterHackers.MatterControl.SlicerConfiguration set; } - public SlicePresetsWindow(EventHandler functionToCallOnSave, string filterLabel, string filterTag, bool showList = true, string presetKey = null) + public SlicePresetsWindow(SettingsLayer persistenceLayer, NamedSettingsLayers layerType, string presetsKey) : base(640, 480) { AlwaysOnTopOfMain = true; Title = LocalizedString.Get("Slice Presets Editor"); - this.filterTag = filterTag; - this.filterLabel = filterLabel; - - this.functionToCallOnSave = functionToCallOnSave; + this.presetsKey = presetsKey; + this.persistenceLayer = persistenceLayer; + this.layerType = layerType; BackgroundColor = ActiveTheme.Instance.PrimaryBackgroundColor; - ChangeToSlicePresetDetail(); + DoChangeToSlicePresetDetail(); ShowAsSystemWindow(); this.MinimumSize = new Vector2(640, 480); } - public void ChangeToSlicePresetList() - { - this.ActivePresetLayer = null; - UiThread.RunOnIdle(DoChangeToSlicePresetList); - } - - private void DoChangeToSlicePresetList() - { - GuiWidget slicePresetWidget = new SlicePresetListWidget(this); - this.RemoveAllChildren(); - this.AddChild(slicePresetWidget); - this.Invalidate(); - } - - public void ChangeToSlicePresetFromID(string collectionId) - { - throw new NotImplementedException(); - //ChangeToSlicePresetDetail(GetCollection(collectionId)); - } - - public void ChangeToSlicePresetDetail(SliceSettingsCollection collection = null) - { - if (collection != null) - { - Dictionary settingsDictionary = new Dictionary(); - foreach (SliceSetting s in GetCollectionSettings(collection.Id)) - { - settingsDictionary[s.Name] = s; - } - this.ActivePresetLayer = new ClassicSqlitePrinterProfiles.ClassicSettingsLayer(collection, settingsDictionary); - } - UiThread.RunOnIdle(DoChangeToSlicePresetDetail); - } - - private SliceSettingsCollection GetCollection(int collectionId) - { - return Datastore.Instance.dbSQLite.Table().Where(v => v.Id == collectionId).Take(1).FirstOrDefault(); - } - - private IEnumerable GetPresets(string filterTag) - { - //Retrieve a list of presets from the Datastore - string query = string.Format("SELECT * FROM SliceSettingsCollection WHERE Tag = {0};", filterTag); - return Datastore.Instance.dbSQLite.Query(query); - } - - public IEnumerable GetCollectionSettings(int collectionId) - { - //Retrieve a list of slice settings from the Datastore - string query = string.Format("SELECT * FROM SliceSetting WHERE SettingsCollectionID = {0};", collectionId); - return Datastore.Instance.dbSQLite.Query(query); - } - private void DoChangeToSlicePresetDetail() { - GuiWidget macroDetailWidget = new SlicePresetDetailWidget(this); this.RemoveAllChildren(); - this.AddChild(macroDetailWidget); + this.AddChild(new SlicePresetDetailWidget(this, persistenceLayer, layerType, presetsKey)); this.Invalidate(); } }