tbf/Statistics/UserControls/MidOrWaterMetersCtrl.cs
2022-08-30 14:58:26 +02:00

336 lines
14 KiB
C#

///
/// Copyright (c) 2018 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Results.Entities;
using Statistics.Resources;
using System.Globalization;
namespace Statistics.UserControls
{
public partial class MidOrWaterMetersCtrl : UserControl
{
public int MidId; /// 1-based MID Id.
public StatisticsDlg ParentDlg;
public bool IsFlowEnabled(int i)
{
switch (i)
{
case 1: return flow1CheckBox.Checked;
case 2: return flow2CheckBox.Checked;
case 3: return flow3CheckBox.Checked;
case 4: return flow4CheckBox.Checked;
case 5: return flow5CheckBox.Checked;
default: return false;
}
}
public double FlowFrom(int i)
{
switch (i)
{
case 1: return flow1From;
case 2: return flow2From;
case 3: return flow3From;
case 4: return flow4From;
case 5: return flow5From;
default: return 0;
}
}
public double FlowTo(int i)
{
switch (i)
{
case 1: return flow1To;
case 2: return flow2To;
case 3: return flow3To;
case 4: return flow4To;
case 5: return flow5To;
default: return 0;
}
}
/// All flows are in m3/h
double flow1From;
double flow1To;
double flow2From;
double flow2To;
double flow3From;
double flow3To;
double flow4From;
double flow4To;
double flow5From;
double flow5To;
StatisticsCtrl GetStatistics(int i)
{
if ((i >= 0) && (i < benchesTabControl.TabPages.Count) && benchesTabControl.TabPages[i].Controls.Count > 0)
{
return benchesTabControl.TabPages[i].Controls[0] as StatisticsCtrl;
}
else return null;
}
public IList<TestRslt> UpdateRequest(int benchId)
{
return ParentDlg.UpdateRequest(benchId, MidId);
}
public MidOrWaterMetersCtrl()
{
InitializeComponent();
ManageCheckGroupBox(flow1CheckBox, flow1GroupBox);
ManageCheckGroupBox(flow2CheckBox, flow2GroupBox);
ManageCheckGroupBox(flow3CheckBox, flow3GroupBox);
ManageCheckGroupBox(flow4CheckBox, flow4GroupBox);
ManageCheckGroupBox(flow5CheckBox, flow5GroupBox);
for (int i = 0; i < Program.LocalSettings.BenchNames.Length; i++)
{
TabPage tp = new TabPage();
tp.Text = Program.LocalSettings.BenchNames[i];
StatisticsCtrl statistics = new StatisticsCtrl();
statistics.Id = i;
statistics.MidCtrl = this;
statistics.Dock = DockStyle.Fill;
tp.Controls.Add(statistics);
benchesTabControl.TabPages.Add(tp);
}
}
void Localize()
{
flow1CheckBox.Text = string.Format("{0} 1", Strings.Flow);
flow2CheckBox.Text = string.Format("{0} 2", Strings.Flow);
flow3CheckBox.Text = string.Format("{0} 3", Strings.Flow);
flow4CheckBox.Text = string.Format("{0} 4", Strings.Flow);
flow5CheckBox.Text = string.Format("{0} 5", Strings.Flow);
fromLabel2.Text = Strings.From;
fromLabel3.Text = Strings.From;
fromLabel4.Text = Strings.From;
fromLabel5.Text = Strings.From;
fromLabel6.Text = Strings.From;
toLabel2.Text = Strings.To;
toLabel3.Text = Strings.To;
toLabel4.Text = Strings.To;
toLabel5.Text = Strings.To;
toLabel6.Text = Strings.To;
}
public void Data2UI()
{
LocalSettings ls = Program.LocalSettings;
flow1CheckBox.Checked = ls.GetFlowEnabled(MidId, 0);
flow2CheckBox.Checked = ls.GetFlowEnabled(MidId, 1);
flow3CheckBox.Checked = ls.GetFlowEnabled(MidId, 2);
flow4CheckBox.Checked = ls.GetFlowEnabled(MidId, 3);
flow5CheckBox.Checked = ls.GetFlowEnabled(MidId, 4);
flow1From = ls.GetFlowFrom(MidId, 0);
flow2From = ls.GetFlowFrom(MidId, 1);
flow3From = ls.GetFlowFrom(MidId, 2);
flow4From = ls.GetFlowFrom(MidId, 3);
flow5From = ls.GetFlowFrom(MidId, 4);
flow1FromTextBox.Text = flow1From.ToString();
flow2FromTextBox.Text = flow2From.ToString();
flow3FromTextBox.Text = flow3From.ToString();
flow4FromTextBox.Text = flow4From.ToString();
flow5FromTextBox.Text = flow5From.ToString();
flow1To = ls.GetFlowTo(MidId, 0);
flow2To = ls.GetFlowTo(MidId, 1);
flow3To = ls.GetFlowTo(MidId, 2);
flow4To = ls.GetFlowTo(MidId, 3);
flow5To = ls.GetFlowTo(MidId, 4);
flow1ToTextBox.Text = flow1To.ToString();
flow2ToTextBox.Text = flow2To.ToString();
flow3ToTextBox.Text = flow3To.ToString();
flow4ToTextBox.Text = flow4To.ToString();
flow5ToTextBox.Text = flow5To.ToString();
}
/// <summary>
///
/// </summary>
/// <returns>true when successful</returns>
public void UI2Data()
{
LocalSettings ls = Program.LocalSettings;
ls.SetFlowEnabled(MidId, 0, flow1CheckBox.Checked);
ls.SetFlowEnabled(MidId, 1, flow2CheckBox.Checked);
ls.SetFlowEnabled(MidId, 2, flow3CheckBox.Checked);
ls.SetFlowEnabled(MidId, 3, flow4CheckBox.Checked);
ls.SetFlowEnabled(MidId, 4, flow5CheckBox.Checked);
ls.SetFlowFrom(MidId, 0, flow1From);
ls.SetFlowFrom(MidId, 1, flow2From);
ls.SetFlowFrom(MidId, 2, flow3From);
ls.SetFlowFrom(MidId, 3, flow4From);
ls.SetFlowFrom(MidId, 4, flow5From);
ls.SetFlowTo(MidId, 0, flow1To);
ls.SetFlowTo(MidId, 1, flow2To);
ls.SetFlowTo(MidId, 2, flow3To);
ls.SetFlowTo(MidId, 3, flow4To);
ls.SetFlowTo(MidId, 4, flow5To);
}
private void flow1CheckBox_CheckedChanged(object sender, EventArgs e) { ManageCheckGroupBox(flow1CheckBox, flow1GroupBox); }
private void flow2CheckBox_CheckedChanged(object sender, EventArgs e) { ManageCheckGroupBox(flow2CheckBox, flow2GroupBox); }
private void flow3CheckBox_CheckedChanged(object sender, EventArgs e) { ManageCheckGroupBox(flow3CheckBox, flow3GroupBox); }
private void flow4CheckBox_CheckedChanged(object sender, EventArgs e) { ManageCheckGroupBox(flow4CheckBox, flow4GroupBox); }
private void flow5CheckBox_CheckedChanged(object sender, EventArgs e) { ManageCheckGroupBox(flow5CheckBox, flow5GroupBox); }
private void ManageCheckGroupBox(CheckBox chk, GroupBox grp)
{
/// Make sure the CheckBox isn't in the GroupBox. This will only happen the first time.
if (chk.Parent == grp)
{
grp.Parent.Controls.Add(chk); /// Reparent the CheckBox so it's not in the GroupBox.
chk.Location = new Point(chk.Left + grp.Left, chk.Top + grp.Top); /// Adjust the CheckBox's location.
chk.BringToFront(); /// Move the CheckBox to the top of the stacking order.
}
/// Enable or disable the GroupBox.
grp.Enabled = chk.Checked;
}
private void flow1FromTextBox_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(flow1FromTextBox.Text))
{
flow1From = 0;
flow1FromExclamation.Visible = false;
}
else
flow1FromExclamation.Visible = !((double.TryParse(flow1FromTextBox.Text, NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out flow1From) ||
double.TryParse(flow1FromTextBox.Text, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out flow1From)) &&
(flow1From >= 0));
}
private void flow1ToTextBox_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(flow1ToTextBox.Text))
{
flow1To = 0;
flow1ToExclamation.Visible = false;
}
else
flow1ToExclamation.Visible = !((double.TryParse(flow1ToTextBox.Text, NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out flow1To) ||
double.TryParse(flow1ToTextBox.Text, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out flow1To)) &&
(flow1To >= 0));
}
private void flow2FromTextBox_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(flow2FromTextBox.Text))
{
flow2From = 0;
flow2FromExclamation.Visible = false;
}
else
flow2FromExclamation.Visible = !((double.TryParse(flow2FromTextBox.Text, NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out flow2From) ||
double.TryParse(flow2FromTextBox.Text, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out flow2From)) &&
(flow2From >= 0));
}
private void flow2ToTextBox_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(flow2ToTextBox.Text))
{
flow2To = 0;
flow2ToExclamation.Visible = false;
}
else
flow2ToExclamation.Visible = !((double.TryParse(flow2ToTextBox.Text, NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out flow2To) ||
double.TryParse(flow2ToTextBox.Text, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out flow2To)) &&
(flow2To >= 0));
}
private void flow3FromTextBox_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(flow3FromTextBox.Text))
{
flow3From = 0;
flow3FromExclamation.Visible = false;
}
else
flow3FromExclamation.Visible = !((double.TryParse(flow3FromTextBox.Text, NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out flow3From) ||
double.TryParse(flow3FromTextBox.Text, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out flow3From)) &&
(flow3From >= 0));
}
private void flow3ToTextBox_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(flow3ToTextBox.Text))
{
flow3To = 0;
flow3ToExclamation.Visible = false;
}
else
flow3ToExclamation.Visible = !((double.TryParse(flow3ToTextBox.Text, NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out flow3To) ||
double.TryParse(flow3ToTextBox.Text, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out flow3To)) &&
(flow3To >= 0));
}
private void flow4FromTextBox_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(flow4FromTextBox.Text))
{
flow4From = 0;
flow4FromExclamation.Visible = false;
}
else
flow4FromExclamation.Visible = !((double.TryParse(flow4FromTextBox.Text, NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out flow4From) ||
double.TryParse(flow4FromTextBox.Text, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out flow4From)) &&
(flow4From >= 0));
}
private void flow4ToTextBox_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(flow4ToTextBox.Text))
{
flow4To = 0;
flow4ToExclamation.Visible = false;
}
else
flow4ToExclamation.Visible = !((double.TryParse(flow4ToTextBox.Text, NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out flow4To) ||
double.TryParse(flow4ToTextBox.Text, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out flow4To)) &&
(flow4To >= 0));
}
private void flow5FromTextBox_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(flow5FromTextBox.Text))
{
flow5From = 0;
flow5FromExclamation.Visible = false;
}
else
flow5FromExclamation.Visible = !((double.TryParse(flow5FromTextBox.Text, NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out flow5From) ||
double.TryParse(flow5FromTextBox.Text, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out flow5From)) &&
(flow5From >= 0));
}
private void flow5ToTextBox_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(flow5ToTextBox.Text))
{
flow5To = 0;
flow5ToExclamation.Visible = false;
}
else
flow5ToExclamation.Visible = !((double.TryParse(flow5ToTextBox.Text, NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out flow5To) ||
double.TryParse(flow5ToTextBox.Text, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out flow5To)) &&
(flow5To >= 0));
}
}
}