namespace OINA.Extender.Testharness
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Forms;
using OINA.Extender;
using OINA.Extender.MicroscopeControl;
using OINA.Extender.Testharness.Properties;
///
/// Microscope Control Panel Form
///
public partial class MicroscopeControlPanel : Form
{
///
/// IMicroscopeController object
///
private IMicroscopeController microscopeController = null;
///
/// MicroscopeControlPanel Constructor
///
public MicroscopeControlPanel()
{
this.InitializeComponent();
// Create microscope controller
this.microscopeController = AcquireFactory.CreateMicroscopeControl();
this.microscopeController.ColumnChange += this.OnMicroscopeColumnChange;
this.microscopeController.StageChange += this.OnMicroscopeStageChange;
this.microscopeController.ColumnConnected += this.OnMicroscopeColumnChange;
this.microscopeController.StageConnected += this.OnMicroscopeStageChange;
// Initial UI contents
this.UpdateMicroscopeColumnText();
this.UpdateMicroscopeStageText();
}
///
/// Show a Message Box
///
/// MBox Message
/// MBox Title
private static void MsgBox(string msg, string title)
{
MessageBox.Show(msg, title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
}
///
/// Gets a double value from a textbox
///
/// Textbox to get the number from
/// The value, or null if not a valid number
private static double? GetDoubleFromTextbox(TextBox textBox)
{
double value;
if (!double.TryParse(textBox.Text, out value))
{
return null;
}
return value;
}
#region Event handlers
///
/// OnMicroscopeStageChange
///
/// sender object
/// EventArgs
private void OnMicroscopeStageChange(object sender, EventArgs e)
{
this.BeginInvoke((Action)this.UpdateMicroscopeStageText);
}
///
/// OnMicroscopeColumnChange
///
/// sender object
/// EventArgs
private void OnMicroscopeColumnChange(object sender, EventArgs e)
{
this.BeginInvoke((Action)this.UpdateMicroscopeColumnText);
}
#endregion
#region Get microscope status and update the UI
///
/// UpdateMicroscopeStageText Method
///
private void UpdateMicroscopeStageText()
{
this.gbStage.Text = @"Stage " + this.microscopeController.StageConnectionStatus.Name;
var stageCapabilities = this.microscopeController.StageCapabilities;
var stageConditions = this.microscopeController.StageConditions;
if (stageCapabilities.StageX.CanRead)
{
this.textX.Text = stageConditions.StageX.ToString(@"F2", CultureInfo.InvariantCulture);
this.tbX.Text = this.textX.Text;
}
if (stageCapabilities.StageY.CanRead)
{
this.textY.Text = stageConditions.StageY.ToString(@"F2", CultureInfo.InvariantCulture);
this.tbY.Text = this.textY.Text;
}
if (stageCapabilities.StageZ.CanRead)
{
this.textZ.Text = stageConditions.StageZ.ToString(@"F2", CultureInfo.InvariantCulture);
this.tbZ.Text = this.textZ.Text;
}
if (stageCapabilities.StageT.CanRead)
{
this.textTilt.Text = stageConditions.StageT.ToString(@"F2", CultureInfo.InvariantCulture);
this.tbTilt.Text = this.textTilt.Text;
}
if (stageCapabilities.StageR.CanRead)
{
this.textRotate.Text = stageConditions.StageR.ToString(@"F2", CultureInfo.InvariantCulture);
this.tbRotate.Text = this.textRotate.Text;
}
this.textBacklash.Text = Convert.ToBoolean(stageConditions.BacklashOn).ToString(CultureInfo.InvariantCulture);
}
///
/// UpdateMicroscopeColumnText Method
///
private void UpdateMicroscopeColumnText()
{
this.gbColumn.Text = @"Column " + this.microscopeController.ColumnConnectionStatus.Name;
var columnCapabilities = this.microscopeController.ColumnCapabilities;
var columnConditions = this.microscopeController.ColumnConditions;
if (columnCapabilities.Magnification.CanRead)
{
this.textMag.Text = columnConditions.Magnification.ToString(@"F2", CultureInfo.InvariantCulture);
this.tbMag.Text = this.textMag.Text;
}
if (columnCapabilities.WorkingDistance.CanRead)
{
this.textWD.Text = columnConditions.WorkingDistance.ToString(@"F2", CultureInfo.InvariantCulture);
this.tbWD.Text = this.textWD.Text;
}
if (columnCapabilities.HighVoltage.CanRead)
{
this.textHV.Text = columnConditions.HighVoltage.ToString(@"F2", CultureInfo.InvariantCulture);
this.tbHV.Text = this.textHV.Text;
}
if (columnCapabilities.BeamOn.CanRead)
{
this.textBeamOn.Text = Convert.ToBoolean(columnConditions.BeamOn).ToString(CultureInfo.InvariantCulture);
}
if (columnCapabilities.FilamentOn.CanRead)
{
this.textFilamentOn.Text = Convert.ToBoolean(columnConditions.FilamentOn).ToString(CultureInfo.InvariantCulture);
}
}
#endregion
#region Control Microscope from UI
///
/// SetColumnConditions button click handler
///
/// sender object
/// EventArgs
private void SetColumnConditionsButton_Click(object sender, EventArgs e)
{
double? mag = GetDoubleFromTextbox(this.tbMag);
double? wd = GetDoubleFromTextbox(this.tbWD);
double? hv = GetDoubleFromTextbox(this.tbHV);
if (mag + wd + hv == null)
{
MsgBox(Resources.BadNumberFormatMessage, Resources.BadNumberFormatTitle);
return;
}
Dictionary columnDictionary = new Dictionary
{
{ Column.Magnification, (double)mag },
{ Column.HighVoltage, (double)hv },
{ Column.WorkingDistance, (double)wd }
};
this.microscopeController.SetColumnConditions(columnDictionary);
}
///
/// Set Magnification button click handler
///
/// sender object
/// EventArgs
private void SetMagButton_Click(object sender, EventArgs e)
{
double? mag = GetDoubleFromTextbox(this.tbMag);
if (mag == null)
{
MsgBox(Resources.BadNumberFormatMessage, Resources.BadNumberFormatTitle);
return;
}
Dictionary columnDictionary = new Dictionary
{
{ Column.Magnification, (double)mag }
};
this.microscopeController.SetColumnConditions(columnDictionary);
}
///
/// Set Working Distance button click handler
///
/// sender object
/// EventArgs
private void SetWDButton_Click(object sender, EventArgs e)
{
double? wd = GetDoubleFromTextbox(this.tbWD);
if (wd == null)
{
MsgBox(Resources.BadNumberFormatMessage, Resources.BadNumberFormatTitle);
return;
}
Dictionary columnDictionary = new Dictionary
{
{ Column.WorkingDistance, (double)wd }
};
this.microscopeController.SetColumnConditions(columnDictionary);
}
///
/// Set High Voltage button click handler
///
/// sender object
/// EventArgs
private void SetHVButton_Click(object sender, EventArgs e)
{
double? hv = GetDoubleFromTextbox(this.tbHV);
if (hv == null)
{
MsgBox(Resources.BadNumberFormatMessage, Resources.BadNumberFormatTitle);
return;
}
Dictionary columnDictionary = new Dictionary
{
{ Column.HighVoltage, (double)hv }
};
this.microscopeController.SetColumnConditions(columnDictionary);
}
///
/// SetStageConditions button click handler
///
/// sender object
/// EventArgs
private void SetStageConditionsButton_Click(object sender, EventArgs e)
{
double? stageX = GetDoubleFromTextbox(this.tbX);
double? stageY = GetDoubleFromTextbox(this.tbY);
double? stageZ = GetDoubleFromTextbox(this.tbZ);
double? stageT = GetDoubleFromTextbox(this.tbTilt);
double? stageR = GetDoubleFromTextbox(this.tbRotate);
if (stageX + stageY + stageZ + stageT + stageR == null)
{
MsgBox(Resources.BadNumberFormatMessage, Resources.BadNumberFormatTitle);
return;
}
var stageDictionary = new Dictionary
{
{ Stage.StageX, (double)stageX },
{ Stage.StageY, (double)stageY },
{ Stage.StageZ, (double)stageZ },
{ Stage.StageT, (double)stageT },
{ Stage.StageR, (double)stageR }
};
this.microscopeController.SetStageConditions(stageDictionary);
}
#endregion
///
/// Raises the event.
///
/// A that contains the event data.
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
IMicroscopeController controller = this.microscopeController;
if (controller != null)
{
controller.ColumnChange -= this.OnMicroscopeColumnChange;
controller.StageChange -= this.OnMicroscopeStageChange;
controller.ColumnConnected -= this.OnMicroscopeColumnChange;
controller.StageConnected -= this.OnMicroscopeStageChange;
}
base.OnClosing(e);
}
}
}