291 lines
8.8 KiB
C#
291 lines
8.8 KiB
C#
///
|
|
/// Copyright (c) 2013-2015 Sensus Metering Systems
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Windows.Forms;
|
|
using log4net;
|
|
using Config.Entities;
|
|
using TBF.Resources;
|
|
|
|
namespace TBF.UiControls
|
|
{
|
|
public partial class MetrologyDlgPressMeterTab : UserControl, IMetrologyDlgTab
|
|
{
|
|
static readonly ILog log = LogManager.GetLogger(typeof(MetrologyDlgPressMeterTab));
|
|
|
|
/// <summary>
|
|
/// Component entity that has a list of 'measurement-correction' pairs
|
|
/// to be updated in the database on OK.
|
|
/// </summary>
|
|
Component meterEntity;
|
|
public Component MeterEntity
|
|
{
|
|
get { return meterEntity; }
|
|
set { meterEntity = value; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// A list of 'measurement-correction' pairs to be deleted from the database on OK.
|
|
/// </summary>
|
|
public IList<MeasurementCorrection> ToBeRemoved
|
|
{
|
|
get { return toBeRemoved; }
|
|
set { toBeRemoved = value; }
|
|
}
|
|
IList<MeasurementCorrection> toBeRemoved;
|
|
|
|
MetrologyDlg parent;
|
|
bool unlocked;
|
|
Control measurementTextBox;
|
|
Control correctionTextBox;
|
|
|
|
public MetrologyDlgPressMeterTab()
|
|
{
|
|
InitializeComponent();
|
|
toBeRemoved = new List<MeasurementCorrection>();
|
|
}
|
|
|
|
private void MetrologyDlgPressMeterTab_Load(object sender, System.EventArgs e)
|
|
{
|
|
if (meterEntity == null) return;
|
|
|
|
parent = ParentForm as MetrologyDlg;
|
|
|
|
/// Panel1
|
|
cmpntNameLabel.Text = meterEntity.Name;
|
|
|
|
componentLabel.Text = Strings.Component;
|
|
testGroupBox.Text = Strings.Test_measured_corrected;
|
|
measuredLabel.Text = Strings.Measured;
|
|
correctedLabel.Text = Strings.Corrected;
|
|
measuredTextBox.Text = string.Empty;
|
|
correctedTextBox.Text = string.Empty;
|
|
|
|
/// Panel2
|
|
measurementTextBox = new TextBox();
|
|
correctionTextBox = new TextBox();
|
|
this.Controls.Add(measurementTextBox);
|
|
this.Controls.Add(correctionTextBox);
|
|
|
|
listViewEx.SubItemClicked += new Results.Forms.SubItemEventHandler(listViewEx_SubItemClicked);
|
|
listViewEx.SubItemRightClicked += new Results.Forms.SubItemEventHandler(listViewEx_SubItemRightClicked);
|
|
listViewEx.SubItemEndEditing += new Results.Forms.SubItemEndEditingEventHandler(listViewEx_SubItemEndEditing);
|
|
|
|
listViewEx.Columns.Add(new ColumnHeader() { Text = Strings.Nr, Width = 60 });
|
|
listViewEx.Columns.Add(new ColumnHeader() { Text = string.Format("{0} [bar]", Strings.Pressure), Width = 100 });
|
|
listViewEx.Columns.Add(new ColumnHeader() { Text = string.Format("{0} [mbar]", Strings.Correction), Width = 100 });
|
|
|
|
RefreshAll();
|
|
}
|
|
|
|
void RefreshAll()
|
|
{
|
|
listViewEx.Items.Clear();
|
|
foreach (var corr in MeterEntity.Corrections) AddOneLVI(corr);
|
|
}
|
|
|
|
void AddOneLVI(MeasurementCorrection corr)
|
|
{
|
|
int nr = listViewEx.Items.Count + 1;
|
|
ListViewItem lvi = new ListViewItem(nr.ToString());
|
|
lvi.SubItems.Add(corr.Measurement.ToString());
|
|
lvi.SubItems.Add((corr.Correction * 1000.0f).ToString());
|
|
lvi.Tag = corr;
|
|
listViewEx.Items.Add(lvi);
|
|
}
|
|
|
|
void listViewEx_SubItemClicked(object sender, Results.Forms.SubItemEventArgs e)
|
|
{
|
|
if (unlocked && e.SubItem == 1)
|
|
{
|
|
listViewEx.StartEditing(measurementTextBox, e.Item, e.SubItem);
|
|
}
|
|
else if (unlocked && e.SubItem == 2)
|
|
{
|
|
listViewEx.StartEditing(correctionTextBox, e.Item, e.SubItem);
|
|
}
|
|
}
|
|
|
|
void listViewEx_SubItemRightClicked(object sender, Results.Forms.SubItemEventArgs e)
|
|
{
|
|
if (!unlocked || e.SubItem < 1 || e.SubItem > 2) return;
|
|
|
|
if (DialogResult.Yes == MessageBox.Show(Strings.Do_you_want_to_copy_this_value_to_all_cells_below_this_cell,
|
|
Strings.Confirmation, MessageBoxButtons.YesNo, MessageBoxIcon.Question))
|
|
{
|
|
int columnNr = e.SubItem;
|
|
string value = null;
|
|
System.Drawing.Color backColor = System.Drawing.Color.White;
|
|
|
|
foreach (ListViewItem item in listViewEx.Items)
|
|
{
|
|
if (value != null && item.SubItems.Count > columnNr)
|
|
{
|
|
item.SubItems[columnNr].Text = value;
|
|
item.SubItems[columnNr].BackColor = backColor;
|
|
ParseItem(item, columnNr, value);
|
|
}
|
|
else if (item == e.Item)
|
|
{
|
|
value = item.SubItems[columnNr].Text;
|
|
backColor = item.SubItems[columnNr].BackColor;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void listViewEx_SubItemEndEditing(object sender, Results.Forms.SubItemEndEditingEventArgs e)
|
|
{
|
|
if (ParseItem(e.Item, e.SubItem, e.DisplayText)) return;
|
|
|
|
/// New value NOK, revert the original text
|
|
e.DisplayText = e.Item.Text;
|
|
e.Cancel = true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Parse text of a ListViewItem
|
|
/// </summary>
|
|
/// <returns>true = value parsed OK</returns>
|
|
bool ParseItem(ListViewItem item, int subItem, string value)
|
|
{
|
|
float fvalue;
|
|
if (subItem == 1 && Utils.TryParseEFloat(value, out fvalue) && fvalue >= 0)
|
|
{
|
|
(item.Tag as MeasurementCorrection).Measurement = fvalue;
|
|
return true;
|
|
}
|
|
else if (subItem == 2 && Utils.TryParseEFloat(value, out fvalue))
|
|
{
|
|
(item.Tag as MeasurementCorrection).Correction = fvalue / 1000.0f;
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public void Unlock()
|
|
{
|
|
unlocked = true;
|
|
|
|
if (listViewEx.SelectedItems.Count == 1)
|
|
{
|
|
int ix = listViewEx.SelectedIndices[0];
|
|
listViewEx.Focus();
|
|
listViewEx.Items[ix].Selected = true;
|
|
listViewEx.Items[ix].EnsureVisible();
|
|
}
|
|
}
|
|
|
|
public void OkBtnClicked()
|
|
{
|
|
}
|
|
|
|
public void CancelBtnClicked()
|
|
{
|
|
}
|
|
|
|
public void AddOne()
|
|
{
|
|
MeasurementCorrection corr = new MeasurementCorrection();
|
|
MeterEntity.Corrections.Add(corr);
|
|
AddOneLVI(corr);
|
|
|
|
listViewEx.Focus();
|
|
listViewEx.SelectedItems.Clear();
|
|
listViewEx.Items[listViewEx.Items.Count - 1].Selected = true;
|
|
listViewEx.Items[listViewEx.Items.Count - 1].EnsureVisible();
|
|
}
|
|
|
|
public void RemoveSelected()
|
|
{
|
|
if (listViewEx.SelectedItems.Count != 1) return;
|
|
|
|
int removeItemNr = listViewEx.SelectedIndices[0];
|
|
|
|
MeasurementCorrection selected =
|
|
(MeasurementCorrection)listViewEx.SelectedItems[0].Tag;
|
|
if (selected.Id != 0) toBeRemoved.Add(selected);
|
|
MeterEntity.Corrections.RemoveAt(removeItemNr);
|
|
RefreshAll();
|
|
|
|
if (removeItemNr < listViewEx.Items.Count)
|
|
{
|
|
listViewEx.Focus();
|
|
listViewEx.Items[removeItemNr].Selected = true;
|
|
listViewEx.Items[removeItemNr].EnsureVisible();
|
|
}
|
|
else
|
|
{
|
|
parent.UpdateButtonStates(SharedButtons.SelectedItemPos.None);
|
|
}
|
|
}
|
|
|
|
public void MoveUpSelected()
|
|
{
|
|
}
|
|
|
|
public void MoveDownSelected()
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Event handler that indicates the selected item position
|
|
/// and updates 'Remove', 'Up' and 'Down' buttons in the parent dialog.
|
|
/// </summary>
|
|
private void listViewEx_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
if (listViewEx.SelectedItems.Count != 1)
|
|
{
|
|
parent.UpdateButtonStates(SharedButtons.SelectedItemPos.None);
|
|
}
|
|
else if (listViewEx.Items.Count == 1)
|
|
{
|
|
parent.UpdateButtonStates(SharedButtons.SelectedItemPos.FirstAndLast);
|
|
}
|
|
else if (listViewEx.SelectedIndices[0] == 0)
|
|
{
|
|
parent.UpdateButtonStates(SharedButtons.SelectedItemPos.First);
|
|
}
|
|
else if (listViewEx.SelectedIndices[0] == (listViewEx.Items.Count - 1))
|
|
{
|
|
parent.UpdateButtonStates(SharedButtons.SelectedItemPos.Last);
|
|
}
|
|
else
|
|
{
|
|
parent.UpdateButtonStates(SharedButtons.SelectedItemPos.Middle);
|
|
}
|
|
}
|
|
|
|
private void measuredTextBox_TextChanged(object sender, EventArgs e)
|
|
{
|
|
float measured;
|
|
if (float.TryParse(measuredTextBox.Text,
|
|
NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowExponent,
|
|
CultureInfo.CurrentCulture,
|
|
out measured) ||
|
|
float.TryParse(measuredTextBox.Text,
|
|
NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowExponent,
|
|
CultureInfo.InvariantCulture,
|
|
out measured))
|
|
{
|
|
double corrected = Config.Formulas.CorrectedValue(measured, meterEntity.Corrections);
|
|
correctedTextBox.Text = corrected.ToString();
|
|
}
|
|
}
|
|
|
|
public string GetWarningsBeforeSaving(ref Dictionary<string, string> renmInfo)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
public void UpdateRelatedItems(Dictionary<string, string> renameInfo)
|
|
{
|
|
}
|
|
}
|
|
}
|