tbf/TBF/UI/Procedures/TestWizard/TestProfileSelection.cs

619 lines
23 KiB
C#

///
/// 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<Config.Entities.Profile> profiles;
IList<Config.Entities.FeedingPath> fPaths;
IList<Config.Entities.BenchPath> bPaths;
IList<Config.Entities.OutputPath> oPaths;
public Profile SelectedProfile;
public IList<Test> SelectedTests;
public double Qn;
public string MetroClass;
public double QpQiRatio;
public double QnAux;
public string MetroClassAux;
public double QpQiRatioAux;
IList<string> selectorVals; /// Empty string or null (which is permitted) is not part of this list
TextBox testNameTextBox;
ComboBox selectorComboBox;
public TestProfileSelection(IList<Config.Entities.Profile> profiles,
IList<Config.Entities.FeedingPath> fPaths,
IList<Config.Entities.BenchPath> bPaths,
IList<Config.Entities.OutputPath> oPaths)
{
this.profiles = profiles;
this.fPaths = fPaths;
this.bPaths = bPaths;
this.oPaths = oPaths;
InitializeComponent();
/// Find possible selector values
selectorVals = new List<string>();
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<Test>();
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.Q3RCompound:
qnLabel.Text = "Q3 main";
qnAuxLabel.Text = "Q3 aux";
break;
case ProfileType.QnClassColdWater:
case ProfileType.QnClassHotWater:
case ProfileType.Manual:
default:
qnLabel.Text = "Qn";
break;
case ProfileType.QnClassCompound:
qnLabel.Text = "Qn main";
qnAuxLabel.Text = "Qn aux";
break;
case ProfileType.QpQi:
qnLabel.Text = "qp";
break;
case ProfileType.QpQiCompound:
qnLabel.Text = "qp main";
qnAuxLabel.Text = "qp aux";
break;
}
}
void RedrawMClassCombo(Profile profile)
{
string oriText = metroClassComboBox.Text;
metroClassComboBox.Items.Clear();
metroClassAuxComboBox.Items.Clear();
bool clearTextFlag = true;
if (profile != null)
{
string[] mClasses;
switch (profile.ProfileType)
{
default:
case ProfileType.Q3R:
case ProfileType.Q3RCompound:
mClasses = 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 = new string[] { "A", "B", "C" };
break;
case ProfileType.QnClassHotWater:
mClasses = new string[] { "A", "B", "C", "D" };
break;
case ProfileType.QnClassCompound:
mClasses = new string[] { "A", "B" };
break;
case ProfileType.QpQi:
case ProfileType.QpQiCompound:
mClasses = new string[] { "1", "2", "3" };
break;
}
foreach (var mc in mClasses)
{
metroClassComboBox.Items.Add(mc);
metroClassAuxComboBox.Items.Add(mc);
if (oriText == mc) clearTextFlag = false;
}
}
if (clearTextFlag) metroClassComboBox.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.Q3RCompound || profile.ProfileType == ProfileType.QnClassCompound
|| profile.ProfileType == ProfileType.QpQiCompound)
? (protectedCheckBox.Checked ? TestProfile.ProtectedCompound : TestProfile.UserDefinedCompound)
: (protectedCheckBox.Checked ? TestProfile.Protected : TestProfile.UserDefined);
test.Qtg = compoundDetection ? -1.0
: ptest.Name.Contains(Strings._aux) ? TBF.UI.Procedures.Formulas.GetTargetQ(ptest, QnAux, QpQiRatioAux, MetroClassAux, medium)
: ptest.Name.Contains(Strings._main) ? TBF.UI.Procedures.Formulas.GetTargetQ(ptest, Qn, QpQiRatio, MetroClass, medium)
: TBF.UI.Procedures.Formulas.GetTargetQ(ptest, Qn, QpQiRatio, MetroClass, medium);
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.Q3RCompound ||
SelectedProfile.ProfileType == ProfileType.QnClassCompound ||
SelectedProfile.ProfileType == ProfileType.QpQiCompound);
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.Q3RCompound ||
SelectedProfile.ProfileType == ProfileType.QnClassCompound ||
SelectedProfile.ProfileType == ProfileType.QpQiCompound;
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();
}
}
}