436 lines
18 KiB
C#
436 lines
18 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
using log4net;
|
|
using TBF.Entities;
|
|
using TBF.UiBridge;
|
|
using TBF.Resources;
|
|
|
|
namespace TBF
|
|
{
|
|
public partial class MainWnd : Form
|
|
{
|
|
static readonly ILog log = LogManager.GetLogger(typeof(MainWnd));
|
|
|
|
public UiControls.BenchControlPanel BenchControlPanel;
|
|
public string ProcedureName;
|
|
public Entities.Procedure CurrentProcedure;
|
|
|
|
/// <summary>
|
|
/// This dialog is shown when emergency stop is activated
|
|
/// </summary>
|
|
TBF.Forms.EmergencyStopForm emergencyStopModelessDlg;
|
|
|
|
bool benchInitializationFailed;
|
|
TestProgressControls testProgressControls;
|
|
|
|
/// <summary>
|
|
/// Set when procedures are updated while the ProceduresComboBox is disabled (i.e. during a test).
|
|
/// Reset when the ProceduresComboBox list of items is successfully reloaded in ReloadProcedures().
|
|
/// This flag is tested when ProcedureComboBox is being enabled in OnButtonsEtc(),
|
|
/// optionaly ReloadProcedures() is called.
|
|
/// </summary>
|
|
public bool ProceduresUpdated = false;
|
|
|
|
/// <summary>
|
|
/// MainTabPage ID-s
|
|
/// The order of TabPages in this enum must be the same as the order
|
|
/// of their additions into the mainTabControl in MainWnd.Designer.cs
|
|
/// TODO: Find a better way
|
|
/// </summary>
|
|
public enum MainTabPageId
|
|
{
|
|
Invalid = -1,
|
|
Hydraulics = 0,
|
|
Process,
|
|
Measurement,
|
|
Insert,
|
|
Pictures,
|
|
Results,
|
|
Logs,
|
|
TabPagesCount
|
|
}
|
|
|
|
/// <summary>
|
|
/// Event handlers switching between tab pages
|
|
/// </summary>
|
|
private void homeTSMenuItem_Click(object sender, EventArgs e) { mainTabControl.SelectTab((int)MainTabPageId.Hydraulics); }
|
|
private void processTSMenuItem_Click(object sender, EventArgs e) { mainTabControl.SelectTab((int)MainTabPageId.Process); }
|
|
private void measurementTSMenuItem_Click(object sender, EventArgs e) { mainTabControl.SelectTab((int)MainTabPageId.Measurement); }
|
|
private void picturesTSMItem_Click(object sender, EventArgs e) { mainTabControl.SelectTab((int)MainTabPageId.Pictures); }
|
|
private void resultsTSMenuItem_Click(object sender, EventArgs e) { mainTabControl.SelectTab((int)MainTabPageId.Results); }
|
|
private void logsTSMenuItem_Click(object sender, EventArgs e) { mainTabControl.SelectTab((int)MainTabPageId.Logs); }
|
|
|
|
/// <summary>
|
|
/// Constructor
|
|
/// </summary>
|
|
public MainWnd()
|
|
{
|
|
InitializeComponent();
|
|
|
|
Text = "Test Bench Framework - " + Program.CurrentBench.BenchName
|
|
+ (Program.CurrentBench.IsRealBench ? "" : Strings._offline);
|
|
|
|
usersTSMenuItem.Visible = User.CurrentUser.IsMemberOf(Grp.GID.Administrators) ||
|
|
User.CurrentUser.IsMemberOf(Grp.GID.HeadOfLab);
|
|
|
|
benchInitializationFailed = false;
|
|
///
|
|
if (Program.CurrentBench.IsRealBench)
|
|
{
|
|
try
|
|
{
|
|
BenchControl.StateMachine.InitializeBoardEtc(ctrlBrdComponent);
|
|
testProgressControls = new TestProgressControls(progressFlowLayoutPanel);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
benchInitializationFailed = true;
|
|
log.Fatal("Failed to initialize a test bench component", exc);
|
|
MessageBox.Show(exc.Message, Strings.Error, System.Windows.Forms.MessageBoxButtons.OK,
|
|
System.Windows.Forms.MessageBoxIcon.Exclamation);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Localize()
|
|
{
|
|
procedureGroupBox.Text = Strings.Procedure;
|
|
activityGroupBox.Text = Strings.Activity;
|
|
progressGroupBox.Text = Strings.Progress;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes this window and its controls
|
|
/// </summary>
|
|
private void MainWnd_Load(object sender, EventArgs e)
|
|
{
|
|
Localize();
|
|
|
|
TBF.LocalSettings ls = Program.LocalSettings;
|
|
WindowState = (ls.ManiWndMaximized ? FormWindowState.Maximized : FormWindowState.Normal);
|
|
//if (WindowState != FormWindowState.Normal)
|
|
//{
|
|
// RestoreBounds = new Rectangle((ls.MainWndLeft != 0) ? ls.MainWndLeft : 75,
|
|
// (ls.MainWndTop != 0) ? ls.MainWndTop : 5,
|
|
// (ls.MainWndWidth > 0) ? ls.MainWndWidth : 1250,
|
|
// (ls.MainWndHeight > 0) ? ls.MainWndHeight : 750);
|
|
//}
|
|
//else
|
|
{
|
|
Width = (ls.MainWndWidth > 0) ? ls.MainWndWidth : 1250;
|
|
Height = (ls.MainWndHeight > 0) ? ls.MainWndHeight : 750;
|
|
Left = (ls.MainWndLeft != 0) ? ls.MainWndLeft : 75;
|
|
Top = (ls.MainWndTop != 0) ? ls.MainWndTop : 5;
|
|
}
|
|
|
|
|
|
if (Program.CurrentBench.IsRealBench && !benchInitializationFailed)
|
|
{
|
|
try
|
|
{
|
|
BenchControl.StateMachine.InitializeDevices();
|
|
|
|
//emergencyStopModelessDlg = new TBF.Forms.EmergencyStopForm();
|
|
//emergencyStopModelessDlg.Show();
|
|
|
|
Bridge.ButtonsEtcHandler += delegate(object sndr, ButtonsEtcEventArgs args)
|
|
{
|
|
if (InvokeRequired) { Invoke(new EventHandler<ButtonsEtcEventArgs>(OnButtonsEtc), sndr, args); }
|
|
else OnButtonsEtc(sndr, args);
|
|
};
|
|
|
|
Bridge.ActivityHandler += delegate(object sndr, UiBridge.ActivityEventArgs args)
|
|
{
|
|
if (InvokeRequired) { Invoke(new EventHandler<UiBridge.ActivityEventArgs>(OnActivity), sndr, args); }
|
|
else OnActivity(sndr, args);
|
|
};
|
|
|
|
Bridge.StateChangedHandler += delegate(object sndr, UiBridge.StateChangedEventArgs args)
|
|
{
|
|
if (InvokeRequired) { Invoke(new EventHandler<UiBridge.StateChangedEventArgs>(OnStateChanged), sndr, args); }
|
|
else OnStateChanged(sndr, args);
|
|
};
|
|
|
|
Bridge.TestProgressHandler += delegate(object sndr, UiBridge.TestProgressEventArgs args)
|
|
{
|
|
if (InvokeRequired) { Invoke(new EventHandler<UiBridge.TestProgressEventArgs>(OnTestProgress), sndr, args); }
|
|
else OnTestProgress(sndr, args);
|
|
};
|
|
|
|
Bridge.ProcedureSelectedHandler += delegate(object sndr, UiBridge.ProcedureSelectedEventArgs args)
|
|
{
|
|
if (InvokeRequired) { Invoke(new EventHandler<UiBridge.ProcedureSelectedEventArgs>(OnProcedureSelected), sndr, args); }
|
|
else OnProcedureSelected(sndr, args);
|
|
};
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
benchInitializationFailed = true;
|
|
log.Fatal("Failed to initialize a test bench component", exc);
|
|
MessageBox.Show(exc.Message, Strings.Error, System.Windows.Forms.MessageBoxButtons.OK,
|
|
System.Windows.Forms.MessageBoxIcon.Exclamation);
|
|
}
|
|
}
|
|
|
|
procedureComboBox.Text = Program.LocalSettings.LastProcedureName;
|
|
ProcedureName = Program.LocalSettings.LastProcedureName;
|
|
rightHorizSplitContainer.SplitterDistance = Program.LocalSettings.RightPaneHorizSplitterDistance;
|
|
|
|
/// Text boxes for data input, with border and white background
|
|
CustomStyle.Set(0, DockStyle.Fill, Color.White, BorderStyle.FixedSingle, false,
|
|
new System.Drawing.Font("Verdana", 10.5F, FontStyle.Regular, GraphicsUnit.Point, (byte)(238)));
|
|
|
|
// Read only text boxes (labels), its background matches the window background
|
|
|
|
/// Measurement
|
|
CustomStyle.Set(1, DockStyle.Fill, Color.FromArgb(150, 180, 200), BorderStyle.None, true,
|
|
new System.Drawing.Font("Verdana", 10.5F, FontStyle.Regular, GraphicsUnit.Point, (byte)(238)));
|
|
|
|
/// Not used
|
|
CustomStyle.Set(2, DockStyle.Fill, Color.OliveDrab, BorderStyle.None, true,
|
|
new System.Drawing.Font("Verdana", 10.5F, FontStyle.Regular, GraphicsUnit.Point, (byte)(238)));
|
|
|
|
/// Procedure
|
|
CustomStyle.Set(3, DockStyle.Fill, Color.FromArgb(200, 200, 150), BorderStyle.None, true,
|
|
new System.Drawing.Font("Verdana", 10.5F, FontStyle.Regular, GraphicsUnit.Point, (byte)(238)));
|
|
|
|
/// Initialize bench control user interface(s)
|
|
/// Initialize bench control user interface(s)
|
|
BenchControlPanel = new TBF.UiControls.BenchControlPanel();
|
|
BenchControlPanel.BackColor = System.Drawing.Color.LightGray;
|
|
new System.ComponentModel.ComponentResourceManager(typeof(MainWnd)).ApplyResources(BenchControlPanel, "benchControlPanel");
|
|
BenchControlPanel.Name = "benchControlPanel";
|
|
rightHorizSplitContainer.Panel2.Controls.Add(BenchControlPanel);
|
|
ReloadProcedures();
|
|
|
|
/// Select the initial tab page
|
|
mainTabControl.SelectTab((int)MainTabPageId.Hydraulics);
|
|
UpdateUser();
|
|
|
|
if (Program.CurrentBench.IsRealBench)
|
|
{
|
|
if (!benchInitializationFailed)
|
|
{
|
|
BenchControl.StateMachine.Start();
|
|
log.Info("Test bench started");
|
|
}
|
|
else
|
|
{
|
|
BenchControlPanel.OnButtonsEtc(null, new ButtonsEtcEventArgs(TBF.UiBridge.ButtonsEtc.None));
|
|
MessageBox.Show(Strings.Bench_is_not_running,
|
|
Strings.Warning,
|
|
System.Windows.Forms.MessageBoxButtons.OK,
|
|
System.Windows.Forms.MessageBoxIcon.Exclamation);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Pass any pressed keys to the Bench control panel.
|
|
/// Note that MainWnd.KeyPreview must be set to true.
|
|
/// </summary>
|
|
private void MainWnd_KeyPress(object sender, KeyPressEventArgs e)
|
|
{
|
|
if (BenchControlPanel != null && BenchControlPanel.ProcessKey(e.KeyChar))
|
|
{
|
|
e.Handled = true;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Invoked from the UiBridge to update the activity label.
|
|
/// </summary>
|
|
void OnActivity(object sender, UiBridge.ActivityEventArgs args)
|
|
{
|
|
activityLabel.Text = args.Activity;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Invoked from the UiBridge to update the current state status bar item.
|
|
/// </summary>
|
|
void OnStateChanged(object sender, StateChangedEventArgs args)
|
|
{
|
|
activityStatusLabel.Text = args.StateChangedMsg;
|
|
}
|
|
|
|
void OnTestProgress(object sender, UiBridge.TestProgressEventArgs args)
|
|
{
|
|
mainProgressBar.Value = (int)(100.499f * args.OveralProgress);
|
|
}
|
|
|
|
void OnProcedureSelected(object sender, UiBridge.ProcedureSelectedEventArgs args)
|
|
{
|
|
mainProgressBar.Value = 0;
|
|
}
|
|
|
|
void OnButtonsEtc(object sender, UiBridge.ButtonsEtcEventArgs args)
|
|
{
|
|
if (((args.Flags & ButtonsEtc.ShowBenchEmpty) != 0) ||
|
|
((args.Flags & ButtonsEtc.ShowBenchFilled) != 0))
|
|
{
|
|
return;
|
|
}
|
|
|
|
procedureComboBox.Enabled = ((args.Flags & UiBridge.ButtonsEtc.ProcedureCmbBoxEn) != 0);
|
|
|
|
if (procedureComboBox.Enabled && ProceduresUpdated)
|
|
{
|
|
ReloadProcedures();
|
|
}
|
|
}
|
|
|
|
public void UpdateUser()
|
|
{
|
|
userInfoStatusLabel.Text = Strings.User_ + Entities.User.CurrentUser.Name + " , ";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Stop the bench when program closes
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void MainWnd_FormClosed(object sender, FormClosedEventArgs e)
|
|
{
|
|
//BenchControl.StateMachine.Stop();
|
|
}
|
|
|
|
private void benchComponentsTSMItem_Click(object s, EventArgs e) { new ComponentsManagerDlg().ShowDialog(); }
|
|
private void benchPathsTSMItem_Click(object s, EventArgs e) { new PathsDlg().ShowDialog(); }
|
|
private void benchTransitionsTSMItem_Click(object s, EventArgs e) { new TransitionsDlg().ShowDialog(); }
|
|
private void benchMetrologyTSMItem_Click(object s, EventArgs e) { new MetrologyDlg().ShowDialog(); }
|
|
private void proceduresTSMItem_Click(object s, EventArgs e) { new ProceduresDlg().ShowDialog(); }
|
|
private void localSettingsTSMItem_Click(object s, EventArgs e) { new PreferencesDlg().ShowDialog(); }
|
|
private void databaseSettingsTSMItem_Click(object s, EventArgs e) { new BenchesDlg().ShowDialog(); }
|
|
private void usersTSMItem_Click(object s, EventArgs e) { new Forms.EditUsers().ShowDialog(); }
|
|
private void aboutTSMItem_Click(object s, EventArgs e)
|
|
{
|
|
new Forms.AboutDlg("1.0.0.237", "05.09.2014").ShowDialog();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Read the database and re-initialize procedureComboBox items.
|
|
/// Try to preserve the original selection.
|
|
/// </summary>
|
|
public void ReloadProcedures()
|
|
{
|
|
if (procedureComboBox.Enabled)
|
|
{
|
|
string oriProcName = procedureComboBox.Text;
|
|
|
|
IList<Procedure> procedures = FluentCommon.CreateSession(Database.Procedures)
|
|
.CreateQuery("FROM Procedure ORDER BY ItemNr")
|
|
.List<Procedure>();
|
|
|
|
procedureComboBox.Items.Clear();
|
|
foreach (var proc in procedures) procedureComboBox.Items.Add(proc.Name);
|
|
|
|
if (procedureComboBox.Items.Contains(oriProcName))
|
|
{
|
|
procedureComboBox.Text = oriProcName;
|
|
ProcedureName = oriProcName;
|
|
}
|
|
else if (procedures.Count > 0)
|
|
{
|
|
procedureComboBox.Text = procedures[0].Name;
|
|
ProcedureName = procedures[0].Name;
|
|
}
|
|
else
|
|
{
|
|
procedureComboBox.Text = string.Empty;
|
|
ProcedureName = null;
|
|
}
|
|
|
|
ProceduresUpdated = false;
|
|
|
|
BenchControlPanel.ReloadTests();
|
|
}
|
|
else
|
|
{
|
|
ProceduresUpdated = true;
|
|
}
|
|
}
|
|
|
|
private void procedureComboBox_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
ProcedureName = procedureComboBox.Text;
|
|
BenchControlPanel.ReloadTests();
|
|
if (CurrentProcedure != null && CurrentProcedure.Description != null)
|
|
{
|
|
descriptionLabel.Text = CurrentProcedure.Description;
|
|
}
|
|
if (CurrentProcedure != null && testProgressControls != null)
|
|
{
|
|
testProgressControls.ProcedureSelectedInUI(this, new UiBridge.ProcedureSelectedEventArgs(CurrentProcedure));
|
|
}
|
|
if (!string.IsNullOrEmpty(ProcedureName))
|
|
{
|
|
Program.LocalSettings.LastProcedureName = ProcedureName;
|
|
Program.LocalSettings.Save();
|
|
}
|
|
}
|
|
|
|
private void rightHorizSplitContainer_SplitterMoved(object sender, SplitterEventArgs e)
|
|
{
|
|
Program.LocalSettings.RightPaneHorizSplitterDistance = rightHorizSplitContainer.SplitterDistance;
|
|
Program.LocalSettings.Save();
|
|
}
|
|
|
|
private void MainWnd_FormClosing(object sender, FormClosingEventArgs e)
|
|
{
|
|
Program.LocalSettings.ManiWndMaximized = (WindowState == FormWindowState.Maximized);
|
|
if (WindowState == FormWindowState.Normal)
|
|
{
|
|
Program.LocalSettings.MainWndLeft = Location.X;
|
|
Program.LocalSettings.MainWndTop = Location.Y;
|
|
Program.LocalSettings.MainWndWidth = Size.Width;
|
|
Program.LocalSettings.MainWndHeight = Size.Height;
|
|
}
|
|
else
|
|
{
|
|
/// Maximized or minimized (when restarted, minimized window is restored as normal)
|
|
Program.LocalSettings.MainWndLeft = RestoreBounds.Left;
|
|
Program.LocalSettings.MainWndTop = RestoreBounds.Top;
|
|
Program.LocalSettings.MainWndWidth = RestoreBounds.Width;
|
|
Program.LocalSettings.MainWndHeight = RestoreBounds.Height;
|
|
}
|
|
Program.LocalSettings.Save();
|
|
|
|
if (DialogResult == DialogResult.None)
|
|
{
|
|
exitToolStripMenuItem_Click(sender, e);
|
|
}
|
|
}
|
|
|
|
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
|
|
{
|
|
bool oriEmgStopState = false;
|
|
if (emergencyStopModelessDlg != null)
|
|
{
|
|
oriEmgStopState = emergencyStopModelessDlg.Visible;
|
|
emergencyStopModelessDlg.Visible = false;
|
|
}
|
|
|
|
if (DialogResult.Yes == MessageBox.Show("Do you want to exit the Test Bench Framework?", "Closing the program",
|
|
MessageBoxButtons.YesNo, MessageBoxIcon.Question))
|
|
{
|
|
BenchControl.StateMachine.Stop();
|
|
UiBridge.Bridge.OnEmergencyStopChanged(this, UiBridge.EmergencyStopEventArgs.State.CloseForm);
|
|
new Forms.ClosingProgram().ShowDialog();
|
|
DialogResult = DialogResult.OK;
|
|
Close();
|
|
return;
|
|
}
|
|
else if (e is FormClosingEventArgs)
|
|
{
|
|
(e as FormClosingEventArgs).Cancel = true;
|
|
}
|
|
|
|
if (emergencyStopModelessDlg != null) emergencyStopModelessDlg.Visible = oriEmgStopState;
|
|
}
|
|
}
|
|
} |