2016-04-18 11:31:31 -07:00
/ *
2017-05-24 14:19:02 -07:00
Copyright ( c ) 2017 , Lars Brubaker , John Lewin
2016-04-18 11:31:31 -07:00
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 .
* /
2017-02-28 07:12:19 -08:00
using System ;
2016-04-18 11:31:31 -07:00
using MatterHackers.Agg ;
2017-02-28 07:12:19 -08:00
using MatterHackers.Agg.Image ;
2017-08-20 02:34:39 -07:00
using MatterHackers.Agg.Platform ;
2014-01-29 19:09:30 -08:00
using MatterHackers.Agg.UI ;
2017-05-24 14:19:02 -07:00
using MatterHackers.Agg.VertexSource ;
2014-01-29 19:09:30 -08:00
using MatterHackers.Localizations ;
2015-04-08 15:20:10 -07:00
using MatterHackers.MatterControl.PrinterCommunication ;
using MatterHackers.MatterControl.SlicerConfiguration ;
2017-08-20 13:23:36 -07:00
using MatterHackers.SerialPortCommunication.FrostedSerial ;
2014-01-29 19:09:30 -08:00
namespace MatterHackers.MatterControl.ActionBar
{
2017-05-24 14:19:02 -07:00
public class ResetButton : GuiWidget
{
private readonly string resetConnectionText = "Reset\nConnection" . Localize ( ) . ToUpper ( ) ;
private EventHandler unregisterEvents ;
2015-04-08 15:20:10 -07:00
2017-09-15 12:08:00 -07:00
public ResetButton ( PrinterConfig printer , TextImageButtonFactory buttonFactory )
2017-05-24 14:19:02 -07:00
{
2017-08-07 15:47:27 -07:00
this . HAnchor = HAnchor . Stretch | HAnchor . Fit ;
this . VAnchor = VAnchor . Fit ;
2017-05-24 14:19:02 -07:00
this . BackgroundColor = ActiveTheme . Instance . PrimaryBackgroundColor ;
2015-04-08 15:20:10 -07:00
2017-05-24 14:19:02 -07:00
Button resetConnectionButton = buttonFactory . Generate ( resetConnectionText , "e_stop4.png" ) ;
2017-09-15 12:08:00 -07:00
resetConnectionButton . Visible = printer . Settings . GetValue < bool > ( SettingsKey . show_reset_connection ) ;
2017-05-24 14:19:02 -07:00
resetConnectionButton . Click + = ( s , e ) = >
2016-07-26 09:30:12 -07:00
{
2017-09-15 12:08:00 -07:00
UiThread . RunOnIdle ( printer . Connection . RebootBoard ) ;
2017-05-24 14:19:02 -07:00
} ;
this . AddChild ( resetConnectionButton ) ;
2016-08-31 15:10:41 -07:00
2017-05-24 14:19:02 -07:00
ActiveSliceSettings . SettingChanged . RegisterEvent ( ( s , e ) = >
{
var stringEvent = e as StringEventArgs ;
if ( stringEvent ? . Data = = SettingsKey . show_reset_connection )
2016-07-26 09:30:12 -07:00
{
2017-09-15 12:08:00 -07:00
resetConnectionButton . Visible = printer . Settings . GetValue < bool > ( SettingsKey . show_reset_connection ) ;
2017-05-24 14:19:02 -07:00
}
} , ref unregisterEvents ) ;
}
}
2015-04-08 15:20:10 -07:00
2017-05-24 14:19:02 -07:00
public class SimpleButton : FlowLayoutWidget
{
public ImageBuffer Image { get ; set ; }
public BorderDouble ImageMargin { get ; set ; }
public BorderDouble ImagePadding { get ; set ; }
public double FontSize { get ; set ; } = 12 ;
2016-08-31 15:10:41 -07:00
2017-05-24 14:19:02 -07:00
public int BorderWidth { get ; set ; }
2016-05-13 11:37:22 -07:00
2017-05-24 14:19:02 -07:00
public RGBA_Bytes BorderColor { get ; set ; } = RGBA_Bytes . Transparent ;
2016-06-09 09:23:23 -07:00
2017-05-24 14:19:02 -07:00
private int borderRadius = 0 ;
public SimpleButton ( string text , ImageBuffer image = null )
{
2017-08-07 15:47:27 -07:00
this . HAnchor = HAnchor . Left | HAnchor . Fit ;
this . VAnchor = VAnchor . Top | VAnchor . Fit ;
2017-05-24 14:19:02 -07:00
this . Text = text ;
this . Image = image ;
2016-07-26 09:30:12 -07:00
2017-05-24 14:19:02 -07:00
this . AddChild ( new GuiWidget ( ) { BackgroundColor = RGBA_Bytes . Green , Height = 10 , Width = 20 } ) ;
UiThread . RunOnIdle ( ( ) = >
{
if ( this . Image ! = null )
2016-07-26 09:30:12 -07:00
{
2017-05-24 14:19:02 -07:00
this . AddChild (
new ImageWidget ( this . Image )
2016-07-26 09:30:12 -07:00
{
2017-05-24 14:19:02 -07:00
Margin = ImageMargin ,
Padding = ImagePadding ,
2017-08-07 15:47:27 -07:00
VAnchor = VAnchor . Stretch ,
2017-05-24 14:19:02 -07:00
} ) ;
}
2015-04-08 15:20:10 -07:00
2017-05-24 14:19:02 -07:00
if ( ! string . IsNullOrEmpty ( this . Text ) )
{
this . AddChild ( new TextWidget ( this . Text , pointSize : this . FontSize ) ) ;
}
2016-08-31 15:10:41 -07:00
2017-05-24 14:19:02 -07:00
this . AddChild ( new GuiWidget ( ) { BackgroundColor = RGBA_Bytes . Red , Height = 10 , Width = 20 } ) ;
} ) ;
2015-04-08 15:20:10 -07:00
}
2017-05-24 14:19:02 -07:00
public override void OnLayout ( LayoutEventArgs layoutEventArgs )
2015-04-08 15:20:10 -07:00
{
2017-05-24 14:19:02 -07:00
base . OnLayout ( layoutEventArgs ) ;
2015-04-08 15:20:10 -07:00
}
2017-05-24 14:19:02 -07:00
/ *
public override void OnLoad ( EventArgs args )
2015-04-08 15:20:10 -07:00
{
2017-05-24 14:19:02 -07:00
if ( this . Image ! = null )
2015-04-09 12:05:05 -07:00
{
2017-05-24 14:19:02 -07:00
this . AddChild ( new ImageWidget ( this . Image )
{
Margin = ImageMargin
} ) ;
2015-04-09 12:05:05 -07:00
}
2015-04-08 15:20:10 -07:00
2017-05-24 14:19:02 -07:00
if ( ! string . IsNullOrEmpty ( this . Text ) )
2015-04-08 15:20:10 -07:00
{
2017-05-24 14:19:02 -07:00
this . AddChild ( new TextWidget ( this . Text , pointSize : this . FontSize ) ) ;
2015-04-08 15:20:10 -07:00
}
2017-05-24 14:19:02 -07:00
base . OnLoad ( args ) ;
} * /
public override void OnDraw ( Graphics2D graphics2D )
{
if ( this . BorderColor . Alpha0To255 > 0 )
2015-04-08 15:20:10 -07:00
{
2017-05-24 14:19:02 -07:00
RectangleDouble borderRectangle = LocalBounds ;
if ( BorderWidth > 0 )
{
if ( BorderWidth = = 1 )
{
graphics2D . Rectangle ( borderRectangle , this . BorderColor ) ;
}
else
{
graphics2D . Render (
new Stroke (
new RoundedRect ( borderRectangle , this . borderRadius ) ,
BorderWidth ) ,
this . BorderColor ) ;
}
}
2015-04-08 15:20:10 -07:00
}
2017-05-24 14:19:02 -07:00
base . OnDraw ( graphics2D ) ;
2015-04-08 15:20:10 -07:00
}
2017-05-24 14:19:02 -07:00
}
public class PrinterConnectButton : GuiWidget
{
private readonly string disconnectAndCancelTitle = "Disconnect and stop the current print?" . Localize ( ) ;
private readonly string disconnectAndCancelMessage = "WARNING: Disconnecting will stop the current print.\n\nAre you sure you want to disconnect?" . Localize ( ) ;
private GuiWidget connectButton ;
private Button disconnectButton ;
2015-04-08 15:20:10 -07:00
2017-05-24 14:19:02 -07:00
private EventHandler unregisterEvents ;
2017-09-15 12:08:00 -07:00
private PrinterConfig printer ;
2017-05-24 14:19:02 -07:00
2017-09-15 12:08:00 -07:00
public PrinterConnectButton ( PrinterConfig printer , TextImageButtonFactory buttonFactory , BorderDouble margin )
2015-04-08 15:20:10 -07:00
{
2017-09-15 12:08:00 -07:00
this . printer = printer ;
2017-08-07 15:47:27 -07:00
this . HAnchor = HAnchor . Left | HAnchor . Fit ;
this . VAnchor = VAnchor . Fit ;
2017-08-07 07:12:59 -07:00
this . Margin = 0 ;
this . Padding = 0 ;
2017-05-24 14:19:02 -07:00
2017-08-20 02:34:39 -07:00
connectButton = buttonFactory . Generate ( "Connect" . Localize ( ) . ToUpper ( ) , AggContext . StaticData . LoadIcon ( "connect.png" , 14 , 14 ) ) ;
2017-06-02 13:17:30 -07:00
connectButton . Name = "Connect to printer button" ;
connectButton . ToolTipText = "Connect to the currently selected printer" . Localize ( ) ;
2017-05-24 14:19:02 -07:00
connectButton . Click + = ( s , e ) = >
{
if ( connectButton . Enabled )
{
2017-09-15 12:08:00 -07:00
if ( printer . Settings . PrinterSelected )
2017-05-24 14:19:02 -07:00
{
UserRequestedConnectToActivePrinter ( ) ;
}
}
} ;
this . AddChild ( connectButton ) ;
2017-08-20 02:34:39 -07:00
disconnectButton = buttonFactory . Generate ( "Disconnect" . Localize ( ) . ToUpper ( ) , AggContext . StaticData . LoadIcon ( "connect.png" , 14 , 14 ) ) ;
2017-05-24 14:19:02 -07:00
disconnectButton . Name = "Disconnect from printer button" ;
disconnectButton . Visible = false ;
disconnectButton . ToolTipText = "Disconnect from current printer" . Localize ( ) ;
disconnectButton . Click + = ( s , e ) = > UiThread . RunOnIdle ( ( ) = >
{
2017-09-15 12:08:00 -07:00
if ( printer . Connection . PrinterIsPrinting )
2017-05-24 14:19:02 -07:00
{
StyledMessageBox . ShowMessageBox (
( bool disconnectCancel ) = >
{
if ( disconnectCancel )
{
2017-09-15 12:08:00 -07:00
printer . Connection . Stop ( false ) ;
printer . Connection . Disable ( ) ;
2017-05-24 14:19:02 -07:00
}
} ,
disconnectAndCancelMessage ,
disconnectAndCancelTitle ,
StyledMessageBox . MessageType . YES_NO ,
"Disconnect" . Localize ( ) ,
"Stay Connected" . Localize ( ) ) ;
}
else
{
2017-09-15 12:08:00 -07:00
printer . Connection . Disable ( ) ;
2017-05-24 14:19:02 -07:00
}
} ) ;
2017-05-24 19:11:51 -07:00
this . AddChild ( disconnectButton ) ;
2015-04-08 15:20:10 -07:00
2017-05-24 14:19:02 -07:00
foreach ( var child in Children )
2016-08-31 15:10:41 -07:00
{
2017-08-07 15:47:27 -07:00
child . VAnchor = VAnchor . Top ;
child . HAnchor = HAnchor . Left ;
2017-05-24 14:19:02 -07:00
child . Cursor = Cursors . Hand ;
2017-08-07 07:12:59 -07:00
child . Margin = margin ;
2016-08-31 15:10:41 -07:00
}
2017-05-24 14:19:02 -07:00
// Bind connect button states to active printer state
this . SetVisibleStates ( null , null ) ;
2017-09-15 12:08:00 -07:00
printer . Connection . EnableChanged . RegisterEvent ( SetVisibleStates , ref unregisterEvents ) ;
printer . Connection . CommunicationStateChanged . RegisterEvent ( SetVisibleStates , ref unregisterEvents ) ;
2017-05-24 14:19:02 -07:00
}
public override void OnClosed ( ClosedEventArgs e )
{
unregisterEvents ? . Invoke ( this , null ) ;
base . OnClosed ( e ) ;
}
2017-09-02 08:36:54 -07:00
public void UserRequestedConnectToActivePrinter ( )
2017-05-24 14:19:02 -07:00
{
2017-09-15 12:08:00 -07:00
if ( printer . Settings . PrinterSelected )
2017-08-07 06:54:21 -07:00
{
#if __ANDROID__
2017-09-15 12:08:00 -07:00
if ( ! printer . Settings . GetValue < bool > ( SettingsKey . enable_network_printing )
2017-08-07 06:54:21 -07:00
& & ! FrostedSerialPort . HasPermissionToDevice ( ) )
{
// Opens the USB device permissions dialog which will call back into our UsbDevice broadcast receiver to connect
FrostedSerialPort . RequestPermissionToDevice ( RunTroubleShooting ) ;
}
else
#endif
{
2017-09-15 12:08:00 -07:00
printer . Connection . HaltConnectionThread ( ) ;
printer . Connection . ConnectToActivePrinter ( true ) ;
2017-08-07 06:54:21 -07:00
}
}
2017-05-24 14:19:02 -07:00
}
2017-08-20 13:23:36 -07:00
private static void RunTroubleShooting ( )
{
2017-08-23 15:51:29 -07:00
WizardWindow . Show < SetupWizardTroubleshooting > ( ) ;
2017-08-20 13:23:36 -07:00
}
2017-05-24 14:19:02 -07:00
private void SetVisibleStates ( object sender , EventArgs e )
{
UiThread . RunOnIdle ( ( ) = >
2016-08-31 15:10:41 -07:00
{
2017-09-15 12:08:00 -07:00
if ( printer . Connection . PrinterIsConnected )
2017-05-24 14:19:02 -07:00
{
disconnectButton . Visible = true ;
connectButton . Visible = false ;
}
else
{
disconnectButton . Visible = false ;
connectButton . Visible = true ;
}
2017-09-15 12:08:00 -07:00
var communicationState = printer . Connection . CommunicationState ;
2017-05-24 14:19:02 -07:00
// Ensure connect buttons are locked while long running processes are executing to prevent duplicate calls into said actions
2017-09-15 12:08:00 -07:00
connectButton . Enabled = printer . Settings . PrinterSelected
2017-06-13 17:32:38 -07:00
& & communicationState ! = CommunicationStates . AttemptingToConnect ;
2017-05-24 14:19:02 -07:00
2017-06-13 17:32:38 -07:00
disconnectButton . Enabled = communicationState ! = CommunicationStates . Disconnecting ;
2017-05-24 14:19:02 -07:00
} ) ;
2015-04-08 15:20:10 -07:00
}
}
}