377 lines
14 KiB
C#
377 lines
14 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
using log4net;
|
|
using Config.Entities;
|
|
using Results.Resources;
|
|
|
|
namespace Results.Forms
|
|
{
|
|
public partial class BatchResultsDlg : Form
|
|
{
|
|
static readonly ILog log = LogManager.GetLogger(typeof(BatchResultsDlg));
|
|
|
|
DateTime lastRedraw;
|
|
DateTime lastSizeChange;
|
|
bool loaded;
|
|
|
|
///
|
|
/// Public members
|
|
///
|
|
public MetersArrangement MetersArrangement;
|
|
public TestsArrangement TestsArrangement;
|
|
public int NrMetersInOneGroup;
|
|
|
|
public IList<Results.WMeterRsltItemSpec> RsltItems_Screen_SingleWM;
|
|
public IList<Results.WMeterRsltItemSpec> RsltItems_Screen_CombinedWM;
|
|
public IList<Results.WMeterRsltItemSpec> RsltItems_Screen_HeatM;
|
|
public int[] RsltsClmnWidths { set { rsltsClmnWidths = value; } }
|
|
|
|
public int PopupResultsLeft;
|
|
public int PopupResultsTop;
|
|
public int PopupResultsWidth;
|
|
public int PopupResultsHeight;
|
|
|
|
public Results.BatchResults Results { set { Display(value); } }
|
|
|
|
///
|
|
/// Private members
|
|
///
|
|
Results.BatchResults results;
|
|
MetersKind metersKind;
|
|
|
|
ListView firstListView;
|
|
public int[] rsltsClmnWidths;
|
|
int rsltsClmnCount { get { return (rsltsClmnWidths != null) ? rsltsClmnWidths.Length : 0; } }
|
|
|
|
int lvWidth;
|
|
int lvHeight;
|
|
|
|
|
|
Timer timer;
|
|
|
|
|
|
public BatchResultsDlg()
|
|
{
|
|
InitializeComponent();
|
|
Text = Strings.Results;
|
|
|
|
lastSizeChange = DateTime.Now;
|
|
|
|
// Create a timer with a ten second interval.
|
|
timer = new Timer();
|
|
timer.Interval = 500; /// ms
|
|
timer.Tick += new EventHandler(OnTimer);
|
|
timer.Start();
|
|
}
|
|
|
|
private void BatchResultsDlg_Load(object sender, EventArgs e)
|
|
{
|
|
if (PopupResultsLeft != 0) Left = PopupResultsLeft;
|
|
if (PopupResultsTop != 0) Top = PopupResultsTop;
|
|
if (PopupResultsWidth != 0) Width = PopupResultsWidth;
|
|
if (PopupResultsHeight != 0) Height = PopupResultsHeight;
|
|
|
|
loaded = true;
|
|
if (results != null) Redraw();
|
|
}
|
|
|
|
public void Display(Results.BatchResults results)
|
|
{
|
|
this.results = results;
|
|
if (loaded) Redraw();
|
|
}
|
|
|
|
|
|
IList<Results.WMeterRsltItemSpec> GetRsltItems(MetersKind metersKind)
|
|
{
|
|
IList<Results.WMeterRsltItemSpec> srcItems;
|
|
switch (metersKind)
|
|
{
|
|
default:
|
|
case MetersKind.Single: srcItems = RsltItems_Screen_SingleWM; break;
|
|
case MetersKind.Combined: srcItems = RsltItems_Screen_CombinedWM; break;
|
|
case MetersKind.HeatMeter: srcItems = RsltItems_Screen_HeatM; break;
|
|
}
|
|
|
|
IList<Results.WMeterRsltItemSpec> copiedItems = new List<Results.WMeterRsltItemSpec>();
|
|
foreach (var it in srcItems) copiedItems.Add(it.Clone());
|
|
return copiedItems;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Re-draw the results. Apply new settings if they changed.
|
|
/// </summary>
|
|
public void Redraw()
|
|
{
|
|
lastRedraw = DateTime.Now;
|
|
|
|
this.SuspendLayout();
|
|
|
|
try
|
|
{
|
|
/// Determine how many water meters were enabled for result evaluation and printing
|
|
int enabledWMsCount = 0;
|
|
for (int i = 0; i < results.WMPositionsCount; i++)
|
|
{
|
|
if (results.WaterMeters[i] != null && !results.WaterMeters[i].Disabled)
|
|
{
|
|
enabledWMsCount++;
|
|
}
|
|
}
|
|
|
|
int metersInOneGroup = Math.Max(1, Math.Min(enabledWMsCount, NrMetersInOneGroup));
|
|
int nrGroups = Math.Max(1, (enabledWMsCount + metersInOneGroup - 1) / metersInOneGroup);
|
|
|
|
if (MetersArrangement == MetersArrangement.Horizontally)
|
|
{
|
|
flowLayoutPanel.FlowDirection = FlowDirection.LeftToRight;
|
|
lvWidth = flowLayoutPanel.Width / metersInOneGroup - 6;
|
|
lvHeight = flowLayoutPanel.Height / nrGroups - 6;
|
|
}
|
|
else
|
|
{
|
|
flowLayoutPanel.FlowDirection = FlowDirection.TopDown;
|
|
lvWidth = flowLayoutPanel.Width / nrGroups - 6;
|
|
lvHeight = flowLayoutPanel.Height / metersInOneGroup - 6;
|
|
}
|
|
flowLayoutPanel.Controls.Clear();
|
|
flowLayoutPanel.WrapContents = true;
|
|
flowLayoutPanel.AutoScroll = true;
|
|
|
|
bool first = true;
|
|
for (int i = 0; i < results.WMPositionsCount; i++)
|
|
{
|
|
if (results.WaterMeters[i] != null && !results.WaterMeters[i].Disabled)
|
|
{
|
|
ListView lview = (TestsArrangement == TestsArrangement.Rows)
|
|
? GetResultsTestsAreRows(results.WaterMeters[i], i + 1)
|
|
: GetResultsTestsAreColumns(results.WaterMeters[i], i + 1);
|
|
if (lview != null)
|
|
{
|
|
flowLayoutPanel.Controls.Add(lview);
|
|
if (first)
|
|
{
|
|
firstListView = lview;
|
|
first = false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
log.FatalFormat("Redrawing results failed : {0}", e.Message);
|
|
if (e.InnerException != null)
|
|
{
|
|
log.FatalFormat("InnerMessage : {0}", e.InnerException.Message);
|
|
}
|
|
log.FatalFormat("StackTrace : {0}{1}", Environment.NewLine, e.StackTrace);
|
|
}
|
|
|
|
this.ResumeLayout(false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get an empty ListView control with appropriate parameters.
|
|
/// </summary>
|
|
/// <returns>ListView control</returns>
|
|
ListView GetListView()
|
|
{
|
|
ListView lview = new ListView();
|
|
lview.AllowColumnReorder = false;
|
|
lview.Dock = System.Windows.Forms.DockStyle.None;
|
|
lview.FullRowSelect = true;
|
|
lview.GridLines = true;
|
|
lview.Location = new System.Drawing.Point(0, 0);
|
|
lview.Size = new System.Drawing.Size(lvWidth, lvHeight);
|
|
lview.TabIndex = 0;
|
|
lview.UseCompatibleStateImageBehavior = false;
|
|
lview.View = System.Windows.Forms.View.Details;
|
|
return lview;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Get decorated test names for a specified watermeter
|
|
/// </summary>
|
|
/// <param name="wMtr">Watermeter results entity</param>
|
|
/// <returns>A list of strings</returns>
|
|
IList<string> GetAllDecoratedTestNames(Results.Entities.WaterMeter wMtr)
|
|
{
|
|
IList<string> testNames = new List<string>();
|
|
|
|
foreach (var mtr in wMtr.MeterTestRslts)
|
|
{
|
|
if (mtr.IsPilotRslt() && mtr.TestDone && (mtr.Publish() != Config.Entities.Publish.Never)
|
|
&& (mtr.Publish() != Config.Entities.Publish.Internal))
|
|
{
|
|
testNames.Add(mtr.Name());
|
|
}
|
|
}
|
|
|
|
return testNames;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Get a ListView control filled with results of the meter 'mtr'
|
|
/// </summary>
|
|
/// <param name="mtr">Water meter number (1-based): 1 .. Config.Data.WMsCount</param>
|
|
/// <returns>ListView object</returns>
|
|
ListView GetResultsTestsAreRows(Results.Entities.WaterMeter wMtr, int printedWMNr)
|
|
{
|
|
metersKind = wMtr.Compound() ? MetersKind.Combined : (wMtr.HeatMeter() ? MetersKind.HeatMeter : MetersKind.Single);
|
|
|
|
ListView lview = GetListView();
|
|
|
|
IList<Results.WMeterRsltItemSpec> items = GetRsltItems(metersKind);
|
|
|
|
if (items == null || items.Count == 0) return lview;
|
|
|
|
// Header
|
|
lview.Columns.Add(printedWMNr.ToString(), (rsltsClmnCount > 0) ? rsltsClmnWidths[0] : 40);
|
|
int col = 1;
|
|
foreach (var item in items)
|
|
{
|
|
lview.Columns.Add(item.Caption, (rsltsClmnCount > col) ? rsltsClmnWidths[col] : 70);
|
|
col++;
|
|
}
|
|
|
|
|
|
IList<string> testNames = GetAllDecoratedTestNames(wMtr);
|
|
int ix = 0;
|
|
foreach (var mtr in wMtr.MeterTestRslts)
|
|
{
|
|
if (mtr != null && mtr.IsPilotRslt() && mtr.TestDone && mtr.Publish() != Config.Entities.Publish.Never
|
|
&& mtr.Publish() != Config.Entities.Publish.Internal)
|
|
{
|
|
ListViewItem lvi = new ListViewItem(testNames[ix++]);
|
|
|
|
for (int i = 0; i < items.Count; i++)
|
|
{
|
|
string str = items[i].Print(wMtr, mtr.Name());
|
|
|
|
/// Extract and use color information
|
|
string[] texts = str.Split(new char[] { '|' });
|
|
if (texts.Length == 1)
|
|
{
|
|
lvi.SubItems.Add(str);
|
|
}
|
|
else if (texts.Length == 2)
|
|
{
|
|
Color color = texts[1].Equals("Green") ? Color.Green : (texts[1].Equals("Red") ? Color.Red : Color.White);
|
|
lvi.UseItemStyleForSubItems = false;
|
|
lvi.SubItems.Add(texts[0]);
|
|
lvi.SubItems[lvi.SubItems.Count - 1].BackColor = color;
|
|
}
|
|
else
|
|
{
|
|
lvi.SubItems.Add(string.Empty);
|
|
}
|
|
}
|
|
|
|
lview.Items.Add(lvi);
|
|
}
|
|
}
|
|
|
|
return lview;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Get a ListView control filled with results of the meter 'mtr'
|
|
/// </summary>
|
|
/// <param name="mtr">Water meter number (1-based): 1 .. Config.Data.WMsCount</param>
|
|
/// <returns>ListView object</returns>
|
|
ListView GetResultsTestsAreColumns(Results.Entities.WaterMeter wMtr, int printedWMNr)
|
|
{
|
|
metersKind = wMtr.Compound() ? MetersKind.Combined : (wMtr.HeatMeter() ? MetersKind.HeatMeter : MetersKind.Single);
|
|
|
|
ListView lview = GetListView();
|
|
|
|
IList<Results.WMeterRsltItemSpec> items = GetRsltItems(metersKind);
|
|
|
|
if (items == null || items.Count == 0) return lview;
|
|
|
|
// Header
|
|
lview.Columns.Add(printedWMNr.ToString(), (rsltsClmnCount > 0) ? rsltsClmnWidths[0] : 40);
|
|
int i = 1;
|
|
foreach (var str in GetAllDecoratedTestNames(wMtr))
|
|
{
|
|
lview.Columns.Add(str, (rsltsClmnCount > i) ? rsltsClmnWidths[i] : 70);
|
|
i++;
|
|
}
|
|
|
|
foreach (var item in items)
|
|
{
|
|
ListViewItem lvi = new ListViewItem(item.Caption);
|
|
|
|
foreach (var mtr in wMtr.MeterTestRslts)
|
|
{
|
|
if (mtr != null && mtr.IsPilotRslt() && mtr.TestDone && mtr.Publish() != Config.Entities.Publish.Never &&
|
|
mtr.Publish() != Config.Entities.Publish.Internal)
|
|
{
|
|
string str = item.Print(wMtr, mtr.Name());
|
|
|
|
string[] texts = str.Split(new char[] { '|' });
|
|
if (texts.Length == 1)
|
|
{
|
|
lvi.SubItems.Add(str);
|
|
}
|
|
else if (texts.Length == 2)
|
|
{
|
|
Color color = texts[1].Equals("Green") ? Color.Green : (texts[1].Equals("Red") ? Color.Red : Color.White);
|
|
lvi.UseItemStyleForSubItems = false;
|
|
lvi.SubItems.Add(texts[0]);
|
|
lvi.SubItems[lvi.SubItems.Count - 1].BackColor = color;
|
|
}
|
|
else
|
|
{
|
|
lvi.SubItems.Add(string.Empty);
|
|
}
|
|
}
|
|
}
|
|
|
|
lview.Items.Add(lvi);
|
|
}
|
|
|
|
return lview;
|
|
}
|
|
|
|
private void BatchResultsDlg_FormClosing(object sender, FormClosingEventArgs e)
|
|
{
|
|
PopupResultsLeft = Left;
|
|
PopupResultsTop = Top;
|
|
PopupResultsWidth = Width;
|
|
PopupResultsHeight = Height;
|
|
}
|
|
|
|
private void flowLayoutPanel_SizeChanged(object sender, EventArgs e)
|
|
{
|
|
lastSizeChange = DateTime.Now;
|
|
}
|
|
|
|
private void OnTimer(object source, EventArgs e)
|
|
{
|
|
if (DateTime.Compare(lastSizeChange, lastRedraw) > 0)
|
|
{
|
|
/// The last size change was later then the last redraw
|
|
|
|
DateTime now = DateTime.Now;
|
|
if (DateTime.Compare(lastSizeChange + new TimeSpan(0, 0, 1), now) < 0)
|
|
{
|
|
/// More then 1s since the last size change
|
|
Redraw();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|