2015-04-08 15:20:10 -07:00
using MatterHackers.Agg ;
2014-09-19 19:17:12 -07:00
using MatterHackers.Agg.ImageProcessing ;
using MatterHackers.Agg.PlatformAbstract ;
using MatterHackers.Agg.UI ;
using MatterHackers.Localizations ;
using MatterHackers.MatterControl.ConfigurationPage.PrintLeveling ;
using MatterHackers.MatterControl.CustomWidgets ;
using MatterHackers.MatterControl.EeProm ;
2015-04-08 15:20:10 -07:00
using MatterHackers.MatterControl.PrinterCommunication ;
using MatterHackers.MatterControl.SlicerConfiguration ;
using System ;
using System.IO ;
2014-09-19 19:17:12 -07:00
namespace MatterHackers.MatterControl.ConfigurationPage
{
2015-04-08 15:20:10 -07:00
public class HardwareSettingsWidget : SettingsViewBase
{
private Button openGcodeTerminalButton ;
private Button openCameraButton ;
2014-09-19 19:17:12 -07:00
2015-04-08 15:20:10 -07:00
private DisableableWidget eePromControlsContainer ;
private DisableableWidget terminalCommunicationsContainer ;
private DisableableWidget printLevelingContainer ;
2014-09-23 16:11:06 -07:00
2015-04-08 15:20:10 -07:00
private event EventHandler unregisterEvents ;
2014-09-23 16:11:06 -07:00
2015-04-08 15:20:10 -07:00
public HardwareSettingsWidget ( )
2014-10-09 14:33:55 -07:00
: base ( LocalizedString . Get ( "Hardware Settings" ) )
2015-04-08 15:20:10 -07:00
{
eePromControlsContainer = new DisableableWidget ( ) ;
eePromControlsContainer . AddChild ( GetEEPromControl ( ) ) ;
terminalCommunicationsContainer = new DisableableWidget ( ) ;
2014-10-10 12:50:59 -07:00
terminalCommunicationsContainer . AddChild ( GetGcodeTerminalControl ( ) ) ;
2014-09-23 16:11:06 -07:00
2015-01-23 08:03:59 -08:00
printLevelingContainer = new DisableableWidget ( ) ;
if ( ! ActiveSliceSettings . Instance . HasHardwareLeveling ( ) )
{
printLevelingContainer . AddChild ( GetAutoLevelControl ( ) ) ;
mainContainer . AddChild ( printLevelingContainer ) ;
}
2015-04-08 15:20:10 -07:00
mainContainer . AddChild ( new HorizontalLine ( separatorLineColor ) ) ;
mainContainer . AddChild ( eePromControlsContainer ) ;
2014-10-10 12:50:59 -07:00
mainContainer . AddChild ( new HorizontalLine ( separatorLineColor ) ) ;
mainContainer . AddChild ( terminalCommunicationsContainer ) ;
2015-01-11 18:32:43 -08:00
DisableableWidget cameraContainer = new DisableableWidget ( ) ;
cameraContainer . AddChild ( GetCameraControl ( ) ) ;
2014-09-19 19:17:12 -07:00
2015-01-11 18:32:43 -08:00
if ( ApplicationSettings . Instance . get ( "HardwareHasCamera" ) = = "true" )
{
mainContainer . AddChild ( new HorizontalLine ( separatorLineColor ) ) ;
mainContainer . AddChild ( cameraContainer ) ;
}
2014-09-19 19:17:12 -07:00
2015-04-08 15:20:10 -07:00
AddChild ( mainContainer ) ;
AddHandlers ( ) ;
SetVisibleControls ( ) ;
}
private EditLevelingSettingsWindow editLevelingSettingsWindow ;
private TextWidget printLevelingStatusLabel ;
private FlowLayoutWidget GetAutoLevelControl ( )
{
FlowLayoutWidget buttonRow = new FlowLayoutWidget ( ) ;
buttonRow . HAnchor = HAnchor . ParentLeftRight ;
buttonRow . Margin = new BorderDouble ( 0 , 4 ) ;
TextWidget notificationSettingsLabel = new TextWidget ( "Automatic Print Leveling" ) ;
notificationSettingsLabel . AutoExpandBoundsToText = true ;
notificationSettingsLabel . TextColor = ActiveTheme . Instance . PrimaryTextColor ;
notificationSettingsLabel . VAnchor = VAnchor . ParentCenter ;
Button editButton = textImageButtonFactory . GenerateEditButton ( ) ;
editButton . VAnchor = Agg . UI . VAnchor . ParentCenter ;
editButton . Click + = ( sender , e ) = >
{
UiThread . RunOnIdle ( ( state ) = >
{
if ( editLevelingSettingsWindow = = null )
{
editLevelingSettingsWindow = new EditLevelingSettingsWindow ( ) ;
editLevelingSettingsWindow . Closed + = ( sender2 , e2 ) = >
{
editLevelingSettingsWindow = null ;
} ;
}
else
{
editLevelingSettingsWindow . BringToFront ( ) ;
}
} ) ;
} ;
Button runPrintLevelingButton = textImageButtonFactory . Generate ( "Configure" . Localize ( ) . ToUpper ( ) ) ;
runPrintLevelingButton . Margin = new BorderDouble ( left : 6 ) ;
runPrintLevelingButton . VAnchor = VAnchor . ParentCenter ;
runPrintLevelingButton . Click + = ( sender , e ) = >
{
UiThread . RunOnIdle ( ( state ) = >
{
LevelWizardBase . ShowPrintLevelWizard ( LevelWizardBase . RuningState . UserRequestedCalibration ) ;
} ) ;
} ;
Agg . Image . ImageBuffer levelingImage = StaticData . Instance . LoadIcon ( Path . Combine ( "PrintStatusControls" , "leveling-24x24.png" ) ) ;
if ( ! ActiveTheme . Instance . IsDarkTheme )
{
InvertLightness . DoInvertLightness ( levelingImage ) ;
}
ImageWidget levelingIcon = new ImageWidget ( levelingImage ) ;
levelingIcon . Margin = new BorderDouble ( right : 6 ) ;
2014-09-23 16:11:06 -07:00
2015-04-22 08:57:55 -07:00
CheckBox printLevelingSwitch = ImageButtonFactory . CreateToggleSwitch ( ActivePrinterProfile . Instance . DoPrintLeveling ) ;
2015-04-22 08:43:09 -07:00
printLevelingSwitch . VAnchor = VAnchor . ParentCenter ;
printLevelingSwitch . Margin = new BorderDouble ( left : 16 ) ;
printLevelingSwitch . CheckedStateChanged + = ( sender , e ) = >
2015-03-19 10:25:34 -07:00
{
2015-04-22 08:43:09 -07:00
ActivePrinterProfile . Instance . DoPrintLeveling = printLevelingSwitch . Checked ;
2015-03-19 10:25:34 -07:00
} ;
2014-09-23 16:11:06 -07:00
2015-04-08 15:20:10 -07:00
printLevelingStatusLabel = new TextWidget ( "" ) ;
2014-09-23 16:11:06 -07:00
printLevelingStatusLabel . AutoExpandBoundsToText = true ;
printLevelingStatusLabel . TextColor = ActiveTheme . Instance . PrimaryTextColor ;
printLevelingStatusLabel . VAnchor = VAnchor . ParentCenter ;
2015-04-08 15:20:10 -07:00
GuiWidget hSpacer = new GuiWidget ( ) ;
2014-09-23 16:11:06 -07:00
hSpacer . HAnchor = HAnchor . ParentLeftRight ;
2015-04-08 15:20:10 -07:00
ActivePrinterProfile . Instance . DoPrintLevelingChanged . RegisterEvent ( ( sender , e ) = >
{
SetPrintLevelButtonVisiblity ( ) ;
2015-04-22 08:43:09 -07:00
printLevelingSwitch . Checked = ActivePrinterProfile . Instance . DoPrintLeveling ;
2015-04-08 15:20:10 -07:00
} , ref unregisterEvents ) ;
2014-09-23 16:11:06 -07:00
2015-04-08 15:20:10 -07:00
buttonRow . AddChild ( levelingIcon ) ;
buttonRow . AddChild ( printLevelingStatusLabel ) ;
buttonRow . AddChild ( editButton ) ;
buttonRow . AddChild ( new HorizontalSpacer ( ) ) ;
buttonRow . AddChild ( runPrintLevelingButton ) ;
2015-04-22 08:43:09 -07:00
buttonRow . AddChild ( printLevelingSwitch ) ;
2015-03-26 09:14:33 -07:00
2015-04-08 15:20:10 -07:00
SetPrintLevelButtonVisiblity ( ) ;
return buttonRow ;
}
2014-09-19 19:17:12 -07:00
2015-04-08 15:20:10 -07:00
public override void OnClosed ( EventArgs e )
{
if ( unregisterEvents ! = null )
{
unregisterEvents ( this , null ) ;
}
base . OnClosed ( e ) ;
}
2015-03-19 10:25:34 -07:00
2015-01-11 18:32:43 -08:00
private FlowLayoutWidget GetCameraControl ( )
{
FlowLayoutWidget buttonRow = new FlowLayoutWidget ( ) ;
buttonRow . HAnchor = HAnchor . ParentLeftRight ;
2015-04-08 15:20:10 -07:00
buttonRow . Margin = new BorderDouble ( 0 , 4 ) ;
2015-01-11 18:32:43 -08:00
Agg . Image . ImageBuffer cameraIconImage = StaticData . Instance . LoadIcon ( Path . Combine ( "PrintStatusControls" , "camera-24x24.png" ) ) ;
if ( ! ActiveTheme . Instance . IsDarkTheme )
{
InvertLightness . DoInvertLightness ( cameraIconImage ) ;
}
ImageWidget cameraIcon = new ImageWidget ( cameraIconImage ) ;
2015-03-26 09:14:33 -07:00
cameraIcon . Margin = new BorderDouble ( right : 6 ) ;
2015-01-11 18:32:43 -08:00
2015-04-07 11:23:23 -07:00
TextWidget cameraLabel = new TextWidget ( "Camera Monitoring" ) ;
2015-03-26 09:14:33 -07:00
cameraLabel . AutoExpandBoundsToText = true ;
cameraLabel . TextColor = ActiveTheme . Instance . PrimaryTextColor ;
cameraLabel . VAnchor = VAnchor . ParentCenter ;
2015-01-11 18:32:43 -08:00
openCameraButton = textImageButtonFactory . Generate ( "Preview" . Localize ( ) . ToUpper ( ) ) ;
openCameraButton . Click + = new EventHandler ( openCameraPreview_Click ) ;
2015-04-08 15:20:10 -07:00
openCameraButton . Margin = new BorderDouble ( left : 6 ) ;
2015-01-11 18:32:43 -08:00
buttonRow . AddChild ( cameraIcon ) ;
2015-03-26 09:14:33 -07:00
buttonRow . AddChild ( cameraLabel ) ;
2015-01-11 18:32:43 -08:00
buttonRow . AddChild ( new HorizontalSpacer ( ) ) ;
2015-03-26 09:14:33 -07:00
buttonRow . AddChild ( openCameraButton ) ;
2015-04-08 15:20:10 -07:00
#if __ANDROID__
2015-03-26 09:14:33 -07:00
GuiWidget publishImageSwitchContainer = new FlowLayoutWidget ( ) ;
publishImageSwitchContainer . VAnchor = VAnchor . ParentCenter ;
publishImageSwitchContainer . Margin = new BorderDouble ( left : 16 ) ;
2015-04-22 08:57:55 -07:00
CheckBox toggleSwitch = ImageButtonFactory . CreateToggleSwitch ( PrinterSettings . Instance . get ( "PublishBedImage" ) = = "true" ) ;
toggleSwitch . CheckedStateChanged + = ( sender , e ) = >
2015-03-18 18:47:40 -07:00
{
2015-04-22 08:57:55 -07:00
CheckBox thisControl = sender as CheckBox ;
PrinterSettings . Instance . set ( "PublishBedImage" , thisControl . Checked ? "true" : "false" ) ;
2015-03-18 18:47:40 -07:00
} ;
2015-04-28 12:38:11 -07:00
publishImageSwitchContainer . AddChild ( toggleSwitch ) ;
2015-03-26 09:14:33 -07:00
publishImageSwitchContainer . SetBoundsToEncloseChildren ( ) ;
buttonRow . AddChild ( publishImageSwitchContainer ) ;
2015-03-18 18:47:40 -07:00
#endif
2015-01-11 18:32:43 -08:00
return buttonRow ;
}
2015-04-08 15:20:10 -07:00
private FlowLayoutWidget GetGcodeTerminalControl ( )
2014-10-10 12:50:59 -07:00
{
FlowLayoutWidget buttonRow = new FlowLayoutWidget ( ) ;
buttonRow . HAnchor = HAnchor . ParentLeftRight ;
2015-04-08 15:20:10 -07:00
buttonRow . Margin = new BorderDouble ( 0 , 4 ) ;
2014-10-10 12:50:59 -07:00
2015-04-08 15:20:10 -07:00
Agg . Image . ImageBuffer terminalSettingsImage = StaticData . Instance . LoadIcon ( Path . Combine ( "PrintStatusControls" , "terminal-24x24.png" ) ) ;
if ( ! ActiveTheme . Instance . IsDarkTheme )
{
InvertLightness . DoInvertLightness ( terminalSettingsImage ) ;
}
2014-12-17 08:41:12 -08:00
2015-04-08 15:20:10 -07:00
ImageWidget terminalIcon = new ImageWidget ( terminalSettingsImage ) ;
terminalIcon . Margin = new BorderDouble ( right : 6 , bottom : 6 ) ;
2014-12-17 08:41:12 -08:00
2014-10-10 12:50:59 -07:00
TextWidget gcodeTerminalLabel = new TextWidget ( "Gcode Terminal" ) ;
gcodeTerminalLabel . AutoExpandBoundsToText = true ;
gcodeTerminalLabel . TextColor = ActiveTheme . Instance . PrimaryTextColor ;
gcodeTerminalLabel . VAnchor = VAnchor . ParentCenter ;
openGcodeTerminalButton = textImageButtonFactory . Generate ( "Show Terminal" . Localize ( ) . ToUpper ( ) ) ;
2014-10-24 13:41:13 -07:00
openGcodeTerminalButton . Click + = new EventHandler ( openGcodeTerminalButton_Click ) ;
2014-10-10 12:50:59 -07:00
2015-04-08 15:20:10 -07:00
buttonRow . AddChild ( terminalIcon ) ;
buttonRow . AddChild ( gcodeTerminalLabel ) ;
2014-10-10 12:50:59 -07:00
buttonRow . AddChild ( new HorizontalSpacer ( ) ) ;
buttonRow . AddChild ( openGcodeTerminalButton ) ;
return buttonRow ;
}
2015-04-08 15:20:10 -07:00
private static EePromMarlinWindow openEePromMarlinWidget = null ;
private static EePromRepetierWindow openEePromRepetierWidget = null ;
private string noEepromMappingMessage = "Oops! There is no eeprom mapping for your printer's firmware." . Localize ( ) + "\n\n" + "You may need to wait a minute for your printer to finish initializing." . Localize ( ) ;
private string noEepromMappingTitle = "Warning - No EEProm Mapping" . Localize ( ) ;
private FlowLayoutWidget GetEEPromControl ( )
{
FlowLayoutWidget buttonRow = new FlowLayoutWidget ( ) ;
buttonRow . HAnchor = HAnchor . ParentLeftRight ;
buttonRow . Margin = new BorderDouble ( 0 , 4 ) ;
TextWidget notificationSettingsLabel = new TextWidget ( "EEProm Settings" . Localize ( ) ) ;
notificationSettingsLabel . AutoExpandBoundsToText = true ;
notificationSettingsLabel . TextColor = ActiveTheme . Instance . PrimaryTextColor ;
notificationSettingsLabel . VAnchor = VAnchor . ParentCenter ;
Agg . Image . ImageBuffer eePromImage = StaticData . Instance . LoadIcon ( Path . Combine ( "PrintStatusControls" , "leveling-24x24.png" ) ) ;
if ( ! ActiveTheme . Instance . IsDarkTheme )
{
InvertLightness . DoInvertLightness ( eePromImage ) ;
}
ImageWidget eePromIcon = new ImageWidget ( eePromImage ) ;
eePromIcon . Margin = new BorderDouble ( right : 6 ) ;
Button configureEePromButton = textImageButtonFactory . Generate ( "Configure" . Localize ( ) . ToUpper ( ) ) ;
2015-02-20 12:05:44 -08:00
configureEePromButton . Click + = new EventHandler ( configureEePromButton_Click ) ;
2015-04-08 15:20:10 -07:00
//buttonRow.AddChild(eePromIcon);
buttonRow . AddChild ( notificationSettingsLabel ) ;
buttonRow . AddChild ( new HorizontalSpacer ( ) ) ;
buttonRow . AddChild ( configureEePromButton ) ;
return buttonRow ;
}
private void AddHandlers ( )
{
PrinterConnectionAndCommunication . Instance . CommunicationStateChanged . RegisterEvent ( onPrinterStatusChanged , ref unregisterEvents ) ;
PrinterConnectionAndCommunication . Instance . EnableChanged . RegisterEvent ( onPrinterStatusChanged , ref unregisterEvents ) ;
}
private void openCameraPreview_Click ( object sender , EventArgs e )
2015-01-11 18:32:43 -08:00
{
2015-01-13 10:56:20 -08:00
MatterControlApplication . Instance . OpenCameraPreview ( ) ;
2015-01-11 18:32:43 -08:00
}
2015-04-08 15:20:10 -07:00
private void configureEePromButton_Click ( object sender , EventArgs mouseEvent )
{
UiThread . RunOnIdle ( ( state ) = >
{
2014-09-23 16:11:06 -07:00
#if false // This is to force the creation of the repetier window for testing when we don't have repetier firmware.
new MatterHackers . MatterControl . EeProm . EePromRepetierWidget ( ) ;
#else
2015-04-08 15:20:10 -07:00
switch ( PrinterConnectionAndCommunication . Instance . FirmwareType )
{
case PrinterConnectionAndCommunication . FirmwareTypes . Repetier :
if ( openEePromRepetierWidget ! = null )
{
openEePromRepetierWidget . BringToFront ( ) ;
}
else
{
openEePromRepetierWidget = new EePromRepetierWindow ( ) ;
openEePromRepetierWidget . Closed + = ( RepetierWidget , RepetierEvent ) = >
{
openEePromRepetierWidget = null ;
} ;
}
break ;
case PrinterConnectionAndCommunication . FirmwareTypes . Marlin :
if ( openEePromMarlinWidget ! = null )
{
openEePromMarlinWidget . BringToFront ( ) ;
}
else
{
openEePromMarlinWidget = new EePromMarlinWindow ( ) ;
openEePromMarlinWidget . Closed + = ( marlinWidget , marlinEvent ) = >
{
openEePromMarlinWidget = null ;
} ;
}
break ;
default :
StyledMessageBox . ShowMessageBox ( null , noEepromMappingMessage , noEepromMappingTitle , StyledMessageBox . MessageType . OK ) ;
break ;
}
2014-09-23 16:11:06 -07:00
#endif
2015-04-08 15:20:10 -07:00
} ) ;
}
private void openGcodeTerminalButton_Click ( object sender , EventArgs mouseEvent )
2014-10-10 12:50:59 -07:00
{
UiThread . RunOnIdle ( ( state ) = >
2015-01-21 11:45:42 -08:00
{
TerminalWindow . Show ( ) ;
} ) ;
2014-10-10 12:50:59 -07:00
}
2015-04-08 15:20:10 -07:00
private void onPrinterStatusChanged ( object sender , EventArgs e )
{
SetVisibleControls ( ) ;
this . Invalidate ( ) ;
}
private void SetPrintLevelButtonVisiblity ( )
{
if ( ActivePrinterProfile . Instance . DoPrintLeveling )
{
printLevelingStatusLabel . Text = LocalizedString . Get ( "Automatic Print Leveling (enabled)" ) ;
}
else
{
printLevelingStatusLabel . Text = LocalizedString . Get ( "Automatic Print Leveling (disabled)" ) ;
}
}
private void SetVisibleControls ( )
{
if ( ActivePrinterProfile . Instance . ActivePrinter = = null )
{
// no printer selected
eePromControlsContainer . SetEnableLevel ( DisableableWidget . EnableLevel . Disabled ) ;
terminalCommunicationsContainer . SetEnableLevel ( DisableableWidget . EnableLevel . Enabled ) ;
printLevelingContainer . SetEnableLevel ( DisableableWidget . EnableLevel . Disabled ) ;
//cloudMonitorContainer.SetEnableLevel(DisableableWidget.EnableLevel.Disabled);
}
else // we at least have a printer selected
{
//cloudMonitorContainer.SetEnableLevel(DisableableWidget.EnableLevel.Enabled);
switch ( PrinterConnectionAndCommunication . Instance . CommunicationState )
{
case PrinterConnectionAndCommunication . CommunicationStates . Disconnecting :
case PrinterConnectionAndCommunication . CommunicationStates . ConnectionLost :
case PrinterConnectionAndCommunication . CommunicationStates . Disconnected :
case PrinterConnectionAndCommunication . CommunicationStates . AttemptingToConnect :
case PrinterConnectionAndCommunication . CommunicationStates . FailedToConnect :
eePromControlsContainer . SetEnableLevel ( DisableableWidget . EnableLevel . Disabled ) ;
printLevelingContainer . SetEnableLevel ( DisableableWidget . EnableLevel . Enabled ) ;
terminalCommunicationsContainer . SetEnableLevel ( DisableableWidget . EnableLevel . Enabled ) ;
break ;
case PrinterConnectionAndCommunication . CommunicationStates . FinishedPrint :
case PrinterConnectionAndCommunication . CommunicationStates . Connected :
eePromControlsContainer . SetEnableLevel ( DisableableWidget . EnableLevel . Enabled ) ;
printLevelingContainer . SetEnableLevel ( DisableableWidget . EnableLevel . Enabled ) ;
terminalCommunicationsContainer . SetEnableLevel ( DisableableWidget . EnableLevel . Enabled ) ;
break ;
case PrinterConnectionAndCommunication . CommunicationStates . PrintingFromSd :
eePromControlsContainer . SetEnableLevel ( DisableableWidget . EnableLevel . Disabled ) ;
printLevelingContainer . SetEnableLevel ( DisableableWidget . EnableLevel . Disabled ) ;
terminalCommunicationsContainer . SetEnableLevel ( DisableableWidget . EnableLevel . Enabled ) ;
break ;
case PrinterConnectionAndCommunication . CommunicationStates . PreparingToPrint :
case PrinterConnectionAndCommunication . CommunicationStates . Printing :
switch ( PrinterConnectionAndCommunication . Instance . PrintingState )
{
case PrinterConnectionAndCommunication . DetailedPrintingState . HomingAxis :
case PrinterConnectionAndCommunication . DetailedPrintingState . HeatingBed :
case PrinterConnectionAndCommunication . DetailedPrintingState . HeatingExtruder :
case PrinterConnectionAndCommunication . DetailedPrintingState . Printing :
eePromControlsContainer . SetEnableLevel ( DisableableWidget . EnableLevel . Disabled ) ;
printLevelingContainer . SetEnableLevel ( DisableableWidget . EnableLevel . Disabled ) ;
terminalCommunicationsContainer . SetEnableLevel ( DisableableWidget . EnableLevel . Enabled ) ;
break ;
default :
throw new NotImplementedException ( ) ;
}
break ;
case PrinterConnectionAndCommunication . CommunicationStates . Paused :
eePromControlsContainer . SetEnableLevel ( DisableableWidget . EnableLevel . Enabled ) ;
printLevelingContainer . SetEnableLevel ( DisableableWidget . EnableLevel . Disabled ) ;
terminalCommunicationsContainer . SetEnableLevel ( DisableableWidget . EnableLevel . Enabled ) ;
break ;
default :
throw new NotImplementedException ( ) ;
}
}
}
}
2014-09-19 19:17:12 -07:00
}