271 lines
8.2 KiB
C#
271 lines
8.2 KiB
C#
///
|
|
/// Copyright (c) 2016 Sensus Metering Systems
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Windows.Forms;
|
|
using log4net;
|
|
using Common.Forms;
|
|
using TBF.Rig.GenericDevices;
|
|
using TBF.Resources;
|
|
using TBF.UI.Shared;
|
|
|
|
namespace TBF.Rig.TestMethods.Endurance
|
|
{
|
|
public partial class CycleDlg : Form, IParentOfListViewEx
|
|
{
|
|
static readonly ILog log = LogManager.GetLogger(typeof(CycleDlg));
|
|
|
|
/// Used when re-scaling the dialog: 100% = 96dpi, 125% = 120dpi, 150% = 144dpi
|
|
public readonly int Dpi;
|
|
|
|
/// <summary>
|
|
/// Transition sequences to be updated by this dialog.
|
|
/// Set by the constructor or updated by the parent after creation and before loading.
|
|
/// </summary>
|
|
public IList<CycleStep> EnduranceCycle;
|
|
|
|
|
|
/// Auxiliary public lists used also by user controls in tab pages
|
|
public IList<Rig.Generic.IComponent> TbfComponents;
|
|
public IList<IValve> Valves;
|
|
|
|
|
|
ITabWithListViewEx seqStepsCtrl;
|
|
|
|
|
|
CycleDlg()
|
|
{
|
|
EnduranceCycle = new List<CycleStep>();
|
|
}
|
|
|
|
public CycleDlg(IList<CycleStep> cycle)
|
|
{
|
|
/// Detect display setting: 100% = 96dpi, 125% = 120dpi, 150% = 144dpi.
|
|
Dpi = (int)this.CreateGraphics().DpiX;
|
|
|
|
InitializeComponent();
|
|
|
|
/// SharedDlgButtons configuration
|
|
sharedButtons.ParentForm = this;
|
|
sharedButtons.RequiredGroupMembership = new Common.GID[] { Common.GID.Metrologists };
|
|
sharedButtons.OptionalButtons = SharedButtons.Buttons.Add |
|
|
SharedButtons.Buttons.Remove |
|
|
SharedButtons.Buttons.Up |
|
|
SharedButtons.Buttons.Down;
|
|
sharedButtons.Unlocked += Unlocked;
|
|
sharedButtons.OKClicked += okButton_Click;
|
|
sharedButtons.CancelClicked += cancelButton_Click;
|
|
sharedButtons.AddClicked += addButton_Click;
|
|
sharedButtons.RemoveClicked += removeButton_Click;
|
|
sharedButtons.UpClicked += upButton_Click;
|
|
sharedButtons.DownClicked += downButton_Click;
|
|
|
|
seqStepsCtrl = new CycleStepsCtrl() as ITabWithListViewEx;
|
|
|
|
/// Prepare a list of valves for the endurance test
|
|
TbfComponents = Rig.TbfComponents.LoadComponentsFromDB(TBF.DB.CreateSession(Common.DBKind.Config));
|
|
Valves = new List<IValve>();
|
|
for (int bitNr = 0; bitNr < 8; bitNr++)
|
|
{
|
|
foreach (var vlv in TbfComponents)
|
|
{
|
|
if (vlv is Rig.Elde.Valve.Valve && (vlv as Rig.Elde.Valve.Valve).BitPosition == bitNr)
|
|
{
|
|
Valves.Add(vlv as IValve);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
EnduranceCycle = (cycle != null) ? cycle : new List<CycleStep>(); /// TODO: Load from component parameters
|
|
|
|
/// Create a tab with sequence steps for each sequence ordered by ItemNr.
|
|
AddCycleStepsTab(EnduranceCycle);
|
|
}
|
|
|
|
void AddCycleStepsTab(IList<CycleStep> sequence)
|
|
{
|
|
TabPage nwTab = new TabPage();
|
|
nwTab.Tag = sequence;
|
|
CycleStepsCtrl newCtrl = new CycleStepsCtrl();
|
|
seqStepsCtrl = newCtrl;
|
|
nwTab.Controls.Add(newCtrl);
|
|
tabControl.TabPages.Add(nwTab);
|
|
newCtrl.Initialize(sequence, this, tabControl.TabPages[tabControl.TabPages.Count - 1]);
|
|
newCtrl.SelectedIndexChanged += new System.EventHandler(SelectedIndexChanged);
|
|
}
|
|
|
|
private void CycleDlg_Load(object sender, EventArgs e)
|
|
{
|
|
Localize();
|
|
|
|
LocalSettings ls = Program.LocalSettings;
|
|
Width = (ls.EnduranceDlgWidth > 0) ? ls.EnduranceDlgWidth : 1050;
|
|
Height = (ls.EnduranceDlgHeight > 0) ? ls.EnduranceDlgHeight : 360;
|
|
Left = (ls.EnduranceDlgLeft != 0) ? ls.EnduranceDlgLeft : 150;
|
|
Top = (ls.EnduranceDlgTop != 0) ? ls.EnduranceDlgTop : 150;
|
|
|
|
/// Now refresh the content of the dialog
|
|
RefreshAllTabs();
|
|
}
|
|
|
|
void Localize()
|
|
{
|
|
Text = Strings.Endurance_cycle_steps;
|
|
}
|
|
|
|
void RefreshAllTabs()
|
|
{
|
|
if (seqStepsCtrl != null) (seqStepsCtrl as ListViewEx).Refresh();
|
|
}
|
|
|
|
private void Unlocked(object sender, EventArgs e)
|
|
{
|
|
if (seqStepsCtrl != null) seqStepsCtrl.Unlock();
|
|
}
|
|
|
|
private void okButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (seqStepsCtrl != null) seqStepsCtrl.OkBtnClicked();
|
|
|
|
//if (EnduranceCycle != null)
|
|
//{
|
|
// using (var transaction = Session.BeginTransaction())
|
|
// {
|
|
// try
|
|
// {
|
|
// Session.SaveOrUpdate(EnduranceCycle);
|
|
// transaction.Commit();
|
|
// }
|
|
// catch (Exception e2)
|
|
// {
|
|
// MessageBox.Show(Strings.Cannot_save_changes, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
|
// log.ErrorFormat("Update of sequence 'Tr1' in the database failed: {0}", e2.Message);
|
|
// }
|
|
// }
|
|
//}
|
|
|
|
Program.LocalSettings.EnduranceDlgLeft = Location.X;
|
|
Program.LocalSettings.EnduranceDlgTop = Location.Y;
|
|
Program.LocalSettings.EnduranceDlgWidth = Size.Width;
|
|
Program.LocalSettings.EnduranceDlgHeight = Size.Height;
|
|
Program.LocalSettings.Save();
|
|
|
|
DialogResult = DialogResult.OK;
|
|
Close();
|
|
return;
|
|
}
|
|
|
|
private void cancelButton_Click(object sender, EventArgs e)
|
|
{
|
|
Program.LocalSettings.EnduranceDlgLeft = Location.X;
|
|
Program.LocalSettings.EnduranceDlgTop = Location.Y;
|
|
Program.LocalSettings.EnduranceDlgWidth = Size.Width;
|
|
Program.LocalSettings.EnduranceDlgHeight = Size.Height;
|
|
Program.LocalSettings.Save();
|
|
|
|
DialogResult = DialogResult.Cancel;
|
|
Close();
|
|
return;
|
|
}
|
|
|
|
int slctdTab;
|
|
|
|
private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
if (seqStepsCtrl != null)
|
|
{
|
|
seqStepsCtrl.OkBtnClicked();
|
|
}
|
|
slctdTab = tabControl.SelectedIndex;
|
|
sharedButtons.EnableButtons(SharedButtons.Buttons.Add);
|
|
sharedButtons.DisableButtons(SharedButtons.Buttons.Remove);
|
|
RefreshAllTabs();
|
|
}
|
|
|
|
private void addButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (seqStepsCtrl != null) seqStepsCtrl.AddOne();
|
|
}
|
|
|
|
private void removeButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (seqStepsCtrl != null) seqStepsCtrl.RemoveSelected();
|
|
}
|
|
|
|
private void upButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (seqStepsCtrl != null) seqStepsCtrl.MoveUpSelected();
|
|
}
|
|
|
|
private void downButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (seqStepsCtrl != null) seqStepsCtrl.MoveDownSelected();
|
|
}
|
|
|
|
public void UpdateButtonStates(SharedButtons.SelectedItemPos selectedItemPos)
|
|
{
|
|
if (selectedItemPos == SharedButtons.SelectedItemPos.None)
|
|
{
|
|
sharedButtons.DisableButtons(SharedButtons.Buttons.Up |
|
|
SharedButtons.Buttons.Down);
|
|
}
|
|
else if (selectedItemPos == SharedButtons.SelectedItemPos.First)
|
|
{
|
|
sharedButtons.EnableButtons(SharedButtons.Buttons.Down);
|
|
sharedButtons.DisableButtons(SharedButtons.Buttons.Up);
|
|
}
|
|
else if (selectedItemPos == SharedButtons.SelectedItemPos.Last)
|
|
{
|
|
sharedButtons.EnableButtons(SharedButtons.Buttons.Up);
|
|
sharedButtons.DisableButtons(SharedButtons.Buttons.Down);
|
|
}
|
|
else if (selectedItemPos == SharedButtons.SelectedItemPos.FirstAndLast)
|
|
{
|
|
sharedButtons.DisableButtons(SharedButtons.Buttons.Up |
|
|
SharedButtons.Buttons.Down);
|
|
}
|
|
else
|
|
{
|
|
sharedButtons.EnableButtons(SharedButtons.Buttons.Up |
|
|
SharedButtons.Buttons.Down);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Common part for three xxxListViewEx_SelectedIndexChanged event handlers
|
|
/// </summary>
|
|
/// <param name="listViewEx"></param>
|
|
void SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
var listViewEx = sender as ListViewEx;
|
|
if (listViewEx.SelectedItems.Count != 1)
|
|
{
|
|
UpdateButtonStates(SharedButtons.SelectedItemPos.None);
|
|
}
|
|
else if (listViewEx.Items.Count == 1)
|
|
{
|
|
UpdateButtonStates(SharedButtons.SelectedItemPos.FirstAndLast);
|
|
}
|
|
else if (listViewEx.SelectedIndices[0] == 0)
|
|
{
|
|
UpdateButtonStates(SharedButtons.SelectedItemPos.First);
|
|
}
|
|
else if (listViewEx.SelectedIndices[0] == (listViewEx.Items.Count - 1))
|
|
{
|
|
UpdateButtonStates(SharedButtons.SelectedItemPos.Last);
|
|
}
|
|
else
|
|
{
|
|
UpdateButtonStates(SharedButtons.SelectedItemPos.Middle);
|
|
}
|
|
}
|
|
|
|
private void CycleDlg_FormClosing(object sender, FormClosingEventArgs e)
|
|
{
|
|
if (Users.CurrentUser.Restore(this)) Program.MainWnd.UpdateUser();
|
|
}
|
|
}
|
|
}
|