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 ;
using MatterHackers.Agg.PlatformAbstract ;
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 ;
2014-01-29 19:09:30 -08:00
namespace MatterHackers.MatterControl.ActionBar
{
2017-05-27 17:48:32 -07:00
public class PrinterSelectEditDropdown : FlowLayoutWidget , IIgnoredPopupChild
2015-04-08 15:20:10 -07:00
{
2016-06-07 16:19:26 -07:00
private PrinterSelector printerSelector ;
2017-05-24 14:19:02 -07:00
private GuiWidget printerSelectorAndEditOverlay ;
private Button editPrinterButton ;
2016-12-29 06:55:12 -08:00
private EventHandler unregisterEvents ;
2015-04-08 15:20:10 -07:00
2017-05-24 14:19:02 -07:00
public PrinterSelectEditDropdown ( )
2016-09-28 15:31:40 -07:00
{
2017-05-24 14:19:02 -07:00
printerSelector = new PrinterSelector ( )
{
2017-05-27 17:48:32 -07:00
HAnchor = HAnchor . FitToChildren ,
2017-05-24 14:19:02 -07:00
Cursor = Cursors . Hand ,
2017-05-24 19:11:51 -07:00
Margin = 0
2017-05-24 14:19:02 -07:00
} ;
printerSelector . AddPrinter + = ( s , e ) = > WizardWindow . ShowPrinterSetup ( true ) ;
this . AddChild ( printerSelector ) ;
2017-06-02 13:17:30 -07:00
editPrinterButton = CreatePrinterEditButton ( ) ;
2017-05-24 14:19:02 -07:00
this . AddChild ( editPrinterButton ) ;
printerSelectorAndEditOverlay = new GuiWidget ( )
{
HAnchor = HAnchor . ParentLeftRight ,
VAnchor = VAnchor . ParentBottomTop ,
Selectable = false ,
} ;
this . AddChild ( printerSelectorAndEditOverlay ) ;
PrinterConnectionAndCommunication . Instance . EnableChanged . RegisterEvent ( SetVisibleStates , ref unregisterEvents ) ;
PrinterConnectionAndCommunication . Instance . CommunicationStateChanged . RegisterEvent ( SetVisibleStates , ref unregisterEvents ) ;
2016-09-28 15:31:40 -07:00
}
2017-06-02 13:17:30 -07:00
public static Button CreatePrinterEditButton ( )
{
var button = TextImageButtonFactory . GetThemedEditButton ( ) ;
button . Name = "Edit Printer Button" ;
button . VAnchor = VAnchor . ParentCenter ;
button . Click + = UiNavigation . OpenEditPrinterWizard_Click ;
return button ;
}
2017-02-03 13:06:08 -08:00
public override void OnClosed ( ClosedEventArgs e )
2015-04-09 12:05:05 -07:00
{
2016-04-18 11:31:31 -07:00
unregisterEvents ? . Invoke ( this , null ) ;
2015-04-09 12:05:05 -07:00
base . OnClosed ( e ) ;
2015-04-08 15:20:10 -07:00
}
2017-05-24 14:19:02 -07:00
private void SetVisibleStates ( object sender , EventArgs e )
2015-04-08 15:20:10 -07:00
{
2017-05-24 14:19:02 -07:00
UiThread . RunOnIdle ( ( ) = >
2015-04-08 15:20:10 -07:00
{
2017-05-24 14:19:02 -07:00
bool printerIsPrintingOrPaused = PrinterConnectionAndCommunication . Instance . PrinterIsPrinting
| | PrinterConnectionAndCommunication . Instance . PrinterIsPaused ;
2017-02-28 07:12:19 -08:00
2017-05-24 14:19:02 -07:00
editPrinterButton . Enabled = ActiveSliceSettings . Instance . PrinterSelected & & ! printerIsPrintingOrPaused ;
printerSelector . Enabled = ! printerIsPrintingOrPaused ;
if ( printerIsPrintingOrPaused )
2016-07-26 09:30:12 -07:00
{
2017-05-24 14:19:02 -07:00
printerSelectorAndEditOverlay . BackgroundColor = new RGBA_Bytes ( ActiveTheme . Instance . PrimaryBackgroundColor , 150 ) ;
}
else
{
printerSelectorAndEditOverlay . BackgroundColor = new RGBA_Bytes ( 0 , 0 , 0 , 0 ) ;
}
} ) ;
}
}
2016-07-26 09:30:12 -07:00
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-05-24 14:19:02 -07:00
public ResetButton ( TextImageButtonFactory buttonFactory )
{
this . HAnchor = HAnchor . ParentLeftRight | HAnchor . FitToChildren ;
this . VAnchor = VAnchor . FitToChildren ;
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" ) ;
resetConnectionButton . Visible = ActiveSliceSettings . Instance . GetValue < bool > ( SettingsKey . show_reset_connection ) ;
resetConnectionButton . Click + = ( s , e ) = >
2016-07-26 09:30:12 -07:00
{
2017-05-24 14:19:02 -07:00
UiThread . RunOnIdle ( PrinterConnectionAndCommunication . Instance . RebootBoard ) ;
} ;
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-05-24 14:19:02 -07:00
resetConnectionButton . Visible = ActiveSliceSettings . Instance . GetValue < bool > ( SettingsKey . show_reset_connection ) ;
}
} , 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 )
{
this . HAnchor = HAnchor . ParentLeft | HAnchor . FitToChildren ;
this . VAnchor = VAnchor . ParentTop | VAnchor . FitToChildren ;
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 ,
VAnchor = VAnchor . ParentBottomTop ,
} ) ;
}
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 ;
public PrinterConnectButton ( TextImageButtonFactory buttonFactory )
2015-04-08 15:20:10 -07:00
{
2017-05-24 14:19:02 -07:00
this . HAnchor = HAnchor . ParentLeft | HAnchor . FitToChildren ;
this . VAnchor = VAnchor . FitToChildren ;
2017-06-02 13:17:30 -07:00
connectButton = buttonFactory . Generate ( "Connect" . Localize ( ) . ToUpper ( ) , StaticData . Instance . LoadIcon ( "connect.png" , 16 , 16 ) ) ;
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 )
{
if ( ActiveSliceSettings . Instance . PrinterSelected )
{
UserRequestedConnectToActivePrinter ( ) ;
}
}
} ;
this . AddChild ( connectButton ) ;
disconnectButton = buttonFactory . Generate ( "Disconnect" . Localize ( ) . ToUpper ( ) , StaticData . Instance . LoadIcon ( "connect.png" , 16 , 16 ) ) ;
disconnectButton . Name = "Disconnect from printer button" ;
disconnectButton . Visible = false ;
disconnectButton . ToolTipText = "Disconnect from current printer" . Localize ( ) ;
disconnectButton . Click + = ( s , e ) = > UiThread . RunOnIdle ( ( ) = >
{
if ( PrinterConnectionAndCommunication . Instance . PrinterIsPrinting )
{
StyledMessageBox . ShowMessageBox (
( bool disconnectCancel ) = >
{
if ( disconnectCancel )
{
PrinterConnectionAndCommunication . Instance . Stop ( false ) ;
PrinterConnectionAndCommunication . Instance . Disable ( ) ;
}
} ,
disconnectAndCancelMessage ,
disconnectAndCancelTitle ,
StyledMessageBox . MessageType . YES_NO ,
"Disconnect" . Localize ( ) ,
"Stay Connected" . Localize ( ) ) ;
}
else
{
PrinterConnectionAndCommunication . Instance . Disable ( ) ;
}
} ) ;
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-05-24 14:19:02 -07:00
child . VAnchor = VAnchor . ParentTop ;
child . HAnchor = HAnchor . ParentLeft ;
child . Cursor = Cursors . Hand ;
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 ) ;
PrinterConnectionAndCommunication . Instance . EnableChanged . RegisterEvent ( SetVisibleStates , ref unregisterEvents ) ;
PrinterConnectionAndCommunication . Instance . CommunicationStateChanged . RegisterEvent ( SetVisibleStates , ref unregisterEvents ) ;
}
public override void OnClosed ( ClosedEventArgs e )
{
unregisterEvents ? . Invoke ( this , null ) ;
base . OnClosed ( e ) ;
}
static public void UserRequestedConnectToActivePrinter ( )
{
PrinterConnectionAndCommunication . Instance . HaltConnectionThread ( ) ;
PrinterConnectionAndCommunication . Instance . ConnectToActivePrinter ( true ) ;
}
private void SetVisibleStates ( object sender , EventArgs e )
{
UiThread . RunOnIdle ( ( ) = >
2016-08-31 15:10:41 -07:00
{
2017-05-24 14:19:02 -07:00
if ( PrinterConnectionAndCommunication . Instance . PrinterIsConnected )
{
disconnectButton . Visible = true ;
connectButton . Visible = false ;
}
else
{
disconnectButton . Visible = false ;
connectButton . Visible = true ;
}
var communicationState = PrinterConnectionAndCommunication . Instance . CommunicationState ;
// Ensure connect buttons are locked while long running processes are executing to prevent duplicate calls into said actions
connectButton . Enabled = ActiveSliceSettings . Instance . PrinterSelected
& & communicationState ! = PrinterConnectionAndCommunication . CommunicationStates . AttemptingToConnect ;
disconnectButton . Enabled = communicationState ! = PrinterConnectionAndCommunication . CommunicationStates . Disconnecting ;
} ) ;
2015-04-08 15:20:10 -07:00
}
}
}