/// /// Copyright (c) 2019-2023 Sensus Slovensko a.s. /// using System; using System.Collections.Generic; using System.Windows.Forms; using log4net; using Common; using Common.Forms; using Config.Entities; using TBF.Resources; using TBF.Rig; namespace TBF.UI.Procedures.TestWizard { public partial class TestProfileSelection : Form { static readonly ILog log = LogManager.GetLogger(typeof(TestProfileSelection)); enum Column { Name, Part, Qtg, Qfrom, Qto, FromToUnit, Selector, ErrLimLo, ErrLimHi, Method, } const string EmptySelector = "---"; IList profiles; IList fPaths; IList bPaths; IList oPaths; public Profile SelectedProfile; public IList SelectedTests; public double Qn; public string MetroClass; public double QpQiRatio; public double QnAux; public string MetroClassAux; public double QpQiRatioAux; IList selectorVals; /// Empty string or null (which is permitted) is not part of this list TextBox testNameTextBox; ComboBox selectorComboBox; public TestProfileSelection(IList profiles, IList fPaths, IList bPaths, IList oPaths) { this.profiles = profiles; this.fPaths = fPaths; this.bPaths = bPaths; this.oPaths = oPaths; InitializeComponent(); /// Find possible selector values selectorVals = new List(); foreach (var p in fPaths) if (!string.IsNullOrEmpty(p.Selector) && !selectorVals.Contains(p.Selector)) selectorVals.Add(p.Selector); foreach (var p in bPaths) if (!string.IsNullOrEmpty(p.Selector) && !selectorVals.Contains(p.Selector)) selectorVals.Add(p.Selector); foreach (var p in oPaths) if (!string.IsNullOrEmpty(p.Selector) && !selectorVals.Contains(p.Selector)) selectorVals.Add(p.Selector); testNameTextBox = new TextBox(); testNameTextBox.Visible = false; this.Controls.Add(testNameTextBox); selectorComboBox = new ComboBox(); selectorComboBox.Visible = false; selectorComboBox.Items.Add(EmptySelector); foreach (var s in selectorVals) selectorComboBox.Items.Add(s); this.Controls.Add(selectorComboBox); /// ListViewEx-es configuration listViewEx.Columns.Add(Strings.Name, 70); listViewEx.Columns.Add(Strings.Part, 40); listViewEx.Columns.Add(string.Format("{0} [m3/h]", Strings.Q_tg), 80); listViewEx.Columns.Add(string.Format("{0}", Strings.Q_from), 80); listViewEx.Columns.Add(string.Format("{0}", Strings.Q_to), 70); listViewEx.Columns.Add(string.Format("Unit", Strings.Q_to), 40); listViewEx.Columns.Add("Selector"); listViewEx.Columns.Add(Strings.Err_limit_neg_pct_chdr, 80); listViewEx.Columns.Add(Strings.Err_limit_pos_pct_chdr, 80); listViewEx.Columns.Add(Strings.Method_chdr, 150); listViewEx.SubItemClicked += new SubItemEventHandler(listViewEx_SubItemClicked); listViewEx.SubItemEndEditing += new SubItemEndEditingEventHandler(listViewEx_SubItemEndEditing); if (profiles != null) { foreach (var p in profiles) profileComboBox.Items.Add(p.Name); } foreach (var s in new string[] { "10", "25", "50", "100", "250" }) qpQiRatioComboBox.Items.Add(s); SelectedTests = new List(); nextButton.Enabled = false; } private void TestProfileSelection_Load(object sender, EventArgs e) { Localize(); #if LANG_PL protectedCheckBox.Checked = true; #endif } void Localize() { Text = Strings.Test_profiles_selection; nextButton.Text = Strings.NextBtnText; cancelButton.Text = Strings.CancelBtnText; protectedCheckBox.Text = Strings.Protected_procedure; } void RedrawQn(Profile profile) { switch (profile.ProfileType) { case ProfileType.Q3R: qnLabel.Text = "Q3"; break; case ProfileType.QnClassColdWater: case ProfileType.QnClassHotWater: case ProfileType.Manual: default: qnLabel.Text = "Qn"; break; case ProfileType.CompoundQ3RQ3R: qnLabel.Text = "Q3 main"; qnAuxLabel.Text = "Q3 aux"; break; case ProfileType.CompoundQnClassQnClass: qnLabel.Text = "Qn main"; qnAuxLabel.Text = "Qn aux"; break; case ProfileType.CompoundQ3RQnClass: qnLabel.Text = "Q3 main"; qnAuxLabel.Text = "Qn aux"; break; case ProfileType.CompoundQnClassQ3R: qnLabel.Text = "Qn main"; qnAuxLabel.Text = "Q3 aux"; break; case ProfileType.QpQi: qnLabel.Text = "qp"; break; case ProfileType.CompoundQpQiQpQi: qnLabel.Text = "qp main"; qnAuxLabel.Text = "qp aux"; break; } } void RedrawMClassCombo(Profile profile) { string oriText = metroClassComboBox.Text; string oriTextAux = metroClassAuxComboBox.Text; metroClassComboBox.Items.Clear(); metroClassAuxComboBox.Items.Clear(); bool clearTextFlag = true; bool clearAuxTextFlag = true; if (profile != null) { string[] mClasses; string[] mClassesAux; switch (profile.ProfileType) { default: case ProfileType.Q3R: case ProfileType.CompoundQ3RQ3R: mClasses = mClassesAux = new string[] { "R40", "R50", "R63", "R80", "R100", "R125", "R160", "R200", "R250", "R315", "R400", "R500", "R630", "R800", "R1000", "R1250", "R1600", "R2000", "R2500" }; break; case ProfileType.QnClassColdWater: mClasses = mClassesAux = new string[] { "A", "B", "C" }; break; case ProfileType.QnClassHotWater: mClasses = mClassesAux = new string[] { "A", "B", "C", "D" }; break; case ProfileType.CompoundQnClassQnClass: mClasses = mClassesAux = new string[] { "A", "B" }; break; case ProfileType.CompoundQ3RQnClass: mClasses = new string[] { "R40", "R50", "R63", "R80", "R100", "R125", "R160", "R200", "R250", "R315", "R400", "R500", "R630", "R800", "R1000", "R1250", "R1600", "R2000", "R2500" }; mClassesAux = new string[] { "A", "B" }; break; case ProfileType.CompoundQnClassQ3R: mClasses = new string[] { "A", "B" }; mClassesAux = new string[] { "R40", "R50", "R63", "R80", "R100", "R125", "R160", "R200", "R250", "R315", "R400", "R500", "R630", "R800", "R1000", "R1250", "R1600", "R2000", "R2500" }; break; case ProfileType.QpQi: case ProfileType.CompoundQpQiQpQi: mClasses = mClassesAux = new string[] { "1", "2", "3" }; break; } foreach (var mc in mClasses) { metroClassComboBox.Items.Add(mc); if (mc == oriText) clearTextFlag = false; } foreach (var mc in mClassesAux) { metroClassAuxComboBox.Items.Add(mc); if (mc == oriTextAux) clearAuxTextFlag = false; } } if (clearTextFlag) metroClassComboBox.Text = string.Empty; if (clearAuxTextFlag) metroClassAuxComboBox.Text = string.Empty; } void RedrawPTests(Profile profile) { SelectedTests.Clear(); listViewEx.Items.Clear(); if (profile == null) return; try { int itemNr = 0; foreach (var ptest in profile.Tests) { var methodCmpnt = TbfComponents.FindComponent(ptest.Method); bool compoundDetection = methodCmpnt != null && methodCmpnt is Rig.TestMethods.CombinedWithDetection.TestMethod; Medium medium = (profile.ProfileType == ProfileType.QnClassHotWater) ? Medium.HotWater : Medium.ColdWater; Test test = new Test(ptest.Name, itemNr++, null); /// Procedure reference to be added later test.Part = ptest.Part; test.Profile = (profile.ProfileType == ProfileType.QpQi) ? (protectedCheckBox.Checked ? TestProfile.ProtectedHeatMeter : TestProfile.UserDefinedHeatMeter) : (profile.ProfileType == ProfileType.CompoundQ3RQ3R || profile.ProfileType == ProfileType.CompoundQnClassQnClass || profile.ProfileType == ProfileType.CompoundQ3RQnClass || profile.ProfileType == ProfileType.CompoundQnClassQ3R || profile.ProfileType == ProfileType.CompoundQpQiQpQi) ? (protectedCheckBox.Checked ? TestProfile.ProtectedCompound : TestProfile.UserDefinedCompound) : (protectedCheckBox.Checked ? TestProfile.Protected : TestProfile.UserDefined); test.Qtg = Formulas.GetTargetQ(ptest, medium, Qn, QpQiRatio, MetroClass, QnAux, QpQiRatioAux, MetroClassAux); test.IsFromToInPct = test.Qtg <= 0 || Rig.Sequences.ProcessData.IsFromToInPct; test.Qfrom = test.IsFromToInPct ? 100 * ptest.CoefQfrom : test.Qtg * ptest.CoefQfrom; test.Qto = test.IsFromToInPct ? 100 * ptest.CoefQto : test.Qtg * ptest.CoefQto; test.TempLimLo = Convert.ToSingle(ptest.TempLimLo); test.TempLimHi = Convert.ToSingle(ptest.TempLimHi); test.Publish = ptest.Publish; test.DoEvaluate = ptest.Evaluate; test.Method = (ptest.Method != null) ? ptest.Method : string.Empty; if (profile.ProfileType != ProfileType.QpQi) { test.ErrLimLo = Convert.ToSingle(ptest.ErrLimLo); test.ErrLimHi = Convert.ToSingle(ptest.ErrLimHi); } else { int metroClass; if (int.TryParse(MetroClass, out metroClass)) { test.ErrLimLo = Convert.ToSingle(metroClass); } test.ErrLimHi = -Convert.ToSingle(Qn); } ListViewItem lvi; if (profile.ProfileType != ProfileType.QpQi) { lvi = new ListViewItem(new string[] { ptest.Name, (ptest.Part > 0) ? ptest.Part.ToString() : "-", test.Qtg < 0 ? Strings.determine : test.Qtg.ToString(), test.Qfrom.ToString(), test.Qto.ToString(), test.IsFromToInPct ? "%" : "m3/h", EmptySelector, ptest.ErrLimLo.ToString(), ptest.ErrLimHi.ToString(), ptest.Method != null ? ptest.Method : string.Empty }); } else { lvi = new ListViewItem(new string[] { ptest.Name, (ptest.Part > 0) ? ptest.Part.ToString() : "-", test.Qtg < 0 ? Strings.determine : test.Qtg.ToString(), test.Qfrom.ToString(), test.Qto.ToString(), test.IsFromToInPct ? "%" : "m3/h", EmptySelector, Strings.calculated, Strings.calculated, ptest.Method != null ? ptest.Method : string.Empty }); } lvi.Tag = test; listViewEx.Items.Add(lvi); SelectedTests.Add(test); } } catch (Exception) { MessageBox.Show("Something went wrong"); } } private void profileComboBox_SelectedIndexChanged(object sender, EventArgs e) { Profile newProfile = null; foreach (var p in profiles) { if (profileComboBox.Text == p.Name) { newProfile = p; break; } } if (SelectedProfile != newProfile) { SelectedProfile = newProfile; qpQiRatioGroupBox.Visible = (SelectedProfile.ProfileType == ProfileType.QpQi); metroClassGroupBox.Visible = (SelectedProfile.ProfileType != ProfileType.Manual); qnAuxLabel.Visible = qnAuxTextBox.Visible = metroClassAuxComboBox.Visible = (SelectedProfile.ProfileType == ProfileType.CompoundQ3RQ3R || SelectedProfile.ProfileType == ProfileType.CompoundQnClassQnClass || SelectedProfile.ProfileType == ProfileType.CompoundQ3RQnClass || SelectedProfile.ProfileType == ProfileType.CompoundQnClassQ3R || SelectedProfile.ProfileType == ProfileType.CompoundQpQiQpQi); profileTypeLabel.Text = (newProfile != null) ? newProfile.ProfileType.ToString() : string.Empty; profileDescriptionLabel.Text = (newProfile != null) ? newProfile.Description : string.Empty; RedrawQn(newProfile); RedrawMClassCombo(newProfile); if (IsFormValid()) { RedrawPTests(newProfile); nextButton.Enabled = true; } else { RedrawPTests(null); nextButton.Enabled = false; } } } private void qnTextBox_TextChanged(object sender, EventArgs e) { if (IsFormValid()) { RedrawPTests(SelectedProfile); nextButton.Enabled = true; } else { RedrawPTests(null); nextButton.Enabled = false; } } private void metroClassComboBox_TextChanged(object sender, EventArgs e) { metroClassComboBox_SelectedIndexChanged(sender, e); } private void metroClassComboBox_SelectedIndexChanged(object sender, EventArgs e) { if (IsFormValid()) { RedrawPTests(SelectedProfile); nextButton.Enabled = true; } else { RedrawPTests(null); nextButton.Enabled = false; } } private void qpQiRatioComboBox_SelectedIndexChanged(object sender, EventArgs e) { if (IsFormValid()) { RedrawPTests(SelectedProfile); nextButton.Enabled = true; } else { RedrawPTests(null); nextButton.Enabled = false; } } private void qnAuxTextBox_TextChanged(object sender, EventArgs e) { if (IsFormValid()) { RedrawPTests(SelectedProfile); nextButton.Enabled = true; } else { RedrawPTests(null); nextButton.Enabled = false; } } private void metroClassAuxComboBox_TextChanged(object sender, EventArgs e) { metroClassAuxComboBox_SelectedIndexChanged(sender, e); } private void metroClassAuxComboBox_SelectedIndexChanged(object sender, EventArgs e) { if (IsFormValid()) { RedrawPTests(SelectedProfile); nextButton.Enabled = true; } else { RedrawPTests(null); nextButton.Enabled = false; } } bool IsProfileValid() { return profileComboBox.Items.Contains(profileComboBox.Text); /// CB text must be one of the items } bool IsQnValid() { if (!string.IsNullOrEmpty(qnTextBox.Text) && Utils.TryParseUDouble(qnTextBox.Text, out Qn) && (Qn > 0)) { return true; } else { Qn = 0; return false; } } bool IsQnAuxValid() { if (!string.IsNullOrEmpty(qnAuxTextBox.Text) && Utils.TryParseUDouble(qnAuxTextBox.Text, out QnAux) && (QnAux > 0)) { return true; } else { QnAux = 0; return false; } } bool IsMetroClassValid() { if (SelectedProfile.ProfileType == ProfileType.Manual) { MetroClass = string.Empty; return true; } if (!string.IsNullOrEmpty(metroClassComboBox.Text) && (metroClassComboBox.Items.Count == 0 || metroClassComboBox.Items.Contains(metroClassComboBox.Text))) { MetroClass = metroClassComboBox.Text; return true; } MetroClass = string.Empty; return false; } bool IsMetroClassAuxValid() { if (SelectedProfile.ProfileType == ProfileType.Manual) { MetroClassAux = string.Empty; return true; } if (!string.IsNullOrEmpty(metroClassAuxComboBox.Text) && (metroClassAuxComboBox.Items.Count == 0 || metroClassAuxComboBox.Items.Contains(metroClassAuxComboBox.Text))) { MetroClassAux = metroClassAuxComboBox.Text; return true; } MetroClassAux = string.Empty; return false; } bool IsQpQiRatioValid() { if (SelectedProfile.ProfileType != ProfileType.QpQi) { QpQiRatio = 1.0; return true; } if (!string.IsNullOrEmpty(qpQiRatioComboBox.Text) && qpQiRatioComboBox.Items.Contains(qpQiRatioComboBox.Text)) { QpQiRatio = double.Parse(qpQiRatioComboBox.Text); return true; } QpQiRatio = 1.0; return false; } bool IsFormValid() { bool isCompound = SelectedProfile.ProfileType == ProfileType.CompoundQ3RQ3R || SelectedProfile.ProfileType == ProfileType.CompoundQnClassQnClass || SelectedProfile.ProfileType == ProfileType.CompoundQ3RQnClass || SelectedProfile.ProfileType == ProfileType.CompoundQnClassQ3R || SelectedProfile.ProfileType == ProfileType.CompoundQpQiQpQi; return IsProfileValid() && IsQnValid() && IsMetroClassValid() && IsQpQiRatioValid() && (!isCompound || (IsQnAuxValid() && IsMetroClassAuxValid())); } private void listViewEx_SubItemClicked(object sender, SubItemEventArgs e) { if (e.SubItem == (int)Column.Name) { listViewEx.StartEditing(testNameTextBox, e.Item, e.SubItem); } else if (e.SubItem == (int)Column.Selector) { listViewEx.StartEditing(selectorComboBox, e.Item, e.SubItem); } } private void listViewEx_SubItemEndEditing(object sender, SubItemEndEditingEventArgs e) { Test test = e.Item.Tag as Test; if (e.SubItem == (int)Column.Name) { foreach (var item in listViewEx.Items) { var lvi = item as ListViewItem; if (lvi != null && lvi != e.Item && (lvi.Tag is Test) && (lvi.Tag as Test).Name == e.DisplayText) { /// Test name was not unique, revert to the original one e.DisplayText = e.Item.SubItems[e.SubItem].Text; e.Cancel = true; return; } } /// OK test.Name = e.DisplayText; } else if (e.SubItem == (int)Column.Selector) { if (!selectorComboBox.Items.Contains(e.DisplayText)) { /// Test name was not unique, revert to the original one e.DisplayText = e.Item.SubItems[e.SubItem].Text; e.Cancel = true; return; } } } private void nextButton_Click(object sender, EventArgs e) { if (IsFormValid()) { foreach (var item in listViewEx.Items) { var lvi = item as ListViewItem; string selector = (lvi.SubItems[(int)Column.Selector].Text == EmptySelector) ? string.Empty : lvi.SubItems[(int)Column.Selector].Text; Test test = lvi.Tag as Test; if (test != null) { /// Depending on the chosen Q select the paths double Qaver = (test.QfromM3ph() + test.QtoM3ph()) / 2.0; foreach (var path in fPaths) { if ((path.Qfrom <= Qaver) && (Qaver <= path.Qto) && (string.IsNullOrEmpty(selector) || string.IsNullOrEmpty(path.Selector) || path.Selector == selector)) { test.FeedingPath = path.Name; break; } } foreach (var path in bPaths) { if ((path.Qfrom <= Qaver) && (Qaver <= path.Qto) && (string.IsNullOrEmpty(selector) || string.IsNullOrEmpty(path.Selector) || path.Selector == selector)) { test.BenchPath = path.Name; break; } } foreach (var path in oPaths) { if ((path.Qfrom <= Qaver) && (Qaver <= path.Qto) && (string.IsNullOrEmpty(selector) || string.IsNullOrEmpty(path.Selector) || path.Selector == selector)) { test.OutputPath = path.Name; break; } } } } DialogResult = DialogResult.OK; Close(); } else { MessageBox.Show("Something's wrong"); } } private void cancelButton_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; Close(); } } }