311 lines
10 KiB
C#
311 lines
10 KiB
C#
///
|
|
/// Copyright (c) 2013-2017 Senus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Windows.Forms;
|
|
using Common;
|
|
using Config;
|
|
using Config.Entities;
|
|
using TBF.Resources;
|
|
|
|
namespace TBF.UI.Procedures.TestWizard
|
|
{
|
|
public partial class FlowSelection : Form
|
|
{
|
|
enum QSpec
|
|
{
|
|
Qfrom_Qto,
|
|
Q_dQ,
|
|
Q_dPct,
|
|
}
|
|
|
|
const string EmptySelector = "---";
|
|
|
|
Test test;
|
|
IList<MetersPath> sensorPaths;
|
|
ICollection<string> selectorValues; /// Empty string (permitted) is not part of this collection
|
|
QSpec qSpec;
|
|
Unit flowUnit;
|
|
|
|
public string Selector { get { return (selectorComboBox.Text == EmptySelector) ? string.Empty : selectorComboBox.Text; } }
|
|
|
|
public FlowSelection()
|
|
{
|
|
InitializeComponent();
|
|
|
|
qSpec = QSpec.Qfrom_Qto;
|
|
Text = Strings.Flow_selection;
|
|
qFromLabel.Text = Strings.Q_from;
|
|
qToLabel.Text = Strings.Q_to;
|
|
sensorsLabel.Text = Strings.Sensors;
|
|
|
|
for (Unit u = 0; u < Unit.Count; u++)
|
|
{
|
|
if (Units.IsFlow(u))
|
|
{
|
|
var str = u.ToDescription();
|
|
unitComboBox.Items.Add(str);
|
|
unit2ComboBox.Items.Add(str);
|
|
unit3ComboBox.Items.Add(str);
|
|
}
|
|
}
|
|
}
|
|
|
|
public FlowSelection(bool first, bool last, Test test,
|
|
IList<MetersPath> sensorPaths,
|
|
ICollection<string> selectorValues)
|
|
: this()
|
|
{
|
|
this.test = test;
|
|
this.sensorPaths = sensorPaths;
|
|
this.selectorValues = selectorValues;
|
|
|
|
flowUnit = test.FlowUnit;
|
|
unitComboBox.Text = unit2ComboBox.Text = unit3ComboBox.Text = flowUnit.ToDescription();
|
|
|
|
if (first) backButton.Visible = false;
|
|
|
|
backButton.Text = Strings.BackBtnText;
|
|
nextButton.Text = last ? Strings.FinishBtnText : Strings.NextBtnText;
|
|
cancelButton.Text = Strings.CancelBtnText;
|
|
}
|
|
|
|
private void FlowSelection_Load(object sender, EventArgs e)
|
|
{
|
|
if (test == null) return;
|
|
|
|
foreach (var path in sensorPaths) sensorsComboBox.Items.Add(path.Name);
|
|
|
|
selectorComboBox.Items.Add(EmptySelector);
|
|
foreach (var selector in selectorValues) selectorComboBox.Items.Add(selector);
|
|
|
|
qFromTextBox.Text = Units.ConvertTo(flowUnit, test.Qfrom).ToString();
|
|
qToTextBox.Text = Units.ConvertTo(flowUnit, test.Qto).ToString();
|
|
nextButton.Enabled = IsFormValid();
|
|
}
|
|
|
|
void UpdateTest()
|
|
{
|
|
double qFrom, qTo;
|
|
switch (qSpec)
|
|
{
|
|
default:
|
|
case QSpec.Qfrom_Qto:
|
|
qFrom = Units.ConvertFrom(flowUnit, Utils.ParseUDouble(qFromTextBox.Text));
|
|
qTo = Units.ConvertFrom(flowUnit, Utils.ParseUDouble(qToTextBox.Text));
|
|
break;
|
|
|
|
case QSpec.Q_dQ:
|
|
var Q = Units.ConvertFrom(flowUnit, Utils.ParseUDouble(q2TextBox.Text));
|
|
var dQ = Units.ConvertFrom(flowUnit, Utils.ParseUDouble(dQTextBox.Text));
|
|
qFrom = (Q - dQ);
|
|
qTo = (Q + dQ);
|
|
break;
|
|
|
|
case QSpec.Q_dPct:
|
|
var Q2 = Units.ConvertFrom(flowUnit, Utils.ParseUDouble(q3TextBox.Text));
|
|
var dQpct = Utils.ParseUDouble(dQpctTextBox.Text);
|
|
qFrom = (float)(Q2 * (1 - dQpct / 100.0));
|
|
qTo = (float)(Q2 * (1 + dQpct / 100.0));
|
|
break;
|
|
}
|
|
|
|
test.Qfrom = (float)qFrom;
|
|
test.Qto = (float)qTo;
|
|
test.FlowUnit = flowUnit;
|
|
|
|
if (sensorsComboBox.Items.Contains(sensorsComboBox.Text))
|
|
{
|
|
test.MetersPath = sensorsComboBox.Text;
|
|
}
|
|
}
|
|
|
|
bool IsFormValid()
|
|
{
|
|
float dummy;
|
|
switch (qSpec)
|
|
{
|
|
default:
|
|
case QSpec.Qfrom_Qto:
|
|
return Utils.TryParseUFloat(qFromTextBox.Text, out dummy) &&
|
|
Utils.TryParseUFloat(qToTextBox.Text, out dummy) &&
|
|
selectorComboBox.Items.Contains(selectorComboBox.Text) &&
|
|
sensorsComboBox.Items.Contains(sensorsComboBox.Text);
|
|
|
|
case QSpec.Q_dQ:
|
|
return Utils.TryParseUFloat(q2TextBox.Text, out dummy) &&
|
|
Utils.TryParseUFloat(dQTextBox.Text, out dummy) &&
|
|
selectorComboBox.Items.Contains(selectorComboBox.Text) &&
|
|
sensorsComboBox.Items.Contains(sensorsComboBox.Text);
|
|
|
|
case QSpec.Q_dPct:
|
|
return Utils.TryParseUFloat(q3TextBox.Text, out dummy) &&
|
|
Utils.TryParseUFloat(dQpctTextBox.Text, out dummy) &&
|
|
selectorComboBox.Items.Contains(selectorComboBox.Text) &&
|
|
sensorsComboBox.Items.Contains(sensorsComboBox.Text);
|
|
}
|
|
}
|
|
|
|
private void backButton_Click(object sender, EventArgs e)
|
|
{
|
|
DialogResult = DialogResult.Retry;
|
|
Close();
|
|
}
|
|
|
|
private void nextButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (test != null && IsFormValid())
|
|
{
|
|
UpdateTest();
|
|
DialogResult = DialogResult.OK;
|
|
Close();
|
|
}
|
|
else
|
|
{
|
|
DialogResult = DialogResult.None;
|
|
}
|
|
}
|
|
|
|
private void cancelButton_Click(object sender, EventArgs e)
|
|
{
|
|
DialogResult = DialogResult.Cancel;
|
|
Close();
|
|
}
|
|
|
|
private void anyTextBox_TextChanged(object sender, EventArgs e)
|
|
{
|
|
nextButton.Enabled = IsFormValid();
|
|
}
|
|
|
|
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
double Q1,Q2;
|
|
|
|
switch ((QSpec)tabControl1.SelectedIndex)
|
|
{
|
|
default:
|
|
case QSpec.Qfrom_Qto:
|
|
if (qSpec == QSpec.Q_dQ &&
|
|
Utils.TryParseUDouble(q2TextBox.Text, out Q1) &&
|
|
Utils.TryParseUDouble(dQTextBox.Text, out Q2))
|
|
{
|
|
qFromTextBox.Text = (Q1 - Q2).ToString();
|
|
qToTextBox.Text = (Q1 + Q2).ToString();
|
|
}
|
|
else if (qSpec == QSpec.Q_dPct &&
|
|
Utils.TryParseUDouble(q3TextBox.Text, out Q1) &&
|
|
Utils.TryParseUDouble(dQpctTextBox.Text, out Q2))
|
|
{
|
|
qFromTextBox.Text = (Q1 * (1 - Q2 / 100.0)).ToString();
|
|
qToTextBox.Text = (Q1 * (1 + Q2 / 100.0)).ToString();
|
|
}
|
|
|
|
qSpec = QSpec.Qfrom_Qto;
|
|
break;
|
|
|
|
case QSpec.Q_dQ:
|
|
if (qSpec == QSpec.Qfrom_Qto &&
|
|
Utils.TryParseUDouble(qFromTextBox.Text, out Q1) &&
|
|
Utils.TryParseUDouble(qToTextBox.Text, out Q2))
|
|
{
|
|
q2TextBox.Text = ((Q1 + Q2) / 2.0).ToString();
|
|
dQTextBox.Text = ((Q2 - Q1) / 2.0).ToString();
|
|
}
|
|
else if (qSpec == QSpec.Q_dPct &&
|
|
Utils.TryParseUDouble(q3TextBox.Text, out Q1) &&
|
|
Utils.TryParseUDouble(dQpctTextBox.Text, out Q2))
|
|
{
|
|
q2TextBox.Text = q3TextBox.Text;
|
|
dQTextBox.Text = (Q1 * Q2 / 100.0).ToString();
|
|
}
|
|
|
|
qSpec = QSpec.Q_dQ;
|
|
break;
|
|
|
|
case QSpec.Q_dPct:
|
|
if (qSpec == QSpec.Qfrom_Qto &&
|
|
Utils.TryParseUDouble(qFromTextBox.Text, out Q1) &&
|
|
Utils.TryParseUDouble(qToTextBox.Text, out Q2))
|
|
{
|
|
q3TextBox.Text = ((Q1 + Q2) / 2.0).ToString();
|
|
dQpctTextBox.Text = (Q1 + Q2 != 0) ? (100.0 * (Q2 - Q1) / (Q1 + Q2)).ToString() : string.Empty;
|
|
}
|
|
else if (qSpec == QSpec.Q_dQ &&
|
|
Utils.TryParseUDouble(q2TextBox.Text, out Q1) &&
|
|
Utils.TryParseUDouble(dQTextBox.Text, out Q2) &&
|
|
Q1 != 0)
|
|
{
|
|
q3TextBox.Text = q2TextBox.Text;
|
|
dQpctTextBox.Text = (Q1 != 0) ? (100.0 * Q2 / Q1).ToString() : string.Empty;
|
|
}
|
|
|
|
qSpec = QSpec.Q_dPct;
|
|
break;
|
|
}
|
|
|
|
nextButton.Enabled = IsFormValid();
|
|
}
|
|
|
|
Unit newUnit;
|
|
bool suppresSelIxChanged;
|
|
|
|
private void unitComboBox_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
if (suppresSelIxChanged) return;
|
|
|
|
newUnit = Units.FromDescription(unitComboBox.Text);
|
|
|
|
if (Units.IsQuantity(newUnit, Quantity.Flow))
|
|
{
|
|
flowUnit = newUnit;
|
|
suppresSelIxChanged = true;
|
|
unit2ComboBox.Text = unit3ComboBox.Text = unitComboBox.Text;
|
|
suppresSelIxChanged = false;
|
|
}
|
|
}
|
|
private void unitComboBox_TextChanged(object sender, EventArgs e)
|
|
{
|
|
unitComboBox_SelectedIndexChanged(sender, e);
|
|
}
|
|
|
|
private void unit2ComboBox_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
if (suppresSelIxChanged) return;
|
|
|
|
newUnit = Units.FromDescription(unit2ComboBox.Text);
|
|
|
|
if (Units.IsQuantity(newUnit, Quantity.Flow))
|
|
{
|
|
flowUnit = newUnit;
|
|
suppresSelIxChanged = true;
|
|
unitComboBox.Text = unit3ComboBox.Text = unit2ComboBox.Text;
|
|
suppresSelIxChanged = false;
|
|
}
|
|
}
|
|
private void unit2ComboBox_TextChanged(object sender, EventArgs e)
|
|
{
|
|
unit2ComboBox_SelectedIndexChanged(sender, e);
|
|
}
|
|
|
|
private void unit3ComboBox_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
if (suppresSelIxChanged) return;
|
|
|
|
newUnit = Units.FromDescription(unit3ComboBox.Text);
|
|
|
|
if (Units.IsQuantity(newUnit, Quantity.Flow))
|
|
{
|
|
flowUnit = newUnit;
|
|
suppresSelIxChanged = true;
|
|
unitComboBox.Text = unit2ComboBox.Text = unit3ComboBox.Text;
|
|
suppresSelIxChanged = false;
|
|
}
|
|
}
|
|
private void unit3ComboBox_TextChanged(object sender, EventArgs e)
|
|
{
|
|
unit3ComboBox_SelectedIndexChanged(sender, e);
|
|
}
|
|
}
|
|
}
|