mattercontrol/ActionBar/PrinterActionRow.cs

286 lines
11 KiB
C#
Raw Normal View History

2014-01-29 19:09:30 -08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using MatterHackers.Agg.Image;
using MatterHackers.Agg;
using MatterHackers.Agg.UI;
using MatterHackers.VectorMath;
using MatterHackers.MatterControl;
using MatterHackers.MatterControl.PrintQueue;
using MatterHackers.MatterControl.DataStorage;
2015-02-25 16:14:32 -08:00
using MatterHackers.MatterControl.SlicerConfiguration;
2014-01-29 19:09:30 -08:00
using MatterHackers.MatterControl.PrinterControls.PrinterConnections;
using MatterHackers.MatterControl.PrinterCommunication;
2014-01-29 19:09:30 -08:00
using MatterHackers.Localizations;
namespace MatterHackers.MatterControl.ActionBar
{
class PrinterActionRow : ActionRowBase
{
TextImageButtonFactory actionBarButtonFactory = new TextImageButtonFactory();
Button connectPrinterButton;
Button disconnectPrinterButton;
Button selectActivePrinterButton;
Button resetConnectionButton;
2014-01-29 19:09:30 -08:00
ConnectionWindow connectionWindow;
bool connectionWindowIsOpen = false;
protected override void Initialize()
{
2014-03-21 16:18:47 -07:00
actionBarButtonFactory.normalTextColor = ActiveTheme.Instance.PrimaryTextColor;
actionBarButtonFactory.hoverTextColor = ActiveTheme.Instance.PrimaryTextColor;
actionBarButtonFactory.pressedTextColor = ActiveTheme.Instance.PrimaryTextColor;
2014-01-29 19:09:30 -08:00
2014-03-21 16:18:47 -07:00
actionBarButtonFactory.disabledTextColor = ActiveTheme.Instance.TabLabelUnselected;
2014-01-29 19:09:30 -08:00
actionBarButtonFactory.disabledFillColor = ActiveTheme.Instance.PrimaryBackgroundColor;
actionBarButtonFactory.disabledBorderColor = ActiveTheme.Instance.PrimaryBackgroundColor;
actionBarButtonFactory.hoverFillColor = ActiveTheme.Instance.PrimaryBackgroundColor;
2014-01-29 19:09:30 -08:00
actionBarButtonFactory.invertImageLocation = true;
actionBarButtonFactory.borderWidth = 0;
this.BackgroundColor = ActiveTheme.Instance.PrimaryBackgroundColor;
}
protected override void AddChildElements()
{
actionBarButtonFactory.invertImageLocation = false;
actionBarButtonFactory.borderWidth = 1;
if (ActiveTheme.Instance.IsDarkTheme)
{
actionBarButtonFactory.normalBorderColor = new RGBA_Bytes(77, 77, 77);
}
else
{
actionBarButtonFactory.normalBorderColor = new RGBA_Bytes(190, 190, 190);
}
actionBarButtonFactory.hoverBorderColor = new RGBA_Bytes(128, 128, 128);
string connectString = "Connect".Localize().ToUpper();
2014-01-29 19:09:30 -08:00
connectPrinterButton = actionBarButtonFactory.Generate(connectString, "icon_power_32x32.png");
if (ApplicationController.Instance.WidescreenMode)
{
connectPrinterButton.Margin = new BorderDouble(0, 0, 3, 3);
}
else
{
connectPrinterButton.Margin = new BorderDouble(6, 0, 3, 3);
}
2014-04-27 20:24:58 -07:00
connectPrinterButton.VAnchor = VAnchor.ParentTop;
2014-01-29 19:09:30 -08:00
connectPrinterButton.Cursor = Cursors.Hand;
string disconnectString = "Disconnect".Localize().ToUpper();
2014-01-29 19:09:30 -08:00
disconnectPrinterButton = actionBarButtonFactory.Generate(disconnectString, "icon_power_32x32.png");
if (ApplicationController.Instance.WidescreenMode)
{
disconnectPrinterButton.Margin = new BorderDouble(0, 0, 3, 3);
}
else
{
disconnectPrinterButton.Margin = new BorderDouble(6, 0, 3, 3);
}
2014-04-27 20:24:58 -07:00
disconnectPrinterButton.VAnchor = VAnchor.ParentTop;
2014-01-29 19:09:30 -08:00
disconnectPrinterButton.Cursor = Cursors.Hand;
selectActivePrinterButton = new PrinterSelectButton();
selectActivePrinterButton.HAnchor = HAnchor.ParentLeftRight;
selectActivePrinterButton.Cursor = Cursors.Hand;
if (ApplicationController.Instance.WidescreenMode)
2014-03-21 18:44:01 -07:00
{
2014-04-27 20:49:29 -07:00
selectActivePrinterButton.Margin = new BorderDouble(0, 6,0,3);
2014-03-21 18:44:01 -07:00
}
else
{
2014-04-27 20:49:29 -07:00
selectActivePrinterButton.Margin = new BorderDouble(0, 6, 6, 3);
2014-03-21 18:44:01 -07:00
}
2015-02-25 16:14:32 -08:00
string resetConnectionText = "Reset\nConnection".Localize().ToUpper();
resetConnectionButton = actionBarButtonFactory.Generate(resetConnectionText, "e_stop4.png");
2015-02-25 16:14:32 -08:00
if (ApplicationController.Instance.WidescreenMode)
{
resetConnectionButton.Margin = new BorderDouble(0, 0, 3, 3);
2015-02-25 16:14:32 -08:00
}
else
{
resetConnectionButton.Margin = new BorderDouble(6, 0, 3, 3);
2015-02-25 16:14:32 -08:00
}
2014-03-21 16:18:47 -07:00
2015-02-25 17:56:25 -08:00
// Bind connect button states to active printer state
this.SetConnectionButtonVisibleState(null);
2014-01-29 19:09:30 -08:00
actionBarButtonFactory.invertImageLocation = true;
this.AddChild(connectPrinterButton);
this.AddChild(disconnectPrinterButton);
this.AddChild(selectActivePrinterButton);
this.AddChild(resetConnectionButton);
2014-03-21 16:18:47 -07:00
//this.AddChild(CreateOptionsMenu());
2014-01-29 19:09:30 -08:00
}
event EventHandler unregisterEvents;
protected override void AddHandlers()
{
ActivePrinterProfile.Instance.ActivePrinterChanged.RegisterEvent(ReloadPrinterSelectionWidget, ref unregisterEvents);
ActivePrinterProfile.Instance.ActivePrinterChanged.RegisterEvent(onActivePrinterChanged, ref unregisterEvents);
PrinterConnectionAndCommunication.Instance.EnableChanged.RegisterEvent(onPrinterStatusChanged, ref unregisterEvents);
PrinterConnectionAndCommunication.Instance.CommunicationStateChanged.RegisterEvent(onPrinterStatusChanged, ref unregisterEvents);
2014-01-29 19:09:30 -08:00
selectActivePrinterButton.Click += new EventHandler(onSelectActivePrinterButton_Click);
connectPrinterButton.Click += new EventHandler(onConnectButton_Click);
disconnectPrinterButton.Click += new EventHandler(onDisconnectButtonClick);
resetConnectionButton.Click += new EventHandler(resetConnectionButton_Click);
2014-01-29 19:09:30 -08:00
base.AddHandlers();
}
public override void OnClosed(EventArgs e)
{
if (unregisterEvents != null)
{
unregisterEvents(this, null);
}
base.OnClosed(e);
}
void onConnectButton_Click(object sender, EventArgs mouseEvent)
2014-01-29 19:09:30 -08:00
{
Button buttonClicked = ((Button)sender);
2014-01-29 19:09:30 -08:00
if (buttonClicked.Enabled)
{
if (ActivePrinterProfile.Instance.ActivePrinter == null)
2014-01-29 19:09:30 -08:00
{
OpenConnectionWindow(ConnectToActivePrinter);
2014-01-29 19:09:30 -08:00
}
else
{
ConnectToActivePrinter();
}
}
}
void resetConnectionButton_Click(object sender, EventArgs mouseEvent)
2015-02-25 16:14:32 -08:00
{
PrinterConnectionAndCommunication.Instance.RebootBoard();
}
2014-01-29 19:09:30 -08:00
void ConnectToActivePrinter()
{
PrinterConnectionAndCommunication.Instance.HaltConnectionThread();
PrinterConnectionAndCommunication.Instance.ConnectToActivePrinter();
2014-01-29 19:09:30 -08:00
}
void onSelectActivePrinterButton_Click(object sender, EventArgs mouseEvent)
2014-01-29 19:09:30 -08:00
{
OpenConnectionWindow();
}
public delegate void ConnectOnSelectFunction();
ConnectOnSelectFunction functionToCallOnSelect;
void OpenConnectionWindow(ConnectOnSelectFunction functionToCallOnSelect = null)
2014-01-29 19:09:30 -08:00
{
if (this.connectionWindowIsOpen == false)
{
connectionWindow = new ConnectionWindow();
this.connectionWindowIsOpen = true;
//This function gets called on printer selection (see onActivePrinterChanged)
this.functionToCallOnSelect = functionToCallOnSelect;
2014-01-29 19:09:30 -08:00
connectionWindow.Closed += new EventHandler(ConnectionWindow_Closed);
}
else
{
if (connectionWindow != null)
{
connectionWindow.BringToFront();
}
}
}
void ConnectionWindow_Closed(object sender, EventArgs e)
{
this.connectionWindowIsOpen = false;
}
void ReloadPrinterSelectionWidget(object sender, EventArgs e)
{
//selectActivePrinterButton.Invalidate();
}
void onActivePrinterChanged(object sender, EventArgs e)
{
connectPrinterButton.Enabled = true;
if (functionToCallOnSelect != null)
{
functionToCallOnSelect();
functionToCallOnSelect = null;
}
2014-01-29 19:09:30 -08:00
}
void onDisconnectButtonClick(object sender, EventArgs e)
2014-01-29 19:09:30 -08:00
{
UiThread.RunOnIdle(OnIdleDisconnect);
}
string disconnectAndCancelMessage = "Disconnect and cancel the current print?".Localize();
string disconnectAndCancelTitle = "WARNING: Disconnecting will cancel the print.".Localize();
2014-01-29 19:09:30 -08:00
void OnIdleDisconnect(object state)
{
if (PrinterConnectionAndCommunication.Instance.PrinterIsPrinting)
2014-01-29 19:09:30 -08:00
{
StyledMessageBox.ShowMessageBox(onConfirmStopPrint, disconnectAndCancelMessage, disconnectAndCancelTitle, StyledMessageBox.MessageType.YES_NO);
2014-01-29 19:09:30 -08:00
}
else
2014-01-29 19:09:30 -08:00
{
PrinterConnectionAndCommunication.Instance.Disable();
2014-01-29 19:09:30 -08:00
selectActivePrinterButton.Invalidate();
}
}
void onConfirmStopPrint(bool messageBoxResponse)
{
if (messageBoxResponse)
{
PrinterConnectionAndCommunication.Instance.Stop();
PrinterConnectionAndCommunication.Instance.Disable();
selectActivePrinterButton.Invalidate();
}
}
2015-02-25 17:56:25 -08:00
void SetConnectionButtonVisibleState(object state)
{
if (PrinterConnectionAndCommunication.Instance.PrinterIsConnected)
2014-01-29 19:09:30 -08:00
{
disconnectPrinterButton.Visible = true;
connectPrinterButton.Visible = false;
2014-01-29 19:09:30 -08:00
}
else
2014-01-29 19:09:30 -08:00
{
disconnectPrinterButton.Visible = false;
connectPrinterButton.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
connectPrinterButton.Enabled = communicationState != PrinterConnectionAndCommunication.CommunicationStates.AttemptingToConnect;
disconnectPrinterButton.Enabled = communicationState != PrinterConnectionAndCommunication.CommunicationStates.Disconnecting;
resetConnectionButton.Visible = PrinterConnectionAndCommunication.Instance.PrinterIsConnected && ActiveSliceSettings.Instance.ShowResetConnection();
2014-01-29 19:09:30 -08:00
}
void onPrinterStatusChanged(object sender, EventArgs e)
2014-01-29 19:09:30 -08:00
{
2015-02-25 17:56:25 -08:00
UiThread.RunOnIdle(SetConnectionButtonVisibleState);
2014-01-29 19:09:30 -08:00
}
}
}