Files
tbf/TBF/UI/Bench/Paths/PathsFeedingCtrl.cs

507 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 Config.Entities;
using TBF.Rig.GenericDevices;
using TBF.Resources;
using TBF.UI.Shared;
namespace TBF.UI.Bench.Paths
{
public class PathsFeedingCtrl : BaseWithListViewEx<Config.Entities.FeedingPath>, ITabWithListViewEx
{
static readonly ILog log = LogManager.GetLogger(typeof(PathsFeedingCtrl));
/// <summary>
/// Fixed ListViewEx columns
/// </summary>
enum Column
{
Name,
Qfrom,
Qto,
FlowUnit,
Selector,
Pump,
FixedColumnsCount,
}
readonly int fixedColumnsCount = (int)Column.FixedColumnsCount;
int regvCount;
int valvesCount;
PathsDlg parent;
Control parentControl;
Control[] editors;
public PathsFeedingCtrl()
: base()
{
FixedRowsCount = 1;
}
public void Initialize(PathsDlg parent, Control parentControl)
{
this.parent = parent;
this.parentControl = parentControl;
/// Load the paths
MyItems = parent.Session.QueryOver<FeedingPath>()
.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.FeedingColumnCount > 0) ? ls.FeedingColumnWidths[0] : 60 * parent.Dpi / Constants.Dpi100pct);
Columns.Add(Strings.Q_from, (ls.FeedingColumnCount > 1) ? ls.FeedingColumnWidths[1] : 80 * parent.Dpi / Constants.Dpi100pct);
Columns.Add(Strings.Q_to, (ls.FeedingColumnCount > 2) ? ls.FeedingColumnWidths[2] : 80 * parent.Dpi / Constants.Dpi100pct);
Columns.Add("Unit", (ls.FeedingColumnCount > 3) ? ls.FeedingColumnWidths[3] : 50 * parent.Dpi / Constants.Dpi100pct);
Columns.Add("Selector", (ls.FeedingColumnCount > 4) ? ls.FeedingColumnWidths[4] : 60 * parent.Dpi / Constants.Dpi100pct);
Columns.Add(Strings.Pump, (ls.FeedingColumnCount > 5) ? ls.FeedingColumnWidths[5] : 60 * parent.Dpi / Constants.Dpi100pct);
int clmn = (int)Column.FixedColumnsCount;
foreach (var rv in parent.FeedingRegValves)
{
Columns.Add(string.Format("{0} [%]", rv.Name), (ls.FeedingColumnCount > clmn) ? ls.FeedingColumnWidths[clmn] : 50 * parent.Dpi / Constants.Dpi100pct);
clmn++;
}
regvCount = parent.FeedingRegValves.Count;
foreach (var vlv in parent.Valves)
{
if (vlv.Category == TBF.Rig.ValveCategory.Feeding)
{
Columns.Add(vlv.Name, (ls.FeedingColumnCount > clmn) ? ls.FeedingColumnWidths[clmn] : 50 * parent.Dpi / Constants.Dpi100pct);
clmn++;
}
}
valvesCount = clmn - (int)Column.FixedColumnsCount - regvCount;
/// Flow unit
var flowUnitBox = new ComboBox();
for (Unit u = 0; u < Unit.Count; u++) if (Units.IsFlow(u)) flowUnitBox.Items.Add(u.ToDescription());
/// Pump
ComboBox pumpCBox = new ComboBox();
pumpCBox.Items.Add("---");
foreach (var cmpnt in parent.TbfComponents)
{
if (cmpnt is IPump) pumpCBox.Items.Add(cmpnt.Cfg.Name);
}
editors = new Control[fixedColumnsCount + regvCount + valvesCount];
///
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.Pump] = pumpCBox;
for (int i = 0; i < regvCount; i++)
{
ComboBox cb = new ComboBox();
cb.Items.Add("---");
cb.Items.Add("0");
cb.Items.Add("25");
cb.Items.Add("50");
cb.Items.Add("75");
cb.Items.Add("100");
editors[fixedColumnsCount + i] = cb;
}
/// Prepare valve combo-boxes
for (int i = 0; i < valvesCount; i++)
{
ComboBox cb = new ComboBox();
cb.Items.Add("---");
cb.Items.Add(Strings.open);
cb.Items.Add(Strings.close);
editors[fixedColumnsCount + regvCount + 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 IHasItemNr) &&
((e.Item.Tag as 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.FeedingPath;
double ddummy;
if (e.SubItem >= fixedColumnsCount + regvCount)
{
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.FeedingPath;
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.FeedingPath entity = (Config.Entities.FeedingPath)myItem;
Rig.FeedingPath path = new Rig.FeedingPath(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.Pump != null ? path.Pump.Cfg.Name : "---");
string[] rvPosStr = (entity.RegVPositions != null) ? entity.RegVPositions.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.Category == TBF.Rig.ValveCategory.Feeding)
{
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.FeedingPath entity = (Config.Entities.FeedingPath)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;
ComboBox pumpCB = editors[(int)Column.Pump] as ComboBox;
if (pumpCB.Items.Contains(lvi.SubItems[(int)Column.Pump].Text))
{
entity.Pump = lvi.SubItems[(int)Column.Pump].Text.Equals("---") ? string.Empty : lvi.SubItems[(int)Column.Pump].Text;
}
///
/// Regulation valves
///
string regulVPositions = string.Empty;
for (int i = fixedColumnsCount; i < fixedColumnsCount + regvCount; i++)
{
if (regulVPositions.Length > 0) regulVPositions += ";";
float fdummy;
if (lvi.SubItems[i].Text.Equals("---") ||
(Utils.TryParseUFloat(lvi.SubItems[i].Text, out fdummy) && (fdummy >= 0) && (fdummy <= 100.0f)))
{
regulVPositions += lvi.SubItems[i].Text;
}
}
entity.RegVPositions = regulVPositions;
///
/// Valves
///
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 == TBF.Rig.ValveCategory.Feeding)
{
string text = lvi.SubItems[fixedColumnsCount + regvCount + 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.FeedingPath entity;
if (MyItems.Count < FixedRowsCount)
{
entity = new Config.Entities.FeedingPath(Strings._none_, MyItems.Count);
}
else
{
/// Find an unused test name
string newName;
for (int nameId = 1; true; nameId++)
{
newName = Strings.New_feeding_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.FeedingPath(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.FeedingPath) && tst.FeedingPath.Equals(entity.OriName)) occurances++;
if (occurances > 0)
{
warnings += string.Format(Strings._0_path_1_used_by_2_tests_will_be_removed_3, Strings.FeedingTabPageTitle, 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.FeedingPath) && tst.FeedingPath.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.FeedingTabPageTitle, 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.FeedingPath == ri.Key)
.List();
foreach (var t in tests)
{
t.FeedingPath = 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.FeedingColumnCount)
{
anyColumnDiffers = true;
}
else
{
for (int i = 0; i < Columns.Count; i++)
{
if (Columns[i].Width != ls.FeedingColumnWidths[i]) { anyColumnDiffers = true; break; }
}
}
if (anyColumnDiffers)
{
/// Update column widths
ls.FeedingColumnWidths = new int[Columns.Count];
for (int i = 0; i < Columns.Count; i++)
{
ls.FeedingColumnWidths[i] = Columns[i].Width;
}
ls.Save();
}
}
}
}