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

229 lines
7.1 KiB
C#

///
/// Copyright (c) 2013-2017 Senus Slovensko a.s.
///
using System;
using System.Windows.Forms;
using Common;
using Config;
using Config.Entities;
using TBF.Resources;
namespace TBF.UI.Procedures.TestWizard
{
public partial class VolumeAndErrorSelection : Form
{
enum TabId
{
WaterMeterTab,
HeatMeterTab,
}
Test test;
TabId tabId;
Unit volumeUnit;
public VolumeAndErrorSelection()
{
InitializeComponent();
tabId = 0;
Text = Strings.Volume_and_error_selection;
volumeLabel.Text = Strings.Volume;
durationLabel.Text = Strings.Duration_s;
errLoPctLabel.Text = Strings.Err_limit_neg_pct_chdr;
errHiPctLabel.Text = Strings.Err_limit_pos_pct_chdr;
uncertaintyLabel.Text = Strings.Uncertainty_pct_chdr;
backButton.Text = Strings.BackBtnText;
nextButton.Text = Strings.FinishBtnText;
cancelButton.Text = Strings.CancelBtnText;
tabPage1.Text = Strings.Water_Meter;
tabPage2.Text = Strings.Heat_meter;
classLabel.Text = Strings.Metrological_class;
qpLabel.Text = "Qp [m3/h]";
for (Unit u = 0; u < Unit.Count; u++)
{
if (Units.IsVolume(u)) unitComboBox.Items.Add(u.ToDescription());
}
}
public VolumeAndErrorSelection(bool first, bool last, Test test)
: this()
{
this.test = test;
volumeUnit = test.VolumeUnit;
unitComboBox.Text = volumeUnit.ToDescription();
if (first) backButton.Visible = false;
if (last) nextButton.Text = Strings.FinishBtnText;
}
private void VolumeAndErrorSelection_Load(object sender, EventArgs e)
{
if (test == null) return;
volumeTextBox.Text = Units.ConvertTo(volumeUnit, test.Volume).ToString();
durationTextBox.Text = test.TestTime.ToString();
errLoPctTextBox.Text = test.ErrLimLo.ToString();
errHiPctTextBox.Text = test.ErrLimHi.ToString();
uncertaintyTextBox.Text = test.Uncertainty.ToString();
classComboBox.Text = "2";
volumeTextBox.TextChanged += new System.EventHandler(this.volumeTextBox_TextChanged);
durationTextBox.TextChanged += new System.EventHandler(this.durationTextBox_TextChanged);
nextButton.Enabled = IsFormValid();
}
bool IsFormValid()
{
int idummy;
double dummy;
bool tabsValid;
switch (tabId)
{
default:
case TabId.WaterMeterTab:
tabsValid = Utils.TryParseSDouble(errLoPctTextBox.Text, out dummy)
&& Utils.TryParseSDouble(errHiPctTextBox.Text, out dummy);
break;
case TabId.HeatMeterTab:
tabsValid = int.TryParse(classComboBox.Text, out idummy)
&& idummy >= 1 && idummy <= 3
&& Utils.TryParseUDouble(qpTextBox.Text, out dummy);
break;
}
/// Return complete result
return tabsValid && Utils.TryParseUDouble(volumeTextBox.Text, out dummy)
&& Utils.TryParseUDouble(durationTextBox.Text, out dummy)
&& Utils.TryParseUDouble(uncertaintyTextBox.Text, out dummy);
}
void UpdateTest()
{
test.Volume = Units.ConvertFrom(volumeUnit, Utils.ParseUDouble(volumeTextBox.Text));
test.VolumeUnit = volumeUnit;
test.TestTime = Utils.ParseUDouble(durationTextBox.Text);
test.Uncertainty = Utils.ParseUFloat(uncertaintyTextBox.Text);
switch (tabId)
{
default:
case TabId.WaterMeterTab:
test.ErrLimLo = Utils.ParseSFloat(errLoPctTextBox.Text);
test.ErrLimHi = Utils.ParseSFloat(errHiPctTextBox.Text);
break;
case TabId.HeatMeterTab:
test.ErrLimLo = (float)int.Parse(classComboBox.Text);
test.ErrLimHi = -Utils.ParseUFloat(qpTextBox.Text);
break;
}
}
private void backButton_Click(object sender, EventArgs e)
{
if (test != null && IsFormValid()) UpdateTest();
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();
}
bool supressNotifications;
private void volumeTextBox_TextChanged(object sender, EventArgs e)
{
if (supressNotifications) return;
double Qave_lps = (test.Qfrom + test.Qto) / 7.2;
double volume;
if (Utils.TryParseUDouble(volumeTextBox.Text, out volume))
{
supressNotifications = true;
durationTextBox.Text = (Units.ConvertFrom(volumeUnit, volume) / Qave_lps).ToString();
supressNotifications = false;
}
anyTextBox_TextChanged(sender, e);
}
private void unitComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
var newUnit = Units.FromDescription(unitComboBox.Text);
if (Units.IsQuantity(newUnit, Quantity.Volume))
{
volumeUnit = newUnit;
double volume;
if (Utils.TryParseUDouble(volumeTextBox.Text, out volume))
{
double Qave_lps = (test.Qfrom + test.Qto) / 7.2;
supressNotifications = true;
durationTextBox.Text = (Units.ConvertFrom(volumeUnit, volume) / Qave_lps).ToString();
supressNotifications = false;
}
}
}
private void unitComboBox_TextChanged(object sender, EventArgs e)
{
unitComboBox_SelectedIndexChanged(sender, e);
}
private void durationTextBox_TextChanged(object sender, EventArgs e)
{
if (supressNotifications) return;
double testTime;
if (Utils.TryParseUDouble(durationTextBox.Text, out testTime))
{
double Qave_lps = (test.Qfrom + test.Qto) / 7.2;
supressNotifications = true;
volumeTextBox.Text = (testTime * Units.ConvertFrom(volumeUnit, Qave_lps)).ToString();
supressNotifications = false;
}
anyTextBox_TextChanged(sender, e);
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
switch ((TabId)tabControl1.SelectedIndex)
{
default:
case TabId.WaterMeterTab:
tabId = TabId.WaterMeterTab;
break;
case TabId.HeatMeterTab:
tabId = TabId.HeatMeterTab;
break;
}
nextButton.Enabled = IsFormValid();
}
}
}