2014-01-29 19:09:30 -08:00
|
|
|
|
/*
|
2017-11-08 15:20:57 -08:00
|
|
|
|
Copyright (c) 2017, Lars Brubaker, John Lewin
|
2014-01-29 19:09:30 -08:00
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
2015-04-08 15:20:10 -07:00
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
2014-01-29 19:09:30 -08:00
|
|
|
|
|
|
|
|
|
|
1. Redistributions of source code must retain the above copyright notice, this
|
2015-04-08 15:20:10 -07:00
|
|
|
|
list of conditions and the following disclaimer.
|
2014-01-29 19:09:30 -08:00
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
|
this list of conditions and the following disclaimer in the documentation
|
2015-04-08 15:20:10 -07:00
|
|
|
|
and/or other materials provided with the distribution.
|
2014-01-29 19:09:30 -08:00
|
|
|
|
|
|
|
|
|
|
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
|
2015-04-08 15:20:10 -07:00
|
|
|
|
of the authors and should not be interpreted as representing official policies,
|
2014-01-29 19:09:30 -08:00
|
|
|
|
either expressed or implied, of the FreeBSD Project.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2016-12-03 12:08:39 -08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2014-01-29 19:09:30 -08:00
|
|
|
|
using MatterHackers.Agg;
|
|
|
|
|
|
using MatterHackers.Agg.UI;
|
2015-04-08 15:20:10 -07:00
|
|
|
|
using MatterHackers.Localizations;
|
2014-01-29 19:09:30 -08:00
|
|
|
|
using MatterHackers.MatterControl.FieldValidation;
|
2016-04-18 11:31:31 -07:00
|
|
|
|
using MatterHackers.MatterControl.SlicerConfiguration;
|
2014-01-29 19:09:30 -08:00
|
|
|
|
|
|
|
|
|
|
namespace MatterHackers.MatterControl
|
|
|
|
|
|
{
|
2017-11-08 15:56:37 -08:00
|
|
|
|
public class MacroDetailPage : DialogPage
|
2016-12-03 12:08:39 -08:00
|
|
|
|
{
|
2017-11-08 15:19:23 -08:00
|
|
|
|
private List<FormField> formFields;
|
|
|
|
|
|
private PrinterSettings printerSettings;
|
2016-12-03 12:08:39 -08:00
|
|
|
|
|
2017-11-08 15:19:23 -08:00
|
|
|
|
public MacroDetailPage(GCodeMacro gcodeMacro, PrinterSettings printerSettings)
|
2016-12-03 12:08:39 -08:00
|
|
|
|
{
|
2017-11-08 15:19:23 -08:00
|
|
|
|
// Form validation fields
|
2022-07-15 17:28:39 -07:00
|
|
|
|
ThemedTextEditWidget macroNameInput;
|
|
|
|
|
|
ThemedTextEditWidget macroCommandInput;
|
2020-08-14 07:37:40 -07:00
|
|
|
|
WrappedTextWidget macroCommandError;
|
|
|
|
|
|
WrappedTextWidget macroNameError;
|
2016-12-03 12:08:39 -08:00
|
|
|
|
|
2017-11-08 15:19:23 -08:00
|
|
|
|
this.HeaderText = "Edit Macro".Localize();
|
2017-09-05 10:33:14 -07:00
|
|
|
|
this.printerSettings = printerSettings;
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
2017-11-08 15:19:23 -08:00
|
|
|
|
var elementMargin = new BorderDouble(top: 3);
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
2018-11-03 10:12:27 -07:00
|
|
|
|
contentRow.Padding += 3;
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
2018-11-03 10:12:27 -07:00
|
|
|
|
contentRow.AddChild(new TextWidget("Macro Name".Localize() + ":", 0, 0, 12)
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
2018-11-03 09:13:07 -07:00
|
|
|
|
TextColor = theme.TextColor,
|
2017-11-08 15:19:23 -08:00
|
|
|
|
HAnchor = HAnchor.Stretch,
|
|
|
|
|
|
Margin = new BorderDouble(0, 0, 0, 1)
|
|
|
|
|
|
});
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
2022-07-15 17:28:39 -07:00
|
|
|
|
contentRow.AddChild(macroNameInput = new ThemedTextEditWidget(GCodeMacro.FixMacroName(gcodeMacro.Name), theme)
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
2017-11-08 15:19:23 -08:00
|
|
|
|
HAnchor = HAnchor.Stretch
|
|
|
|
|
|
});
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
2020-08-14 07:37:40 -07:00
|
|
|
|
contentRow.AddChild(macroNameError = new WrappedTextWidget("Give the macro a name".Localize() + ".", 10)
|
2017-11-08 15:19:23 -08:00
|
|
|
|
{
|
2018-11-03 09:13:07 -07:00
|
|
|
|
TextColor = theme.TextColor,
|
2017-11-08 15:19:23 -08:00
|
|
|
|
Margin = elementMargin
|
|
|
|
|
|
});
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
2018-11-03 10:12:27 -07:00
|
|
|
|
contentRow.AddChild(new TextWidget("Macro Commands".Localize() + ":", 0, 0, 12)
|
2017-11-08 15:19:23 -08:00
|
|
|
|
{
|
2018-11-03 09:13:07 -07:00
|
|
|
|
TextColor = theme.TextColor,
|
2017-11-08 15:19:23 -08:00
|
|
|
|
HAnchor = HAnchor.Stretch,
|
|
|
|
|
|
Margin = new BorderDouble(0, 0, 0, 1)
|
|
|
|
|
|
});
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
2022-07-15 17:28:39 -07:00
|
|
|
|
macroCommandInput = new ThemedTextEditWidget(gcodeMacro.GCode, theme, pixelHeight: 120, multiLine: true, typeFace: ApplicationController.GetTypeFace(NamedTypeFace.Liberation_Mono))
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
2017-11-08 15:19:23 -08:00
|
|
|
|
HAnchor = HAnchor.Stretch,
|
|
|
|
|
|
VAnchor = VAnchor.Stretch
|
2015-04-08 15:20:10 -07:00
|
|
|
|
};
|
2017-08-07 15:47:27 -07:00
|
|
|
|
macroCommandInput.ActualTextEditWidget.VAnchor = VAnchor.Stretch;
|
2017-11-08 15:19:23 -08:00
|
|
|
|
macroCommandInput.DrawFromHintedCache();
|
2018-11-03 10:12:27 -07:00
|
|
|
|
contentRow.AddChild(macroCommandInput);
|
2016-12-03 12:08:39 -08:00
|
|
|
|
|
2020-08-14 07:37:40 -07:00
|
|
|
|
contentRow.AddChild(macroCommandError = new WrappedTextWidget("This should be in 'G-Code'".Localize() + ".", 10)
|
2017-11-08 15:19:23 -08:00
|
|
|
|
{
|
2018-11-03 09:13:07 -07:00
|
|
|
|
TextColor = theme.TextColor,
|
2017-11-08 15:19:23 -08:00
|
|
|
|
Margin = elementMargin
|
|
|
|
|
|
});
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
2017-11-08 15:19:23 -08:00
|
|
|
|
var container = new FlowLayoutWidget
|
|
|
|
|
|
{
|
|
|
|
|
|
Margin = new BorderDouble(0, 5),
|
|
|
|
|
|
HAnchor = HAnchor.Stretch
|
|
|
|
|
|
};
|
2016-12-08 15:39:23 -08:00
|
|
|
|
|
2018-11-03 10:12:27 -07:00
|
|
|
|
contentRow.AddChild(container);
|
2017-11-08 15:19:23 -08:00
|
|
|
|
|
2018-09-05 07:43:28 -07:00
|
|
|
|
var saveMacroButton = theme.CreateDialogButton("Save".Localize());
|
|
|
|
|
|
saveMacroButton.Click += (s, e) =>
|
2017-09-06 18:16:45 -07:00
|
|
|
|
{
|
2017-11-08 15:19:23 -08:00
|
|
|
|
UiThread.RunOnIdle(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ValidateMacroForm())
|
|
|
|
|
|
{
|
|
|
|
|
|
// SaveActiveMacro
|
|
|
|
|
|
gcodeMacro.Name = macroNameInput.Text;
|
|
|
|
|
|
gcodeMacro.GCode = macroCommandInput.Text;
|
|
|
|
|
|
|
|
|
|
|
|
if (!printerSettings.Macros.Contains(gcodeMacro))
|
|
|
|
|
|
{
|
|
|
|
|
|
printerSettings.Macros.Add(gcodeMacro);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-05 07:43:28 -07:00
|
|
|
|
printerSettings.NotifyMacrosChanged();
|
2018-09-05 07:44:21 -07:00
|
|
|
|
printerSettings.Save();
|
2018-09-05 07:43:28 -07:00
|
|
|
|
|
2018-06-19 15:02:25 -07:00
|
|
|
|
this.DialogWindow.ChangeToPage(new MacroListPage(printerSettings));
|
2017-11-08 15:19:23 -08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2018-09-05 07:43:28 -07:00
|
|
|
|
this.AddPageAction(saveMacroButton);
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
2017-11-08 15:19:23 -08:00
|
|
|
|
// Define field validation
|
|
|
|
|
|
var validationMethods = new ValidationMethods();
|
|
|
|
|
|
var stringValidationHandlers = new FormField.ValidationHandler[] { validationMethods.StringIsNotEmpty };
|
|
|
|
|
|
|
|
|
|
|
|
formFields = new List<FormField>
|
2016-12-03 12:08:39 -08:00
|
|
|
|
{
|
2017-11-08 15:19:23 -08:00
|
|
|
|
new FormField(macroNameInput, macroNameError, stringValidationHandlers),
|
|
|
|
|
|
new FormField(macroCommandInput, macroCommandError, stringValidationHandlers)
|
|
|
|
|
|
};
|
2016-12-03 12:08:39 -08:00
|
|
|
|
}
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
2017-11-08 15:19:23 -08:00
|
|
|
|
protected override void OnCancel(out bool abortCancel)
|
2016-12-03 12:08:39 -08:00
|
|
|
|
{
|
2017-11-08 15:19:23 -08:00
|
|
|
|
abortCancel = true;
|
2018-06-19 15:02:25 -07:00
|
|
|
|
this.DialogWindow.ChangeToPage(new MacroListPage(printerSettings));
|
2015-04-08 15:20:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private bool ValidateMacroForm()
|
|
|
|
|
|
{
|
|
|
|
|
|
bool formIsValid = true;
|
|
|
|
|
|
foreach (FormField formField in formFields)
|
|
|
|
|
|
{
|
|
|
|
|
|
formField.FieldErrorMessageWidget.Visible = false;
|
2017-11-08 15:19:23 -08:00
|
|
|
|
|
|
|
|
|
|
if (!formField.Validate())
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
|
|
|
|
|
formIsValid = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-11-08 15:19:23 -08:00
|
|
|
|
|
2015-04-08 15:20:10 -07:00
|
|
|
|
return formIsValid;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2014-01-29 19:09:30 -08:00
|
|
|
|
}
|