mattercontrol/SlicerConfiguration/SettingsControlSelectors.cs

328 lines
13 KiB
C#

/*
Copyright (c) 2014, Kevin Pope
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.Linq;
using System.Text;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Utilities;
using MatterHackers.Agg;
using MatterHackers.Agg.UI;
using MatterHackers.VectorMath;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.CustomWidgets;
using MatterHackers.MatterControl.DataStorage;
namespace MatterHackers.MatterControl.SlicerConfiguration
{
public class SliceSelectorWidget : FlowLayoutWidget
{
Button editButton;
ImageButtonFactory imageButtonFactory = new ImageButtonFactory();
string filterTag;
string filterLabel;
public AnchoredDropDownList DropDownList;
private TupleList<string, Func<bool>> DropDownMenuItems = new TupleList<string, Func<bool>>();
public SliceSelectorWidget(string label, RGBA_Bytes accentColor, string tag=null)
: base(FlowDirection.TopToBottom)
{
this.filterLabel = label;
if (tag == null)
{
this.filterTag = label.ToLower();
}
else
{
this.filterTag = tag;
}
this.HAnchor = HAnchor.ParentLeftRight;
this.VAnchor = Agg.UI.VAnchor.ParentBottomTop;
this.BackgroundColor = ActiveTheme.Instance.PrimaryBackgroundColor;
GuiWidget accentBar = new GuiWidget(1, 5);
accentBar.BackgroundColor = accentColor;
accentBar.HAnchor = HAnchor.ParentLeftRight;
TextWidget labelText = new TextWidget(LocalizedString.Get(label).ToUpper());
labelText.TextColor = ActiveTheme.Instance.PrimaryTextColor;
labelText.HAnchor = Agg.UI.HAnchor.ParentCenter;
labelText.Margin = new BorderDouble(0, 3, 0, 6);
this.AddChild(labelText);
this.AddChild(GetPulldownContainer());
this.AddChild(new VerticalSpacer());
this.AddChild(accentBar);
}
public virtual FlowLayoutWidget GetPulldownContainer()
{
DropDownList = CreateDropdown();
FlowLayoutWidget container = new FlowLayoutWidget();
container.HAnchor = HAnchor.ParentLeftRight;
container.Padding = new BorderDouble(6, 0);
editButton = imageButtonFactory.Generate("icon_edit_white.png", "icon_edit_gray.png");
editButton.VAnchor = VAnchor.ParentCenter;
editButton.Margin = new BorderDouble(right:6);
editButton.Click += (sender, e) =>
{
if (filterTag == "material")
{
if (ApplicationController.Instance.EditMaterialPresetsWindow == null)
{
ApplicationController.Instance.EditMaterialPresetsWindow = new SlicePresetsWindow(ReloadOptions, filterLabel, filterTag);
ApplicationController.Instance.EditMaterialPresetsWindow.Closed += (popupWindowSender, popupWindowSenderE) => { ApplicationController.Instance.EditMaterialPresetsWindow = null; };
}
else
{
ApplicationController.Instance.EditMaterialPresetsWindow.BringToFront();
}
}
if (filterTag == "quality")
{
if(ApplicationController.Instance.EditQualityPresetsWindow == null)
{
ApplicationController.Instance.EditQualityPresetsWindow = new SlicePresetsWindow(ReloadOptions, filterLabel, filterTag);
ApplicationController.Instance.EditQualityPresetsWindow.Closed += (popupWindowSender, popupWindowSenderE) => {ApplicationController.Instance.EditQualityPresetsWindow = null; };
}
else
{
ApplicationController.Instance.EditQualityPresetsWindow.BringToFront();
}
}
};
container.AddChild(editButton);
container.AddChild(DropDownList);
return container;
}
protected void ReloadOptions(object sender, EventArgs e)
{
ApplicationController.Instance.ReloadAdvancedControlsPanel();
}
IEnumerable<DataStorage.SliceSettingsCollection> GetCollections()
{
IEnumerable<DataStorage.SliceSettingsCollection> results = Enumerable.Empty<DataStorage.SliceSettingsCollection>();
//Retrieve a list of collections matching from the Datastore
if (ActivePrinterProfile.Instance.ActivePrinter != null)
{
string query = string.Format("SELECT * FROM SliceSettingsCollection WHERE Tag = '{0}' AND PrinterId = {1} ORDER BY Name;", filterTag, ActivePrinterProfile.Instance.ActivePrinter.Id);
results = (IEnumerable<DataStorage.SliceSettingsCollection>)DataStorage.Datastore.Instance.dbSQLite.Query<DataStorage.SliceSettingsCollection>(query);
}
return results;
}
void onItemSelect(object sender, EventArgs e)
{
MenuItem item = (MenuItem)sender;
if (filterTag == "material")
{
if (ActivePrinterProfile.Instance.GetMaterialSetting(1) != Int32.Parse(item.Value))
{
ActivePrinterProfile.Instance.SetMaterialSetting(1, Int32.Parse(item.Value));
}
}
else if (filterTag == "quality")
{
if (ActivePrinterProfile.Instance.ActiveQualitySettingsID != Int32.Parse(item.Value))
{
ActivePrinterProfile.Instance.ActiveQualitySettingsID = Int32.Parse(item.Value);
}
}
UiThread.RunOnIdle((state) =>
{
ActiveSliceSettings.Instance.LoadAllSettings();
ApplicationController.Instance.ReloadAdvancedControlsPanel();
});
}
void onNewItemSelect(object sender, EventArgs e)
{
UiThread.RunOnIdle((state) =>
{
ActiveSliceSettings.Instance.LoadAllSettings();
ApplicationController.Instance.ReloadAdvancedControlsPanel();
if (filterTag == "material")
{
if(ApplicationController.Instance.EditMaterialPresetsWindow == null)
{
ApplicationController.Instance.EditMaterialPresetsWindow = new SlicePresetsWindow(ReloadOptions, filterLabel, filterTag, false, 0);
ApplicationController.Instance.EditMaterialPresetsWindow.Closed += (popupWindowSender, popupWindowSenderE) => { ApplicationController.Instance.EditMaterialPresetsWindow = null; };
}
else
{
ApplicationController.Instance.EditMaterialPresetsWindow.ChangeToSlicePresetFromID(0);
ApplicationController.Instance.EditMaterialPresetsWindow.BringToFront();
}
}
if(filterTag == "quality")
{
if(ApplicationController.Instance.EditQualityPresetsWindow == null)
{
ApplicationController.Instance.EditQualityPresetsWindow = new SlicePresetsWindow(ReloadOptions, filterLabel, filterTag, false, 0);
ApplicationController.Instance.EditQualityPresetsWindow.Closed +=(popupWindowSender, popupWindowSenderE) => {ApplicationController.Instance.EditQualityPresetsWindow = null; };
}
else
{
ApplicationController.Instance.EditQualityPresetsWindow.ChangeToSlicePresetFromID(0);
ApplicationController.Instance.EditQualityPresetsWindow.BringToFront();
}
}
});
}
AnchoredDropDownList CreateDropdown()
{
AnchoredDropDownList dropDownList = new AnchoredDropDownList("- default -", maxHeight: 300);
dropDownList.Margin = new BorderDouble(0, 3);
dropDownList.MinimumSize = new Vector2(dropDownList.LocalBounds.Width, dropDownList.LocalBounds.Height);
MenuItem defaultMenuItem = dropDownList.AddItem("- default -", "0");
defaultMenuItem.Selected += new EventHandler(onItemSelect);
IEnumerable<DataStorage.SliceSettingsCollection> collections = GetCollections();
foreach (DataStorage.SliceSettingsCollection collection in collections)
{
MenuItem menuItem = dropDownList.AddItem(collection.Name, collection.Id.ToString());
menuItem.Selected += new EventHandler(onItemSelect);
}
MenuItem addNewPreset = dropDownList.AddItem("<< Add >>", "new");
addNewPreset.Selected += new EventHandler(onNewItemSelect);
if (filterTag == "material")
{
try
{
dropDownList.SelectedValue = ActivePrinterProfile.Instance.GetMaterialSetting(1).ToString();
}
catch
{
//Unable to set selected value
}
}
else if (filterTag == "quality")
{
try
{
dropDownList.SelectedValue = ActivePrinterProfile.Instance.ActiveQualitySettingsID.ToString();
}
catch
{
//Unable to set selected value
}
}
return dropDownList;
}
}
public class SliceEngineSelector : SliceSelectorWidget
{
public AnchoredDropDownList EngineMenuDropList;
private TupleList<string, Func<bool>> engineOptionsMenuItems = new TupleList<string, Func<bool>>();
public SliceEngineSelector(string label, RGBA_Bytes accentColor)
:base(label, accentColor)
{
}
public override FlowLayoutWidget GetPulldownContainer()
{
EngineMenuDropList = CreateSliceEngineDropdown();
FlowLayoutWidget container = new FlowLayoutWidget();
container.HAnchor = HAnchor.ParentLeftRight;
container.Padding = new BorderDouble(6, 0);
container.AddChild(EngineMenuDropList);
return container;
}
AnchoredDropDownList CreateSliceEngineDropdown()
{
AnchoredDropDownList engineMenuDropList = new AnchoredDropDownList("Engine ");
engineMenuDropList.Margin = new BorderDouble(0,3);
{
//Add Each SliceEngineInfo Objects to DropMenu
foreach (SliceEngineInfo engineMenuItem in SlicingQueue.AvailableSliceEngines)
{
MenuItem item = engineMenuDropList.AddItem(engineMenuItem.Name);
ActivePrinterProfile.SlicingEngineTypes itemEngineType = engineMenuItem.GetSliceEngineType();
item.Selected += (sender, e) =>
{
ActivePrinterProfile.Instance.ActiveSliceEngineType = itemEngineType;
ApplicationController.Instance.ReloadAdvancedControlsPanel();
};
//Set item as selected if it matches the active slice engine
if (engineMenuItem.GetSliceEngineType() == ActivePrinterProfile.Instance.ActiveSliceEngineType)
{
engineMenuDropList.SelectedLabel = engineMenuItem.Name;
}
}
//If nothing is selected (ie selected engine is not available) set to
if (engineMenuDropList.SelectedLabel == "")
{
try
{
engineMenuDropList.SelectedLabel = MatterSliceInfo.DisplayName;
}
catch
{
throw new Exception("MatterSlice is not available, for some strange reason");
}
}
}
engineMenuDropList.MinimumSize = new Vector2(engineMenuDropList.LocalBounds.Width, engineMenuDropList.LocalBounds.Height);
return engineMenuDropList;
}
}
}