mattercontrol/SlicerConfiguration/SlicePresetsWindow/SlicePresetsWindow.cs

372 lines
15 KiB
C#
Raw Normal View History

2014-03-30 17:54:05 -07:00
/*
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 System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using MatterHackers.Agg;
using MatterHackers.Agg.UI;
using MatterHackers.VectorMath;
using MatterHackers.Agg.Image;
using MatterHackers.MatterControl.DataStorage;
using MatterHackers.MatterControl.FieldValidation;
using MatterHackers.Localizations;
2014-04-05 10:42:09 -07:00
using MatterHackers.MatterControl.CustomWidgets;
2014-03-30 17:54:05 -07:00
namespace MatterHackers.MatterControl.SlicerConfiguration
{
2014-04-05 15:51:13 -07:00
public class SlicePresetDetailWidget : GuiWidget
2014-03-30 17:54:05 -07:00
{
2014-04-05 15:51:13 -07:00
TextImageButtonFactory buttonFactory = new TextImageButtonFactory();
2014-03-30 17:54:05 -07:00
LinkButtonFactory linkButtonFactory = new LinkButtonFactory();
SlicePresetsWindow windowController;
MHTextEditWidget macroNameInput;
TextWidget macroNameError;
MHTextEditWidget macroCommandInput;
TextWidget macroCommandError;
2014-04-05 15:51:13 -07:00
public SlicePresetDetailWidget(SlicePresetsWindow windowController)
2014-03-30 17:54:05 -07:00
{
this.windowController = windowController;
2014-04-05 15:51:13 -07:00
if (this.windowController.ActivePresetLayer == null)
2014-03-30 17:54:05 -07:00
{
2014-04-05 15:51:13 -07:00
initSlicePreset();
2014-03-30 17:54:05 -07:00
}
linkButtonFactory.fontSize = 10;
2014-04-05 15:51:13 -07:00
}
2014-03-30 17:54:05 -07:00
2014-04-05 15:51:13 -07:00
void AddElements()
{
FlowLayoutWidget mainContainer = new FlowLayoutWidget(FlowDirection.TopToBottom);
mainContainer.Padding = new BorderDouble(3);
mainContainer.AnchorAll();
2014-03-30 17:54:05 -07:00
2014-04-05 15:51:13 -07:00
mainContainer.AddChild(GetTopRow());
mainContainer.AddChild(GetMiddleRow());
mainContainer.AddChild(GetBottomRow());
2014-03-30 17:54:05 -07:00
2014-04-05 15:51:13 -07:00
this.AddChild(mainContainer);
}
2014-03-30 17:54:05 -07:00
2014-04-05 15:51:13 -07:00
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);
2014-03-30 17:54:05 -07:00
2014-04-05 15:51:13 -07:00
container.AddChild(labelText);
container.AddChild(new HorizontalSpacer());
return container;
}
2014-03-30 17:54:05 -07:00
2014-04-05 15:51:13 -07:00
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);
return container;
}
2014-03-30 17:54:05 -07:00
2014-04-05 15:51:13 -07:00
FlowLayoutWidget GetBottomRow()
{
FlowLayoutWidget container = new FlowLayoutWidget();
container.HAnchor = HAnchor.ParentLeftRight;
2014-03-30 17:54:05 -07:00
2014-04-05 15:51:13 -07:00
Button addPresetButton = buttonFactory.Generate(LocalizedString.Get("Add"), "icon_circle_plus.png");
Button cancelButton = buttonFactory.Generate(LocalizedString.Get("Cancel"));
cancelButton.Click += (sender, e) =>
2014-03-30 17:54:05 -07:00
{
UiThread.RunOnIdle((state) =>
{
2014-04-05 15:51:13 -07:00
Close();
2014-03-30 17:54:05 -07:00
});
};
2014-04-05 15:51:13 -07:00
container.AddChild(addPresetButton);
container.AddChild(new HorizontalSpacer());
container.AddChild(cancelButton);
2014-03-30 17:54:05 -07:00
2014-04-05 15:51:13 -07:00
return container;
2014-03-30 17:54:05 -07:00
}
private FlowLayoutWidget createMacroNameContainer()
{
FlowLayoutWidget container = new FlowLayoutWidget(FlowDirection.TopToBottom);
container.Margin = new BorderDouble(0, 5);
BorderDouble elementMargin = new BorderDouble(top: 3);
string macroNameLabelTxt = LocalizedString.Get("Macro Name");
string macroNameLabelTxtFull = string.Format("{0}:", macroNameLabelTxt);
TextWidget macroNameLabel = new TextWidget(macroNameLabelTxtFull, 0, 0, 12);
macroNameLabel.TextColor = ActiveTheme.Instance.PrimaryTextColor;
macroNameLabel.HAnchor = HAnchor.ParentLeftRight;
macroNameLabel.Margin = new BorderDouble(0, 0, 0, 1);
2014-04-05 15:51:13 -07:00
macroNameInput = new MHTextEditWidget(windowController.ActivePresetLayer.settingsCollectionData.Name);
2014-03-30 17:54:05 -07:00
macroNameInput.HAnchor = HAnchor.ParentLeftRight;
string giveMacroANameLbl = LocalizedString.Get("Give your macro a name");
string giveMacroANameLblFull = string.Format("{0}.", giveMacroANameLbl);
macroNameError = new TextWidget(giveMacroANameLblFull, 0, 0, 10);
macroNameError.TextColor = ActiveTheme.Instance.PrimaryTextColor;
macroNameError.HAnchor = HAnchor.ParentLeftRight;
macroNameError.Margin = elementMargin;
container.AddChild(macroNameLabel);
container.AddChild(macroNameInput);
container.AddChild(macroNameError);
container.HAnchor = HAnchor.ParentLeftRight;
return container;
}
2014-04-05 15:51:13 -07:00
2014-03-30 17:54:05 -07:00
private bool ValidateMacroForm()
{
ValidationMethods validationMethods = new ValidationMethods();
List<FormField> formFields = new List<FormField> { };
FormField.ValidationHandler[] stringValidationHandlers = new FormField.ValidationHandler[] { validationMethods.StringIsNotEmpty };
FormField.ValidationHandler[] nameValidationHandlers = new FormField.ValidationHandler[] { validationMethods.StringIsNotEmpty, validationMethods.StringHasNoSpecialChars };
formFields.Add(new FormField(macroNameInput, macroNameError, nameValidationHandlers));
formFields.Add(new FormField(macroCommandInput, macroCommandError, stringValidationHandlers));
bool formIsValid = true;
foreach (FormField formField in formFields)
{
formField.FieldErrorMessageWidget.Visible = false;
bool fieldIsValid = formField.Validate();
if (!fieldIsValid)
{
formIsValid = false;
}
}
return formIsValid;
}
2014-04-05 15:51:13 -07:00
void initSlicePreset()
2014-03-30 17:54:05 -07:00
{
2014-04-05 15:51:13 -07:00
Dictionary<string, DataStorage.SliceSetting> settingsDictionary = new Dictionary<string, DataStorage.SliceSetting>();
DataStorage.SliceSettingsCollection collection = new DataStorage.SliceSettingsCollection();
collection.Name = "Default";
collection.Tag = windowController.filterTag;
windowController.ActivePresetLayer = new SettingsLayer(collection, settingsDictionary);
2014-03-30 17:54:05 -07:00
}
void saveMacro_Click(object sender, MouseEventArgs mouseEvent)
{
UiThread.RunOnIdle((state) =>
{
if (ValidateMacroForm())
{
2014-04-05 15:51:13 -07:00
saveActivePresets();
2014-03-30 17:54:05 -07:00
windowController.functionToCallOnSave(this, null);
windowController.ChangeToSlicePresetList();
}
});
}
2014-04-05 15:51:13 -07:00
void saveActivePresets()
2014-03-30 17:54:05 -07:00
{
2014-04-05 15:51:13 -07:00
windowController.ActivePresetLayer.settingsCollectionData.Name = macroNameInput.Text;
windowController.ActivePresetLayer.settingsCollectionData.Commit();
2014-03-30 17:54:05 -07:00
}
}
2014-04-05 15:51:13 -07:00
public class SlicePresetListWidget : GuiWidget
2014-03-30 17:54:05 -07:00
{
SlicePresetsWindow windowController;
2014-04-05 10:42:09 -07:00
TextImageButtonFactory buttonFactory;
2014-04-05 15:51:13 -07:00
LinkButtonFactory linkButtonFactory;
2014-04-05 10:42:09 -07:00
2014-04-05 15:51:13 -07:00
public SlicePresetListWidget(SlicePresetsWindow windowController)
2014-03-30 17:54:05 -07:00
{
this.windowController = windowController;
this.AnchorAll();
2014-04-05 15:51:13 -07:00
linkButtonFactory = new LinkButtonFactory();
2014-04-05 10:42:09 -07:00
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;
2014-03-30 17:54:05 -07:00
AddElements();
}
void AddElements()
{
FlowLayoutWidget mainContainer = new FlowLayoutWidget(FlowDirection.TopToBottom);
mainContainer.Padding = new BorderDouble(3);
mainContainer.AnchorAll();
mainContainer.AddChild(GetTopRow());
mainContainer.AddChild(GetMiddleRow());
mainContainer.AddChild(GetBottmRow());
this.AddChild(mainContainer);
}
FlowLayoutWidget GetTopRow()
{
FlowLayoutWidget container = new FlowLayoutWidget();
container.HAnchor = HAnchor.ParentLeftRight;
2014-04-05 15:51:13 -07:00
container.Padding = new BorderDouble(0, 6);
TextWidget labelText = new TextWidget("{0} Presets:".FormatWith(windowController.filterLabel.Localize()), pointSize:14);
2014-04-05 10:42:09 -07:00
2014-04-05 15:51:13 -07:00
container.AddChild(labelText);
container.AddChild(new HorizontalSpacer());
2014-03-30 17:54:05 -07:00
return container;
}
FlowLayoutWidget GetMiddleRow()
{
FlowLayoutWidget container = new FlowLayoutWidget();
container.HAnchor = HAnchor.ParentLeftRight;
container.VAnchor = Agg.UI.VAnchor.ParentBottomTop;
container.BackgroundColor = ActiveTheme.Instance.SecondaryBackgroundColor;
2014-04-05 10:42:09 -07:00
container.Margin = new BorderDouble(0, 3);
2014-03-30 17:54:05 -07:00
return container;
}
FlowLayoutWidget GetBottmRow()
{
FlowLayoutWidget container = new FlowLayoutWidget();
container.HAnchor = HAnchor.ParentLeftRight;
2014-04-05 10:42:09 -07:00
Button addPresetButton = buttonFactory.Generate(LocalizedString.Get("Add"), "icon_circle_plus.png");
Button cancelButton = buttonFactory.Generate(LocalizedString.Get("Cancel"));
2014-04-05 15:51:13 -07:00
cancelButton.Click += (sender, e) =>
{
UiThread.RunOnIdle((state) =>
{
Close();
});
};
2014-04-05 10:42:09 -07:00
container.AddChild(addPresetButton);
container.AddChild(new HorizontalSpacer());
container.AddChild(cancelButton);
2014-03-30 17:54:05 -07:00
return container;
}
2014-04-05 15:51:13 -07:00
IEnumerable<DataStorage.SliceSettingsCollection> GetCollections()
{
IEnumerable<DataStorage.SliceSettingsCollection> results = Enumerable.Empty<DataStorage.SliceSettingsCollection>();
//Retrieve a list of collections matching from the Datastore
string query = string.Format("SELECT * FROM SliceSettingsCollection WHERE Tag = {0};", windowController.filterTag);
results = (IEnumerable<DataStorage.SliceSettingsCollection>)DataStorage.Datastore.Instance.dbSQLite.Query<DataStorage.SliceSettingsCollection>(query);
return results;
}
2014-03-30 17:54:05 -07:00
}
public class SlicePresetsWindow : SystemWindow
{
public EventHandler functionToCallOnSave;
2014-04-05 10:42:09 -07:00
public string filterTag;
2014-04-05 15:51:13 -07:00
public string filterLabel;
public SettingsLayer ActivePresetLayer;
2014-03-30 17:54:05 -07:00
2014-04-05 15:51:13 -07:00
public SlicePresetsWindow(EventHandler functionToCallOnSave, string filterLabel, string filterTag)
2014-03-30 17:54:05 -07:00
: base(420, 560)
{
2014-04-05 15:51:13 -07:00
2014-03-30 17:54:05 -07:00
Title = LocalizedString.Get("Slice Presets Editor");
2014-04-05 15:51:13 -07:00
2014-03-30 17:54:05 -07:00
this.filterTag = filterTag;
2014-04-05 15:51:13 -07:00
this.filterLabel = filterLabel;
this.MinimumSize = new Vector2(420, 560);
2014-03-30 17:54:05 -07:00
this.functionToCallOnSave = functionToCallOnSave;
2014-04-05 15:51:13 -07:00
2014-03-30 17:54:05 -07:00
BackgroundColor = ActiveTheme.Instance.PrimaryBackgroundColor;
ChangeToSlicePresetList();
ShowAsSystemWindow();
}
public void ChangeToSlicePresetList()
{
2014-04-05 15:51:13 -07:00
this.ActivePresetLayer = null;
UiThread.RunOnIdle(DoChangeToSlicePresetList);
2014-03-30 17:54:05 -07:00
}
2014-04-05 15:51:13 -07:00
private void DoChangeToSlicePresetList(object state)
2014-03-30 17:54:05 -07:00
{
2014-04-05 15:51:13 -07:00
GuiWidget slicePresetWidget = new SlicePresetListWidget(this);
2014-03-30 17:54:05 -07:00
this.RemoveAllChildren();
this.AddChild(slicePresetWidget);
this.Invalidate();
}
2014-04-05 15:51:13 -07:00
public void ChangeToSlicePresetDetail(SliceSettingsCollection collection = null)
{
Dictionary<string, DataStorage.SliceSetting> settingsDictionary = new Dictionary<string, DataStorage.SliceSetting>();
IEnumerable<DataStorage.SliceSetting> settingsList = GetCollectionSettings(collection.Id);
foreach (DataStorage.SliceSetting s in settingsList)
{
settingsDictionary[s.Name] = s;
}
this.ActivePresetLayer = new SettingsLayer(collection, settingsDictionary);
UiThread.RunOnIdle(DoChangeToSlicePresetDetail);
}
IEnumerable<DataStorage.SliceSetting> GetCollectionSettings(int collectionId)
2014-03-30 17:54:05 -07:00
{
2014-04-05 15:51:13 -07:00
//Retrieve a list of slice settings from the Datastore
string query = string.Format("SELECT * FROM SliceSetting WHERE SettingsCollectionID = {0};", collectionId);
IEnumerable<DataStorage.SliceSetting> result = (IEnumerable<DataStorage.SliceSetting>)DataStorage.Datastore.Instance.dbSQLite.Query<DataStorage.SliceSetting>(query);
return result;
2014-03-30 17:54:05 -07:00
}
2014-04-05 15:51:13 -07:00
private void DoChangeToSlicePresetDetail(object state)
2014-03-30 17:54:05 -07:00
{
2014-04-05 15:51:13 -07:00
GuiWidget macroDetailWidget = new SlicePresetDetailWidget(this);
2014-03-30 17:54:05 -07:00
this.RemoveAllChildren();
this.AddChild(macroDetailWidget);
this.Invalidate();
}
}
}