/*=============================================================================
Project: SharpImage
Module: siFormVariableSlider.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 SharpImage.Rendering;
namespace SharpImage.Forms
{
public partial class siFormVariableSlider : siFormTool
{
#region siSliderVariable Struct
//=====================================================================
private struct siSliderVariable
{
private string m_Name;
private double m_Default;
private double m_Minimum;
private double m_Maximum;
private double m_Increment;
///
/// Default constructor.
///
///
///
///
///
///
public siSliderVariable(string name, double defaultvalue, double min, double max, double inc)
{
m_Name = name;
m_Default = defaultvalue;
m_Minimum = min;
m_Maximum = max;
m_Increment = inc;
}
///
/// Gets/sets the name of the slider variable.
///
public string Name
{
get { return this.m_Name; }
set { this.m_Name = value; }
}
///
/// Gets/sets the default value of the slider variable.
///
public double Default
{
get { return this.m_Default; }
set { this.m_Default = value; }
}
///
/// Gets/sets the minimum value of the slider variable.
///
public double Minimum
{
get { return this.m_Minimum; }
set { this.m_Minimum = value; }
}
///
/// Gets/sets the maximum value of the slider variable.
///
public double Maximum
{
get { return this.m_Maximum; }
set { this.m_Maximum = value; }
}
///
/// Gets/sets the increment of the slider variable.
///
public double Increment
{
get { return this.m_Increment; }
set { this.m_Increment = value; }
}
}
//=====================================================================
#endregion
#region Construction and Disposal
//=====================================================================
///
/// Default constructor
///
/// The input to apply the variables to.
public siFormVariableSlider(itk.itkImageBase input)
{
// Init component
InitializeComponent();
// Set member info
this.Input = input;
// Hide all the scrollbars and labels
this.SetNumberOfVariables(0);
}
//=====================================================================
#endregion
#region Properties
//=====================================================================
private bool IgnoreScrollEvents = false;
private itk.itkImageBase Input;
private Dictionary m_CurrentValues = new Dictionary();
private Dictionary m_VariableInfo = new Dictionary();
private string m_ValueStringFormat = "000.00";
///
/// Gets the current value of the variable (accessed by name).
///
public double this[string name]
{
get { return this.m_CurrentValues[name]; }
}
///
/// Gets the current value of the variable (accessed by name).
///
public Dictionary CurrentValues
{
get { return this.m_CurrentValues; }
}
///
/// Gets the default value of the variable (accessed by name).
///
private Dictionary VariableInfo
{
get { return this.m_VariableInfo; }
}
///
/// Gets the number of variables added to this form.
///
public int NumberOfVariables
{
get { return this.VariableInfo.Count; }
}
///
/// Gets if the function should be applied in-place or should
/// create a new renderer.
///
public bool ApplyInPlace
{
get { return this.radioInPlace.Checked; }
}
///
/// Get/set the formatting string for the variable values.
/// Eg. "000.00" or "000" or "". Default = "000.00".
///
public string ValueStringFormat
{
get { return this.m_ValueStringFormat; }
set { this.m_ValueStringFormat = value; }
}
//=====================================================================
#endregion
#region Events and Delegates
//=====================================================================
public delegate void VariableChangedHandler(string name, Dictionary values);
private VariableChangedHandler m_EventStorage_VariableChanged;
///
/// An event raised when a variable value is changed.
///
public event VariableChangedHandler VariableChanged
{
add { this.m_EventStorage_VariableChanged += value; }
remove { this.m_EventStorage_VariableChanged -= value; }
}
///
/// Raise the VariableChanged event using this.CurrentValues.
///
/// The name of variable which was last changed
protected void RaiseVariableChanged(string name)
{
// Fire event
if (this.m_EventStorage_VariableChanged != null)
{
this.m_EventStorage_VariableChanged(name, this.CurrentValues);
this.UpdateFormFromCurrentVariables();
}
}
//=====================================================================
#endregion
#region Public Methods
//=====================================================================
///
/// Add a variable to the form.
///
///
///
///
///
///
public void AddVariable(string name, double defaultValue, double min, double max, double inc)
{
siSliderVariable var = new siSliderVariable(name, defaultValue, min, max, inc);
this.VariableInfo.Add(name, var);
this.CurrentValues.Add(name, var.Default);
}
///
/// Initialise the form. This method assumes that the variables have been added and
/// the Maximum, Minimum, Increment properties configured.
///
public void Initialise()
{
// Signal we are ignoring scroll events
this.IgnoreScrollEvents = true;
// Show/hide scrollbars and lables
this.SetNumberOfVariables(this.VariableInfo.Count);
// Setup Minimum, Maximum, Increment
int index = 1;
foreach (siSliderVariable var in this.VariableInfo.Values)
{
// Set minimum and maximum
HScrollBar scroll = this.GetVariableScrollBar(index);
scroll.Minimum = 0;
scroll.Maximum = (int)Math.Ceiling((var.Maximum - var.Minimum) / var.Increment);
// Set default value
scroll.Value = (int)Math.Ceiling((var.Default - var.Minimum) / var.Increment);
// Set tag
scroll.Tag = var.Name;
// Set default label
Label label = this.GetVariableLabel(index);
label.Text = var.Name + ": " + var.Default.ToString(this.ValueStringFormat);
label.Invalidate();
label.Refresh();
// Increment index
index++;
}
// Signal we are ignoring scroll events
this.IgnoreScrollEvents = false;
}
///
/// Reset the values to their default settings.
///
public void Reset()
{
// Signal we are ignoring scroll events
this.IgnoreScrollEvents = true;
int index = 1;
foreach (siSliderVariable var in this.VariableInfo.Values)
{
// Update current value
HScrollBar scroll = this.GetVariableScrollBar(index);
scroll.Value = (int)((var.Default - var.Minimum) / var.Increment);
this.CurrentValues[var.Name] = var.Default;
// Update label
Label label = this.GetVariableLabel(index);
label.Text = var.Name + ": " + var.Default.ToString(this.ValueStringFormat);
label.Invalidate();
label.Refresh();
// Increment index
index++;
}
// Raise the event
this.RaiseVariableChanged(this.CurrentValues.Keys.GetEnumerator().Current);
// Signal we are ignoring scroll events
this.IgnoreScrollEvents = false;
}
public void OnRendererClosed(siRenderer renderer, EventArgs e)
{
// Close this form
this.Close();
}
//=====================================================================
#endregion
#region Private Methods
//=====================================================================
private void cmdReset_Click(object sender, EventArgs e)
{
this.Reset();
}
private void scrollVariable_ValueChanged(object sender, EventArgs e)
{
// Check if we are ignoring scroll events
if (this.IgnoreScrollEvents)
return;
// Update current variables
this.UpdateCurrentVariablesFromForm();
// Raise the event
this.RaiseVariableChanged((string)(sender as HScrollBar).Tag);
}
private void UpdateCurrentVariablesFromForm()
{
int index = 1;
foreach (siSliderVariable var in this.VariableInfo.Values)
{
// Update current value
HScrollBar scroll = this.GetVariableScrollBar(index);
double newValue = ((double)scroll.Value * var.Increment) + var.Minimum;
this.CurrentValues[var.Name] = newValue;
// Update label
Label label = this.GetVariableLabel(index);
label.Text = var.Name + ": " + newValue.ToString(this.ValueStringFormat);
label.Invalidate();
label.Refresh();
// Increment index
index++;
}
}
private void UpdateFormFromCurrentVariables()
{
int index = 1;
foreach (siSliderVariable var in this.VariableInfo.Values)
{
// Update current value
HScrollBar scroll = this.GetVariableScrollBar(index);
scroll.Value = (int)((this.CurrentValues[var.Name] - var.Minimum) / var.Increment);
// Update label
Label label = this.GetVariableLabel(index);
label.Text = var.Name + ": " + this.CurrentValues[var.Name].ToString(this.ValueStringFormat);
label.Invalidate();
label.Refresh();
// Increment index
index++;
}
}
private void scrollVariable_Scroll(object sender, ScrollEventArgs e)
{
}
private HScrollBar GetVariableScrollBar(int index)
{
switch (index)
{
case 1:
return this.scrollVariable1;
case 2:
return this.scrollVariable2;
case 3:
return this.scrollVariable3;
case 4:
return this.scrollVariable4;
case 5:
return this.scrollVariable5;
case 6:
return this.scrollVariable6;
case 7:
return this.scrollVariable7;
case 8:
return this.scrollVariable8;
default:
throw new ArgumentOutOfRangeException("Index", index, "The total number of variables supported by siFormVariableSlider is 8.");
}
}
private Label GetVariableLabel(int index)
{
switch (index)
{
case 1:
return this.lblVariable1;
case 2:
return this.lblVariable2;
case 3:
return this.lblVariable3;
case 4:
return this.lblVariable4;
case 5:
return this.lblVariable5;
case 6:
return this.lblVariable6;
case 7:
return this.lblVariable7;
case 8:
return this.lblVariable8;
default:
throw new ArgumentOutOfRangeException("Index", index, "The total number of variables supported by siFormVariableSlider is 8.");
}
}
///
/// Sets the number of sliders and labels shown on the GUI.
///
///
private void SetNumberOfVariables(int count)
{
switch (count)
{
case 0:
#region Hide/show Scrollbars and Labels
this.scrollVariable1.Visible = false;
this.scrollVariable2.Visible = false;
this.scrollVariable3.Visible = false;
this.scrollVariable4.Visible = false;
this.scrollVariable5.Visible = false;
this.scrollVariable6.Visible = false;
this.scrollVariable7.Visible = false;
this.scrollVariable8.Visible = false;
this.lblVariable1.Visible = false;
this.lblVariable2.Visible = false;
this.lblVariable3.Visible = false;
this.lblVariable4.Visible = false;
this.lblVariable5.Visible = false;
this.lblVariable6.Visible = false;
this.lblVariable7.Visible = false;
this.lblVariable8.Visible = false;
#endregion
break;
case 1:
#region Hide/show Scrollbars and Labels
this.scrollVariable1.Visible = true;
this.scrollVariable2.Visible = false;
this.scrollVariable3.Visible = false;
this.scrollVariable4.Visible = false;
this.scrollVariable5.Visible = false;
this.scrollVariable6.Visible = false;
this.scrollVariable7.Visible = false;
this.scrollVariable8.Visible = false;
this.lblVariable1.Visible = true;
this.lblVariable2.Visible = false;
this.lblVariable3.Visible = false;
this.lblVariable4.Visible = false;
this.lblVariable5.Visible = false;
this.lblVariable6.Visible = false;
this.lblVariable7.Visible = false;
this.lblVariable8.Visible = false;
#endregion
break;
case 2:
#region Hide/show Scrollbars and Labels
this.scrollVariable1.Visible = true;
this.scrollVariable2.Visible = true;
this.scrollVariable3.Visible = false;
this.scrollVariable4.Visible = false;
this.scrollVariable5.Visible = false;
this.scrollVariable6.Visible = false;
this.scrollVariable7.Visible = false;
this.scrollVariable8.Visible = false;
this.lblVariable1.Visible = true;
this.lblVariable2.Visible = true;
this.lblVariable3.Visible = false;
this.lblVariable4.Visible = false;
this.lblVariable5.Visible = false;
this.lblVariable6.Visible = false;
this.lblVariable7.Visible = false;
this.lblVariable8.Visible = false;
#endregion
break;
case 3:
#region Hide/show Scrollbars and Labels
this.scrollVariable1.Visible = true;
this.scrollVariable2.Visible = true;
this.scrollVariable3.Visible = true;
this.scrollVariable4.Visible = false;
this.scrollVariable5.Visible = false;
this.scrollVariable6.Visible = false;
this.scrollVariable7.Visible = false;
this.scrollVariable8.Visible = false;
this.lblVariable1.Visible = true;
this.lblVariable2.Visible = true;
this.lblVariable3.Visible = true;
this.lblVariable4.Visible = false;
this.lblVariable5.Visible = false;
this.lblVariable6.Visible = false;
this.lblVariable7.Visible = false;
this.lblVariable8.Visible = false;
#endregion
break;
case 4:
#region Hide/show Scrollbars and Labels
this.scrollVariable1.Visible = true;
this.scrollVariable2.Visible = true;
this.scrollVariable3.Visible = true;
this.scrollVariable4.Visible = true;
this.scrollVariable5.Visible = false;
this.scrollVariable6.Visible = false;
this.scrollVariable7.Visible = false;
this.scrollVariable8.Visible = false;
this.lblVariable1.Visible = true;
this.lblVariable2.Visible = true;
this.lblVariable3.Visible = true;
this.lblVariable4.Visible = true;
this.lblVariable5.Visible = false;
this.lblVariable6.Visible = false;
this.lblVariable7.Visible = false;
this.lblVariable8.Visible = false;
#endregion
break;
case 5:
#region Hide/show Scrollbars and Labels
this.scrollVariable1.Visible = true;
this.scrollVariable2.Visible = true;
this.scrollVariable3.Visible = true;
this.scrollVariable4.Visible = true;
this.scrollVariable5.Visible = true;
this.scrollVariable6.Visible = false;
this.scrollVariable7.Visible = false;
this.scrollVariable8.Visible = false;
this.lblVariable1.Visible = true;
this.lblVariable2.Visible = true;
this.lblVariable3.Visible = true;
this.lblVariable4.Visible = true;
this.lblVariable5.Visible = true;
this.lblVariable6.Visible = false;
this.lblVariable7.Visible = false;
this.lblVariable8.Visible = false;
#endregion
break;
case 6:
#region Hide/show Scrollbars and Labels
this.scrollVariable1.Visible = true;
this.scrollVariable2.Visible = true;
this.scrollVariable3.Visible = true;
this.scrollVariable4.Visible = true;
this.scrollVariable5.Visible = true;
this.scrollVariable6.Visible = true;
this.scrollVariable7.Visible = false;
this.scrollVariable8.Visible = false;
this.lblVariable1.Visible = true;
this.lblVariable2.Visible = true;
this.lblVariable3.Visible = true;
this.lblVariable4.Visible = true;
this.lblVariable5.Visible = true;
this.lblVariable6.Visible = true;
this.lblVariable7.Visible = false;
this.lblVariable8.Visible = false;
#endregion
break;
case 7:
#region Hide/show Scrollbars and Labels
this.scrollVariable1.Visible = true;
this.scrollVariable2.Visible = true;
this.scrollVariable3.Visible = true;
this.scrollVariable4.Visible = true;
this.scrollVariable5.Visible = true;
this.scrollVariable6.Visible = true;
this.scrollVariable7.Visible = true;
this.scrollVariable8.Visible = false;
this.lblVariable1.Visible = true;
this.lblVariable2.Visible = true;
this.lblVariable3.Visible = true;
this.lblVariable4.Visible = true;
this.lblVariable5.Visible = true;
this.lblVariable6.Visible = true;
this.lblVariable7.Visible = true;
this.lblVariable8.Visible = false;
#endregion
break;
case 8:
#region Hide/show Scrollbars and Labels
this.scrollVariable1.Visible = true;
this.scrollVariable2.Visible = true;
this.scrollVariable3.Visible = true;
this.scrollVariable4.Visible = true;
this.scrollVariable5.Visible = true;
this.scrollVariable6.Visible = true;
this.scrollVariable7.Visible = true;
this.scrollVariable8.Visible = true;
this.lblVariable1.Visible = true;
this.lblVariable2.Visible = true;
this.lblVariable3.Visible = true;
this.lblVariable4.Visible = true;
this.lblVariable5.Visible = true;
this.lblVariable6.Visible = true;
this.lblVariable7.Visible = true;
this.lblVariable8.Visible = true;
#endregion
break;
default:
throw new ArgumentOutOfRangeException("NumberOfVariables", count, "The total number of variables supported by this form is 8.");
}
}
//=====================================================================
#endregion
}
}