mattercontrol/ActionBar/PrintActionRow.cs

482 lines
16 KiB
C#
Raw Normal View History

2015-04-08 15:20:10 -07:00
using MatterHackers.Agg;
2014-01-29 19:09:30 -08:00
using MatterHackers.Agg.UI;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.DataStorage;
2015-04-08 15:20:10 -07:00
using MatterHackers.MatterControl.PrinterCommunication;
using MatterHackers.MatterControl.PrintQueue;
using MatterHackers.MatterControl.SlicerConfiguration;
2015-04-09 12:05:05 -07:00
#if __ANDROID__
using MatterHackers.SerialPortCommunication.FrostedSerial;
#endif
2015-04-08 15:20:10 -07:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
2014-01-29 19:09:30 -08:00
namespace MatterHackers.MatterControl.ActionBar
{
2015-04-08 15:20:10 -07:00
internal class PrintActionRow : ActionRowBase
{
private List<TooltipButton> activePrintButtons = new List<TooltipButton>();
private TooltipButton addButton;
2015-04-09 09:46:42 -07:00
private List<TooltipButton> allPrintButtons = new List<TooltipButton>();
2015-04-08 15:20:10 -07:00
private TooltipButton cancelButton;
2015-04-09 09:46:42 -07:00
private TooltipButton cancelConnectButton;
private string cancelCurrentPrintMessage = "Cancel the current print?".Localize();
private string cancelCurrentPrintTitle = "Cancel Print?".Localize();
2015-04-09 12:05:05 -07:00
private TooltipButton connectButton;
private TooltipButton resetConnectionButton;
2015-04-08 15:20:10 -07:00
private TooltipButton doneWithCurrentPartButton;
2015-04-09 09:46:42 -07:00
private TooltipButton pauseButton;
2015-04-08 15:20:10 -07:00
private QueueDataView queueDataView;
2015-04-09 09:46:42 -07:00
private TooltipButton removeButton;
private TooltipButton reprintButton;
private TooltipButton resumeButton;
private TooltipButton skipButton;
private TooltipButton startButton;
private MatterHackers.MatterControl.TextImageButtonFactory textImageButtonFactory = new MatterHackers.MatterControl.TextImageButtonFactory();
private Stopwatch timeSincePrintStarted = new Stopwatch();
2015-04-09 12:05:05 -07:00
2015-04-08 15:20:10 -07:00
public PrintActionRow(QueueDataView queueDataView)
{
this.queueDataView = queueDataView;
}
2015-04-09 09:46:42 -07:00
private event EventHandler unregisterEvents;
public override void OnClosed(EventArgs e)
2015-04-08 15:20:10 -07:00
{
2015-04-09 09:46:42 -07:00
if (unregisterEvents != null)
{
unregisterEvents(this, null);
}
base.OnClosed(e);
}
2015-04-08 15:20:10 -07:00
2015-04-09 09:46:42 -07:00
public void ThemeChanged(object sender, EventArgs e)
{
this.Invalidate();
2015-04-08 15:20:10 -07:00
}
protected override void AddChildElements()
{
addButton = (TooltipButton)textImageButtonFactory.GenerateTooltipButton(LocalizedString.Get("Add"), "icon_circle_plus.png");
2014-03-11 15:24:47 -07:00
addButton.tooltipText = LocalizedString.Get("Add a file to be printed");
2015-04-09 12:05:05 -07:00
addButton.Margin = new BorderDouble(6, 6, 6, 3);
2014-01-29 19:09:30 -08:00
2014-11-05 16:33:22 -08:00
startButton = (TooltipButton)textImageButtonFactory.GenerateTooltipButton(LocalizedString.Get("Print"), "icon_play_32x32.png");
2014-03-11 15:24:47 -07:00
startButton.tooltipText = LocalizedString.Get("Begin printing the selected item.");
2015-04-09 12:05:05 -07:00
startButton.Margin = new BorderDouble(6, 6, 6, 3);
string connectButtonText = LocalizedString.Get("Connect");
string connectButtonMessage = LocalizedString.Get("Connect to the printer");
connectButton = (TooltipButton)textImageButtonFactory.GenerateTooltipButton(connectButtonText, "icon_power_32x32.png");
connectButton.tooltipText = connectButtonMessage;
connectButton.Margin = new BorderDouble(6, 6, 6, 3);
string resetConnectionButtontText = "Reset".Localize();
string resetConnectionButtonMessage = "Reboots the firmware on the controller".Localize();
resetConnectionButton = (TooltipButton)textImageButtonFactory.GenerateTooltipButton(resetConnectionButtontText, "e_stop4.png");
resetConnectionButton.tooltipText = resetConnectionButtonMessage;
resetConnectionButton.Margin = new BorderDouble(6, 6, 6, 3);
2014-01-29 19:09:30 -08:00
string skipButtonText = LocalizedString.Get("Skip");
2014-03-11 15:24:47 -07:00
string skipButtonMessage = LocalizedString.Get("Skip the current item and move to the next in queue");
skipButton = makeButton(skipButtonText, skipButtonMessage);
2014-01-29 19:09:30 -08:00
string removeButtonText = LocalizedString.Get("Remove");
2014-03-11 15:24:47 -07:00
string removeButtonMessage = LocalizedString.Get("Remove current item from queue");
removeButton = makeButton(removeButtonText, removeButtonMessage);
2014-01-29 19:09:30 -08:00
string pauseButtonText = LocalizedString.Get("Pause");
2014-03-11 15:24:47 -07:00
string pauseButtonMessage = LocalizedString.Get("Pause the current print");
pauseButton = makeButton(pauseButtonText, pauseButtonMessage);
2014-01-29 19:09:30 -08:00
string cancelCancelButtonText = LocalizedString.Get("Cancel Connect");
2015-04-08 15:20:10 -07:00
string cancelConnectButtonMessage = LocalizedString.Get("Stop trying to connect to the printer.");
cancelConnectButton = makeButton(cancelCancelButtonText, cancelConnectButtonMessage);
string cancelButtonText = LocalizedString.Get("Cancel");
2014-03-11 15:24:47 -07:00
string cancelButtonMessage = LocalizedString.Get("Stop the current print");
cancelButton = makeButton(cancelButtonText, cancelButtonMessage);
2015-04-09 12:05:05 -07:00
string resumeButtonText = "Resume".Localize();
string resumeButtonMessage = "Resume the current print".Localize();
resumeButton = makeButton(resumeButtonText, resumeButtonMessage);
2015-04-09 12:05:05 -07:00
string reprintButtonText = "Print Again".Localize();
2015-04-08 15:20:10 -07:00
string reprintButtonMessage = LocalizedString.Get("Print current item again");
reprintButton = makeButton(reprintButtonText, reprintButtonMessage);
2015-04-08 15:20:10 -07:00
string doneCurrentPartButtonText = LocalizedString.Get("Done");
string doenCurrentPartButtonMessage = LocalizedString.Get("Move to next print in queue");
doneWithCurrentPartButton = makeButton(doneCurrentPartButtonText, doenCurrentPartButtonMessage);
2014-01-29 19:09:30 -08:00
2015-04-09 12:05:05 -07:00
this.Margin = new BorderDouble(0, 0, 10, 0);
this.HAnchor = HAnchor.FitToChildren;
this.AddChild(connectButton);
allPrintButtons.Add(connectButton);
2015-04-08 15:20:10 -07:00
this.AddChild(addButton);
allPrintButtons.Add(addButton);
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
this.AddChild(startButton);
allPrintButtons.Add(startButton);
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
this.AddChild(pauseButton);
allPrintButtons.Add(pauseButton);
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
this.AddChild(resumeButton);
allPrintButtons.Add(resumeButton);
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
this.AddChild(doneWithCurrentPartButton);
allPrintButtons.Add(doneWithCurrentPartButton);
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
this.AddChild(skipButton);
allPrintButtons.Add(skipButton);
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
this.AddChild(cancelButton);
allPrintButtons.Add(cancelButton);
2014-01-29 19:09:30 -08:00
this.AddChild(cancelConnectButton);
2015-04-08 15:20:10 -07:00
allPrintButtons.Add(cancelConnectButton);
2015-04-08 15:20:10 -07:00
this.AddChild(reprintButton);
allPrintButtons.Add(reprintButton);
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
this.AddChild(removeButton);
allPrintButtons.Add(removeButton);
2014-01-29 19:09:30 -08:00
2015-04-09 12:05:05 -07:00
this.AddChild(resetConnectionButton);
allPrintButtons.Add(resetConnectionButton);
2015-04-08 15:20:10 -07:00
SetButtonStates();
}
protected override void AddHandlers()
{
PrinterConnectionAndCommunication.Instance.ActivePrintItemChanged.RegisterEvent(onStateChanged, ref unregisterEvents);
PrinterConnectionAndCommunication.Instance.CommunicationStateChanged.RegisterEvent(onStateChanged, ref unregisterEvents);
addButton.Click += new EventHandler(onAddButton_Click);
startButton.Click += new EventHandler(onStartButton_Click);
skipButton.Click += new EventHandler(onSkipButton_Click);
removeButton.Click += new EventHandler(onRemoveButton_Click);
resumeButton.Click += new EventHandler(onResumeButton_Click);
pauseButton.Click += new EventHandler(onPauseButton_Click);
2015-04-09 12:05:05 -07:00
connectButton.Click += new EventHandler(onConnectButton_Click);
resetConnectionButton.Click += (sender, e) => { UiThread.RunOnIdle(ResetConnectionButton_Click); };
cancelButton.Click += (sender, e) => { UiThread.RunOnIdle(CancelButton_Click); };
2015-04-08 15:20:10 -07:00
cancelConnectButton.Click += (sender, e) => { UiThread.RunOnIdle(CancelConnectionButton_Click); };
reprintButton.Click += new EventHandler(onReprintButton_Click);
doneWithCurrentPartButton.Click += new EventHandler(onDoneWithCurrentPartButton_Click);
ActiveTheme.Instance.ThemeChanged.RegisterEvent(ThemeChanged, ref unregisterEvents);
}
2015-04-09 09:46:42 -07:00
protected void DisableActiveButtons()
2015-04-08 15:20:10 -07:00
{
2015-04-09 09:46:42 -07:00
foreach (TooltipButton button in this.activePrintButtons)
{
2015-04-09 09:46:42 -07:00
button.Enabled = false;
2015-04-08 15:20:10 -07:00
}
}
2015-04-09 09:46:42 -07:00
protected void EnableActiveButtons()
2015-04-08 15:20:10 -07:00
{
2015-04-09 09:46:42 -07:00
foreach (TooltipButton button in this.activePrintButtons)
2015-04-08 15:20:10 -07:00
{
2015-04-09 09:46:42 -07:00
button.Enabled = true;
2015-04-08 15:20:10 -07:00
}
}
2015-04-09 09:46:42 -07:00
protected override void Initialize()
2015-04-08 15:20:10 -07:00
{
2015-04-09 09:46:42 -07:00
textImageButtonFactory.normalTextColor = RGBA_Bytes.White;
textImageButtonFactory.disabledTextColor = RGBA_Bytes.LightGray;
textImageButtonFactory.hoverTextColor = RGBA_Bytes.White;
textImageButtonFactory.pressedTextColor = RGBA_Bytes.White;
textImageButtonFactory.AllowThemeToAdjustImage = false;
2015-04-08 15:20:10 -07:00
2015-04-09 09:46:42 -07:00
textImageButtonFactory.borderWidth = 1;
2015-04-09 12:05:05 -07:00
textImageButtonFactory.FixedHeight = 52 * TextWidget.GlobalPointSizeScaleRatio;
textImageButtonFactory.fontSize = 14;
2015-04-09 09:46:42 -07:00
textImageButtonFactory.normalBorderColor = new RGBA_Bytes(255, 255, 255, 100);
textImageButtonFactory.hoverBorderColor = new RGBA_Bytes(255, 255, 255, 100);
2015-04-08 15:20:10 -07:00
}
2015-04-09 12:05:05 -07:00
2015-04-08 15:20:10 -07:00
protected TooltipButton makeButton(string buttonText, string buttonToolTip = "")
{
TooltipButton button = (TooltipButton)textImageButtonFactory.GenerateTooltipButton(buttonText);
button.tooltipText = buttonToolTip;
button.Margin = new BorderDouble(0, 6, 6, 3);
return button;
}
//Set the states of the buttons based on the status of PrinterCommunication
protected void SetButtonStates()
{
this.activePrintButtons.Clear();
2015-04-09 12:05:05 -07:00
if (!PrinterConnectionAndCommunication.Instance.PrinterIsConnected
&& PrinterConnectionAndCommunication.Instance.CommunicationState != PrinterConnectionAndCommunication.CommunicationStates.AttemptingToConnect)
{
if (ActiveTheme.Instance.IsTouchScreen)
2015-04-09 12:05:05 -07:00
{
this.activePrintButtons.Add(connectButton);
}
ShowActiveButtons();
EnableActiveButtons();
}
else if (PrinterConnectionAndCommunication.Instance.ActivePrintItem == null)
2015-04-08 15:20:10 -07:00
{
this.activePrintButtons.Add(addButton);
ShowActiveButtons();
EnableActiveButtons();
}
else
{
switch (PrinterConnectionAndCommunication.Instance.CommunicationState)
{
case PrinterConnectionAndCommunication.CommunicationStates.AttemptingToConnect:
this.activePrintButtons.Add(cancelConnectButton);
EnableActiveButtons();
break;
case PrinterConnectionAndCommunication.CommunicationStates.Connected:
this.activePrintButtons.Add(startButton);
//Show 'skip' button if there are more items in queue
if (QueueData.Instance.Count > 1)
{
this.activePrintButtons.Add(skipButton);
}
this.activePrintButtons.Add(removeButton);
EnableActiveButtons();
break;
case PrinterConnectionAndCommunication.CommunicationStates.PreparingToPrint:
this.activePrintButtons.Add(cancelButton);
EnableActiveButtons();
break;
case PrinterConnectionAndCommunication.CommunicationStates.PrintingFromSd:
case PrinterConnectionAndCommunication.CommunicationStates.Printing:
if (!timeSincePrintStarted.IsRunning)
{
timeSincePrintStarted.Restart();
}
if (!PrinterConnectionAndCommunication.Instance.PrintWasCanceled)
{
this.activePrintButtons.Add(pauseButton);
this.activePrintButtons.Add(cancelButton);
}
else if (ActiveTheme.Instance.IsTouchScreen)
2015-04-09 12:05:05 -07:00
{
this.activePrintButtons.Add(resetConnectionButton);
}
2015-04-08 15:20:10 -07:00
EnableActiveButtons();
break;
case PrinterConnectionAndCommunication.CommunicationStates.Paused:
this.activePrintButtons.Add(resumeButton);
this.activePrintButtons.Add(cancelButton);
EnableActiveButtons();
break;
case PrinterConnectionAndCommunication.CommunicationStates.FinishedPrint:
this.activePrintButtons.Add(reprintButton);
this.activePrintButtons.Add(doneWithCurrentPartButton);
EnableActiveButtons();
break;
default:
DisableActiveButtons();
break;
}
}
2015-04-09 12:05:05 -07:00
if (PrinterConnectionAndCommunication.Instance.PrinterIsConnected
&& ActiveSliceSettings.Instance.ShowResetConnection()
&& ActiveTheme.Instance.IsTouchScreen)
2015-04-09 12:05:05 -07:00
{
this.activePrintButtons.Add(resetConnectionButton);
ShowActiveButtons();
EnableActiveButtons();
}
2015-04-08 15:20:10 -07:00
ShowActiveButtons();
}
2015-04-09 09:46:42 -07:00
protected void ShowActiveButtons()
{
foreach (TooltipButton button in this.allPrintButtons)
{
if (activePrintButtons.IndexOf(button) >= 0)
{
button.Visible = true;
}
else
{
button.Visible = false;
}
}
}
private void AddButtonOnIdle(object state)
{
FileDialog.OpenFileDialog(
new OpenFileDialogParams(ApplicationSettings.OpenPrintableFileParams, multiSelect: true),
(openParams) =>
{
if (openParams.FileNames != null)
{
foreach (string loadedFileName in openParams.FileNames)
{
QueueData.Instance.AddItem(new PrintItemWrapper(new PrintItem(Path.GetFileNameWithoutExtension(loadedFileName), Path.GetFullPath(loadedFileName))));
}
}
});
}
private void CancelButton_Click(object state)
{
if (timeSincePrintStarted.IsRunning && timeSincePrintStarted.ElapsedMilliseconds > (2 * 60 * 1000))
{
StyledMessageBox.ShowMessageBox(onConfirmCancelPrint, cancelCurrentPrintMessage, cancelCurrentPrintTitle, StyledMessageBox.MessageType.YES_NO);
}
else
{
CancelPrinting();
2015-04-09 12:05:05 -07:00
UiThread.RunOnIdle((state2) => { SetButtonStates(); });
2015-04-09 09:46:42 -07:00
}
}
private void CancelConnectionButton_Click(object state)
{
CancelPrinting();
}
private void CancelPrinting()
{
if (PrinterConnectionAndCommunication.Instance.CommunicationState == PrinterConnectionAndCommunication.CommunicationStates.PreparingToPrint)
{
SlicingQueue.Instance.CancelCurrentSlicing();
}
PrinterConnectionAndCommunication.Instance.Stop();
timeSincePrintStarted.Reset();
UiThread.RunOnIdle((state2) => { SetButtonStates(); });
}
private void onAddButton_Click(object sender, EventArgs mouseEvent)
{
UiThread.RunOnIdle(AddButtonOnIdle);
}
2015-04-09 12:05:05 -07:00
private void ResetConnectionButton_Click(object state)
{
PrinterConnectionAndCommunication.Instance.RebootBoard();
}
private void onConnectButton_Click(object sender, EventArgs mouseEvent)
{
if (ActivePrinterProfile.Instance.ActivePrinter == null)
{
#if __ANDROID__
SetupWizardWindow.Show();
#else
PrinterActionRow.OpenConnectionWindow(true);
#endif
}
else
{
#if __ANDROID__
if (!FrostedSerialPort.HasPermissionToDevice())
{
// Opens the USB device permissions dialog which will call back into our UsbDevice broadcast receiver to connect
FrostedSerialPort.RequestPermissionToDevice();
}
else
#endif
{
ConnectToActivePrinter();
}
}
}
private void ConnectToActivePrinter()
{
PrinterConnectionAndCommunication.Instance.HaltConnectionThread();
PrinterConnectionAndCommunication.Instance.ConnectToActivePrinter();
}
2015-04-09 09:46:42 -07:00
private void onConfirmCancelPrint(bool messageBoxResponse)
{
if (messageBoxResponse)
{
UiThread.RunOnIdle((state) =>
{
CancelPrinting();
});
}
}
private void onDoneWithCurrentPartButton_Click(object sender, EventArgs mouseEvent)
{
PrinterConnectionAndCommunication.Instance.ResetToReadyState();
QueueData.Instance.RemoveAt(queueDataView.SelectedIndex);
// We don't have to change the selected index because we should be on the next one as we deleted the one
// we were on.
}
private void onPauseButton_Click(object sender, EventArgs mouseEvent)
{
PrinterConnectionAndCommunication.Instance.RequestPause();
}
private void onRemoveButton_Click(object sender, EventArgs mouseEvent)
{
QueueData.Instance.RemoveAt(queueDataView.SelectedIndex);
}
private void onReprintButton_Click(object sender, EventArgs mouseEvent)
{
UiThread.RunOnIdle((state) =>
{
PrinterConnectionAndCommunication.Instance.PrintActivePartIfPossible();
});
}
private void onResumeButton_Click(object sender, EventArgs mouseEvent)
{
if (PrinterConnectionAndCommunication.Instance.PrinterIsPaused)
{
PrinterConnectionAndCommunication.Instance.Resume();
}
}
private void onSkipButton_Click(object sender, EventArgs mouseEvent)
{
if (QueueData.Instance.Count > 1)
{
queueDataView.MoveToNext();
}
}
private void onStartButton_Click(object sender, EventArgs mouseEvent)
{
UiThread.RunOnIdle((state) =>
{
PrinterConnectionAndCommunication.Instance.PrintActivePartIfPossible();
});
}
2015-04-09 12:05:05 -07:00
2015-04-09 09:46:42 -07:00
private void onStateChanged(object sender, EventArgs e)
{
SetButtonStates();
}
2015-04-08 15:20:10 -07:00
}
}