Refactorization : BenchControl -> Rig
This commit is contained in:
@@ -0,0 +1,197 @@
|
||||
///
|
||||
/// Copyright (c) 2017 Sensus Metering Systems
|
||||
///
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Windows.Forms;
|
||||
using log4net;
|
||||
using TBF.Resources;
|
||||
using System.Drawing;
|
||||
using System.Text;
|
||||
|
||||
namespace TBF.Rig.Network.Camera.Display
|
||||
{
|
||||
public partial class DisplayForm : Form, GenericDevices.IHasCompleted
|
||||
{
|
||||
private static readonly ILog log = LogManager.GetLogger(typeof(DisplayForm));
|
||||
|
||||
#region Closing this display from another thread
|
||||
|
||||
/// <summary>
|
||||
/// Called from the state machine when an operation forces a modeless dialog close.
|
||||
/// </summary>
|
||||
public void CloseForm(object sender, EventArgs args)
|
||||
{
|
||||
if (CloseFormHandler == null) return;
|
||||
try { CloseFormHandler(sender, null); }
|
||||
catch (Exception e) { log.Error("CloseFormHandler(...) failed", e); }
|
||||
}
|
||||
public event EventHandler<EventArgs> CloseFormHandler;
|
||||
|
||||
public void StartCloseFormHandler()
|
||||
{
|
||||
CloseFormHandler += delegate(object sender, EventArgs args)
|
||||
{
|
||||
if (InvokeRequired) { Invoke(new EventHandler<EventArgs>(OnCloseForm), sender, args); }
|
||||
else OnCloseForm(sender, args);
|
||||
};
|
||||
}
|
||||
|
||||
private void OnCloseForm(object sender, EventArgs args)
|
||||
{
|
||||
DialogResult = DialogResult.Cancel;
|
||||
Close();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary> Number of text boxes for serial numbers </summary>
|
||||
readonly string[] imageFileNames;
|
||||
readonly int imagesCount;
|
||||
|
||||
readonly GroupBox[] groupBoxes;
|
||||
readonly PictureBox[] pictureBoxes;
|
||||
readonly CheckBox[] checkBoxes;
|
||||
readonly int boxesCount;
|
||||
|
||||
Timer myTimer;
|
||||
|
||||
|
||||
public bool[] SelectedImages;
|
||||
public DialogResult Result; /// DialogResult.None OK Retry or Cancel
|
||||
|
||||
|
||||
/// Set to 'true' when the form closes
|
||||
public bool Completed { get { return completed; } }
|
||||
bool completed;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
/// <param name="waterMetersCount">Number of text boxes for serial numbers</param>
|
||||
public DisplayForm(string[] imageFileNames, string title)
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
this.imageFileNames = imageFileNames;
|
||||
this.imagesCount = (imageFileNames == null) ? 0 : imageFileNames.Length;
|
||||
|
||||
groupBoxes = new GroupBox[] { groupBox1, groupBox2, groupBox3, groupBox4, groupBox5, groupBox6,
|
||||
groupBox7, groupBox8, groupBox9, groupBox10, groupBox11, groupBox12 };
|
||||
|
||||
pictureBoxes = new PictureBox[] { pictureBox1, pictureBox2, pictureBox3, pictureBox4, pictureBox5, pictureBox6,
|
||||
pictureBox7, pictureBox8, pictureBox9, pictureBox10, pictureBox11, pictureBox12 };
|
||||
|
||||
checkBoxes = new CheckBox[] { checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6,
|
||||
checkBox7, checkBox8, checkBox9, checkBox10, checkBox11, checkBox12 };
|
||||
|
||||
boxesCount = pictureBoxes.Length;
|
||||
|
||||
Text = title;
|
||||
|
||||
int lastVisibleGroupBoxIx = 0;
|
||||
for (int i = 0; i < boxesCount; i++)
|
||||
{
|
||||
if (i < imagesCount && !string.IsNullOrEmpty(imageFileNames[i]))
|
||||
{
|
||||
groupBoxes[i].Text = string.Format("{0} {1}", Strings.Water_Meter, i + 1);
|
||||
checkBoxes[i].Checked = true;
|
||||
checkBoxes[i].Text = Strings.Retry;
|
||||
lastVisibleGroupBoxIx = i;
|
||||
}
|
||||
else
|
||||
{
|
||||
groupBoxes[i].Visible = false;
|
||||
checkBoxes[i].Checked = false;
|
||||
}
|
||||
}
|
||||
|
||||
SelectedImages = new bool[imagesCount];
|
||||
|
||||
if (lastVisibleGroupBoxIx < 4)
|
||||
Height = 321;
|
||||
else if (lastVisibleGroupBoxIx < 8)
|
||||
Height = 616;
|
||||
|
||||
Result = DialogResult.None;
|
||||
completed = false;
|
||||
StartCloseFormHandler();
|
||||
}
|
||||
|
||||
/// <summary> Parameterless constructor for all watermeters </summary>
|
||||
public DisplayForm()
|
||||
: this(null, string.Empty)
|
||||
{
|
||||
}
|
||||
|
||||
private void CycleBeginningForm_Load(object sender, EventArgs e)
|
||||
{
|
||||
okButton.Text = Strings.OkBtnText;
|
||||
retryButton.Text = Strings.Retry;
|
||||
abortButton.Text = Strings.Abort;
|
||||
|
||||
myTimer = new Timer();
|
||||
myTimer.Interval = (500);
|
||||
myTimer.Tick += new EventHandler(MyTimer_Tick);
|
||||
myTimer.Start();
|
||||
}
|
||||
|
||||
private void MyTimer_Tick(object sender, EventArgs e)
|
||||
{
|
||||
for (int i = 0; i < Math.Min(imagesCount, boxesCount); i++)
|
||||
{
|
||||
if (imageFileNames[i] != null && System.IO.File.Exists(imageFileNames[i]) &&
|
||||
pictureBoxes[i] != null && pictureBoxes[i].Image == null)
|
||||
{
|
||||
pictureBoxes[i].Image = Image.FromFile(imageFileNames[i]);
|
||||
checkBoxes[i].Checked = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void okButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
log.Warn("OK clicked");
|
||||
Result = DialogResult.OK;
|
||||
CloseAfterButtonClick();
|
||||
}
|
||||
|
||||
private void abortButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
log.Warn("Abort clicked");
|
||||
Result = DialogResult.Abort;
|
||||
CloseAfterButtonClick();
|
||||
}
|
||||
|
||||
private void retryButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
log.Warn("Retry clicked");
|
||||
Result = DialogResult.Retry;
|
||||
CloseAfterButtonClick();
|
||||
}
|
||||
|
||||
void CloseAfterButtonClick()
|
||||
{
|
||||
if (myTimer != null) myTimer.Stop();
|
||||
UpdateData();
|
||||
completed = true;
|
||||
Close();
|
||||
}
|
||||
|
||||
void UpdateData()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.Append("1 ");
|
||||
for (int i = 0; i < Math.Min(imagesCount, boxesCount); i++)
|
||||
{
|
||||
SelectedImages[i] = checkBoxes[i].Checked;
|
||||
sb.AppendFormat("{0}", checkBoxes[i].Checked ? "x" : "-");
|
||||
}
|
||||
sb.AppendFormat(" {0}", Math.Min(imagesCount, boxesCount));
|
||||
|
||||
log.WarnFormat("Data updated: {0}", sb.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user