432 lines
13 KiB
C#
432 lines
13 KiB
C#
///
|
|
/// Copyright (c) 2013-2015 Sensus Metering Systems
|
|
///
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Windows.Forms;
|
|
using log4net;
|
|
using Config.Entities;
|
|
using TBF.BenchControl;
|
|
using TBF.BenchControl.GenericDevices;
|
|
using TBF.Resources;
|
|
|
|
namespace TBF.UiControls
|
|
{
|
|
public partial class TransitionStepsCtrl : BaseWithListViewEx<TransitionStep>, ITabWithListViewEx
|
|
{
|
|
static readonly ILog log = LogManager.GetLogger(typeof(TransitionStepsCtrl));
|
|
|
|
/// <summary>
|
|
/// Fixed ListViewEx columns
|
|
/// </summary>
|
|
enum Column
|
|
{
|
|
Duration,
|
|
Message,
|
|
Condition,
|
|
FixedColumnsCount,
|
|
}
|
|
readonly int fixedCount = (int)Column.FixedColumnsCount;
|
|
|
|
TransitionSequence sequence;
|
|
|
|
TransitionsDlg parent;
|
|
Control parentControl;
|
|
Control[] editors;
|
|
|
|
int fmPumpCount; /// Number of pumps with FM control
|
|
int regvCount; /// Number of regulating valves
|
|
int vCount; /// Number of valves
|
|
|
|
public TransitionStepsCtrl()
|
|
: base()
|
|
{
|
|
}
|
|
|
|
public void Initialize(TransitionSequence sequence, TransitionsDlg parent, Control parentControl)
|
|
{
|
|
this.sequence = sequence;
|
|
this.parent = parent;
|
|
this.parentControl = parentControl;
|
|
Name = sequence.Name;
|
|
MyItems = sequence.TransitionSteps;
|
|
|
|
SubItemClicked += new SubItemEventHandler(listViewEx_SubItemClicked);
|
|
SubItemEndEditing += new SubItemEndEditingEventHandler(listViewEx_SubItemEndEditing);
|
|
|
|
vCount = 0;
|
|
fmPumpCount = 0;
|
|
regvCount = parent.RegulValves.Count;
|
|
|
|
///
|
|
/// ListViewEx columns
|
|
///
|
|
Columns.Add(Strings.Duration_s, 70 * parent.Dpi / PathsDlg.Dpi100pct);
|
|
Columns.Add(Strings.Message, 100 * parent.Dpi / PathsDlg.Dpi100pct);
|
|
Columns.Add(Strings.End_Condition, 80 * parent.Dpi / PathsDlg.Dpi100pct);
|
|
foreach (var vlv in parent.Valves)
|
|
{
|
|
if (vlv is BenchControl.GenericDevices.IPumpFM)
|
|
{
|
|
Columns.Add(vlv.Cfg.Name + " [%]", 45 * parent.Dpi / PathsDlg.Dpi100pct);
|
|
fmPumpCount++;
|
|
}
|
|
}
|
|
foreach (var rvlv in parent.RegulValves)
|
|
{
|
|
Columns.Add(rvlv.Cfg.Name + " [%]", 55 * parent.Dpi / PathsDlg.Dpi100pct);
|
|
}
|
|
foreach (var vlv in parent.Valves)
|
|
{
|
|
if (!(vlv is BenchControl.GenericDevices.IPumpFM))
|
|
{
|
|
Columns.Add(vlv.Cfg.Name, 40 * parent.Dpi / PathsDlg.Dpi100pct);
|
|
vCount++;
|
|
}
|
|
}
|
|
|
|
///
|
|
/// Prepare valve combo-boxes
|
|
///
|
|
TextBox tb;
|
|
ComboBox cb;
|
|
editors = new Control[fixedCount + fmPumpCount + regvCount + vCount];
|
|
|
|
/// Duration
|
|
editors[(int)Column.Duration] = tb = new TextBox();
|
|
tb.Visible = false;
|
|
parent.Controls.Add(tb);
|
|
|
|
/// Message
|
|
editors[(int)Column.Message] = tb = new TextBox();
|
|
tb.Visible = false;
|
|
parent.Controls.Add(tb);
|
|
|
|
/// End condition
|
|
editors[(int)Column.Condition] = cb = new ComboBox();
|
|
cb.Visible = false;
|
|
cb.Items.Add("---");
|
|
for (int i = 1; i < (int)StepCondition.Count; i++) cb.Items.Add(((StepCondition)i).ToString());
|
|
parent.Controls.Add(cb);
|
|
|
|
/// FM controlled pumps
|
|
for (int i = fixedCount; i < fixedCount + fmPumpCount; i++)
|
|
{
|
|
editors[i] = cb = new ComboBox();
|
|
cb.Visible = false;
|
|
cb.Items.Add("---");
|
|
cb.Items.Add(Strings.dflt);
|
|
cb.Items.Add("0");
|
|
cb.Items.Add("30");
|
|
cb.Items.Add("40");
|
|
cb.Items.Add("50");
|
|
cb.Items.Add("60");
|
|
cb.Items.Add("70");
|
|
cb.Items.Add("75");
|
|
cb.Items.Add("80");
|
|
cb.Items.Add("85");
|
|
cb.Items.Add("90");
|
|
cb.Items.Add("92");
|
|
cb.Items.Add("94");
|
|
cb.Items.Add("96");
|
|
cb.Items.Add("98");
|
|
cb.Items.Add("100");
|
|
parent.Controls.Add(cb);
|
|
}
|
|
|
|
/// Regulation valves
|
|
for (int i = fixedCount + fmPumpCount; i < fixedCount + fmPumpCount + regvCount; i++)
|
|
{
|
|
editors[i] = cb = new ComboBox();
|
|
cb.Visible = false;
|
|
cb.Items.Add("---");
|
|
cb.Items.Add("0");
|
|
cb.Items.Add("25");
|
|
cb.Items.Add("50");
|
|
cb.Items.Add("75");
|
|
cb.Items.Add("100");
|
|
parent.Controls.Add(cb);
|
|
}
|
|
|
|
/// Valves
|
|
for (int i = fixedCount + fmPumpCount + regvCount; i < fixedCount + fmPumpCount + regvCount + vCount; i++)
|
|
{
|
|
editors[i] = cb = new ComboBox();
|
|
cb.Visible = false;
|
|
cb.Items.Add("---");
|
|
cb.Items.Add(Strings.open);
|
|
cb.Items.Add(Strings.close);
|
|
parent.Controls.Add(cb);
|
|
}
|
|
|
|
base.RedrawAll();
|
|
}
|
|
|
|
void listViewEx_SubItemClicked(object sender, SubItemEventArgs e)
|
|
{
|
|
if (!Unlocked || e.SubItem >= editors.Length) return;
|
|
StartEditing(editors[e.SubItem], e.Item, e.SubItem);
|
|
}
|
|
|
|
void listViewEx_SubItemEndEditing(object sender, SubItemEndEditingEventArgs e)
|
|
{
|
|
if (e.SubItem == (int)Column.Duration)
|
|
{
|
|
int duration;
|
|
if (!int.TryParse(e.DisplayText, out duration) || duration <= 0)
|
|
{
|
|
e.DisplayText = e.Item.Text;
|
|
e.Cancel = true;
|
|
}
|
|
}
|
|
else if (e.SubItem == (int)Column.Message)
|
|
{
|
|
if (e.DisplayText.Length >= 255)
|
|
{
|
|
// Message too long
|
|
e.DisplayText = e.Item.SubItems[e.SubItem].Text;
|
|
e.Cancel = true;
|
|
}
|
|
}
|
|
else if (e.SubItem == (int)Column.Condition)
|
|
{
|
|
if (!((ComboBox)editors[(int)Column.Condition]).Items.Contains(e.DisplayText))
|
|
{
|
|
e.DisplayText = e.Item.SubItems[e.SubItem].Text;
|
|
e.Cancel = true;
|
|
}
|
|
}
|
|
else if ((e.SubItem >= fixedCount) && (e.SubItem < fixedCount + fmPumpCount))
|
|
{
|
|
float pct;
|
|
if (!e.DisplayText.Equals("---") &&
|
|
!e.DisplayText.Equals(Strings.dflt) &&
|
|
!(Utils.TryParseUFloat(e.DisplayText, out pct) && (pct >= 0) && (pct <= 100.0f)))
|
|
{
|
|
e.DisplayText = e.Item.SubItems[e.SubItem].Text;
|
|
e.Cancel = true;
|
|
}
|
|
}
|
|
else if ((e.SubItem >= fixedCount + fmPumpCount) && (e.SubItem < fixedCount + fmPumpCount + regvCount))
|
|
{
|
|
float pct;
|
|
if (!e.DisplayText.Equals("---") &&
|
|
!(Utils.TryParseUFloat(e.DisplayText, out pct) && (pct >= 0) && (pct <= 100.0f)))
|
|
{
|
|
e.DisplayText = e.Item.SubItems[e.SubItem].Text;
|
|
e.Cancel = true;
|
|
}
|
|
}
|
|
else if ((e.SubItem >= fixedCount + fmPumpCount + regvCount) && (e.SubItem < fixedCount + fmPumpCount + regvCount + vCount))
|
|
{
|
|
if ((e.DisplayText != Strings.open) && (e.DisplayText != Strings.close) && (e.DisplayText != "---"))
|
|
{
|
|
e.DisplayText = e.Item.SubItems[e.SubItem].Text;
|
|
e.Cancel = true;
|
|
}
|
|
else
|
|
{
|
|
e.Item.SubItems[e.SubItem].BackColor = e.DisplayText == Strings.open ? Color.LightGray : Color.White;
|
|
}
|
|
}
|
|
}
|
|
|
|
public override void DrawOne(object myItem)
|
|
{
|
|
TransitionStep step = (TransitionStep)myItem;
|
|
|
|
IList<IValve> valvesOpen = new List<IValve>();
|
|
if (step.ValvesOpen != null)
|
|
{
|
|
string[] vOpen = step.ValvesOpen.Split(new char[] { ';' });
|
|
foreach (var vName in vOpen) valvesOpen.Add((IValve)TbfComponents.FindComponent(vName, parent.TbfComponents));
|
|
}
|
|
|
|
IList<IValve> valvesClose = new List<IValve>();
|
|
if (step.ValvesClose != null)
|
|
{
|
|
string[] vClose = step.ValvesClose.Split(new char[] { ';' });
|
|
foreach (var vName in vClose) valvesClose.Add((IValve)TbfComponents.FindComponent(vName, parent.TbfComponents));
|
|
}
|
|
|
|
ListViewItem lvi = new ListViewItem(step.Duration.ToString());
|
|
lvi.UseItemStyleForSubItems = false;
|
|
|
|
lvi.SubItems.Add(string.IsNullOrEmpty(step.Message) ? string.Empty : step.Message);
|
|
lvi.SubItems.Add((step.EndCondition == StepCondition.None) ? "---" : step.EndCondition.ToString());
|
|
|
|
string[] fmPumpsStr = (step.PumpWithFMPcts != null) ? step.PumpWithFMPcts.Split(new char[] { ';' })
|
|
: new string[0];
|
|
for (int i = 0; i < fmPumpCount; i++)
|
|
{
|
|
lvi.SubItems.Add((i < fmPumpsStr.Length) ? fmPumpsStr[i] : "---");
|
|
}
|
|
|
|
string[] rvPosStr = (step.RegulValvesPct != null) ? step.RegulValvesPct.Split(new char[] { ';' })
|
|
: new string[0];
|
|
for (int i = 0; i < regvCount; i++)
|
|
{
|
|
lvi.SubItems.Add((i < rvPosStr.Length) ? rvPosStr[i] : "---");
|
|
}
|
|
|
|
foreach (var valve in parent.Valves)
|
|
{
|
|
if (!(valve is BenchControl.GenericDevices.IPumpFM))
|
|
{
|
|
if (valvesOpen != null && valvesOpen.Contains(valve))
|
|
{
|
|
lvi.SubItems.Add(Strings.open);
|
|
lvi.SubItems[lvi.SubItems.Count - 1].BackColor = Color.LightGray;
|
|
}
|
|
else if (valvesClose != null && valvesClose.Contains(valve))
|
|
{
|
|
lvi.SubItems.Add(Strings.close);
|
|
lvi.SubItems[lvi.SubItems.Count - 1].BackColor = Color.White;
|
|
}
|
|
else
|
|
{
|
|
lvi.SubItems.Add("---");
|
|
lvi.SubItems[lvi.SubItems.Count - 1].BackColor = Color.White;
|
|
}
|
|
}
|
|
}
|
|
|
|
lvi.Tag = step;
|
|
|
|
Items.Add(lvi);
|
|
}
|
|
|
|
public override void UpdateOne(ListViewItem lvi, object myItem)
|
|
{
|
|
TransitionStep step = (TransitionStep)myItem;
|
|
|
|
///
|
|
/// Duration
|
|
///
|
|
int duration;
|
|
if (int.TryParse(lvi.Text, out duration) && duration > 0)
|
|
step.Duration = duration;
|
|
else
|
|
lvi.Text = step.Duration.ToString();
|
|
|
|
///
|
|
/// Message
|
|
///
|
|
if (lvi.SubItems[(int)Column.Message].Text.Length == 0)
|
|
step.Message = string.Empty;
|
|
else
|
|
step.Message = lvi.SubItems[(int)Column.Message].Text;
|
|
|
|
///
|
|
/// Condition
|
|
///
|
|
step.EndCondition = StepCondition.None; /// When item text is "---" or a wrong value
|
|
for (int cond = 1; cond < (int)StepCondition.Count; cond++)
|
|
{
|
|
if (lvi.SubItems[(int)Column.Condition].Text.Equals(((StepCondition)cond).ToString()))
|
|
{
|
|
step.EndCondition = (StepCondition)cond;
|
|
break;
|
|
}
|
|
}
|
|
|
|
/// Required for pumps with FM and valves
|
|
string valvesOpen = string.Empty;
|
|
string valvesClose = string.Empty;
|
|
|
|
///
|
|
/// Pumps with FM control
|
|
///
|
|
string fmPumpsPcts = string.Empty;
|
|
int i = 0;
|
|
foreach (var valve in parent.Valves)
|
|
{
|
|
if (valve is BenchControl.GenericDevices.IPumpFM)
|
|
{
|
|
string lviSubitText = lvi.SubItems[fixedCount + i++].Text;
|
|
if (fmPumpsPcts.Length > 0) fmPumpsPcts += ";";
|
|
float fdummy;
|
|
if (lviSubitText.Equals("---"))
|
|
{
|
|
fmPumpsPcts += lviSubitText;
|
|
}
|
|
else if (lviSubitText.Equals(Strings.dflt))
|
|
{
|
|
fmPumpsPcts += lviSubitText;
|
|
if (valvesOpen != string.Empty) valvesOpen += ";";
|
|
valvesOpen += valve.Cfg.Name;
|
|
}
|
|
else if (Utils.TryParseUFloat(lviSubitText, out fdummy) && (fdummy >= 0) && (fdummy <= 100.0f))
|
|
{
|
|
fmPumpsPcts += lviSubitText;
|
|
if (fdummy > 0)
|
|
{
|
|
if (valvesOpen != string.Empty) valvesOpen += ";";
|
|
valvesOpen += valve.Cfg.Name;
|
|
}
|
|
else
|
|
{
|
|
if (valvesClose != string.Empty) valvesClose += ";";
|
|
valvesClose += valve.Cfg.Name;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
step.PumpWithFMPcts = fmPumpsPcts;
|
|
|
|
///
|
|
/// Regulation valves
|
|
///
|
|
string regulValvesPct = string.Empty;
|
|
for (int k = fixedCount + fmPumpCount; k < fixedCount + fmPumpCount + regvCount; k++)
|
|
{
|
|
if (regulValvesPct.Length > 0) regulValvesPct += ";";
|
|
float fdummy;
|
|
if (lvi.SubItems[k].Text.Equals("---") ||
|
|
(Utils.TryParseUFloat(lvi.SubItems[k].Text, out fdummy) && (fdummy >= 0) && (fdummy <= 100.0f)))
|
|
{
|
|
regulValvesPct += lvi.SubItems[k].Text;
|
|
}
|
|
}
|
|
step.RegulValvesPct = regulValvesPct;
|
|
|
|
///
|
|
/// Valves
|
|
///
|
|
for (int j = 0, k = 0; j < parent.Valves.Count; j++)
|
|
{
|
|
if (!(parent.Valves[j] is BenchControl.GenericDevices.IPumpFM))
|
|
{
|
|
string lviSubitText = lvi.SubItems[fixedCount + fmPumpCount + regvCount + k++].Text;
|
|
if (lviSubitText.Equals(Strings.open))
|
|
{
|
|
if (valvesOpen != string.Empty) valvesOpen += ";";
|
|
valvesOpen += parent.Valves[j].Cfg.Name;
|
|
}
|
|
if (lviSubitText.Equals(Strings.close))
|
|
{
|
|
if (valvesClose != string.Empty) valvesClose += ";";
|
|
valvesClose += parent.Valves[j].Cfg.Name;
|
|
}
|
|
}
|
|
}
|
|
step.ValvesOpen = valvesOpen;
|
|
step.ValvesClose = valvesClose;
|
|
}
|
|
|
|
public void AddOne()
|
|
{
|
|
base.AddOne(new TransitionStep(MyItems.Count, sequence));
|
|
}
|
|
|
|
public new void RemoveSelected()
|
|
{
|
|
bool lastItemRemoved = base.RemoveSelected();
|
|
if (lastItemRemoved && parent != null)
|
|
{
|
|
parent.UpdateButtonStates(SharedButtons.SelectedItemPos.None);
|
|
}
|
|
}
|
|
}
|
|
}
|