501 lines
18 KiB
C#
501 lines
18 KiB
C#
///
|
|
/// Copyright (c) 2013-2022 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Windows.Forms;
|
|
using log4net;
|
|
using Common;
|
|
using Common.Forms;
|
|
using TBF.Rig;
|
|
using TBF.Rig.GenericDevices;
|
|
using TBF.Resources;
|
|
using TBF.UI.Shared;
|
|
|
|
namespace TBF.UI.Bench.Paths
|
|
{
|
|
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,
|
|
FlowUnit,
|
|
Selector,
|
|
Tup,
|
|
Tdown,
|
|
Pup,
|
|
Pdown,
|
|
Pdelta,
|
|
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.QueryOver<Config.Entities.BenchPath>()
|
|
.OrderBy(x => x.ItemNr).Asc
|
|
.List();
|
|
|
|
foreach (var path in MyItems)
|
|
{
|
|
path.OriName = path.Name;
|
|
path.FlowUnit = TBF.Rig.Sequences.ProcessData.FlowUnit;
|
|
}
|
|
|
|
SubItemClicked += new SubItemEventHandler(listViewEx_SubItemClicked);
|
|
SubItemRightClicked += new SubItemEventHandler(listViewEx_SubItemRightClicked);
|
|
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 / Constants.Dpi100pct);
|
|
Columns.Add(Strings.Q_from, (ls.BenchColumnCount > 1) ? ls.BenchColumnWidths[1] : 80 * parent.Dpi / Constants.Dpi100pct);
|
|
Columns.Add(Strings.Q_to, (ls.BenchColumnCount > 2) ? ls.BenchColumnWidths[2] : 80 * parent.Dpi / Constants.Dpi100pct);
|
|
Columns.Add("Unit", (ls.BenchColumnCount > 3) ? ls.BenchColumnWidths[3] : 50 * parent.Dpi / Constants.Dpi100pct);
|
|
Columns.Add("Selector", (ls.BenchColumnCount > 4) ? ls.BenchColumnWidths[4] : 60 * parent.Dpi / Constants.Dpi100pct);
|
|
Columns.Add(Strings.T_in, (ls.BenchColumnCount > 5) ? ls.BenchColumnWidths[5] : 50 * parent.Dpi / Constants.Dpi100pct);
|
|
Columns.Add(Strings.T_out, (ls.BenchColumnCount > 6) ? ls.BenchColumnWidths[6] : 50 * parent.Dpi / Constants.Dpi100pct);
|
|
Columns.Add(Strings.P_in, (ls.BenchColumnCount > 7) ? ls.BenchColumnWidths[7] : 50 * parent.Dpi / Constants.Dpi100pct);
|
|
Columns.Add(Strings.P_out, (ls.BenchColumnCount > 8) ? ls.BenchColumnWidths[8] : 50 * parent.Dpi / Constants.Dpi100pct);
|
|
Columns.Add(Strings.P_delta, (ls.BenchColumnCount > 9) ? ls.BenchColumnWidths[9] : 50 * parent.Dpi / Constants.Dpi100pct);
|
|
Columns.Add(Strings.StopBFValve_chdr, (ls.BenchColumnCount > 10) ? ls.BenchColumnWidths[10] : 70 * parent.Dpi / Constants.Dpi100pct);
|
|
int clmn = (int)Column.FixedColumnsCount;
|
|
foreach (var vlv in parent.Valves)
|
|
{
|
|
if (vlv.Category == ValveCategory.Bench)
|
|
{
|
|
Columns.Add(vlv.Cfg.Name, (ls.BenchColumnCount > clmn) ? ls.BenchColumnWidths[clmn] : 50 * parent.Dpi / Constants.Dpi100pct);
|
|
clmn++;
|
|
}
|
|
}
|
|
|
|
/// Flow unit
|
|
var flowUnitBox = new ComboBox();
|
|
for (Unit u = 0; u < Unit.Count; u++) if (Units.IsFlow(u)) flowUnitBox.Items.Add(u.ToDescription());
|
|
|
|
/// Temperatures, pressures
|
|
ComboBox tUpCBox = new ComboBox();
|
|
ComboBox tDownCBox = new ComboBox();
|
|
ComboBox prUpCBox = new ComboBox();
|
|
ComboBox prDownCBox = new ComboBox();
|
|
ComboBox prDeltaCBox = new ComboBox();
|
|
ComboBox stopBFValveComboBox = new ComboBox();
|
|
stopBFValveComboBox.Items.Add("---");
|
|
foreach (var cmpnt in parent.TbfComponents)
|
|
{
|
|
if (cmpnt is Rig.GenericDevices.ITempMeter)
|
|
{
|
|
tUpCBox.Items.Add(cmpnt.Cfg.Name);
|
|
tDownCBox.Items.Add(cmpnt.Cfg.Name);
|
|
}
|
|
if ((cmpnt is Rig.GenericDevices.IPressureMeter) ||
|
|
(cmpnt is Rig.GenericDevices.IAdjustableMeter))
|
|
{
|
|
prUpCBox.Items.Add(cmpnt.Cfg.Name);
|
|
prDownCBox.Items.Add(cmpnt.Cfg.Name);
|
|
prDeltaCBox.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.FlowUnit] = flowUnitBox; /// Flow unit
|
|
editors[(int)Column.Selector] = new TextBox(); /// Selector
|
|
editors[(int)Column.Tup] = tUpCBox;
|
|
editors[(int)Column.Tdown] = tDownCBox;
|
|
editors[(int)Column.Pup] = prUpCBox;
|
|
editors[(int)Column.Pdown] = prDownCBox;
|
|
editors[(int)Column.Pdelta] = prDownCBox;
|
|
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 (e.SubItem != (int)Column.FlowUnit && (!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_SubItemRightClicked(object sender, SubItemEventArgs e)
|
|
{
|
|
if (!Unlocked || e.SubItem >= editors.Length) return;
|
|
|
|
if (DialogResult.Yes == MessageBox.Show(Strings.Do_you_want_to_copy_this_value_to_all_cells_below_this_cell,
|
|
Strings.Confirmation, MessageBoxButtons.YesNo, MessageBoxIcon.Question))
|
|
{
|
|
int columnNr = e.SubItem;
|
|
string value = null;
|
|
Color backColor = Color.White;
|
|
foreach (ListViewItem item in Items)
|
|
{
|
|
if (value != null && item.SubItems.Count > columnNr)
|
|
{
|
|
item.SubItems[columnNr].Text = value;
|
|
item.SubItems[columnNr].BackColor = backColor;
|
|
|
|
if (columnNr == (int)Column.FlowUnit) UpdateFlow(item, Units.FromDescription(value));
|
|
}
|
|
else if (item == e.Item)
|
|
{
|
|
value = item.SubItems[columnNr].Text;
|
|
backColor = item.SubItems[columnNr].BackColor;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void listViewEx_SubItemEndEditing(object sender, SubItemEndEditingEventArgs e)
|
|
{
|
|
var entity = e.Item.Tag as Config.Entities.BenchPath;
|
|
double ddummy;
|
|
|
|
if (e.SubItem >= fixedColumnsCount)
|
|
{
|
|
e.Item.SubItems[e.SubItem].BackColor = e.DisplayText == Strings.open ? Color.LightGray : Color.White;
|
|
}
|
|
else if (e.SubItem == (int)Column.Qfrom)
|
|
{
|
|
if (Utils.TryParseUDouble(e.DisplayText, out ddummy))
|
|
{
|
|
entity.QfromTextChngd = true;
|
|
}
|
|
else
|
|
{
|
|
e.DisplayText = e.Item.SubItems[e.SubItem].Text;
|
|
e.Cancel = true;
|
|
}
|
|
}
|
|
else if (e.SubItem == (int)Column.Qto)
|
|
{
|
|
if (Utils.TryParseUDouble(e.DisplayText, out ddummy))
|
|
{
|
|
entity.QtoTextChngd = true;
|
|
}
|
|
else
|
|
{
|
|
e.DisplayText = e.Item.SubItems[e.SubItem].Text;
|
|
e.Cancel = true;
|
|
}
|
|
}
|
|
else if (e.SubItem == (int)Column.FlowUnit)
|
|
{
|
|
Unit newUnit = Units.FromDescription(e.DisplayText);
|
|
if (Units.IsFlow(newUnit))
|
|
{
|
|
UpdateFlow(e.Item, newUnit);
|
|
}
|
|
else
|
|
{
|
|
e.DisplayText = e.Item.SubItems[e.SubItem].Text;
|
|
e.Cancel = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
void UpdateFlow(ListViewItem item, Unit newUnit)
|
|
{
|
|
var entity = item.Tag as Config.Entities.BenchPath;
|
|
if (entity == null || !Units.IsFlow(newUnit) || newUnit == entity.FlowUnit) return;
|
|
Unit oriUnit = entity.FlowUnit;
|
|
entity.FlowUnit = newUnit;
|
|
|
|
int ix = (int)Column.Qfrom;
|
|
double flowM3ph = Units.ConvertFrom(oriUnit, Utils.ParseUDouble(item.SubItems[ix].Text));
|
|
item.SubItems[ix].Text = Units.ConvertTo(newUnit, entity.QfromTextChngd ? flowM3ph : entity.Qfrom).ToString();
|
|
|
|
ix = (int)Column.Qto;
|
|
flowM3ph = Units.ConvertFrom(oriUnit, Utils.ParseUDouble(item.SubItems[ix].Text));
|
|
item.SubItems[ix].Text = Units.ConvertTo(newUnit, entity.QtoTextChngd ? flowM3ph : entity.Qto).ToString();
|
|
}
|
|
|
|
public override void DrawOne(object myItem)
|
|
{
|
|
Config.Entities.BenchPath entity = (Config.Entities.BenchPath)myItem;
|
|
Rig.BenchPath path = new Rig.BenchPath(entity, parent.TbfComponents);
|
|
|
|
ListViewItem lvi = new ListViewItem(entity.Name);
|
|
lvi.UseItemStyleForSubItems = false;
|
|
|
|
lvi.SubItems.Add(Units.ConvertTo(entity.FlowUnit, path.Qfrom).ToString()); /// Q_from
|
|
lvi.SubItems.Add(Units.ConvertTo(entity.FlowUnit, path.Qto).ToString()); /// Q_to
|
|
lvi.SubItems.Add(entity.FlowUnit.ToDescription());
|
|
lvi.SubItems.Add(path.Selector);
|
|
lvi.SubItems.Add(path.TempMtrUp != null ? path.TempMtrUp.Cfg.Name : "---");
|
|
lvi.SubItems.Add(path.TempMtrDown != null ? path.TempMtrDown.Cfg.Name : "---");
|
|
lvi.SubItems.Add(path.PressMtrUp != null ? path.PressMtrUp.Cfg.Name : "---");
|
|
lvi.SubItems.Add(path.PressMtrDown != null ? path.PressMtrDown.Cfg.Name : "---");
|
|
lvi.SubItems.Add(path.PressMtrDelta != null ? path.PressMtrDelta.Cfg.Name : "---");
|
|
lvi.SubItems.Add((path.StopBFValve != null) ? path.StopBFValve.Cfg.Name : "---");
|
|
|
|
foreach (var valve in parent.Valves)
|
|
{
|
|
if (valve.Category == ValveCategory.Bench)
|
|
{
|
|
if (path.ValvesOpen != null && path.ValvesOpen.Contains(valve))
|
|
{
|
|
lvi.SubItems.Add(Strings.open);
|
|
lvi.SubItems[lvi.SubItems.Count - 1].BackColor = Color.LightGray;
|
|
}
|
|
else if (path.ValvesClose != null && path.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 = entity;
|
|
|
|
Items.Add(lvi);
|
|
}
|
|
|
|
public override void UpdateOne(ListViewItem lvi, object myItem)
|
|
{
|
|
Config.Entities.BenchPath entity = (Config.Entities.BenchPath)myItem;
|
|
|
|
entity.Name = lvi.Text;
|
|
|
|
double Qfrom;
|
|
bool allOk = Utils.TryParseUDouble(lvi.SubItems[(int)Column.Qfrom].Text, out Qfrom);
|
|
entity.Qfrom = Units.ConvertFrom(entity.FlowUnit, Qfrom);
|
|
|
|
double Qto;
|
|
allOk &= Utils.TryParseUDouble(lvi.SubItems[(int)Column.Qto].Text, out Qto);
|
|
entity.Qto = Units.ConvertFrom(entity.FlowUnit, Qto);
|
|
|
|
entity.Selector = lvi.SubItems[(int)Column.Selector].Text;
|
|
entity.TempMtrUp = lvi.SubItems[(int)Column.Tup].Text;
|
|
entity.TempMtrDown = lvi.SubItems[(int)Column.Tdown].Text;
|
|
entity.PressMtrUp = lvi.SubItems[(int)Column.Pup].Text;
|
|
entity.PressMtrDown = lvi.SubItems[(int)Column.Pdown].Text;
|
|
entity.PressMtrDelta = lvi.SubItems[(int)Column.Pdelta].Text;
|
|
entity.ElectricMtrUp = lvi.SubItems[(int)Column.Pup].Text;
|
|
entity.ElectricMtrDown = lvi.SubItems[(int)Column.Pdown].Text;
|
|
entity.ElectricMtrDelta = lvi.SubItems[(int)Column.Pdelta].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.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;
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
public string GetWarningsBeforeSaving(ref Dictionary<string, string> renmInfo)
|
|
{
|
|
base.OkBtnClicked(); /// = UpdateAll()
|
|
|
|
string warnings = string.Empty;
|
|
|
|
foreach (var entity in ToBeRemovedItems)
|
|
{
|
|
if (!string.IsNullOrEmpty(entity.OriName))
|
|
{
|
|
int occurances = 0;
|
|
foreach (var tst in parent.Tests) if (!string.IsNullOrEmpty(tst.BenchPath) && tst.BenchPath.Equals(entity.OriName)) occurances++;
|
|
|
|
if (occurances > 0)
|
|
{
|
|
warnings += string.Format(Strings._0_path_1_used_by_2_tests_will_be_removed_3, Strings.BenchTabPageTitle, entity.Name, occurances, Environment.NewLine);
|
|
renmInfo.Add(entity.OriName, string.Empty);
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (var entity in MyItems)
|
|
{
|
|
if (!string.IsNullOrEmpty(entity.OriName) && entity.OriName != entity.Name)
|
|
{
|
|
int occurances = 0;
|
|
foreach (var tst in parent.Tests) if (!string.IsNullOrEmpty(tst.BenchPath) && tst.BenchPath.Equals(entity.OriName)) occurances++;
|
|
|
|
if (occurances > 0)
|
|
{
|
|
warnings += string.Format(Strings._0_path_1_used_by_2_tests_will_be_renamed_to_3_4, Strings.BenchTabPageTitle, entity.OriName, occurances, entity.Name, Environment.NewLine);
|
|
renmInfo.Add(entity.OriName, entity.Name);
|
|
}
|
|
}
|
|
}
|
|
|
|
return warnings;
|
|
}
|
|
|
|
public void UpdateRelatedItems(Dictionary<string, string> renameInfo)
|
|
{
|
|
foreach (var ri in renameInfo)
|
|
{
|
|
var tests = parent.Session.QueryOver<Config.Entities.Test>()
|
|
.Where(x => x.BenchPath == ri.Key)
|
|
.List();
|
|
|
|
foreach (var t in tests)
|
|
{
|
|
t.BenchPath = ri.Value;
|
|
parent.Session.Update(t);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <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)
|
|
{
|
|
parent.Session.Delete(entity);
|
|
}
|
|
foreach (var entity in MyItems)
|
|
{
|
|
parent.Session.SaveOrUpdate(entity);
|
|
}
|
|
|
|
SaveUISettings();
|
|
}
|
|
|
|
void SaveUISettings()
|
|
{
|
|
LocalSettings ls = Program.LocalSettings;
|
|
|
|
/// Compare list view column widths with the saved ones
|
|
bool anyColumnDiffers = false;
|
|
if (Columns.Count != ls.BenchColumnCount)
|
|
{
|
|
anyColumnDiffers = true;
|
|
}
|
|
else
|
|
{
|
|
for (int i = 0; i < Columns.Count; i++)
|
|
{
|
|
if (Columns[i].Width != ls.BenchColumnWidths[i]) { anyColumnDiffers = true; break; }
|
|
}
|
|
}
|
|
|
|
if (anyColumnDiffers)
|
|
{
|
|
/// Update column widths
|
|
ls.BenchColumnWidths = new int[Columns.Count];
|
|
for (int i = 0; i < Columns.Count; i++)
|
|
{
|
|
ls.BenchColumnWidths[i] = Columns[i].Width;
|
|
}
|
|
|
|
ls.Save();
|
|
}
|
|
}
|
|
}
|
|
}
|