mattercontrol/MatterControlLib/PrinterControls/PrinterConnections/SetupStepComPortTwo.cs

191 lines
6.2 KiB
C#
Raw Normal View History

2017-08-23 23:59:45 -07:00
/*
2019-04-05 13:18:32 -07:00
Copyright (c) 2019, Lars Brubaker, John Lewin
2017-08-23 23:59:45 -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.
*/
using System;
using System.IO;
2017-08-23 23:59:45 -07:00
using System.Linq;
using MatterHackers.Agg;
using MatterHackers.Agg.Platform;
2014-01-29 19:09:30 -08:00
using MatterHackers.Agg.UI;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.PrinterCommunication;
using MatterHackers.SerialPortCommunication.FrostedSerial;
2014-01-29 19:09:30 -08:00
namespace MatterHackers.MatterControl.PrinterControls.PrinterConnections
{
public class SetupStepComPortTwo : DialogPage
2015-04-08 15:20:10 -07:00
{
private string[] startingPortNames;
2018-04-14 20:51:01 -07:00
private GuiWidget nextButton;
private GuiWidget connectButton;
2015-04-08 15:20:10 -07:00
private TextWidget printerErrorMessage;
2017-09-17 21:08:16 -07:00
private PrinterConfig printer;
2015-04-08 15:20:10 -07:00
2017-09-17 21:08:16 -07:00
public SetupStepComPortTwo(PrinterConfig printer)
2015-04-08 15:20:10 -07:00
{
2017-09-17 21:08:16 -07:00
this.printer = printer;
startingPortNames = FrostedSerialPort.GetPortNames();
contentRow.AddChild(createPrinterConnectionMessageContainer());
//Construct buttons
2018-04-14 20:51:01 -07:00
nextButton = theme.CreateDialogButton("Done".Localize());
nextButton.Click += (s, e) => Parent.Close();
nextButton.Visible = false;
2014-01-29 19:09:30 -08:00
2018-04-14 20:51:01 -07:00
connectButton = theme.CreateDialogButton("Connect".Localize());
connectButton.Click += (s, e) =>
{
// Select the first port that's in GetPortNames() but not in startingPortNames
string candidatePort = FrostedSerialPort.GetPortNames().Except(startingPortNames).FirstOrDefault();
if (candidatePort == null)
{
2017-10-31 11:43:25 -07:00
printerErrorMessage.TextColor = Color.Red;
printerErrorMessage.Text = "Oops! Printer could not be detected ".Localize();
}
else
{
printerErrorMessage.TextColor = theme.TextColor;
printerErrorMessage.Text = "Attempting to connect".Localize() + "...";
printer.Settings.Helpers.SetComPort(candidatePort);
printer.Connection.Connect();
connectButton.Visible = false;
}
};
var backButton = theme.CreateDialogButton("<< Back".Localize());
backButton.Click += (s, e) =>
{
DialogWindow.ChangeToPage(new SetupStepComPortOne(printer));
};
this.AddPageAction(nextButton);
this.AddPageAction(backButton);
this.AddPageAction(connectButton);
2018-11-16 08:44:56 -08:00
// Register listeners
printer.Connection.CommunicationStateChanged += Connection_CommunicationStateChanged;
}
protected override void OnCancel(out bool abortCancel)
{
printer.Connection.HaltConnectionThread();
abortCancel = false;
2015-04-08 15:20:10 -07:00
}
public override void OnClosed(EventArgs e)
2015-04-08 15:20:10 -07:00
{
2018-11-16 08:44:56 -08:00
// Unregister listeners
printer.Connection.CommunicationStateChanged -= Connection_CommunicationStateChanged;
2015-04-08 15:20:10 -07:00
base.OnClosed(e);
}
public FlowLayoutWidget createPrinterConnectionMessageContainer()
{
2019-04-05 13:18:32 -07:00
var container = new FlowLayoutWidget(FlowDirection.TopToBottom)
{
VAnchor = VAnchor.Stretch,
Margin = new BorderDouble(5)
};
var elementMargin = new BorderDouble(top: 5);
var printerMessageOne = new TextWidget("MatterControl will now attempt to auto-detect printer.".Localize(), 0, 0, 10)
{
Margin = elementMargin,
TextColor = theme.TextColor,
HAnchor = HAnchor.Stretch
};
container.AddChild(printerMessageOne);
var printerMessageFour = new TextWidget(string.Format("1.) {0}.", "Connect printer (make sure it is on)".Localize()), 0, 0, 12)
{
TextColor = theme.TextColor,
HAnchor = HAnchor.Stretch,
Margin = elementMargin
};
container.AddChild(printerMessageFour);
var printerMessageFive = new TextWidget(string.Format("2.) {0} '{1}'.", "Press".Localize(), "Connect".Localize()), 0, 0, 12)
{
TextColor = theme.TextColor,
HAnchor = HAnchor.Stretch,
Margin = elementMargin
};
2014-01-29 19:09:30 -08:00
2017-09-17 21:08:16 -07:00
printerErrorMessage = new TextWidget("", 0, 0, 10)
{
AutoExpandBoundsToText = true,
2017-10-31 11:43:25 -07:00
TextColor = Color.Red,
2017-09-17 21:08:16 -07:00
HAnchor = HAnchor.Stretch,
Margin = elementMargin
};
2015-04-08 15:20:10 -07:00
container.AddChild(printerErrorMessage);
2019-04-05 13:18:32 -07:00
var removeImage = AggContext.StaticData.LoadImage(Path.Combine("Images", "insert usb.png")).SetPreMultiply();
container.AddChild(new ImageWidget(removeImage)
{
HAnchor = HAnchor.Center,
Margin = new BorderDouble(0, 10),
});
2019-04-05 13:18:32 -07:00
container.AddChild(new GuiWidget
{
VAnchor = VAnchor.Stretch
});
2015-04-08 15:20:10 -07:00
container.HAnchor = HAnchor.Stretch;
2015-04-08 15:20:10 -07:00
return container;
}
2018-11-16 08:44:56 -08:00
private void Connection_CommunicationStateChanged(object sender, EventArgs e)
2015-04-08 15:20:10 -07:00
{
if (printer.Connection.IsConnected)
2015-04-08 15:20:10 -07:00
{
printerErrorMessage.TextColor = theme.TextColor;
2016-04-18 11:31:31 -07:00
printerErrorMessage.Text = "Connection succeeded".Localize() + "!";
nextButton.Visible = true;
connectButton.Visible = false;
this?.Parent?.Close();
2015-04-08 15:20:10 -07:00
}
2017-09-17 21:08:16 -07:00
else if (printer.Connection.CommunicationState != CommunicationStates.AttemptingToConnect)
2015-04-08 15:20:10 -07:00
{
2017-10-31 11:43:25 -07:00
printerErrorMessage.TextColor = Color.Red;
2016-04-18 11:31:31 -07:00
printerErrorMessage.Text = "Uh-oh! Could not connect to printer.".Localize();
connectButton.Visible = true;
nextButton.Visible = false;
2015-04-08 15:20:10 -07:00
}
}
}
}