tbf/Results/Forms/BatchResultsDlg.cs

331 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using log4net;
using Common;
using Results.Resources;
using NHibernate;
using TracingDB.Entities;
namespace Results.Forms
{
public partial class BatchResultsDlg : Form
{
static readonly ILog log = LogManager.GetLogger(typeof(BatchResultsDlg));
///
/// Public members
///
public Results.BatchResults Results;
public IList<Results.WMeterRsltItemSpec> Items;
public MetersArrangement MetersArrangement;
public TestsArrangement TestsArrangement;
public int NrMetersInOneGroup;
public bool ShowDisabledPositions;
public int MinWidth;
public int MinHeight;
public int PopupResultsLeft;
public int PopupResultsTop;
public int PopupResultsWidth;
public int PopupResultsHeight;
public int[] RsltsClmnWidths;
///
/// Private members
///
IOneWMResultsCtrl[] wmRsltsCtrl; /// An array of controls with water meter results.
IOneWMResultsCtrl firstEnabledControl;
DateTime lastRedraw;
DateTime lastSizeChange;
int metersInOneGroup;
int nrGroups;
Timer timer;
public BatchResultsDlg()
{
InitializeComponent();
firstEnabledControl = null;
lastSizeChange = DateTime.Now;
nrGroups = 1;
metersInOneGroup = 1;
/// Create a timer with one second interval.
timer = new Timer();
timer.Interval = 1000; /// 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;
CreateControlsAndFillPanel(Results);
UpdateColumnWidths(RsltsClmnWidths);
UpdateResults(Results);
}
///
/// Handle double-click inside a water meter results control
///
void OnDoubleClick(object sender, Results.Forms.WaterMeterEventArgs args)
{
if (args.WMPosition > 0)
{
foreach (var wm in Results.Batch.WaterMeters)
{
if (!wm.Disabled && (wm.WMPosition == args.WMPosition))
{
ShowMoreWMResults(wm);
return;
}
}
}
MessageBox.Show(Strings.No_water_meter);
return;
}
void UpdateColumnWidths(int[] columnWidths)
{
foreach (var ctrl in wmRsltsCtrl) ctrl.Update(columnWidths);
}
void UpdateScrollpositions(int scrollStart)
{
/// TODO
}
/// <summary>
/// Re-draw the results. Apply new settings if they changed.
/// </summary>
void CreateControlsAndFillPanel(Results.BatchResults results)
{
if ((results == null) || (results.WMPositionsCount == 0))
{
/// There are no water meters at all
flowLayoutPanel.Controls.Clear();
wmRsltsCtrl = new OneWMResultsRowsCtrl[0];
firstEnabledControl = null;
return;
}
///
/// Determine water meter positions count
///
int displayedControlsCount = 0;
for (int i = 0; i < results.Batch.WaterMeters.Count; i++)
{
if ((!results.Batch.WaterMeters[i].Disabled) || ShowDisabledPositions)
{
displayedControlsCount++;
}
}
///
/// Calculate geometry
///
metersInOneGroup = Math.Max(1, Math.Min(displayedControlsCount, NrMetersInOneGroup));
nrGroups = Math.Max(1, (displayedControlsCount + metersInOneGroup - 1) / metersInOneGroup);
int oneWidth;
int oneHeight;
///
if (MetersArrangement == MetersArrangement.Horizontally)
{
flowLayoutPanel.FlowDirection = FlowDirection.LeftToRight;
oneWidth = Math.Max(MinWidth, flowLayoutPanel.Width / metersInOneGroup - 6);
oneHeight = Math.Max(MinHeight, flowLayoutPanel.Height / nrGroups - 6);
}
else
{
flowLayoutPanel.FlowDirection = FlowDirection.TopDown;
oneWidth = Math.Max(MinWidth, flowLayoutPanel.Width / nrGroups - 6);
oneHeight = Math.Max(MinHeight, flowLayoutPanel.Height / metersInOneGroup - 6);
}
///
/// Create controls and fill the FlowLayoutPanel
///
flowLayoutPanel.SuspendLayout();
flowLayoutPanel.Controls.Clear();
flowLayoutPanel.WrapContents = true;
flowLayoutPanel.AutoScroll = true;
wmRsltsCtrl = new IOneWMResultsCtrl[displayedControlsCount];
int ix = 0;
firstEnabledControl = null;
for (int i = 0; i < results.Batch.WaterMeters.Count; i++)
{
if (!results.Batch.WaterMeters[i].Disabled || ShowDisabledPositions)
{
/// Create one water meter position / control
wmRsltsCtrl[ix] = (TestsArrangement == TestsArrangement.Rows)
? new OneWMResultsRowsCtrl() as IOneWMResultsCtrl
: new OneWMResultsColumnsCtrl() as IOneWMResultsCtrl;
wmRsltsCtrl[ix].Width = oneWidth;
wmRsltsCtrl[ix].Height = oneHeight;
if (!results.Batch.WaterMeters[i].Disabled)
{
if ((firstEnabledControl == null) && !results.Batch.WaterMeters[i].Disabled)
{
firstEnabledControl = wmRsltsCtrl[ix];
}
//wmRsltsCtrl[ix].Update(results.WaterMeters[i].WMPosition);
wmRsltsCtrl[ix].Update(Items);
wmRsltsCtrl[ix].WMResultsClickedHandler += delegate(object s, Results.Forms.WaterMeterEventArgs args)
{
if (InvokeRequired)
{
Invoke(new EventHandler<Results.Forms.WaterMeterEventArgs>(OnDoubleClick), s, args);
}
else
{
OnDoubleClick(s, args);
}
};
}
flowLayoutPanel.Controls.Add(wmRsltsCtrl[ix] as UserControl);
ix++;
}
}
flowLayoutPanel.ResumeLayout();
lastRedraw = DateTime.Now;
}
void UpdateResults(Results.BatchResults results)
{
if (results != null && results.Batch != null && results.Batch.WaterMeters != null)
{
int ctrlIx = 0;
for (int wmNr0 = 0; wmNr0 < results.Batch.WaterMeters.Count; wmNr0++)
{
if (!results.Batch.WaterMeters[wmNr0].Disabled || ShowDisabledPositions)
{
if (ctrlIx < wmRsltsCtrl.Length) wmRsltsCtrl[ctrlIx++].Update(results.Batch.WaterMeters[wmNr0]);
}
}
}
}
void ShowMoreWMResults(Results.Entities.WaterMeter wm)
{
if ((TracingDB.DB.SessionFactory == null) || string.IsNullOrEmpty(wm.SerialNr))
{
new Results.Forms.MoreWMResultsDlg(wm, null).ShowDialog();
}
else
{
using (ISession session = TracingDB.DB.SessionFactory.OpenSession())
{
IList<ReferenceRecord> refRecords = session.QueryOver<ReferenceRecord>()
.Where(x => (x.Code == wm.SerialNr))
.OrderBy(x => x.TimeStamp).Desc
.List();
for (int i = 0; i < refRecords.Count; i++)
{
ReferenceRecord refR = refRecords[i];
if ((refR.Records != null) && (refR.Records.Count > 0))
{
/// Reference record has additional records/parts (i.e. it is PCB assembly workflow step)
///
foreach (var rec in refR.Records)
{
if (rec.Part.OraDBType.ToLower().Contains("flowtube"))
{
/// rec.Part is a flowtube
///
IList<ReferenceRecord> flowtubeRRs = session.QueryOver<ReferenceRecord>()
.Where(x => (x.Code == rec.Code))
.OrderBy(x => x.TimeStamp).Desc
.List();
foreach (var rr in flowtubeRRs) refRecords.Add(rr);
}
}
}
}
new Results.Forms.MoreWMResultsDlg(wm, refRecords).ShowDialog();
}
}
}
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 && !flowLayoutPanel.IsDisposed)
{
/// The last size change was more recent then the last redraw
DateTime now = DateTime.Now;
if (DateTime.Compare(lastSizeChange + new TimeSpan(0, 0, 1), now) < 0)
{
/// More then 1 second since the last panel size change
/// Calculate geometry
int oneWidth;
int oneHeight;
///
if (MetersArrangement == MetersArrangement.Horizontally)
{
flowLayoutPanel.FlowDirection = FlowDirection.LeftToRight;
oneWidth = Math.Max(MinWidth, flowLayoutPanel.Width / metersInOneGroup - 6);
oneHeight = Math.Max(MinHeight, flowLayoutPanel.Height / nrGroups - 6);
}
else
{
flowLayoutPanel.FlowDirection = FlowDirection.TopDown;
oneWidth = Math.Max(MinWidth, flowLayoutPanel.Width / nrGroups - 6);
oneHeight = Math.Max(MinHeight, flowLayoutPanel.Height / metersInOneGroup - 6);
}
if (wmRsltsCtrl != null)
{
/// Update size of controls
flowLayoutPanel.SuspendLayout();
///
foreach (var oneCtrl in wmRsltsCtrl)
{
oneCtrl.Width = oneWidth;
oneCtrl.Height = oneHeight;
}
///
flowLayoutPanel.ResumeLayout();
}
}
}
}
}
}