/// /// Copyright (c) 2013-2021 Sensus Slovensko a.s. /// using System; using System.Collections.Generic; using System.Drawing; using System.Globalization; using System.Windows.Forms; using log4net; using Config.Entities; using Common.Forms; using TBF.Rig.GenericDevices; using TBF.Resources; using TBF.UI.Shared; namespace TBF.UI.Bench.Paths { public class PathsFeedingCtrl : BaseWithListViewEx, ITabWithListViewEx { static readonly ILog log = LogManager.GetLogger(typeof(PathsFeedingCtrl)); /// /// Fixed ListViewEx columns /// enum Column { Name, Qfrom, Qto, 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() .OrderBy(x => x.ItemNr).Asc .List(); foreach (var path in MyItems) path.OriName = path.Name; 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(string.Format("{0} [m3/h]", Strings.Q_from), (ls.FeedingColumnCount > 1) ? ls.FeedingColumnWidths[1] : 80 * parent.Dpi / Constants.Dpi100pct); Columns.Add(string.Format("{0} [m3/h]", Strings.Q_to), (ls.FeedingColumnCount > 2) ? ls.FeedingColumnWidths[2] : 80 * parent.Dpi / Constants.Dpi100pct); Columns.Add("Selector", (ls.FeedingColumnCount > 0) ? ls.FeedingColumnWidths[0] : 60 * parent.Dpi / Constants.Dpi100pct); Columns.Add(Strings.Pump, (ls.FeedingColumnCount > 3) ? ls.FeedingColumnWidths[3] : 60 * parent.Dpi / Constants.Dpi100pct); int clmn = (int)Column.FixedColumnsCount; foreach (var rv in parent.FeedingRegulValves) { Columns.Add(rv.Cfg.Name, (ls.FeedingColumnCount > clmn) ? ls.FeedingColumnWidths[clmn] : 50 * parent.Dpi / Constants.Dpi100pct); clmn++; } regvCount = clmn - (int)Column.FixedColumnsCount; foreach (var vlv in parent.Valves) { if (vlv.Category == TBF.Rig.ValveCategory.Feeding) { Columns.Add(vlv.Cfg.Name, (ls.FeedingColumnCount > clmn) ? ls.FeedingColumnWidths[clmn] : 50 * parent.Dpi / Constants.Dpi100pct); clmn++; } } valvesCount = clmn - (int)Column.FixedColumnsCount - regvCount; /// 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 + 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.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 (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 + 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 (!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; } else if (item == e.Item) { value = item.SubItems[columnNr].Text; backColor = item.SubItems[columnNr].BackColor; } } } } void listViewEx_SubItemEndEditing(object sender, SubItemEndEditingEventArgs e) { if (e.SubItem >= fixedColumnsCount + regvCount) { e.Item.SubItems[e.SubItem].BackColor = (e.DisplayText == Strings.open) ? Color.LightGray : Color.White; } } 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(path.Qfrom.ToString()); lvi.SubItems.Add(path.Qto.ToString()); lvi.SubItems.Add(path.Selector); lvi.SubItems.Add(path.Pump != null ? path.Pump.Cfg.Name : "---"); string[] rvPosStr = (entity.RegulValvesPct != null) ? entity.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.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; 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.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 regulValvesPct = string.Empty; for (int i = fixedColumnsCount; i < fixedColumnsCount + regvCount; i++) { if (regulValvesPct.Length > 0) regulValvesPct += ";"; float fdummy; if (lvi.SubItems[i].Text.Equals("---") || (Utils.TryParseUFloat(lvi.SubItems[i].Text, out fdummy) && (fdummy >= 0) && (fdummy <= 100.0f))) { regulValvesPct += lvi.SubItems[i].Text; } } entity.RegulValvesPct = regulValvesPct; /// /// 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 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 renameInfo) { foreach (var ri in renameInfo) { var tests = parent.Session.QueryOver() .Where(x => x.FeedingPath == ri.Key) .List(); foreach (var t in tests) { t.FeedingPath = ri.Value; parent.Session.Update(t); } } } /// /// Update the entities and save them to the database /// 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(); } } } }