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 ;
2017-08-20 02:34:39 -07:00
using MatterHackers.Agg.Platform ;
2014-01-29 19:09:30 -08:00
using MatterHackers.Agg.UI ;
using MatterHackers.Localizations ;
2018-01-16 19:01:09 -08:00
using MatterHackers.MatterControl.CustomWidgets ;
2015-04-08 15:20:10 -07:00
using MatterHackers.MatterControl.PrinterCommunication ;
2018-01-06 12:00:14 -08:00
using MatterHackers.MatterControl.PrinterControls.PrinterConnections ;
2017-12-11 17:33:23 -08:00
using MatterHackers.MatterControl.SlicerConfiguration ;
using MatterHackers.SerialPortCommunication.FrostedSerial ;
2014-01-29 19:09:30 -08:00
namespace MatterHackers.MatterControl.ActionBar
{
2017-12-10 21:10:54 -08:00
public class PrinterConnectButton : FlowLayoutWidget
2017-05-24 14:19:02 -07:00
{
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 ( ) ;
2018-01-16 19:01:09 -08:00
private GuiWidget cancelConnectButton ;
2017-05-24 14:19:02 -07:00
private GuiWidget connectButton ;
2018-01-16 19:01:09 -08:00
private GuiWidget 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
2018-01-06 12:00:14 -08:00
private bool listenForConnectFailed = false ;
private long connectStartMs ;
2017-09-25 22:11:02 -07:00
public PrinterConnectButton ( PrinterConfig printer , ThemeConfig theme )
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
2018-01-16 19:01:09 -08:00
connectButton = new TextIconButton (
"Connect" . Localize ( ) ,
2018-04-12 08:42:10 -07:00
AggContext . StaticData . LoadIcon ( "connect.png" , 14 , 14 , theme . InvertIcons ) ,
2018-01-16 19:01:09 -08:00
theme )
{
Name = "Connect to printer button" ,
ToolTipText = "Connect to the currently selected printer" . Localize ( ) ,
BackgroundColor = theme . ToolbarButtonBackground ,
HoverColor = theme . ToolbarButtonHover ,
MouseDownColor = theme . ToolbarButtonDown ,
} ;
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-12-10 21:10:54 -08:00
// add the cancel stop button
2018-01-16 19:01:09 -08:00
cancelConnectButton = new TextIconButton (
"Cancel" . Localize ( ) ,
2018-04-12 08:42:10 -07:00
AggContext . StaticData . LoadIcon ( "connect.png" , 14 , 14 , theme . InvertIcons ) ,
2018-01-16 19:01:09 -08:00
theme )
{
ToolTipText = "Stop trying to connect to the printer." . Localize ( ) ,
BackgroundColor = theme . ToolbarButtonBackground ,
HoverColor = theme . ToolbarButtonHover ,
MouseDownColor = theme . ToolbarButtonDown ,
} ;
2017-12-10 21:10:54 -08:00
cancelConnectButton . Click + = ( s , e ) = > UiThread . RunOnIdle ( ( ) = >
{
2018-01-06 12:00:14 -08:00
listenForConnectFailed = false ;
2017-12-10 21:10:54 -08:00
ApplicationController . Instance . ConditionalCancelPrint ( ) ;
cancelConnectButton . Enabled = false ;
} ) ;
this . AddChild ( cancelConnectButton ) ;
2018-01-16 19:01:09 -08:00
disconnectButton = new TextIconButton (
"Disconnect" . Localize ( ) ,
2018-04-12 08:42:10 -07:00
AggContext . StaticData . LoadIcon ( "connect.png" , 14 , 14 , theme . InvertIcons ) ,
2018-01-16 19:01:09 -08:00
theme )
{
Name = "Disconnect from printer button" ,
Visible = false ,
ToolTipText = "Disconnect from current printer" . Localize ( ) ,
BackgroundColor = theme . ToolbarButtonBackground ,
HoverColor = theme . ToolbarButtonHover ,
MouseDownColor = theme . ToolbarButtonDown ,
} ;
2017-05-24 14:19:02 -07:00
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-12-10 21:10:54 -08:00
child . VAnchor = VAnchor . Center ;
2017-05-24 14:19:02 -07:00
child . Cursor = Cursors . Hand ;
2017-09-25 22:11:02 -07:00
child . Margin = theme . ButtonSpacing ;
2016-08-31 15:10:41 -07:00
}
2017-05-24 14:19:02 -07:00
2017-12-10 21:10:54 -08:00
printer . Connection . EnableChanged . RegisterEvent ( ( s , e ) = > SetVisibleStates ( ) , ref unregisterEvents ) ;
printer . Connection . CommunicationStateChanged . RegisterEvent ( ( s , e ) = > SetVisibleStates ( ) , ref unregisterEvents ) ;
2018-01-06 12:00:14 -08:00
printer . Connection . ConnectionFailed . RegisterEvent ( ( s , e ) = >
{
#if ! __ANDROID__
// TODO: Someday this functionality should be revised to an awaitable Connect() call in the Connect button that
// shows troubleshooting on failed attempts, rather than hooking the failed event and trying to determine if the
// Connect button started the task
if ( listenForConnectFailed
& & UiThread . CurrentTimerMs - connectStartMs < 25000 )
{
2018-01-18 08:26:21 -08:00
UiThread . RunOnIdle ( ( ) = >
{
// User initiated connect attempt failed, show port selection dialog
DialogWindow . Show ( new SetupStepComPortOne ( printer ) ) ;
} ) ;
2018-01-06 12:00:14 -08:00
}
#endif
listenForConnectFailed = false ;
} , ref unregisterEvents ) ;
2017-05-24 14:19:02 -07:00
2017-12-10 21:10:54 -08:00
this . SetVisibleStates ( ) ;
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
{
2018-01-06 12:00:14 -08:00
listenForConnectFailed = true ;
connectStartMs = UiThread . CurrentTimerMs ;
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 ( ) ;
2018-01-06 12:00:14 -08:00
printer . Connection . Connect ( ) ;
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-11-08 15:56:37 -08:00
DialogWindow . Show < SetupWizardTroubleshooting > ( ) ;
2017-08-20 13:23:36 -07:00
}
2017-12-10 21:10:54 -08:00
private void SetChildVisible ( GuiWidget visibleChild , bool enabled )
2017-05-24 14:19:02 -07:00
{
2017-12-10 21:10:54 -08:00
foreach ( var child in Children )
2016-08-31 15:10:41 -07:00
{
2017-12-10 21:10:54 -08:00
if ( child = = visibleChild )
2017-05-24 14:19:02 -07:00
{
2017-12-10 21:10:54 -08:00
child . Visible = true ;
child . Enabled = enabled ;
2017-05-24 14:19:02 -07:00
}
else
{
2017-12-10 21:10:54 -08:00
child . Visible = false ;
2017-05-24 14:19:02 -07:00
}
2017-12-10 21:10:54 -08:00
}
}
2017-05-24 14:19:02 -07:00
2017-12-10 21:10:54 -08:00
private void SetVisibleStates ( )
{
switch ( printer . Connection . CommunicationState )
{
case CommunicationStates . FailedToConnect :
case CommunicationStates . Disconnected :
case CommunicationStates . ConnectionLost :
SetChildVisible ( connectButton , true ) ;
break ;
case CommunicationStates . Disconnecting :
SetChildVisible ( disconnectButton , false ) ;
break ;
case CommunicationStates . AttemptingToConnect :
SetChildVisible ( cancelConnectButton , true ) ;
break ;
default :
2018-01-06 12:00:14 -08:00
listenForConnectFailed = false ;
2017-12-10 21:10:54 -08:00
SetChildVisible ( disconnectButton , true ) ;
break ;
}
2015-04-08 15:20:10 -07:00
}
}
}