2016-04-18 11:31:31 -07:00
|
|
|
|
/*
|
2018-07-15 10:07:30 -07:00
|
|
|
|
Copyright (c) 2018, 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;
|
2018-11-23 11:06:02 -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
|
|
|
|
{
|
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-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(),
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
2018-06-24 10:34:29 -07:00
|
|
|
|
theme.ApplyPrimaryActionStyle(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;
|
2018-11-12 07:57:13 -08:00
|
|
|
|
printer.CancelPrint();
|
2017-12-10 21:10:54 -08:00
|
|
|
|
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(() =>
|
|
|
|
|
|
{
|
2019-02-06 10:34:19 -08:00
|
|
|
|
if (printer.Connection.Printing)
|
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
|
|
|
|
}
|
|
|
|
|
|
},
|
2018-06-24 09:52:03 -07:00
|
|
|
|
"WARNING: Disconnecting will stop the current print.\n\nAre you sure you want to disconnect?".Localize(),
|
|
|
|
|
|
"Disconnect and stop the current print?".Localize(),
|
2017-05-24 14:19:02 -07:00
|
|
|
|
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
|
|
|
|
|
2018-11-16 08:44:56 -08:00
|
|
|
|
// Register listeners
|
|
|
|
|
|
printer.Connection.CommunicationStateChanged += Connection_CommunicationStateChanged;
|
|
|
|
|
|
printer.Connection.ConnectionFailed += Connection_Failed;
|
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
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-23 16:44:11 -07:00
|
|
|
|
public override void OnClosed(EventArgs e)
|
2017-05-24 14:19:02 -07:00
|
|
|
|
{
|
2018-11-16 08:44:56 -08:00
|
|
|
|
// Unregister listeners
|
|
|
|
|
|
printer.Connection.CommunicationStateChanged -= Connection_CommunicationStateChanged;
|
|
|
|
|
|
printer.Connection.ConnectionFailed -= Connection_Failed;
|
|
|
|
|
|
|
2017-05-24 14:19:02 -07:00
|
|
|
|
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;
|
|
|
|
|
|
|
2019-03-11 11:42:37 -07:00
|
|
|
|
if (AppContext.Platform.HasPermissionToDevice(printer))
|
2017-08-07 06:54:21 -07:00
|
|
|
|
{
|
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
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-16 08:44:56 -08:00
|
|
|
|
private void Connection_Failed(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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2018-11-16 08:44:56 -08:00
|
|
|
|
|
|
|
|
|
|
private void Connection_CommunicationStateChanged(object s, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.SetVisibleStates();
|
|
|
|
|
|
}
|
2015-04-08 15:20:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|