mattercontrol/MatterControlLib/PrinterControls/ManualPrinterControls.cs
Lars Brubaker daec1a0420 added close to terminal and settings (default to close)
added ICloseableTab
changed to selected tab key
added keys to tool tabs
2018-09-07 14:49:35 -07:00

270 lines
9.1 KiB
C#

/*
Copyright (c) 2017, Lars Brubaker, John Lewin
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 MatterHackers.Agg;
using MatterHackers.Agg.UI;
using MatterHackers.MatterControl.CustomWidgets;
using MatterHackers.MatterControl.PrinterCommunication;
using MatterHackers.MatterControl.PrinterControls;
using MatterHackers.MatterControl.SlicerConfiguration;
namespace MatterHackers.MatterControl
{
public static class EnabledWidgetExtensions
{
public static void SetEnabled(this GuiWidget guiWidget, bool enabled)
{
guiWidget.Enabled = enabled;
}
}
public class ManualPrinterControls : ScrollableWidget, ICloseableTab
{
public static RootedObjectEventHandler AddPluginControls = new RootedObjectEventHandler();
private static bool pluginsQueuedToAdd = false;
private GuiWidget fanControlsContainer;
private GuiWidget macroControlsContainer;
private GuiWidget tuningAdjustmentControlsContainer;
private MovementControls movementControlsContainer;
private GuiWidget calibrationControlsContainer;
private EventHandler unregisterEvents;
private ThemeConfig theme;
private PrinterConfig printer;
private FlowLayoutWidget column;
public ManualPrinterControls(PrinterConfig printer, ThemeConfig theme)
{
this.theme = theme;
this.printer = printer;
this.ScrollArea.HAnchor |= HAnchor.Stretch;
this.AnchorAll();
this.AutoScroll = true;
this.HAnchor = HAnchor.Stretch;
this.VAnchor = VAnchor.Stretch;
this.Name = "ManualPrinterControls";
int headingPointSize = theme.H1PointSize;
column = new FlowLayoutWidget(FlowDirection.TopToBottom)
{
HAnchor = HAnchor.MaxFitOrStretch,
VAnchor = VAnchor.Fit,
Name = "ManualPrinterControls.ControlsContainer",
Margin = new BorderDouble(0)
};
this.AddChild(column);
movementControlsContainer = this.RegisterSection(MovementControls.CreateSection(printer, theme)) as MovementControls;
if (!printer.Settings.GetValue<bool>(SettingsKey.has_hardware_leveling))
{
calibrationControlsContainer = this.RegisterSection(CalibrationControls.CreateSection(printer, theme));
}
macroControlsContainer = this.RegisterSection(MacroControls.CreateSection(printer, theme));
if (printer.Settings.GetValue<bool>(SettingsKey.has_fan))
{
fanControlsContainer = this.RegisterSection(FanControls.CreateSection(printer, theme));
}
#if !__ANDROID__
this.RegisterSection(PowerControls.CreateSection(printer, theme));
#endif
tuningAdjustmentControlsContainer = this.RegisterSection(AdjustmentControls.CreateSection(printer, theme));
// HACK: this is a hack to make the layout engine fire again for this control
UiThread.RunOnIdle(() => tuningAdjustmentControlsContainer.Width = tuningAdjustmentControlsContainer.Width + 1);
printer.Connection.CommunicationStateChanged.RegisterEvent(onPrinterStatusChanged, ref unregisterEvents);
printer.Connection.EnableChanged.RegisterEvent(onPrinterStatusChanged, ref unregisterEvents);
SetVisibleControls();
}
public GuiWidget RegisterSection(SectionWidget sectionWidget)
{
// Section not active due to constraints
if (sectionWidget == null)
{
return null;
}
theme.ApplyBoxStyle(sectionWidget);
sectionWidget.ContentPanel.Padding = new BorderDouble(10, 10, 10, 0);
column.AddChild(sectionWidget);
// Disable borders on all SettingsRow children in control panels
foreach(var settingsRow in sectionWidget.ContentPanel.Descendants<SettingsRow>())
{
settingsRow.BorderColor = Color.Transparent;
}
// Return the panel widget rather than the source sectionWidget
return sectionWidget.ContentPanel;
}
public override void OnLoad(EventArgs args)
{
if (!pluginsQueuedToAdd && printer.Settings.GetValue(SettingsKey.include_firmware_updater) == "Simple Arduino")
{
UiThread.RunOnIdle(() =>
{
AddPluginControls.CallEvents(this, null);
pluginsQueuedToAdd = false;
});
pluginsQueuedToAdd = true;
}
base.OnLoad(args);
}
public override void OnClosed(EventArgs e)
{
unregisterEvents?.Invoke(this, null);
base.OnClosed(e);
}
private void onPrinterStatusChanged(object sender, EventArgs e)
{
SetVisibleControls();
UiThread.RunOnIdle(this.Invalidate);
}
private void SetVisibleControls()
{
if (!printer.Settings.PrinterSelected)
{
movementControlsContainer?.SetEnabled(false);
fanControlsContainer?.SetEnabled(false);
macroControlsContainer?.SetEnabled(false);
calibrationControlsContainer?.SetEnabled(false);
tuningAdjustmentControlsContainer?.SetEnabled(false);
}
else // we at least have a printer selected
{
switch (printer.Connection.CommunicationState)
{
case CommunicationStates.Disconnecting:
case CommunicationStates.ConnectionLost:
case CommunicationStates.Disconnected:
case CommunicationStates.AttemptingToConnect:
case CommunicationStates.FailedToConnect:
movementControlsContainer?.SetEnabled(false);
fanControlsContainer?.SetEnabled(false);
macroControlsContainer?.SetEnabled(false);
tuningAdjustmentControlsContainer?.SetEnabled(false);
calibrationControlsContainer?.SetEnabled(false);
foreach (var widget in movementControlsContainer.DisableableWidgets)
{
widget?.SetEnabled(true);
}
movementControlsContainer?.jogControls.SetEnabledLevels(false, false);
break;
case CommunicationStates.FinishedPrint:
case CommunicationStates.Connected:
movementControlsContainer?.SetEnabled(true);
fanControlsContainer?.SetEnabled(true);
macroControlsContainer?.SetEnabled(true);
tuningAdjustmentControlsContainer?.SetEnabled(true);
calibrationControlsContainer?.SetEnabled(true);
foreach (var widget in movementControlsContainer.DisableableWidgets)
{
widget?.SetEnabled(true);
}
movementControlsContainer?.jogControls.SetEnabledLevels(enableBabysteppingMode: false, enableEControls: true);
break;
case CommunicationStates.PrintingFromSd:
movementControlsContainer?.SetEnabled(false);
fanControlsContainer?.SetEnabled(true);
macroControlsContainer?.SetEnabled(false);
tuningAdjustmentControlsContainer?.SetEnabled(false);
calibrationControlsContainer?.SetEnabled(false);
break;
case CommunicationStates.PreparingToPrint:
case CommunicationStates.Printing:
switch (printer.Connection.DetailedPrintingState)
{
case DetailedPrintingState.HomingAxis:
case DetailedPrintingState.HeatingBed:
case DetailedPrintingState.HeatingExtruder:
case DetailedPrintingState.Printing:
fanControlsContainer?.SetEnabled(true);
macroControlsContainer?.SetEnabled(false);
tuningAdjustmentControlsContainer?.SetEnabled(true);
calibrationControlsContainer?.SetEnabled(false);
foreach (var widget in movementControlsContainer.DisableableWidgets)
{
widget?.SetEnabled(false);
}
movementControlsContainer?.jogControls.SetEnabledLevels(enableBabysteppingMode: true, enableEControls: false);
break;
default:
throw new NotImplementedException();
}
break;
case CommunicationStates.Paused:
movementControlsContainer?.SetEnabled(true);
fanControlsContainer?.SetEnabled(true);
macroControlsContainer?.SetEnabled(true);
tuningAdjustmentControlsContainer?.SetEnabled(true);
calibrationControlsContainer?.SetEnabled(true);
foreach (var widget in movementControlsContainer.DisableableWidgets)
{
widget?.SetEnabled(true);
}
movementControlsContainer?.jogControls.SetEnabledLevels(enableBabysteppingMode: false, enableEControls: true);
break;
default:
throw new NotImplementedException();
}
}
}
}
}