/*=============================================================================
Project: SharpImage
Module: siFormTransferFunctionEditor.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.Diagnostics;
using SharpImage.Main;
using SharpImage.Rendering;
using SharpImage.Properties;
namespace SharpImage.Forms
{
public partial class siFormTransferFunctionEditor : siFormTool
{
#region Instance Variables
//=====================================================================
private bool m_WatchForComboSelectedIndexChanged = true;
//=====================================================================
#endregion
#region Construction and Disposal
//=====================================================================
///
/// Default constructor.
///
/// The transfer function to edit.
public siFormTransferFunctionEditor(siTransferFunction tf)
{
// Init components
InitializeComponent();
// Init member variables
this.TransferFunction = tf;
// Set container min size
this.containerMain.Panel1MinSize = tf.Size1 + 2;
// Watch for combo events
this.comboParts.SelectedIndexChanged += new EventHandler(ComboParts_SelectedIndexChanged);
// Watch for the panel events
this.panelPaint.Paint += new PaintEventHandler(OnPanelPaint);
this.panelPaint.MouseDown += new MouseEventHandler(OnPanelMouseDown);
this.panelPaint.MouseUp += new MouseEventHandler(OnPanelMouseUp);
this.panelPaint.MouseMove += new MouseEventHandler(OnPanelMouseMove);
// Add menu items
ToolStripItem menuOpen = this.menuContext.Items.Add("Open From XML File", Resources.Open);
menuOpen.Click += new EventHandler(menuOpen_Click);
ToolStripItem menuSave = this.menuContext.Items.Add("Save To XML File", Resources.Save);
menuSave.Click += new EventHandler(menuSave_Click);
ToolStripItem menuSep1 = this.menuContext.Items.Add("-");
ToolStripItem menuClipboard = this.menuContext.Items.Add("Copy To Clipboard", Resources.Copy);
menuClipboard.Click += new EventHandler(menuClipboard_Click);
ToolStripItem menuSep2 = this.menuContext.Items.Add("-");
ToolStripMenuItem menuRectangleMain = this.menuContext.Items.Add("Add Rectangle") as ToolStripMenuItem;
ToolStripMenuItem menuOvalMain = this.menuContext.Items.Add("Add Oval") as ToolStripMenuItem;
ToolStripMenuItem menuGradientMain = this.menuContext.Items.Add("Add Gradient") as ToolStripMenuItem;
ToolStripMenuItem menuLevoyMain = this.menuContext.Items.Add("Add Levoy") as ToolStripMenuItem;
foreach (String layer in this.TransferFunction.Layers)
{
if (String.Compare(layer, siTransferFunction.UNUSED_LAYER_KEY, true) != 0)
{
ToolStripItem menuRectangle = menuRectangleMain.DropDownItems.Add("To " + layer + " layer");
menuRectangle.Click += new EventHandler(menuRectangle_Click);
menuRectangle.Tag = layer;
}
}
foreach (String layer in this.TransferFunction.Layers)
{
if (String.Compare(layer, siTransferFunction.UNUSED_LAYER_KEY, true) != 0)
{
ToolStripItem menuOval = menuOvalMain.DropDownItems.Add("To " + layer + " layer");
menuOval.Click += new EventHandler(menuOval_Click);
menuOval.Tag = layer;
}
}
foreach (String layer in this.TransferFunction.Layers)
{
if (String.Compare(layer, siTransferFunction.UNUSED_LAYER_KEY, true) != 0)
{
ToolStripItem menuGradient = menuGradientMain.DropDownItems.Add("To " + layer + " layer");
menuGradient.Click += new EventHandler(menuGradient_Click);
menuGradient.Tag = layer;
}
}
foreach (String layer in this.TransferFunction.Layers)
{
if (String.Compare(layer, siTransferFunction.UNUSED_LAYER_KEY, true) != 0)
{
ToolStripItem menuLevoy = menuLevoyMain.DropDownItems.Add("To " + layer + " layer");
menuLevoy.Click += new EventHandler(menuLevoy_Click);
menuLevoy.Tag = layer;
}
}
ToolStripItem menuSep3 = this.menuContext.Items.Add("-");
ToolStripItem menuRemove = this.menuContext.Items.Add("Remove Selected Part");
menuRemove.Click += new EventHandler(menuRemove_Click);
ToolStripItem menuClear = this.menuContext.Items.Add("Clear All Parts", Resources.Delete);
menuClear.Click += new EventHandler(menuClear_Click);
}
//=====================================================================
#endregion
#region Properties
//=====================================================================
#region TransferFunction
//=====================================================================
private siTransferFunction m_TransferFunction = null;
///
/// Gets the transfer function this form is editing.
///
public siTransferFunction TransferFunction
{
get { return this.m_TransferFunction; }
set
{
this.m_TransferFunction = value;
this.m_TransferFunction.Modified += new siTransferFunction.siTransferFunctionModifiedHandler(OnTransferFunctionModified);
this.m_TransferFunction.PartSelected += new siActor.siActorHandler(TransferFunction_PartSelected);
}
}
//=====================================================================
#endregion
//=====================================================================
#endregion
#region Methods
//=====================================================================
public void OnRendererClosed(siRenderer renderer, EventArgs e)
{
// Close this form
this.Close();
}
private void OnTransferFunctionModified(siTransferFunction sender, bool partial)
{
if (!partial)
{
this.panelPaint.Repaint();
this.gridSelectedPart.Refresh();
}
}
private void TransferFunction_PartSelected(siActor sender)
{
// Update the PropertyGrid
this.gridSelectedPart.SelectedObject = sender;
// Update the list
this.comboParts.Items.Clear();
foreach (siTransferFunctionPart part in this.TransferFunction.Parts)
this.comboParts.Items.Add(part);
// Select the current item from the list
m_WatchForComboSelectedIndexChanged = false;
this.comboParts.SelectedItem = sender;
m_WatchForComboSelectedIndexChanged = true;
}
void ComboParts_SelectedIndexChanged(object sender, EventArgs e)
{
if (m_WatchForComboSelectedIndexChanged)
this.TransferFunction.SendPartToFront(this.comboParts.SelectedItem as siTransferFunctionPart);
}
private void OnPanelPaint(object sender, PaintEventArgs e)
{
siTransferFunctionPaintEventArgs etf = new siTransferFunctionPaintEventArgs(e, siTransferFunctionPaintMode.Screen);
this.TransferFunction.Paint(etf);
this.panelPaint.Cursor = this.TransferFunction.Metadata["Cursor"] as Cursor;
}
void OnPanelMouseDown(object sender, MouseEventArgs e)
{
this.TransferFunction.MouseDown(e);
if (e.Button == MouseButtons.Right)
this.menuContext.Show(this.panelPaint, e.Location);
}
void OnPanelMouseUp(object sender, MouseEventArgs e)
{
this.TransferFunction.MouseUp(e);
}
void OnPanelMouseMove(object sender, MouseEventArgs e)
{
this.TransferFunction.MouseMove(e);
}
void menuRectangle_Click(object sender, EventArgs e)
{
siTransferFunctionPartRectangle part = new siTransferFunctionPartRectangle(this.TransferFunction, (sender as ToolStripItem).Tag as String);
this.TransferFunction.AddPartToFront(part);
}
void menuOval_Click(object sender, EventArgs e)
{
siTransferFunctionPartOval part = new siTransferFunctionPartOval(this.TransferFunction, (sender as ToolStripItem).Tag as String);
this.TransferFunction.AddPartToFront(part);
}
void menuGradient_Click(object sender, EventArgs e)
{
siTransferFunctionPartGradient part = new siTransferFunctionPartGradient(this.TransferFunction, (sender as ToolStripItem).Tag as String);
this.TransferFunction.AddPartToFront(part);
}
void menuLevoy_Click(object sender, EventArgs e)
{
siTransferFunctionPartLevoy part = new siTransferFunctionPartLevoy(this.TransferFunction, (sender as ToolStripItem).Tag as String);
this.TransferFunction.AddPartToFront(part);
}
void menuRemove_Click(object sender, EventArgs e)
{
this.TransferFunction.RemovePartAtFront();
this.gridSelectedPart.SelectedObject = null;
}
void menuClear_Click(object sender, EventArgs e)
{
this.TransferFunction.ClearAllParts();
this.gridSelectedPart.SelectedObject = null;
}
void menuOpen_Click(object sender, EventArgs e)
{
// Show the open file dialog
OpenFileDialog ofd = new OpenFileDialog();
ofd.CheckFileExists = true;
ofd.CheckPathExists = true;
ofd.DefaultExt = "xml";
ofd.Filter = "Transfer Function XML files (*.xml)|*.xml";
ofd.Multiselect = false;
ofd.RestoreDirectory = true;
ofd.ShowHelp = false;
ofd.Title = "Please select the Transfer Function XML file to open...";
if (ofd.ShowDialog(this) == DialogResult.OK)
{
try
{
siTransferFunction functionFromXmlFile = siTransferFunction.FromXmlFile(ofd.FileName);
if (functionFromXmlFile != null)
{
// Clear the current function
this.TransferFunction.ClearAllParts();
// Add all the parts from the function read in from the XML file
foreach (siTransferFunctionPart part in functionFromXmlFile.Parts)
this.TransferFunction.AddPartToBack(part);
// Dispose of the transfer function read
functionFromXmlFile.Dispose();
functionFromXmlFile = null;
// Update the form
this.gridSelectedPart.SelectedObject = null;
this.panelPaint.Repaint();
}
}
catch (Exception ex)
{
string message = "An error occured while reading the Transfer Function XML file.\r\n\r\n" + ex.ToString();
string caption = "Unable to open Transfer Function from XML file";
MessageBox.Show(this, message, caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
void menuSave_Click(object sender, EventArgs e)
{
// Show the save file dialog
SaveFileDialog sfd = new SaveFileDialog();
sfd.CheckFileExists = false;
sfd.CheckPathExists = true;
sfd.DefaultExt = "xml";
sfd.Filter = "Transfer Function XML files (*.xml)|*.xml";
sfd.RestoreDirectory = true;
sfd.ShowHelp = false;
sfd.Title = "Please select the Transfer Function XML file to open...";
if (sfd.ShowDialog(this) == DialogResult.OK)
{
try
{
if (this.TransferFunction != null)
this.TransferFunction.ToXmlFile(sfd.FileName);
}
catch (Exception ex)
{
string message = "An error occured while writing the Transfer Function XML file.\r\n\r\n" + ex.ToString();
string caption = "Unable to save Transfer Function to XML file";
MessageBox.Show(this, message, caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
void menuClipboard_Click(object sender, EventArgs e)
{
Bitmap bitmap = this.TransferFunction.GetScreenCapture();
Clipboard.SetImage(bitmap);
}
//=====================================================================
#endregion
}
}