tbf/TestBenchFramework/BenchControl/Network/Camera/Display/Display.cs

154 lines
3.3 KiB
C#

///
/// Copyright (c) 2017 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using Config.Entities;
using TBF.BenchControl.GenericDevices;
using System.Windows.Forms;
namespace TBF.BenchControl.Network.Camera.Display
{
public class Display : ComponentBase, IOperation, ICameraDisplay
{
private static readonly ILog log = LogManager.GetLogger(typeof(Display));
public override string ToString()
{
return string.Format("{0}({1})", this.GetType().Namespace.Substring(17), Cfg.ToString(1));
}
public readonly DisplayCfg DisplayCfg;
///
public Display()
{
}
///
public Display(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
: base(cfg)
{
DisplayCfg = cfg as DisplayCfg;
log.Debug(this.ToString());
}
public bool[] SelectedImages { get { return selectedImages; } }
bool[] selectedImages;
public enum CurrentOp
{
None,
StaticImage,
LiveStream,
}
CurrentOp currentOp;
DialogResult result;
string title;
string[] imageFileNames;
DisplayForm displayForm;
///
delegate void DisplayFormDlgt(Display myRef);
///
void OpenStaticImageDisplay(Display myRef)
{
myRef.displayForm = new DisplayForm(imageFileNames, title);
displayForm.Show();
}
///
void OpenLiveStreamDisplay(Display myRef)
{
myRef.displayForm = new DisplayForm(imageFileNames, title);
displayForm.Show();
}
/// <returns>Reference to the operation</returns>
public IOperation ShowStaticImageOp(string[] imageFileNames, string title)
{
if (currentOp != CurrentOp.None) throw new Exception("Sequence error");
this.imageFileNames = imageFileNames;
this.title = title;
currentOp = CurrentOp.StaticImage;
return this;
}
/// <returns>Reference to the operation</returns>
public IOperation ShowLiveStreamOp(string[] imageFileNames, string title)
{
if (currentOp != CurrentOp.None) throw new Exception("Sequence error");
this.imageFileNames = imageFileNames;
this.title = title;
currentOp = CurrentOp.LiveStream;
return this;
}
/// <summary>Start this operation</summary>
public void Start()
{
result = DialogResult.None;
switch (currentOp)
{
case CurrentOp.StaticImage:
Program.MainWnd.Invoke(new DisplayFormDlgt(OpenStaticImageDisplay), this);
break;
case CurrentOp.LiveStream:
Program.MainWnd.Invoke(new DisplayFormDlgt(OpenLiveStreamDisplay), this);
break;
}
}
/// <summary>Run this operation</summary>
/// <returns>Event.None</returns>
public Event Run()
{
if (!displayForm.Completed)
{
return Event.ModelessFormIsOpen;
}
if (result == DialogResult.None)
{
/// Save the result (once)
selectedImages = displayForm.SelectedImages;
result = displayForm.Result;
displayForm = null;
}
if (result == DialogResult.OK) return Event.OK;
else if (result == DialogResult.Retry) return Event.Retry;
else if (result == DialogResult.Abort) return Event.Abort;
else return Event.None; /// Should never happen
}
/// <summary>Stop this operation</summary>
public void Stop()
{
if (displayForm != null)
{
displayForm.CloseForm(this, null);
displayForm = null;
}
currentOp = CurrentOp.None;
}
}
}