tbf/TestBenchFramework/UiControls/PathsBenchCtrl.cs

292 lines
9.4 KiB
C#

///
/// Copyright (c) 2013-2015 Sensus Metering Systems
///
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Forms;
using log4net;
using TBF.BenchControl;
using TBF.BenchControl.GenericDevices;
using TBF.Resources;
namespace TBF.UiControls
{
public class PathsBenchCtrl : BaseWithListViewEx<Config.Entities.BenchPath>, ITabWithListViewEx
{
static readonly ILog log = LogManager.GetLogger(typeof(PathsBenchCtrl));
/// <summary>
/// Fixed ListViewEx columns
/// </summary>
enum Column
{
Name,
Qfrom,
Qto,
Tin,
Tout,
Pin,
Pout,
StopBFValve,
FixedColumnsCount,
}
readonly int fixedColumnsCount = (int)Column.FixedColumnsCount;
PathsDlg parent;
Control parentControl;
Control[] editors;
public PathsBenchCtrl()
: base()
{
FixedRowsCount = 1;
}
public void Initialize(PathsDlg parent, Control parentControl)
{
this.parent = parent;
this.parentControl = parentControl;
/// Load the paths
MyItems = parent.Session
.CreateQuery("FROM BenchPath ORDER BY ItemNr")
.List<Config.Entities.BenchPath>();
SubItemClicked += new SubItemEventHandler(listViewEx_SubItemClicked);
SubItemEndEditing += new SubItemEndEditingEventHandler(listViewEx_SubItemEndEditing);
/// ListViewEx columns
TBF.LocalSettings ls = Program.LocalSettings;
Columns.Add(Strings.Name, (ls.BenchColumnCount > 0) ? ls.BenchColumnWidths[0] : 60 * parent.Dpi / PathsDlg.Dpi100pct);
Columns.Add(Strings.Q_from_m3h, (ls.BenchColumnCount > 1) ? ls.BenchColumnWidths[1] : 50 * parent.Dpi / PathsDlg.Dpi100pct);
Columns.Add(Strings.Q_to_m3h, (ls.BenchColumnCount > 2) ? ls.BenchColumnWidths[2] : 50 * parent.Dpi / PathsDlg.Dpi100pct);
Columns.Add(Strings.T_in, (ls.BenchColumnCount > 3) ? ls.BenchColumnWidths[3] : 50 * parent.Dpi / PathsDlg.Dpi100pct);
Columns.Add(Strings.T_out, (ls.BenchColumnCount > 4) ? ls.BenchColumnWidths[4] : 50 * parent.Dpi / PathsDlg.Dpi100pct);
Columns.Add(Strings.P_in, (ls.BenchColumnCount > 5) ? ls.BenchColumnWidths[5] : 50 * parent.Dpi / PathsDlg.Dpi100pct);
Columns.Add(Strings.P_out, (ls.BenchColumnCount > 6) ? ls.BenchColumnWidths[6] : 50 * parent.Dpi / PathsDlg.Dpi100pct);
Columns.Add(Strings.StopBFValve_chdr, (ls.BenchColumnCount > 7) ? ls.BenchColumnWidths[7] : 70 * parent.Dpi / PathsDlg.Dpi100pct);
int clmn = (int)Column.FixedColumnsCount;
foreach (var vlv in parent.Valves)
{
if (vlv.Category == ValveCategory.All || vlv.Category == ValveCategory.Bench)
{
Columns.Add(vlv.Cfg.Name, (ls.BenchColumnCount > clmn) ? ls.BenchColumnWidths[clmn] : 50 * parent.Dpi / PathsDlg.Dpi100pct);
clmn++;
}
}
/// Temperatures, pressures
ComboBox tInCBox = new ComboBox();
ComboBox tOutCBox = new ComboBox();
ComboBox prInCBox = new ComboBox();
ComboBox prOutCBox = new ComboBox();
ComboBox stopBFValveComboBox = new ComboBox();
stopBFValveComboBox.Items.Add("---");
foreach (var cmpnt in parent.TbfComponents)
{
if (cmpnt is BenchControl.GenericDevices.ITempMeter)
{
tInCBox.Items.Add(cmpnt.Cfg.Name);
tOutCBox.Items.Add(cmpnt.Cfg.Name);
}
if (cmpnt is BenchControl.GenericDevices.IPressureMeter)
{
prInCBox.Items.Add(cmpnt.Cfg.Name);
prOutCBox.Items.Add(cmpnt.Cfg.Name);
}
if (cmpnt is IValve && (cmpnt as IValve).CoupledTo == null)
{
stopBFValveComboBox.Items.Add(cmpnt.Cfg.Name);
}
}
editors = new Control[fixedColumnsCount + 64];
///
editors[(int)Column.Name] = new TextBox(); /// Name
editors[(int)Column.Qfrom] = new TextBox(); /// Q_from
editors[(int)Column.Qto] = new TextBox(); /// Q_to
editors[(int)Column.Tin] = tInCBox;
editors[(int)Column.Tout] = tOutCBox;
editors[(int)Column.Pin] = prInCBox;
editors[(int)Column.Pout] = prOutCBox;
editors[(int)Column.StopBFValve] = stopBFValveComboBox;
/// Prepare valve combo-boxes (max. 64 valves)
for (int i = 0; i < 64; i++)
{
ComboBox cb = new ComboBox();
cb.Items.Add("---");
cb.Items.Add(Strings.open);
cb.Items.Add(Strings.close);
editors[fixedColumnsCount + i] = cb;
}
///
for (int i = 0; i < editors.Length; i++)
{
editors[i].Visible = false;
parent.Controls.Add(editors[i]);
}
base.RedrawAll();
}
void listViewEx_SubItemClicked(object sender, SubItemEventArgs e)
{
if (!Unlocked || e.SubItem >= editors.Length) return;
if ((e.Item.Tag is Config.Entities.IHasItemNr) &&
((e.Item.Tag as Config.Entities.IHasItemNr).ItemNr < FixedRowsCount) &&
(e.SubItem < fixedColumnsCount))
{
return;
}
StartEditing(editors[e.SubItem], e.Item, e.SubItem);
}
void listViewEx_SubItemEndEditing(object sender, SubItemEndEditingEventArgs e)
{
}
public override void DrawOne(object myItem)
{
Config.Entities.BenchPath entity =(Config.Entities.BenchPath)myItem;
BenchControl.BenchPath path = new BenchControl.BenchPath(entity, parent.TbfComponents);
ListViewItem lvi = new ListViewItem(entity.Name);
lvi.SubItems.Add(path.Qfrom.ToString());
lvi.SubItems.Add(path.Qto.ToString());
lvi.SubItems.Add(path.TempIn != null ? path.TempIn.Cfg.Name : "---");
lvi.SubItems.Add(path.TempOut != null ? path.TempOut.Cfg.Name : "---");
lvi.SubItems.Add(path.PressIn != null ? path.PressIn.Cfg.Name : "---");
lvi.SubItems.Add(path.PressOut != null ? path.PressOut.Cfg.Name : "---");
lvi.SubItems.Add((path.StopBFValve != null) ? path.StopBFValve.Cfg.Name : "---");
foreach (var valve in parent.Valves)
{
if (valve.Category == ValveCategory.All || valve.Category == ValveCategory.Bench)
{
if (path.ValvesOpen != null && path.ValvesOpen.Contains(valve))
{
lvi.SubItems.Add(Strings.open);
}
else if (path.ValvesClose != null && path.ValvesClose.Contains(valve))
{
lvi.SubItems.Add(Strings.close);
}
else
{
lvi.SubItems.Add("---");
}
}
}
lvi.Tag = entity;
Items.Add(lvi);
}
public override void UpdateOne(ListViewItem lvi, object myItem)
{
Config.Entities.BenchPath entity = (Config.Entities.BenchPath)myItem;
entity.Name = lvi.Text;
float Qfrom;
bool allOk = Utils.TryParseUFloat(lvi.SubItems[(int)Column.Qfrom].Text, out Qfrom);
entity.Qfrom = Qfrom;
float Qto;
allOk &= Utils.TryParseUFloat(lvi.SubItems[(int)Column.Qto].Text, out Qto);
entity.Qto = Qto;
entity.TempIn = lvi.SubItems[(int)Column.Tin].Text;
entity.TempOut = lvi.SubItems[(int)Column.Tout].Text;
entity.PressIn = lvi.SubItems[(int)Column.Pin].Text;
entity.PressOut = lvi.SubItems[(int)Column.Pout].Text;
entity.StopBFValve = lvi.SubItems[(int)Column.StopBFValve].Text.Equals("---") ? null : lvi.SubItems[(int)Column.StopBFValve].Text;
string valvesOpen = string.Empty;
string valvesClose = string.Empty;
int k = 0;
for (int j = 0; j < parent.Valves.Count; j++)
{
if (parent.Valves[j].Category == ValveCategory.All || parent.Valves[j].Category == ValveCategory.Bench)
{
string text = lvi.SubItems[fixedColumnsCount + k].Text;
if (text.Equals(Strings.open))
{
if (valvesOpen != string.Empty) valvesOpen += ";";
valvesOpen += parent.Valves[j].Cfg.Name;
}
if (text.Equals(Strings.close))
{
if (valvesClose != string.Empty) valvesClose += ";";
valvesClose += parent.Valves[j].Cfg.Name;
}
k++;
}
}
entity.ValvesOpen = valvesOpen;
entity.ValvesClose = valvesClose;
}
/// <summary>
/// Update the entities and save them to the database
/// </summary>
public new void OkBtnClicked()
{
base.OkBtnClicked();
/// Update the database
foreach (var entity in ToBeRemovedItems)
{
Config.FluentCommon.DeleteFromDb(parent.Session, entity);
}
foreach (var entity in MyItems)
{
Config.FluentCommon.SaveToDb(parent.Session, entity);
}
/// Update the column widths
Program.LocalSettings.BenchColumnWidths = new int[Columns.Count];
for (int i = 0; i < Columns.Count; i++)
{
Program.LocalSettings.BenchColumnWidths[i] = Columns[i].Width;
}
Program.LocalSettings.Save();
}
public void AddOne()
{
Config.Entities.BenchPath entity;
if (MyItems.Count < FixedRowsCount)
{
entity = new Config.Entities.BenchPath(Strings._none_, MyItems.Count);
}
else
{
/// Find an unused test name
string newName;
for (int nameId = 1; true; nameId++)
{
newName = Strings.New_bench_path_name + nameId.ToString();
bool notUsed = true;
foreach (var t in MyItems) if (t.Name.Equals(newName)) notUsed = false;
if (notUsed) break;
}
entity = new Config.Entities.BenchPath(newName, MyItems.Count);
}
base.AddOne(entity);
}
public new void RemoveSelected()
{
bool lastOneRemoved = base.RemoveSelected();
if (lastOneRemoved && parent != null)
{
parent.UpdateButtonStates(SharedButtons.SelectedItemPos.None);
}
}
}
}