2015-04-08 15:20:10 -07:00
using MatterHackers.Agg ;
2016-06-02 16:21:32 -07:00
using MatterHackers.Agg.PlatformAbstract ;
2014-01-29 19:09:30 -08:00
using MatterHackers.Agg.UI ;
using MatterHackers.Localizations ;
2016-04-25 13:35:36 -07:00
using MatterHackers.MatterControl.CustomWidgets ;
2014-06-11 14:52:58 -07:00
using MatterHackers.MatterControl.PrinterCommunication ;
2016-04-18 11:31:31 -07:00
using MatterHackers.MatterControl.SlicerConfiguration ;
2016-06-02 16:21:32 -07:00
using MatterHackers.SerialPortCommunication.FrostedSerial ;
2015-04-08 15:20:10 -07:00
using System ;
2016-06-02 16:21:32 -07:00
using System.Collections.Generic ;
using System.Linq ;
using System.Text.RegularExpressions ;
2016-09-23 16:24:43 -07:00
using MatterHackers.MatterControl.ConfigurationPage.PrintLeveling ;
2014-01-29 19:09:30 -08:00
namespace MatterHackers.MatterControl.PrinterControls.PrinterConnections
{
2016-06-08 09:31:26 -07:00
public class SetupStepComPortManual : ConnectionWizardPage
2015-04-08 15:20:10 -07:00
{
private Button nextButton ;
private Button connectButton ;
private Button refreshButton ;
private Button printerComPortHelpLink ;
2016-04-18 11:31:31 -07:00
2016-06-02 16:21:32 -07:00
private bool printerComPortIsAvailable = false ;
2015-04-08 15:20:10 -07:00
private TextWidget printerComPortHelpMessage ;
2016-04-18 11:31:31 -07:00
private TextWidget printerComPortError ;
2015-04-08 15:20:10 -07:00
2016-12-29 06:55:12 -08:00
private EventHandler unregisterEvents ;
2016-06-02 16:21:32 -07:00
protected List < SerialPortIndexRadioButton > SerialPortButtonsList = new List < SerialPortIndexRadioButton > ( ) ;
2016-04-18 11:31:31 -07:00
2016-06-07 15:45:50 -07:00
public SetupStepComPortManual ( )
2015-04-08 15:20:10 -07:00
{
FlowLayoutWidget printerComPortContainer = createComPortContainer ( ) ;
contentRow . AddChild ( printerComPortContainer ) ;
2014-01-29 19:09:30 -08:00
2016-04-18 11:31:31 -07:00
//Construct buttons
nextButton = textImageButtonFactory . Generate ( "Done" . Localize ( ) ) ;
nextButton . Click + = ( s , e ) = > UiThread . RunOnIdle ( Parent . Close ) ;
nextButton . Visible = false ;
2014-01-29 19:09:30 -08:00
2016-04-18 11:31:31 -07:00
connectButton = textImageButtonFactory . Generate ( "Connect" . Localize ( ) ) ;
connectButton . Click + = ConnectButton_Click ;
2014-01-29 19:09:30 -08:00
2016-04-18 11:31:31 -07:00
refreshButton = textImageButtonFactory . Generate ( "Refresh" . Localize ( ) ) ;
2016-12-09 12:09:28 -08:00
refreshButton . Click + = ( s , e ) = > UiThread . RunOnIdle ( WizardWindow . ChangeToPage < SetupStepComPortManual > ) ;
2015-04-08 15:20:10 -07:00
2016-04-18 11:31:31 -07:00
//Add buttons to buttonContainer
footerRow . AddChild ( nextButton ) ;
footerRow . AddChild ( connectButton ) ;
footerRow . AddChild ( refreshButton ) ;
footerRow . AddChild ( new HorizontalSpacer ( ) ) ;
2016-11-30 13:31:19 -08:00
footerRow . AddChild ( cancelButton ) ;
2015-04-08 15:20:10 -07:00
2016-04-18 11:31:31 -07:00
PrinterConnectionAndCommunication . Instance . CommunicationStateChanged . RegisterEvent ( onPrinterStatusChanged , ref unregisterEvents ) ;
}
2015-04-08 15:20:10 -07:00
2017-02-03 13:06:08 -08:00
public override void OnClosed ( ClosedEventArgs e )
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
unregisterEvents ? . Invoke ( this , null ) ;
2015-04-08 15:20:10 -07:00
base . OnClosed ( e ) ;
}
private FlowLayoutWidget createComPortContainer ( )
{
FlowLayoutWidget container = new FlowLayoutWidget ( FlowDirection . TopToBottom ) ;
container . Margin = new BorderDouble ( 0 ) ;
container . VAnchor = VAnchor . ParentBottomTop ;
BorderDouble elementMargin = new BorderDouble ( top : 3 ) ;
2014-01-29 19:09:30 -08:00
2017-01-04 07:23:30 -08:00
string serialPortLabel = "Serial Port" . Localize ( ) ;
2014-01-29 19:09:30 -08:00
string serialPortLabelFull = string . Format ( "{0}:" , serialPortLabel ) ;
TextWidget comPortLabel = new TextWidget ( serialPortLabelFull , 0 , 0 , 12 ) ;
2016-04-18 11:31:31 -07:00
comPortLabel . TextColor = ActiveTheme . Instance . PrimaryTextColor ;
2015-04-08 15:20:10 -07:00
comPortLabel . Margin = new BorderDouble ( 0 , 0 , 0 , 10 ) ;
comPortLabel . HAnchor = HAnchor . ParentLeftRight ;
2014-01-29 19:09:30 -08:00
2015-05-01 13:28:31 -07:00
FlowLayoutWidget serialPortContainer = new FlowLayoutWidget ( FlowDirection . TopToBottom ) ;
CreateSerialPortControls ( serialPortContainer , null ) ;
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
FlowLayoutWidget comPortMessageContainer = new FlowLayoutWidget ( ) ;
comPortMessageContainer . Margin = elementMargin ;
comPortMessageContainer . HAnchor = HAnchor . ParentLeftRight ;
2014-01-29 19:09:30 -08:00
2016-04-18 11:31:31 -07:00
printerComPortError = new TextWidget ( "Currently available serial ports." . Localize ( ) , 0 , 0 , 10 ) ;
2014-03-24 14:44:43 -07:00
printerComPortError . TextColor = ActiveTheme . Instance . PrimaryTextColor ;
2015-04-08 15:20:10 -07:00
printerComPortError . AutoExpandBoundsToText = true ;
2014-01-29 19:09:30 -08:00
2016-04-18 11:31:31 -07:00
printerComPortHelpLink = linkButtonFactory . Generate ( "What's this?" . Localize ( ) ) ;
2015-04-08 15:20:10 -07:00
printerComPortHelpLink . Margin = new BorderDouble ( left : 5 ) ;
printerComPortHelpLink . VAnchor = VAnchor . ParentBottom ;
2016-04-18 11:31:31 -07:00
printerComPortHelpLink . Click + = ( s , e ) = > printerComPortHelpMessage . Visible = ! printerComPortHelpMessage . Visible ;
2014-01-29 19:09:30 -08:00
2016-12-22 11:08:48 -08:00
printerComPortHelpMessage = new TextWidget ( "The 'Serial Port' section lists all available serial\nports on your device. Changing which USB port the printer\nis conneted to may change the associated serial port.\n\nTip: If you are uncertain, unplug/plug in your printer\nand hit refresh. The new port that appears should be\nyour printer." . Localize ( ) , 0 , 0 , 10 ) ;
2014-03-24 14:44:43 -07:00
printerComPortHelpMessage . TextColor = ActiveTheme . Instance . PrimaryTextColor ;
2015-04-08 15:20:10 -07:00
printerComPortHelpMessage . Margin = new BorderDouble ( top : 10 ) ;
printerComPortHelpMessage . Visible = false ;
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
comPortMessageContainer . AddChild ( printerComPortError ) ;
comPortMessageContainer . AddChild ( printerComPortHelpLink ) ;
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
container . AddChild ( comPortLabel ) ;
2015-05-01 13:28:31 -07:00
container . AddChild ( serialPortContainer ) ;
2015-04-08 15:20:10 -07:00
container . AddChild ( comPortMessageContainer ) ;
container . AddChild ( printerComPortHelpMessage ) ;
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
container . HAnchor = HAnchor . ParentLeftRight ;
return container ;
}
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
private void onPrinterStatusChanged ( object sender , EventArgs e )
{
if ( PrinterConnectionAndCommunication . Instance . PrinterIsConnected )
{
2016-04-18 11:31:31 -07:00
printerComPortHelpLink . Visible = false ;
printerComPortError . TextColor = ActiveTheme . Instance . PrimaryTextColor ;
printerComPortError . Text = "Connection succeeded" . Localize ( ) + "!" ;
nextButton . Visible = true ;
connectButton . Visible = false ;
2016-09-23 16:24:43 -07:00
UiThread . RunOnIdle ( ( ) = > this ? . Parent ? . Close ( ) ) ;
2015-04-08 15:20:10 -07:00
}
else if ( PrinterConnectionAndCommunication . Instance . CommunicationState ! = PrinterConnectionAndCommunication . CommunicationStates . AttemptingToConnect )
{
2016-04-18 11:31:31 -07:00
printerComPortHelpLink . Visible = false ;
printerComPortError . TextColor = RGBA_Bytes . Red ;
printerComPortError . Text = "Uh-oh! Could not connect to printer." . Localize ( ) ;
connectButton . Visible = true ;
nextButton . Visible = false ;
2015-04-08 15:20:10 -07:00
}
}
private void MoveToNextWidget ( object state )
{
2016-06-07 15:45:50 -07:00
WizardWindow . ChangeToInstallDriverOrComPortOne ( ) ;
2015-04-08 15:20:10 -07:00
}
private void ConnectButton_Click ( object sender , EventArgs mouseEvent )
{
try
{
printerComPortHelpLink . Visible = false ;
printerComPortError . TextColor = ActiveTheme . Instance . PrimaryTextColor ;
2016-04-18 11:31:31 -07:00
printerComPortError . Text = "Attempting to connect" . Localize ( ) + "..." ;
printerComPortError . TextColor = ActiveTheme . Instance . PrimaryTextColor ;
2016-07-18 14:15:37 -07:00
ActiveSliceSettings . Instance . Helpers . SetComPort ( GetSelectedSerialPort ( ) ) ;
2015-04-08 15:20:10 -07:00
PrinterConnectionAndCommunication . Instance . ConnectToActivePrinter ( ) ;
2016-04-18 11:31:31 -07:00
2015-04-08 15:20:10 -07:00
connectButton . Visible = false ;
refreshButton . Visible = false ;
}
catch
{
printerComPortHelpLink . Visible = false ;
printerComPortError . TextColor = RGBA_Bytes . Red ;
2016-04-18 11:31:31 -07:00
printerComPortError . Text = "Oops! Please select a serial port." . Localize ( ) ;
2015-04-08 15:20:10 -07:00
}
}
2016-06-02 16:21:32 -07:00
protected void CreateSerialPortControls ( FlowLayoutWidget comPortContainer , string activePrinterSerialPort )
{
int portIndex = 0 ;
2016-11-03 09:57:48 -07:00
string [ ] portsToCreate = FrostedSerialPort . GetPortNames ( ) ;
2016-06-02 16:21:32 -07:00
// Add a radio button for each filtered port
foreach ( string portName in portsToCreate )
{
SerialPortIndexRadioButton comPortOption = createComPortOption ( portName , activePrinterSerialPort = = portName ) ;
if ( comPortOption . Checked )
{
printerComPortIsAvailable = true ;
}
SerialPortButtonsList . Add ( comPortOption ) ;
comPortContainer . AddChild ( comPortOption ) ;
portIndex + + ;
}
// Add a virtual entry for serial ports that were previously configured but are not currently connected
if ( ! printerComPortIsAvailable & & activePrinterSerialPort ! = null )
{
SerialPortIndexRadioButton comPortOption = createComPortOption ( activePrinterSerialPort , true ) ;
comPortOption . Enabled = false ;
comPortContainer . AddChild ( comPortOption ) ;
SerialPortButtonsList . Add ( comPortOption ) ;
portIndex + + ;
}
//If there are still no com ports show a message to that effect
if ( portIndex = = 0 )
{
2017-01-04 07:23:30 -08:00
TextWidget comPortOption = new TextWidget ( "No COM ports available" . Localize ( ) ) ;
2016-06-02 16:21:32 -07:00
comPortOption . Margin = new BorderDouble ( 3 , 6 , 5 , 6 ) ;
comPortOption . TextColor = ActiveTheme . Instance . PrimaryTextColor ;
comPortContainer . AddChild ( comPortOption ) ;
}
}
private SerialPortIndexRadioButton createComPortOption ( string portName , bool isActivePrinterPort )
{
SerialPortIndexRadioButton comPortOption = new SerialPortIndexRadioButton ( portName , portName )
{
HAnchor = HAnchor . ParentLeft ,
Margin = new BorderDouble ( 3 , 3 , 5 , 3 ) ,
TextColor = ActiveTheme . Instance . PrimaryTextColor ,
Checked = isActivePrinterPort
} ;
return comPortOption ;
}
private string GetSelectedSerialPort ( )
{
foreach ( SerialPortIndexRadioButton button in SerialPortButtonsList )
{
if ( button . Checked )
{
return button . PortValue ;
}
}
2017-01-04 07:23:30 -08:00
throw new Exception ( "Could not find a selected button." . Localize ( ) ) ;
2016-06-02 16:21:32 -07:00
}
2015-04-08 15:20:10 -07:00
}
}