2017-08-23 23:59:45 -07:00
/ *
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 System.Collections.Generic ;
using MatterHackers.Agg ;
2017-10-18 14:56:10 -07:00
using MatterHackers.Agg.Platform ;
2014-01-29 19:09:30 -08:00
using MatterHackers.Agg.UI ;
using MatterHackers.Localizations ;
2016-06-14 11:28:13 -07:00
using MatterHackers.MatterControl.SlicerConfiguration ;
2014-01-29 19:09:30 -08:00
namespace MatterHackers.MatterControl.PrinterControls.PrinterConnections
{
2017-11-08 15:56:37 -08:00
public class SetupStepBaudRate : DialogPage
2015-04-08 15:20:10 -07:00
{
2016-06-01 18:17:11 -07:00
private List < RadioButton > BaudRateButtonsList = new List < RadioButton > ( ) ;
2015-04-08 15:20:10 -07:00
private FlowLayoutWidget printerBaudRateContainer ;
private TextWidget printerBaudRateError ;
private GuiWidget baudRateWidget ;
private RadioButton otherBaudRateRadioButton ;
2022-07-15 17:28:39 -07:00
private ThemedTextEditWidget otherBaudRateInput ;
2018-04-14 20:51:01 -07:00
private GuiWidget nextButton ;
private GuiWidget printerBaudRateHelpLink ;
2015-04-08 15:20:10 -07:00
private TextWidget printerBaudRateHelpMessage ;
2017-09-17 21:08:16 -07:00
private PrinterConfig printer ;
public SetupStepBaudRate ( PrinterConfig printer )
2015-04-08 15:20:10 -07:00
{
2017-09-17 21:08:16 -07:00
this . printer = printer ;
2015-04-08 15:20:10 -07:00
printerBaudRateContainer = createPrinterBaudRateContainer ( ) ;
2018-11-03 10:12:27 -07:00
contentRow . AddChild ( printerBaudRateContainer ) ;
2015-04-08 15:20:10 -07:00
{
2018-04-14 20:51:01 -07:00
nextButton = theme . CreateDialogButton ( "Continue" . Localize ( ) ) ;
2017-08-23 17:27:30 -07:00
nextButton . Click + = ( s , e ) = >
{
bool canContinue = this . OnSave ( ) ;
if ( canContinue )
{
2017-09-17 21:08:16 -07:00
UiThread . RunOnIdle ( ( ) = >
{
2018-06-19 15:02:25 -07:00
this . DialogWindow . ChangeToPage ( new SetupStepComPortOne ( printer ) ) ;
2017-09-17 21:08:16 -07:00
} ) ;
2017-08-23 17:27:30 -07:00
}
} ;
this . AddPageAction ( nextButton ) ;
2015-04-08 15:20:10 -07:00
}
BindBaudRateHandlers ( ) ;
}
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
private FlowLayoutWidget createPrinterBaudRateContainer ( )
{
FlowLayoutWidget container = new FlowLayoutWidget ( FlowDirection . TopToBottom ) ;
container . Margin = new BorderDouble ( 0 ) ;
2017-08-07 15:47:27 -07:00
container . VAnchor = VAnchor . Stretch ;
2015-04-08 15:20:10 -07:00
BorderDouble elementMargin = new BorderDouble ( top : 3 ) ;
2014-01-29 19:09:30 -08:00
2017-01-04 07:23:30 -08:00
string baudRateLabelText = "Baud Rate" . Localize ( ) ;
2015-04-08 15:20:10 -07:00
string baudRateLabelTextFull = string . Format ( "{0}:" , baudRateLabelText ) ;
2014-01-29 19:09:30 -08:00
TextWidget baudRateLabel = new TextWidget ( baudRateLabelTextFull , 0 , 0 , 12 ) ;
2018-11-03 09:13:07 -07:00
baudRateLabel . TextColor = theme . TextColor ;
2015-04-08 15:20:10 -07:00
baudRateLabel . Margin = new BorderDouble ( 0 , 0 , 0 , 10 ) ;
2017-08-07 15:47:27 -07:00
baudRateLabel . HAnchor = HAnchor . Stretch ;
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
baudRateWidget = GetBaudRateWidget ( ) ;
2017-08-07 15:47:27 -07:00
baudRateWidget . HAnchor = HAnchor . Stretch ;
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
FlowLayoutWidget baudRateMessageContainer = new FlowLayoutWidget ( ) ;
baudRateMessageContainer . Margin = elementMargin ;
2017-08-07 15:47:27 -07:00
baudRateMessageContainer . HAnchor = HAnchor . Stretch ;
2014-01-29 19:09:30 -08:00
2017-01-04 07:23:30 -08:00
printerBaudRateError = new TextWidget ( "Select the baud rate." . Localize ( ) , 0 , 0 , 10 ) ;
2018-11-03 09:13:07 -07:00
printerBaudRateError . TextColor = theme . TextColor ;
2015-04-08 15:20:10 -07:00
printerBaudRateError . AutoExpandBoundsToText = true ;
2014-01-29 19:09:30 -08:00
2018-07-11 09:26:54 -07:00
printerBaudRateHelpLink = new LinkLabel ( "What's this?" . Localize ( ) , theme )
{
Margin = new BorderDouble ( left : 5 ) ,
VAnchor = VAnchor . Bottom
} ;
2017-01-17 14:47:04 -08:00
printerBaudRateHelpLink . Click + = printerBaudRateHelp_Click ;
2014-01-29 19:09:30 -08:00
2017-01-04 07:23:30 -08:00
printerBaudRateHelpMessage = new TextWidget ( "The term 'Baud Rate' roughly means the speed at which\ndata is transmitted. Baud rates may differ from printer to\nprinter. Refer to your printer manual for more info.\n\nTip: If you are uncertain - try 250000." . Localize ( ) , 0 , 0 , 10 ) ;
2018-11-03 09:13:07 -07:00
printerBaudRateHelpMessage . TextColor = theme . TextColor ;
2015-04-08 15:20:10 -07:00
printerBaudRateHelpMessage . Margin = new BorderDouble ( top : 10 ) ;
printerBaudRateHelpMessage . Visible = false ;
baudRateMessageContainer . AddChild ( printerBaudRateError ) ;
baudRateMessageContainer . AddChild ( printerBaudRateHelpLink ) ;
container . AddChild ( baudRateLabel ) ;
container . AddChild ( baudRateWidget ) ;
container . AddChild ( baudRateMessageContainer ) ;
container . AddChild ( printerBaudRateHelpMessage ) ;
2017-08-07 15:47:27 -07:00
container . HAnchor = HAnchor . Stretch ;
2015-04-08 15:20:10 -07:00
return container ;
}
private void printerBaudRateHelp_Click ( object sender , EventArgs mouseEvent )
{
printerBaudRateHelpMessage . Visible = ! printerBaudRateHelpMessage . Visible ;
}
public GuiWidget GetBaudRateWidget ( )
{
FlowLayoutWidget baudRateContainer = new FlowLayoutWidget ( FlowDirection . TopToBottom ) ;
baudRateContainer . Margin = new BorderDouble ( 0 ) ;
List < string > baudRates = new List < string > { "115200" , "250000" } ;
BorderDouble baudRateMargin = new BorderDouble ( 3 , 3 , 5 , 0 ) ;
foreach ( string baudRate in baudRates )
{
2016-06-01 18:17:11 -07:00
RadioButton baudOption = new RadioButton ( baudRate ) ;
2015-04-08 15:20:10 -07:00
BaudRateButtonsList . Add ( baudOption ) ;
baudOption . Margin = baudRateMargin ;
2018-11-03 09:13:07 -07:00
baudOption . TextColor = theme . TextColor ;
2018-10-05 09:24:57 -07:00
if ( printer . Settings . GetValue ( SettingsKey . baud_rate ) = = baudRate )
2015-04-08 15:20:10 -07:00
{
baudOption . Checked = true ;
}
baudRateContainer . AddChild ( baudOption ) ;
}
2014-01-29 19:09:30 -08:00
2017-01-04 07:23:30 -08:00
otherBaudRateRadioButton = new RadioButton ( "Other" . Localize ( ) ) ;
2015-04-08 15:20:10 -07:00
otherBaudRateRadioButton . Margin = baudRateMargin ;
2018-11-03 09:13:07 -07:00
otherBaudRateRadioButton . TextColor = theme . TextColor ;
2015-04-08 15:20:10 -07:00
baudRateContainer . AddChild ( otherBaudRateRadioButton ) ;
//See if the baud rate of the current print is in the list of displayed rates,
//flag the 'other' option if it is not and prefill the rate.
2022-07-15 17:28:39 -07:00
otherBaudRateInput = new ThemedTextEditWidget ( "" , theme ) ;
2015-04-08 15:20:10 -07:00
otherBaudRateInput . Visible = false ;
2017-08-07 15:47:27 -07:00
otherBaudRateInput . HAnchor = HAnchor . Stretch ;
2015-04-08 15:20:10 -07:00
2018-10-05 09:24:57 -07:00
string currentBaudRate = printer . Settings . GetValue ( SettingsKey . baud_rate ) ;
2016-06-14 11:28:13 -07:00
if ( currentBaudRate ! = null )
2015-04-08 15:20:10 -07:00
{
2016-06-14 11:28:13 -07:00
if ( ! baudRates . Contains ( currentBaudRate ) )
2015-04-08 15:20:10 -07:00
{
otherBaudRateRadioButton . Checked = true ;
2016-06-14 11:28:13 -07:00
otherBaudRateInput . Text = currentBaudRate ;
2015-04-08 15:20:10 -07:00
otherBaudRateInput . Visible = true ;
}
}
baudRateContainer . AddChild ( otherBaudRateInput ) ;
return baudRateContainer ;
}
private void BindBaudRateHandlers ( )
{
2015-06-24 16:59:10 -07:00
otherBaudRateRadioButton . CheckedStateChanged + = BindBaudRate_Select ;
2016-06-01 18:17:11 -07:00
foreach ( RadioButton button in BaudRateButtonsList )
2015-04-08 15:20:10 -07:00
{
2015-06-24 16:59:10 -07:00
button . CheckedStateChanged + = BindBaudRate_Select ;
2015-04-08 15:20:10 -07:00
}
BindBaudRate_Select ( null , null ) ;
}
private void BindBaudRate_Select ( object sender , EventArgs e )
{
if ( otherBaudRateRadioButton . Checked = = true )
{
otherBaudRateInput . Visible = true ;
}
else
{
otherBaudRateInput . Visible = false ;
}
}
private bool OnSave ( )
{
string baudRate = null ;
try
{
baudRate = GetSelectedBaudRate ( ) ;
}
catch
{
printerBaudRateHelpLink . Visible = false ;
2017-10-31 11:43:25 -07:00
printerBaudRateError . TextColor = Color . Red ;
2017-01-04 07:23:30 -08:00
printerBaudRateError . Text = "Oops! Please select a baud rate." . Localize ( ) ;
2015-04-08 15:20:10 -07:00
}
if ( baudRate ! = null )
{
try
{
2018-10-05 09:24:57 -07:00
printer . Settings . Helpers . SetBaudRate ( baudRate ) ;
2015-04-08 15:20:10 -07:00
return true ;
}
catch
{
printerBaudRateHelpLink . Visible = false ;
2017-10-31 11:43:25 -07:00
printerBaudRateError . TextColor = Color . Red ;
2017-01-04 07:23:30 -08:00
printerBaudRateError . Text = "Oops! Baud Rate must be an integer." . Localize ( ) ;
2015-04-08 15:20:10 -07:00
return false ;
}
}
else
{
return false ;
}
}
private string GetSelectedBaudRate ( )
{
2016-06-01 18:17:11 -07:00
foreach ( RadioButton button in BaudRateButtonsList )
2015-04-08 15:20:10 -07:00
{
if ( button . Checked )
{
2016-06-01 18:17:11 -07:00
return button . Text ;
2015-04-08 15:20:10 -07:00
}
}
if ( otherBaudRateRadioButton . Checked )
{
return otherBaudRateInput . Text ;
}
2014-01-29 19:09:30 -08:00
2017-01-04 07:23:30 -08:00
throw new Exception ( "Could not find a selected button." . Localize ( ) ) ;
2015-04-08 15:20:10 -07:00
}
}
}