/*=============================================================================
Project: SharpImage
Module: siFormIteration.cs
Language: C#
Author: Dan Mueller
Date: $Date: 2007-07-06 10:57:00 +1000 (Fri, 06 Jul 2007) $
Revision: $Revision: 2 $
Copyright (c) Queensland University of Technology (QUT) 2007.
All rights reserved.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=============================================================================*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using SharpImage.Rendering;
namespace SharpImage.Forms
{
public partial class siFormIteration : siFormTool
{
#region Instance Variables
//=====================================================================
//=====================================================================
#endregion
#region Construction and Disposal
//=====================================================================
///
/// Default constructor.
///
///
public siFormIteration() : base()
{
InitializeComponent();
// NOTE: There will be no-one listening for the State changed event
this.State = IterationStateEnum.Running;
// Make the buttons not keep focus
this.cmdPlay.GotFocus += new EventHandler(cmd_GotFocus);
this.cmdPause.GotFocus += new EventHandler(cmd_GotFocus);
this.cmdStop.GotFocus += new EventHandler(cmd_GotFocus);
}
//=====================================================================
#endregion
#region Properties
//=====================================================================
private IterationStateEnum m_State;
private string m_DisplayTemplate = string.Empty;
private string m_DisplayInfo = string.Empty;
private Dictionary m_DisplayVariables = new Dictionary();
///
/// Get/set the State of the form.
///
public IterationStateEnum State
{
get { return this.m_State; }
set
{
this.m_State = value;
this.SetButtonState(value);
this.RaiseStateChanged();
}
}
///
/// Get/set the display template.
///
public string DisplayTemplate
{
get { return this.m_DisplayTemplate; }
set { this.m_DisplayTemplate = value; }
}
///
/// Gets the number of iterations to perform between refreshing the label image.
///
public int IterationsBetweenRefresh
{
get { return (int)this.numericNumIterations.Value; }
}
//=====================================================================
#endregion
#region Events and Delegates
//=====================================================================
public delegate void StateChangedHandler(IterationStateEnum newState);
private StateChangedHandler m_EventStorage_StateChanged;
///
/// An event raised when the button State has changed.
///
public event StateChangedHandler StateChanged
{
add { this.m_EventStorage_StateChanged += value; }
remove { this.m_EventStorage_StateChanged -= value; }
}
///
/// Raise the StateChanged event.
///
protected void RaiseStateChanged()
{
// Fire event
if (this.m_EventStorage_StateChanged != null)
this.m_EventStorage_StateChanged(this.State);
}
//=====================================================================
#endregion
#region Public Methods
//=====================================================================
public void SetDisplayVariable(string name, object value)
{
this.SetDisplayVariable(name, value, string.Empty);
}
public void SetDisplayVariable(string name, object value, string formatting)
{
if (!this.m_DisplayVariables.ContainsKey(name))
// Ignore formatting by default
this.m_DisplayVariables.Add(name, value.ToString());
// Try to use formatting for ToString
if (value is Single)
this.m_DisplayVariables[name] = ((Single)value).ToString(formatting);
else if (value is Double)
this.m_DisplayVariables[name] = ((Double)value).ToString(formatting);
else if (value is Decimal)
this.m_DisplayVariables[name] = ((Decimal)value).ToString(formatting);
else if (value is Byte)
this.m_DisplayVariables[name] = ((Byte)value).ToString(formatting);
else if (value is Int16)
this.m_DisplayVariables[name] = ((Int16)value).ToString(formatting);
else if (value is Int32)
this.m_DisplayVariables[name] = ((Int32)value).ToString(formatting);
else if (value is Int64)
this.m_DisplayVariables[name] = ((Int64)value).ToString(formatting);
else
this.m_DisplayVariables[name] = value.ToString();
}
public void RefreshDisplayInfo()
{
this.m_DisplayInfo = this.m_DisplayTemplate;
foreach (string name in this.m_DisplayVariables.Keys)
this.m_DisplayInfo = this.m_DisplayInfo.Replace(name, this.m_DisplayVariables[name]);
this.txtInfo.Text = this.m_DisplayInfo;
this.txtInfo.Invalidate();
}
//=====================================================================
#endregion
#region Private Methods
//=====================================================================
///
/// Set the State of the Play, Pause and Stop buttons.
///
/// This method is thread-safe.
///
private void SetButtonState(IterationStateEnum newState)
{
// Make call thread-safe
if (this.InvokeRequired)
{
this.Invoke(new StateChangedHandler(this.SetButtonState), newState);
return;
}
// Set button State depending on State enum
switch (newState)
{
case IterationStateEnum.Running:
// Fall through
case IterationStateEnum.Resumed:
this.cmdPlay.Enabled = false;
this.cmdPause.Enabled = true;
this.cmdStop.Enabled = true;
break;
case IterationStateEnum.Paused:
this.cmdPlay.Enabled = true;
this.cmdPause.Enabled = false;
this.cmdStop.Enabled = true;
break;
case IterationStateEnum.Stopped:
this.cmdPlay.Enabled = false;
this.cmdPause.Enabled = false;
this.cmdStop.Enabled = false;
break;
} //end switch
}
private void cmdPlay_Click(object sender, EventArgs e)
{
if (this.State == IterationStateEnum.Paused)
this.State = IterationStateEnum.Resumed;
else
this.State = IterationStateEnum.Running;
}
private void cmdPause_Click(object sender, EventArgs e)
{
this.State = IterationStateEnum.Paused;
}
private void cmdStop_Click(object sender, EventArgs e)
{
this.State = IterationStateEnum.Stopped;
}
void cmd_GotFocus(object sender, EventArgs e)
{
this.panelBottom.Focus();
}
private void numericNumIterations_Validated(object sender, EventArgs e)
{
this.panelBottom.Focus();
}
//=====================================================================
#endregion
}
public enum IterationStateEnum
{
Running,
Paused,
Resumed,
Stopped
}
}