mattercontrol/MatterControlLib/PartPreviewWindow/View3D/PrinterBar/PrinterConnectButton.cs
2018-11-09 14:32:04 -08:00

263 lines
No EOL
8.1 KiB
C#

/*
Copyright (c) 2018, 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 MatterHackers.Agg.Platform;
using MatterHackers.Agg.UI;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.CustomWidgets;
using MatterHackers.MatterControl.PrinterCommunication;
using MatterHackers.MatterControl.PrinterControls.PrinterConnections;
using MatterHackers.MatterControl.SlicerConfiguration;
using MatterHackers.SerialPortCommunication.FrostedSerial;
namespace MatterHackers.MatterControl.ActionBar
{
public class PrinterConnectButton : FlowLayoutWidget
{
private GuiWidget cancelConnectButton;
private GuiWidget connectButton;
private GuiWidget disconnectButton;
private EventHandler unregisterEvents;
private PrinterConfig printer;
private bool listenForConnectFailed = false;
private long connectStartMs;
public PrinterConnectButton(PrinterConfig printer, ThemeConfig theme)
{
this.printer = printer;
this.HAnchor = HAnchor.Left | HAnchor.Fit;
this.VAnchor = VAnchor.Fit;
this.Margin = 0;
this.Padding = 0;
connectButton = new TextIconButton(
"Connect".Localize(),
AggContext.StaticData.LoadIcon("connect.png", 14, 14, theme.InvertIcons),
theme)
{
Name = "Connect to printer button",
ToolTipText = "Connect to the currently selected printer".Localize(),
MouseDownColor = theme.ToolbarButtonDown,
};
connectButton.Click += (s, e) =>
{
if (connectButton.Enabled)
{
if (printer.Settings.PrinterSelected)
{
UserRequestedConnectToActivePrinter();
}
}
};
this.AddChild(connectButton);
theme.ApplyPrimaryActionStyle(connectButton);
// add the cancel stop button
cancelConnectButton = new TextIconButton(
"Cancel".Localize(),
AggContext.StaticData.LoadIcon("connect.png", 14, 14, theme.InvertIcons),
theme)
{
ToolTipText = "Stop trying to connect to the printer.".Localize(),
BackgroundColor = theme.ToolbarButtonBackground,
HoverColor = theme.ToolbarButtonHover,
MouseDownColor = theme.ToolbarButtonDown,
};
cancelConnectButton.Click += (s, e) => UiThread.RunOnIdle(() =>
{
listenForConnectFailed = false;
ApplicationController.Instance.ConditionallyCancelPrint();
cancelConnectButton.Enabled = false;
});
this.AddChild(cancelConnectButton);
disconnectButton = new TextIconButton(
"Disconnect".Localize(),
AggContext.StaticData.LoadIcon("connect.png", 14, 14, theme.InvertIcons),
theme)
{
Name = "Disconnect from printer button",
Visible = false,
ToolTipText = "Disconnect from current printer".Localize(),
BackgroundColor = theme.ToolbarButtonBackground,
HoverColor = theme.ToolbarButtonHover,
MouseDownColor = theme.ToolbarButtonDown,
};
disconnectButton.Click += (s, e) => UiThread.RunOnIdle(() =>
{
if (printer.Connection.PrinterIsPrinting)
{
StyledMessageBox.ShowMessageBox(
(bool disconnectCancel) =>
{
if (disconnectCancel)
{
printer.Connection.Stop(false);
printer.Connection.Disable();
}
},
"WARNING: Disconnecting will stop the current print.\n\nAre you sure you want to disconnect?".Localize(),
"Disconnect and stop the current print?".Localize(),
StyledMessageBox.MessageType.YES_NO,
"Disconnect".Localize(),
"Stay Connected".Localize());
}
else
{
printer.Connection.Disable();
}
});
this.AddChild(disconnectButton);
foreach (var child in Children)
{
child.VAnchor = VAnchor.Center;
child.Cursor = Cursors.Hand;
child.Margin = theme.ButtonSpacing;
}
void CommunicationStateChanged(object s, EventArgs e)
{
this.SetVisibleStates();
}
printer.Connection.CommunicationStateChanged += CommunicationStateChanged;
this.Closed += (s, e) => printer.Connection.CommunicationStateChanged -= CommunicationStateChanged;
void EnableChanged(object s, EventArgs e)
{
SetVisibleStates();
}
printer.Connection.EnableChanged += EnableChanged;
this.Closed += (s, e) => printer.Connection.EnableChanged -= EnableChanged;
void ConnectionFailed(object s, EventArgs 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)
{
UiThread.RunOnIdle(() =>
{
// User initiated connect attempt failed, show port selection dialog
DialogWindow.Show(new SetupStepComPortOne(printer));
});
}
#endif
listenForConnectFailed = false;
}
printer.Connection.ConnectionFailed += ConnectionFailed;
this.Closed += (s, e) => printer.Connection.ConnectionFailed -= ConnectionFailed;
this.SetVisibleStates();
}
public override void OnClosed(EventArgs e)
{
unregisterEvents?.Invoke(this, null);
base.OnClosed(e);
}
public void UserRequestedConnectToActivePrinter()
{
if (printer.Settings.PrinterSelected)
{
listenForConnectFailed = true;
connectStartMs = UiThread.CurrentTimerMs;
#if __ANDROID__
if (!printer.Settings.GetValue<bool>(SettingsKey.enable_network_printing)
&& !FrostedSerialPort.HasPermissionToDevice())
{
// Opens the USB device permissions dialog which will call back into our UsbDevice broadcast receiver to connect
FrostedSerialPort.RequestPermissionToDevice(RunTroubleShooting);
}
else
#endif
{
printer.Connection.HaltConnectionThread();
printer.Connection.Connect();
}
}
}
private static void RunTroubleShooting()
{
DialogWindow.Show(
new SetupWizardTroubleshooting(ApplicationController.Instance.ActivePrinter));
}
private void SetChildVisible(GuiWidget visibleChild, bool enabled)
{
foreach (var child in Children)
{
if (child == visibleChild)
{
child.Visible = true;
child.Enabled = enabled;
}
else
{
child.Visible = false;
}
}
}
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:
listenForConnectFailed = false;
SetChildVisible(disconnectButton, true);
break;
}
}
}
}