mattercontrol/PartPreviewWindow/SaveAsWindow.cs

169 lines
6.2 KiB
C#
Raw Normal View History

2015-04-08 15:20:10 -07:00
using MatterHackers.Agg;
using MatterHackers.Agg.UI;
using MatterHackers.Localizations;
2015-04-08 15:20:10 -07:00
using MatterHackers.MatterControl.CustomWidgets;
using MatterHackers.MatterControl.DataStorage;
using MatterHackers.MatterControl.PrintLibrary;
using MatterHackers.MatterControl.PrintQueue;
2015-04-08 15:20:10 -07:00
using System;
using System.IO;
2015-04-08 15:20:10 -07:00
namespace MatterHackers.MatterControl
{
public class SaveAsWindow : SystemWindow
{
private CheckBox addToLibraryOption;
private Action<SaveAsReturnInfo> functionToCallOnSaveAs;
2015-04-08 15:20:10 -07:00
private TextImageButtonFactory textImageButtonFactory = new TextImageButtonFactory();
private MHTextEditWidget textToAddWidget;
2014-03-19 10:12:55 -07:00
public SaveAsWindow(Action<SaveAsReturnInfo> functionToCallOnSaveAs)
2015-04-08 15:20:10 -07:00
: base(480, 250)
{
Title = "MatterControl - Save As";
AlwaysOnTopOfMain = true;
2015-04-08 15:20:10 -07:00
this.functionToCallOnSaveAs = functionToCallOnSaveAs;
FlowLayoutWidget topToBottom = new FlowLayoutWidget(FlowDirection.TopToBottom);
topToBottom.AnchorAll();
topToBottom.Padding = new BorderDouble(3, 0, 3, 5);
// Creates Header
FlowLayoutWidget headerRow = new FlowLayoutWidget(FlowDirection.LeftToRight);
headerRow.HAnchor = HAnchor.ParentLeftRight;
headerRow.Margin = new BorderDouble(0, 3, 0, 0);
headerRow.Padding = new BorderDouble(0, 3, 0, 3);
2014-03-19 10:12:55 -07:00
BackgroundColor = ActiveTheme.Instance.PrimaryBackgroundColor;
2015-04-08 15:20:10 -07:00
//Creates Text and adds into header
2014-03-19 10:12:55 -07:00
{
string saveAsLabel = "Save New Design to Queue".Localize() + ":";
2015-04-08 15:20:10 -07:00
TextWidget elementHeader = new TextWidget(saveAsLabel, pointSize: 14);
2014-03-19 10:12:55 -07:00
elementHeader.TextColor = ActiveTheme.Instance.PrimaryTextColor;
elementHeader.HAnchor = HAnchor.ParentLeftRight;
elementHeader.VAnchor = Agg.UI.VAnchor.ParentBottom;
2015-04-08 15:20:10 -07:00
headerRow.AddChild(elementHeader);
topToBottom.AddChild(headerRow);
this.AddChild(topToBottom);
}
2014-03-19 10:12:55 -07:00
//Creates container in the middle of window
2014-06-05 13:00:31 -07:00
FlowLayoutWidget middleRowContainer = new FlowLayoutWidget(FlowDirection.TopToBottom);
{
2014-06-05 13:00:31 -07:00
middleRowContainer.HAnchor = HAnchor.ParentLeftRight;
middleRowContainer.VAnchor = VAnchor.ParentBottomTop;
middleRowContainer.Padding = new BorderDouble(5);
middleRowContainer.BackgroundColor = ActiveTheme.Instance.SecondaryBackgroundColor;
2014-03-19 10:12:55 -07:00
}
string fileNameLabel = "Design Name".Localize();
2014-03-20 12:01:31 -07:00
TextWidget textBoxHeader = new TextWidget(fileNameLabel, pointSize: 12);
textBoxHeader.TextColor = ActiveTheme.Instance.PrimaryTextColor;
2015-04-08 15:20:10 -07:00
textBoxHeader.Margin = new BorderDouble(5);
2014-03-20 12:01:31 -07:00
textBoxHeader.HAnchor = HAnchor.ParentLeft;
string fileNameLabelFull = "Enter the name of your design.".Localize(); ;
2014-03-20 12:01:31 -07:00
TextWidget textBoxHeaderFull = new TextWidget(fileNameLabelFull, pointSize: 9);
textBoxHeaderFull.TextColor = ActiveTheme.Instance.PrimaryTextColor;
2015-04-08 15:20:10 -07:00
textBoxHeaderFull.Margin = new BorderDouble(5);
2014-03-20 12:01:31 -07:00
textBoxHeaderFull.HAnchor = HAnchor.ParentLeftRight;
//Adds text box and check box to the above container
textToAddWidget = new MHTextEditWidget("", pixelWidth: 300, messageWhenEmptyAndNotSelected: "Enter a Design Name Here".Localize());
textToAddWidget.HAnchor = HAnchor.ParentLeftRight;
textToAddWidget.Margin = new BorderDouble(5);
addToLibraryOption = new CheckBox("Also save to Library".Localize(), ActiveTheme.Instance.PrimaryTextColor);
2015-04-08 15:20:10 -07:00
addToLibraryOption.Margin = new BorderDouble(5);
addToLibraryOption.HAnchor = HAnchor.ParentLeftRight;
2014-06-05 13:00:31 -07:00
middleRowContainer.AddChild(textBoxHeader);
2015-04-08 15:20:10 -07:00
middleRowContainer.AddChild(textBoxHeaderFull);
2014-06-05 13:00:31 -07:00
middleRowContainer.AddChild(textToAddWidget);
middleRowContainer.AddChild(new HorizontalSpacer());
middleRowContainer.AddChild(addToLibraryOption);
topToBottom.AddChild(middleRowContainer);
2015-04-08 15:20:10 -07:00
//Creates button container on the bottom of window
FlowLayoutWidget buttonRow = new FlowLayoutWidget(FlowDirection.LeftToRight);
{
BackgroundColor = ActiveTheme.Instance.PrimaryBackgroundColor;
buttonRow.HAnchor = HAnchor.ParentLeftRight;
2015-04-08 15:20:10 -07:00
buttonRow.Padding = new BorderDouble(0, 3);
}
2015-04-08 15:20:10 -07:00
Button saveAsButton = textImageButtonFactory.Generate("Save As".Localize(), centerText: true);
2014-03-19 10:12:55 -07:00
saveAsButton.Visible = true;
saveAsButton.Cursor = Cursors.Hand;
2015-04-08 15:20:10 -07:00
buttonRow.AddChild(saveAsButton);
2014-03-19 10:12:55 -07:00
2015-04-08 15:20:10 -07:00
saveAsButton.Click += new EventHandler(saveAsButton_Click);
textToAddWidget.ActualTextEditWidget.EnterPressed += new KeyEventHandler(ActualTextEditWidget_EnterPressed);
//Adds SaveAs and Close Button to button container
2015-04-08 15:20:10 -07:00
buttonRow.AddChild(new HorizontalSpacer());
2014-03-19 10:12:55 -07:00
Button cancelButton = textImageButtonFactory.Generate("Cancel".Localize(), centerText: true);
cancelButton.Visible = true;
cancelButton.Cursor = Cursors.Hand;
2015-04-08 15:20:10 -07:00
buttonRow.AddChild(cancelButton);
cancelButton.Click += (sender, e) =>
{
CloseOnIdle();
};
topToBottom.AddChild(buttonRow);
2014-03-19 10:12:55 -07:00
2015-04-08 15:20:10 -07:00
ShowAsSystemWindow();
2014-03-19 10:12:55 -07:00
}
2015-04-08 15:20:10 -07:00
private void ActualTextEditWidget_EnterPressed(object sender, KeyEventArgs keyEvent)
{
SubmitForm();
}
private void saveAsButton_Click(object sender, EventArgs mouseEvent)
{
SubmitForm();
}
private void SubmitForm()
{
string newName = textToAddWidget.ActualTextEditWidget.Text;
if (newName != "")
{
string fileName = Path.ChangeExtension(Path.GetRandomFileName(), ".amf");
string fileNameAndPath = Path.Combine(ApplicationDataStorage.Instance.ApplicationLibraryDataPath, fileName);
SaveAsReturnInfo returnInfo = new SaveAsReturnInfo(newName, fileNameAndPath, addToLibraryOption.Checked);
functionToCallOnSaveAs(returnInfo);
CloseOnIdle();
}
}
2015-04-08 15:20:10 -07:00
public class SaveAsReturnInfo
{
public string fileNameAndPath;
public string newName;
public bool placeInLibrary;
2015-04-08 15:20:10 -07:00
public PrintItemWrapper printItemWrapper;
public SaveAsReturnInfo(string newName, string fileNameAndPath, bool placeInLibrary)
{
this.newName = newName;
this.fileNameAndPath = fileNameAndPath;
this.placeInLibrary = placeInLibrary;
PrintItem printItem = new PrintItem();
printItem.Name = newName;
printItem.FileLocation = Path.GetFullPath(fileNameAndPath);
printItem.PrintItemCollectionID = LibrarySQLiteData.Instance.RootLibraryCollection.Id;
2015-04-08 15:20:10 -07:00
printItem.Commit();
2015-04-08 15:20:10 -07:00
printItemWrapper = new PrintItemWrapper(printItem);
}
}
}
}