mattercontrol/MatterControlLib/EeProm/EePromRepetierWindow.cs

327 lines
9.2 KiB
C#
Raw Normal View History

/*
Copyright (c) 2018, Lars Brubaker, John Lewin
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:
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.
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.
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,
either expressed or implied, of the FreeBSD Project.
*/
//#define SIMULATE_CONNECTION
2017-08-20 15:52:43 -07:00
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using MatterHackers.Agg;
using MatterHackers.Agg.Platform;
using MatterHackers.Agg.UI;
using MatterHackers.Localizations;
2018-06-18 15:04:41 -07:00
using MatterHackers.MatterControl.PartPreviewWindow;
using MatterHackers.MatterControl.SlicerConfiguration;
namespace MatterHackers.MatterControl.EeProm
{
public class EEPromPage : DialogPage
2017-05-19 14:39:57 -07:00
{
private static Regex nameSanitizer = new Regex("[^_a-zA-Z0-9-]", RegexOptions.Compiled);
protected PrinterConfig printer;
2017-05-19 14:39:57 -07:00
public EEPromPage(PrinterConfig printer)
: base("Close".Localize(), useOverflowBar: true)
2017-05-19 14:39:57 -07:00
{
this.HeaderText = "EEProm Settings".Localize();
this.WindowSize = new VectorMath.Vector2(663, 575);
this.printer = printer;
2018-11-16 08:44:56 -08:00
headerRow.Margin = this.headerRow.Margin.Clone(bottom: 0);
printer.Connection.CommunicationStateChanged += CommunicationStateChanged;
2017-05-19 14:39:57 -07:00
}
public override void OnClosed(EventArgs e)
2017-05-19 14:39:57 -07:00
{
2018-11-16 08:44:56 -08:00
// Unregister listeners
printer.Connection.CommunicationStateChanged -= CommunicationStateChanged;
2017-05-19 14:39:57 -07:00
base.OnClosed(e);
}
protected string GetSanitizedPrinterName()
{
// TODO: Determine best file name sanitization implementation: this, MakeValidFileName, something else?
string printerName = printer.Settings.GetValue(SettingsKey.printer_name).Replace(" ", "_");
return nameSanitizer.Replace(printerName, "");
}
2018-11-16 08:44:56 -08:00
private void CommunicationStateChanged(object s, EventArgs e)
{
if (!printer.Connection.IsConnected)
{
this.DialogWindow.CloseOnIdle();
2018-11-16 08:44:56 -08:00
}
}
2017-05-19 14:39:57 -07:00
}
public class RepetierEEPromPage : EEPromPage
2015-04-08 15:20:10 -07:00
{
private EePromRepetierStorage currentEePromSettings;
2018-06-18 14:04:53 -07:00
private FlowLayoutWidget settingsColumn;
public RepetierEEPromPage(PrinterConfig printer)
: base(printer)
2015-04-08 15:20:10 -07:00
{
AlwaysOnTopOfMain = true;
this.WindowTitle = "Firmware EEPROM Settings".Localize();
2015-04-08 15:20:10 -07:00
currentEePromSettings = new EePromRepetierStorage();
var topToBottom = contentRow;
2018-06-18 13:10:53 -07:00
var row = new FlowLayoutWidget
{
HAnchor = HAnchor.Stretch,
};
2017-01-04 07:23:30 -08:00
GuiWidget descriptionWidget = AddDescription("Description".Localize());
descriptionWidget.Margin = new BorderDouble(left: 3);
2015-04-08 15:20:10 -07:00
row.AddChild(descriptionWidget);
CreateSpacer(row);
row.AddChild(new TextWidget("Value".Localize(), pointSize: theme.FontSize10, textColor: theme.TextColor)
2018-06-18 13:10:53 -07:00
{
VAnchor = VAnchor.Center,
Margin = new BorderDouble(left: 5, right: 60)
});
2015-04-08 15:20:10 -07:00
topToBottom.AddChild(row);
{
2018-06-18 13:10:53 -07:00
var settingsAreaScrollBox = new ScrollableWidget(true);
settingsAreaScrollBox.ScrollArea.HAnchor |= HAnchor.Stretch;
2015-04-08 15:20:10 -07:00
settingsAreaScrollBox.AnchorAll();
2018-10-13 17:58:54 -07:00
settingsAreaScrollBox.BackgroundColor = theme.MinimalShade;
2015-04-08 15:20:10 -07:00
topToBottom.AddChild(settingsAreaScrollBox);
2018-06-18 14:04:53 -07:00
settingsColumn = new FlowLayoutWidget(FlowDirection.TopToBottom)
2018-06-18 13:10:53 -07:00
{
HAnchor = HAnchor.MaxFitOrStretch
};
2018-06-18 14:04:53 -07:00
settingsAreaScrollBox.AddChild(settingsColumn);
2015-04-08 15:20:10 -07:00
}
2018-06-18 15:04:41 -07:00
if (headerRow is OverflowBar overflowBar)
{
2018-06-18 15:04:41 -07:00
overflowBar.ExtendOverflowMenu = (popupMenu) =>
{
2018-06-18 15:04:41 -07:00
var menuItem = popupMenu.CreateMenuItem("Import".Localize());
menuItem.Name = "Import Menu Item";
menuItem.Click += (s, e) =>
{
2018-06-18 15:04:41 -07:00
UiThread.RunOnIdle(() =>
{
AggContext.FileDialogs.OpenFileDialog(
new OpenFileDialogParams("EEPROM Settings|*.ini")
{
ActionButtonLabel = "Import EEPROM Settings".Localize(),
Title = "Import EEPROM".Localize(),
},
(openParams) =>
{
if (!string.IsNullOrEmpty(openParams.FileName))
{
currentEePromSettings.Import(openParams.FileName);
RebuildUi();
}
});
}, .1);
2018-06-18 15:04:41 -07:00
};
2018-06-18 15:04:41 -07:00
menuItem = popupMenu.CreateMenuItem("Export".Localize());
menuItem.Name = "Export Menu Item";
menuItem.Click += (s, e) =>
{
UiThread.RunOnIdle(this.ExportSettings, .1);
2018-06-18 15:04:41 -07:00
};
};
}
2018-06-18 15:04:41 -07:00
// put in the save button
var buttonSave = theme.CreateDialogButton("Save To EEPROM".Localize());
buttonSave.Click += (s, e) =>
{
currentEePromSettings.Save(printer.Connection);
currentEePromSettings.Clear();
this.DialogWindow.Close();
2018-06-18 15:04:41 -07:00
};
2018-06-18 15:04:41 -07:00
this.AddPageAction(buttonSave);
var exportButton = theme.CreateDialogButton("Export".Localize());
exportButton.Click += (s, e) =>
{
UiThread.RunOnIdle(this.ExportSettings, .1);
};
this.AddPageAction(exportButton);
2015-04-08 15:20:10 -07:00
currentEePromSettings.Clear();
printer.Connection.LineReceived += currentEePromSettings.Add;
2018-06-18 16:40:43 -07:00
currentEePromSettings.SettingAdded += NewSettingReadFromPrinter;
currentEePromSettings.AskPrinterForSettings(printer.Connection);
#if SIMULATE_CONNECTION
UiThread.RunOnIdle(AddSimulatedItems);
#endif
2015-04-08 15:20:10 -07:00
}
private void ExportSettings()
{
string defaultFileName = $"eeprom_settings_{base.GetSanitizedPrinterName()}.ini";
AggContext.FileDialogs.SaveFileDialog(
new SaveFileDialogParams("EEPROM Settings|*.ini")
{
ActionButtonLabel = "Export EEPROM Settings".Localize(),
Title = "Export EEPROM".Localize(),
FileName = defaultFileName
},
(saveParams) =>
{
if (!string.IsNullOrEmpty(saveParams.FileName))
{
currentEePromSettings.Export(saveParams.FileName);
}
});
}
#if SIMULATE_CONNECTION
int count;
void AddSimulatedItems(object state)
{
NewSettingReadFromPrinter(this, new EePromRepetierParameter("this is a test line " + count.ToString()));
count++;
if (count < 30)
{
UiThread.RunOnIdle(AddSimulatedItems);
}
2014-02-22 14:52:53 -08:00
}
#endif
2014-02-22 14:52:53 -08:00
2015-04-08 15:20:10 -07:00
private static void CreateSpacer(FlowLayoutWidget buttonBar)
{
2018-06-18 13:10:53 -07:00
buttonBar.AddChild(new GuiWidget(1, 1)
{
HAnchor = HAnchor.Stretch
});
2015-04-08 15:20:10 -07:00
}
public override void OnClosed(EventArgs e)
2015-04-08 15:20:10 -07:00
{
2018-06-18 15:04:41 -07:00
if (currentEePromSettings != null)
{
2018-06-18 16:40:43 -07:00
currentEePromSettings.SettingAdded -= NewSettingReadFromPrinter;
2018-06-18 15:04:41 -07:00
}
2015-04-08 15:20:10 -07:00
base.OnClosed(e);
}
bool waitingForUiUpdate = false;
2015-04-08 15:20:10 -07:00
private void NewSettingReadFromPrinter(object sender, EventArgs e)
{
2018-06-18 13:10:53 -07:00
if (e is EePromRepetierParameter newSetting)
2015-04-08 15:20:10 -07:00
{
if (!waitingForUiUpdate)
{
waitingForUiUpdate = true;
UiThread.RunOnIdle(RebuildUi, 1);
}
2015-04-08 15:20:10 -07:00
}
}
private int currentTabIndex = 0;
private void RebuildUi()
2015-04-08 15:20:10 -07:00
{
2018-06-18 13:10:53 -07:00
var tempList = new List<EePromRepetierParameter>();
lock (currentEePromSettings.eePromSettingsList)
2015-04-08 15:20:10 -07:00
{
2018-06-18 13:10:53 -07:00
foreach (var keyValue in currentEePromSettings.eePromSettingsList)
2015-04-08 15:20:10 -07:00
{
tempList.Add(keyValue.Value);
2015-04-08 15:20:10 -07:00
}
}
2015-04-08 15:20:10 -07:00
2018-06-18 14:04:53 -07:00
settingsColumn.CloseAllChildren();
foreach (EePromRepetierParameter newSetting in tempList)
{
if (newSetting != null)
2015-04-08 15:20:10 -07:00
{
2018-06-18 13:10:53 -07:00
var row = new FlowLayoutWidget
{
HAnchor = HAnchor.MaxFitOrStretch,
Padding = new BorderDouble(5, 0)
};
row.AddChild(AddDescription(newSetting.Description));
2018-06-18 13:10:53 -07:00
2018-06-18 14:04:53 -07:00
if ((settingsColumn.Children.Count % 2) == 1)
{
2017-10-31 11:43:25 -07:00
row.BackgroundColor = new Color(0, 0, 0, 30);
}
CreateSpacer(row);
2018-06-18 13:10:53 -07:00
double.TryParse(newSetting.Value, out double currentValue);
var valueEdit = new MHNumberEdit(currentValue, theme, pixelWidth: 80 * GuiWidget.DeviceScale, allowNegatives: true, allowDecimals: true)
2018-06-18 13:10:53 -07:00
{
SelectAllOnFocus = true,
TabIndex = currentTabIndex++,
VAnchor = VAnchor.Center
};
valueEdit.ActuallNumberEdit.EditComplete += (s, e) =>
{
newSetting.Value = valueEdit.ActuallNumberEdit.Value.ToString();
};
row.AddChild(valueEdit);
2018-06-18 14:04:53 -07:00
settingsColumn.AddChild(row);
}
2015-04-08 15:20:10 -07:00
}
waitingForUiUpdate = false;
2015-04-08 15:20:10 -07:00
}
private GuiWidget AddDescription(string description)
{
2018-06-18 13:10:53 -07:00
var holder = new GuiWidget(340, 40);
holder.AddChild(new TextWidget(description, pointSize: theme.DefaultFontSize, textColor: theme.TextColor)
2018-06-18 13:10:53 -07:00
{
VAnchor = VAnchor.Center
});
2015-04-08 15:20:10 -07:00
return holder;
}
}
}