Refactorization : BenchControl -> Rig
This commit is contained in:
@@ -0,0 +1,263 @@
|
||||
///
|
||||
/// Copyright (c) 2015-2019 Sensus Slovensko a.s.
|
||||
///
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Windows.Forms;
|
||||
using log4net;
|
||||
using Config.Entities;
|
||||
using TBF.Resources;
|
||||
|
||||
namespace TBF.Rig.TestMethods.Adjustment
|
||||
{
|
||||
public partial class WMErrorsForm12 : Form, GenericDevices.IHasCompleted
|
||||
{
|
||||
private static readonly ILog log = LogManager.GetLogger(typeof(WMErrorsForm12));
|
||||
|
||||
const int MaxWMsCount = 12; /// Implementation limit of this form
|
||||
|
||||
///
|
||||
/// Updated in constructor
|
||||
///
|
||||
readonly int waterMetersCount; /// Number of text boxes for serial numbers
|
||||
readonly bool[] disabled;
|
||||
int textBoxesCount;
|
||||
Label[] labels;
|
||||
TextBox[] errors;
|
||||
Rectangle[] rects;
|
||||
|
||||
///
|
||||
/// Updated in OnAdjustmentInProgress()
|
||||
///
|
||||
double[] error; /// Created in constructor
|
||||
double errLimLo;
|
||||
double errLimHi;
|
||||
|
||||
///
|
||||
/// Set to 'true' when the form closes
|
||||
///
|
||||
public bool Completed { get { return completed; } }
|
||||
bool completed;
|
||||
|
||||
|
||||
/// <summary> Parameterless constructor for 3 watermeters </summary>
|
||||
public WMErrorsForm12()
|
||||
{
|
||||
InitializeComponent();
|
||||
ControlBox = false;
|
||||
|
||||
textBoxesCount = MaxWMsCount;
|
||||
|
||||
labels = new Label[MaxWMsCount]
|
||||
{
|
||||
wmErrorLabel1, wmErrorLabel2, wmErrorLabel3, wmErrorLabel4, wmErrorLabel5,
|
||||
wmErrorLabel6, wmErrorLabel7, wmErrorLabel8, wmErrorLabel9, wmErrorLabel10,
|
||||
wmErrorLabel11, wmErrorLabel12,
|
||||
};
|
||||
errors = new TextBox[MaxWMsCount]
|
||||
{
|
||||
wmErrorTextBox1, wmErrorTextBox2, wmErrorTextBox3, wmErrorTextBox4, wmErrorTextBox5,
|
||||
wmErrorTextBox6, wmErrorTextBox7, wmErrorTextBox8, wmErrorTextBox9, wmErrorTextBox10,
|
||||
wmErrorTextBox11, wmErrorTextBox12,
|
||||
};
|
||||
|
||||
rects = new Rectangle[MaxWMsCount];
|
||||
for (int i = 0; i < MaxWMsCount; i++)
|
||||
{
|
||||
int row = i % 12;
|
||||
int col = i / 12;
|
||||
rects[i] = new Rectangle(350 + col * 557, 25 + row * 47, 230, 37);
|
||||
}
|
||||
|
||||
completed = false;
|
||||
|
||||
StartForceCloseHandler();
|
||||
|
||||
/// Attach to 'AdjustmentInProgress' handler
|
||||
UiBridge.Bridge.AdjustmentInProgressHandler += delegate(object sender, UiBridge.AdjustmentInProgressEventArgs args)
|
||||
{
|
||||
if (InvokeRequired)
|
||||
{
|
||||
Invoke(new EventHandler<UiBridge.AdjustmentInProgressEventArgs>(OnAdjustmentInProgress), sender, args);
|
||||
}
|
||||
else OnAdjustmentInProgress(sender, args);
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
/// <param name="waterMetersCount">Number of text boxes for serial numbers</param>
|
||||
public WMErrorsForm12(IList<GenericDevices.IWaterMeter> waterMeters)
|
||||
: this()
|
||||
{
|
||||
waterMetersCount = Math.Min(Config.Data.WMsCount, waterMeters.Count);
|
||||
|
||||
ShuffleTextBoxes(waterMetersCount, Config.Data.LineSize);
|
||||
|
||||
disabled = new bool[waterMetersCount];
|
||||
for (int i = 0; i < waterMetersCount; i++)
|
||||
{
|
||||
if (i < MaxWMsCount) disabled[i] = waterMeters[i].Disabled;
|
||||
}
|
||||
|
||||
error = new double[waterMetersCount];
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Make sure the layout of labels/text boxes on the screen
|
||||
/// corresponds to the layout of watermeters of the test bench.
|
||||
/// </summary>
|
||||
/// <param name="wmsCount">Number of watermeters</param>
|
||||
/// <param name="lineSize">Number of watermeters in one line</param>
|
||||
void ShuffleTextBoxes(int wmsCount, int lineSize)
|
||||
{
|
||||
if ((wmsCount < textBoxesCount) && (lineSize > 0))
|
||||
{
|
||||
int nrLines = (wmsCount + lineSize - 1) / lineSize;
|
||||
int gap = (textBoxesCount - wmsCount) / nrLines;
|
||||
|
||||
int dest = 0;
|
||||
for (int l = 0; l < nrLines; l++)
|
||||
{
|
||||
for (int i = 0; i < lineSize; i++)
|
||||
{
|
||||
labels[dest] = labels[l * lineSize + l * gap + i];
|
||||
errors[dest] = errors[l * lineSize + l * gap + i];
|
||||
rects[dest] = rects[l * lineSize + l * gap + i];
|
||||
dest++;
|
||||
}
|
||||
}
|
||||
textBoxesCount = wmsCount;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Localize()
|
||||
{
|
||||
Text = Strings.Water_Meter_States;
|
||||
for (int i = 0; i < textBoxesCount; i++)
|
||||
{
|
||||
labels[i].Text = string.Format("WM{0} {1}", i + 1, Strings.Error_pct);
|
||||
}
|
||||
okButton.Text = Strings.OkBtnText;
|
||||
}
|
||||
|
||||
private void WMErrorsForm12_Load(object sender, EventArgs e)
|
||||
{
|
||||
Localize();
|
||||
|
||||
//Height = 50 + 90 * WaterMetersCount;
|
||||
|
||||
for (int i = 0; i < textBoxesCount; i++)
|
||||
{
|
||||
labels[i].Visible = errors[i].Visible = (disabled == null || i >= disabled.Length || !disabled[i]);
|
||||
errors[i].Text = "---";
|
||||
}
|
||||
}
|
||||
|
||||
private void okButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
completed = true;
|
||||
Close();
|
||||
}
|
||||
|
||||
void OnAdjustmentInProgress(object sender, UiBridge.AdjustmentInProgressEventArgs args)
|
||||
{
|
||||
string testName = args.TestName;
|
||||
|
||||
Results.Entities.TestRslt tr = TBF.Rig.Sequences.ProcessData.BatchRslts.GetTestRslt(testName, 0);
|
||||
if (tr != null)
|
||||
{
|
||||
errLimLo = tr.ErrLimLo() + tr.ErrLimMargin();
|
||||
errLimHi = tr.ErrLimHi() - tr.ErrLimMargin();
|
||||
|
||||
int count = Math.Min(waterMetersCount, TBF.Rig.Sequences.ProcessData.BatchRslts.WMPositionsCount);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
Results.Entities.MeterTestRslt mtr = TBF.Rig.Sequences.ProcessData.BatchRslts.GetMeterTestRslt(testName, i, CompoundMeterId.Single);
|
||||
if (mtr != null) error[i] = mtr.Error;
|
||||
}
|
||||
|
||||
Redraw();
|
||||
}
|
||||
}
|
||||
|
||||
void Redraw()
|
||||
{
|
||||
for (int i = 0; i < textBoxesCount; i++)
|
||||
{
|
||||
errors[i].Text = error[i].ToString("F1");
|
||||
Invalidate(rects[i]);
|
||||
}
|
||||
}
|
||||
|
||||
private void WMErrorsForm_Paint(object sender, PaintEventArgs e)
|
||||
{
|
||||
System.Drawing.Graphics graphics = this.CreateGraphics();
|
||||
|
||||
for (int i = 0; i < waterMetersCount; i++)
|
||||
{
|
||||
if ((i < MaxWMsCount) && (!disabled[i]))
|
||||
{
|
||||
PaintOne(graphics, rects[i], error[i], errLimLo, errLimHi);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PaintOne(System.Drawing.Graphics g, Rectangle r, double value, double limLo, double limHi)
|
||||
{
|
||||
Rectangle rect1 = new Rectangle(r.Left, r.Top, r.Width / 3, r.Height);
|
||||
Rectangle rect2 = new Rectangle(r.Left + r.Width / 3, r.Top, r.Width / 3, r.Height);
|
||||
Rectangle rect3 = new Rectangle(r.Left + 2 * (r.Width / 3), r.Top, r.Width / 3, r.Height);
|
||||
Rectangle rectCent = new Rectangle(r.Left + r.Width / 2 - 1, r.Top, 3, r.Height);
|
||||
|
||||
Brush redBrush = new SolidBrush(Color.FromName("Salmon"));
|
||||
Brush greenBrush = new SolidBrush(Color.FromName("LightGreen"));
|
||||
Brush centBrush = new SolidBrush(Color.FromName("Green"));
|
||||
|
||||
g.FillRectangle(redBrush, rect1);
|
||||
g.FillRectangle(greenBrush, rect2);
|
||||
g.FillRectangle(redBrush, rect3);
|
||||
g.FillRectangle(centBrush, rectCent);
|
||||
|
||||
int x0 = r.Left + r.Width / 2;
|
||||
int dx = r.Height / 3;
|
||||
int x = (int)((double)r.Width * (2.0 * value - limLo - limHi) / (limHi - limLo) / 6.0f + 0.5f);
|
||||
|
||||
if (x0 - dx + x >= r.Left && x0 + dx + x < r.Left + r.Width)
|
||||
{
|
||||
Point[] triangle = new Point[3]
|
||||
{
|
||||
new Point(x0 - dx + x, r.Top),
|
||||
new Point(x0 + dx + x, r.Top),
|
||||
new Point(x0 + x, r.Top + 2 * r.Height / 3),
|
||||
};
|
||||
|
||||
Brush blackBrush = new SolidBrush(Color.Black);
|
||||
g.FillPolygon(blackBrush, triangle);
|
||||
}
|
||||
}
|
||||
|
||||
#region Forced close handling
|
||||
|
||||
public void StartForceCloseHandler()
|
||||
{
|
||||
UiBridge.Bridge.CloseModelessFormHandler += delegate(object sender, EventArgs args)
|
||||
{
|
||||
if (InvokeRequired) { Invoke(new EventHandler<EventArgs>(OnForceClose), sender, args); }
|
||||
else OnForceClose(sender, args);
|
||||
};
|
||||
}
|
||||
|
||||
void OnForceClose(object sender, EventArgs args)
|
||||
{
|
||||
DialogResult = DialogResult.Cancel;
|
||||
Close();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user