Thought experiment on moving a rooted event to an event

This commit is contained in:
Lars Brubaker 2018-11-08 17:16:35 -08:00
parent b2a479a10f
commit 5232361758
4 changed files with 95 additions and 68 deletions

View file

@ -1145,7 +1145,6 @@ namespace MatterHackers.MatterControl
},
iconCollector: (theme) => AggContext.StaticData.LoadIcon("scale_32x32.png", 16, 16, theme.InvertIcons));
this.Graph.RegisterOperation(
typeof(IObject3D),
typeof(ScaleObject3D),
@ -1363,11 +1362,69 @@ namespace MatterHackers.MatterControl
}
}, ref unregisterEvent);
// show countdown for turning off heat if required
PrinterConnection.TemporarilyHoldingTemp.RegisterEvent((s, e) =>
{
var printerConnection = this.ActivePrinter.Connection;
this.InitializeLibrary();
PrinterConnection.AnyConnectionSucceeded.RegisterEvent((s, e) =>
{
// run the print leveling wizard if we need to for this printer
var printer = this.ActivePrinters.Where(p => p.Connection == s).FirstOrDefault();
if (printer != null)
{
UiThread.RunOnIdle(() =>
{
this.RunAnyRequiredPrinterSetup(printer, this.Theme);
});
}
}, ref unregisterEvents);
HashSet<IObject3DEditor> mappedEditors;
objectEditorsByType = new Dictionary<Type, HashSet<IObject3DEditor>>();
// Initialize plugins, passing the MatterControl assembly as the only non-dll instance
//PluginFinder.Initialize(Assembly.GetExecutingAssembly());
foreach (IObject3DEditor editor in PluginFinder.CreateInstancesOf<IObject3DEditor>())
{
foreach (Type type in editor.SupportedTypes())
{
if (!objectEditorsByType.TryGetValue(type, out mappedEditors))
{
mappedEditors = new HashSet<IObject3DEditor>();
objectEditorsByType.Add(type, mappedEditors);
}
mappedEditors.Add(editor);
}
}
}
public void Connection_ErrorReported(object sender, EventArgs e)
{
var foundStringEventArgs = e as FoundStringEventArgs;
if (foundStringEventArgs != null)
{
string message = "Your printer is reporting a HARDWARE ERROR and has been paused. Check the error and cancel the print if required.".Localize()
+ "\n"
+ "\n"
+ "Error Reported".Localize() + ":"
+ $" \"{foundStringEventArgs.LineToCheck}\".";
UiThread.RunOnIdle(() =>
StyledMessageBox.ShowMessageBox((clickedOk) =>
{
if (clickedOk && this.ActivePrinter.Connection.PrinterIsPaused)
{
this.ActivePrinter.Connection.Resume();
}
}, message, "Printer Hardware Error".Localize(), StyledMessageBox.MessageType.YES_NO, "Resume".Localize(), "OK".Localize())
);
}
}
public void Connection_TemporarilyHoldingTemp(object sender, EventArgs e)
{
if (sender is PrinterConnection printerConnection)
{
if (printerConnection.AnyHeatIsOn)
{
var paused = false;
@ -1429,63 +1486,6 @@ namespace MatterHackers.MatterControl
StopToolTip = "Immediately turn off heaters".Localize()
});
}
}, ref unregisterEvents);
PrinterConnection.ErrorReported.RegisterEvent((s, e) =>
{
var foundStringEventArgs = e as FoundStringEventArgs;
if (foundStringEventArgs != null)
{
string message = "Your printer is reporting a HARDWARE ERROR and has been paused. Check the error and cancel the print if required.".Localize()
+ "\n"
+ "\n"
+ "Error Reported".Localize() + ":"
+ $" \"{foundStringEventArgs.LineToCheck}\".";
UiThread.RunOnIdle(() =>
StyledMessageBox.ShowMessageBox((clickedOk) =>
{
if (clickedOk && this.ActivePrinter.Connection.PrinterIsPaused)
{
this.ActivePrinter.Connection.Resume();
}
}, message, "Printer Hardware Error".Localize(), StyledMessageBox.MessageType.YES_NO, "Resume".Localize(), "OK".Localize())
);
}
}, ref unregisterEvent);
this.InitializeLibrary();
PrinterConnection.AnyConnectionSucceeded.RegisterEvent((s, e) =>
{
// run the print leveling wizard if we need to for this printer
var printer = this.ActivePrinters.Where(p => p.Connection == s).FirstOrDefault();
if (printer != null)
{
UiThread.RunOnIdle(() =>
{
this.RunAnyRequiredPrinterSetup(printer, this.Theme);
});
}
}, ref unregisterEvents);
HashSet<IObject3DEditor> mappedEditors;
objectEditorsByType = new Dictionary<Type, HashSet<IObject3DEditor>>();
// Initialize plugins, passing the MatterControl assembly as the only non-dll instance
//PluginFinder.Initialize(Assembly.GetExecutingAssembly());
foreach (IObject3DEditor editor in PluginFinder.CreateInstancesOf<IObject3DEditor>())
{
foreach (Type type in editor.SupportedTypes())
{
if (!objectEditorsByType.TryGetValue(type, out mappedEditors))
{
mappedEditors = new HashSet<IObject3DEditor>();
objectEditorsByType.Add(type, mappedEditors);
}
mappedEditors.Add(editor);
}
}
}

View file

@ -879,7 +879,7 @@ namespace MatterHackers.MatterControl
}
}
public class PrinterConfig
public class PrinterConfig : IDisposable
{
public BedConfig Bed { get; }
@ -897,6 +897,10 @@ namespace MatterHackers.MatterControl
this.Bed = new BedConfig(ApplicationController.Instance.Library.PlatingHistory, this);
this.ViewState = new PrinterViewState();
this.Connection = new PrinterConnection(this);
// Need a way to hook up all the callbacks that exist on a given connection
DoConnectionBinding();
this.Settings = settings;
this.Settings.printer = this;
@ -934,6 +938,17 @@ namespace MatterHackers.MatterControl
this.Connection.ReadLineReplacementString = this.Settings.GetValue(SettingsKey.read_regex);
}
private void DoConnectionBinding()
{
// show countdown for turning off heat if required
this.Connection.TemporarilyHoldingTemp += ApplicationController.Instance.Connection_TemporarilyHoldingTemp;
this.Disposed += (s, e) => this.Connection.TemporarilyHoldingTemp -= ApplicationController.Instance.Connection_TemporarilyHoldingTemp;
// hook up error reporting feedback
this.Connection.ErrorReported += ApplicationController.Instance.Connection_ErrorReported;
this.Disposed += (s, e) => this.Connection.ErrorReported -= ApplicationController.Instance.Connection_ErrorReported;
}
public PrinterViewState ViewState { get; }
private PrinterSettings _settings = PrinterSettings.Empty;
@ -1012,6 +1027,8 @@ namespace MatterHackers.MatterControl
}
}
public event EventHandler Disposed;
internal void SwapToSettings(PrinterSettings printerSettings)
{
_settings = printerSettings;
@ -1087,6 +1104,11 @@ namespace MatterHackers.MatterControl
}
}
}
public void Dispose()
{
Disposed?.Invoke(this, null);
}
}
public class View3DConfig : INotifyPropertyChanged