/*============================================================================= Project: SharpImage Module: siFormSelectPaths.cs Language: C# Author: Dan Mueller Date: $Date$ Revision: $Revision$ 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.IO; 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.Rendering; namespace SharpImage.Forms { public partial class siFormSelectPaths : siFormTool { private List m_Paths = new List(); private int m_PathLabel = 1; private bool m_HandleNodeSelect = true; /// /// Default constructor. /// /// public siFormSelectPaths(siRenderer renderer) : base(renderer) { InitializeComponent(); // Add Form handlers this.FormClosed += new FormClosedEventHandler(FormSelectPaths_FormClosed); } void FormSelectPaths_FormClosed(object sender, FormClosedEventArgs e) { this.ClosePath(); } /// /// Gets the siSeedSelector Actor associated with this form. /// public List Paths { get { return this.m_Paths; } } /// /// Add a new path and select it. /// public TreeNode AddPath() { this.ClosePath(); siPathSelection path = new siPathSelection(); return this.AddPath(path); } /// /// Add the given path and select it. /// public TreeNode AddPath(siPathSelection path) { // Add actor this.Paths.Add(path); this.SelectPath(path); // Add to tree //TreeNode node = this.tree.Nodes.Add("Path " + m_PathLabel.ToString()); TreeNode node = this.tree.Nodes.Add("Path"); m_PathLabel += 1; node.Tag = path; node.ExpandAll(); tree.SelectedNode = node; return node; } public siPathSelection FindSelectedPath() { if (tree.SelectedNode == null) return null; else if (tree.SelectedNode.Tag == null) return null; else if (tree.SelectedNode.Tag is siPathSelection) return tree.SelectedNode.Tag as siPathSelection; else if (tree.SelectedNode.Tag is itk.itkPoint) return tree.SelectedNode.Parent.Tag as siPathSelection; else return null; } /// /// Closes the currently selected path. /// public void ClosePath() { this.ClosePath(this.FindSelectedPath()); } /// /// Closes the given path. /// /// public void ClosePath(siPathSelection path) { if (path != null) { path.PointAdded -= new siPathSelection.siPointsHandler(PathSelection_PointAdded); this.Renderer.RemoveActor(path); } } /// /// Selects the given path, closing the current path first. /// /// public void SelectPath(siPathSelection path) { if (path != null) { // Close the current path and select new path this.ClosePath(); path.PointAdded += new siPathSelection.siPointsHandler(PathSelection_PointAdded); this.Renderer.AddActorToFront(path); } } /// /// Removes the given path from the list. /// /// public void RemovePath(siPathSelection path) { if (path != null) { this.ClosePath(path); this.m_Paths.Remove(path); } } void PathSelection_PointAdded(siPathSelection sender, itk.itkPoint point) { foreach (TreeNode node in tree.Nodes) { if (node.Tag == sender) { this.AddPointsToNode(node); node.ExpandAll(); break; } } } private void cmdAddPath_Click(object sender, EventArgs e) { this.AddPath(); } private void cmdRemovePath_Click(object sender, EventArgs e) { if (tree.SelectedNode.Tag != null && tree.SelectedNode.Tag is itk.itkPoint) { // Remove point itk.itkPoint pointToRemove = tree.SelectedNode.Tag as itk.itkPoint; siPathSelection path = tree.SelectedNode.Parent.Tag as siPathSelection; if (pointToRemove != null && path != null) path.RemovePoint(pointToRemove); this.AddPointsToNode(tree.SelectedNode.Parent); } else if (tree.SelectedNode.Tag != null && tree.SelectedNode.Tag is siPathSelection) { // Remove the whole path TreeNode nodeToRemove = tree.SelectedNode; nodeToRemove.Remove(); this.RemovePath(nodeToRemove.Tag as siPathSelection); } } private void tree_BeforeSelect(object sender, TreeViewCancelEventArgs e) { if (e.Node == null) return; if (!this.m_HandleNodeSelect) return; this.m_HandleNodeSelect = false; if (e.Node.Tag is siPathSelection) { siPathSelection path = e.Node.Tag as siPathSelection; if (path == null) return; this.SelectPath(path); path.SelectPoint(null); this.AddPointsToNode(e.Node); } else if (e.Node.Tag is itk.itkPoint) { siPathSelection path = e.Node.Parent.Tag as siPathSelection; itk.itkPoint point = e.Node.Tag as itk.itkPoint; if (path == null || point == null) return; this.SelectPath(path); path.SelectPoint(point); } this.m_HandleNodeSelect = true; } private void AddPointsToNode(TreeNode node) { siPathSelection path = node.Tag as siPathSelection; if (path == null) return; node.Nodes.Clear(); foreach (itk.itkPoint point in path.AllPoints) { TreeNode child = node.Nodes.Add(path.GetLabel(point) + ": " + point.ToString()); child.Tag = point; } } private void cmdMovePointUp_Click(object sender, EventArgs e) { if (tree.SelectedNode == null) return; if (tree.SelectedNode.Tag == null) return; if (tree.SelectedNode.Tag is siPathSelection) return; siPathSelection path = tree.SelectedNode.Parent.Tag as siPathSelection; path.MovePointUp(tree.SelectedNode.Index); this.AddPointsToNode(tree.SelectedNode.Parent); } private void cmdMovePointDown_Click(object sender, EventArgs e) { if (tree.SelectedNode == null) return; if (tree.SelectedNode.Tag == null) return; if (tree.SelectedNode.Tag is siPathSelection) return; siPathSelection path = tree.SelectedNode.Parent.Tag as siPathSelection; path.MovePointDown(tree.SelectedNode.Index); this.AddPointsToNode(tree.SelectedNode.Parent); } private void cmdOpen_Click(object sender, EventArgs e) { // Show the open file dialog OpenFileDialog ofd = new OpenFileDialog(); ofd.CheckFileExists = true; ofd.CheckPathExists = true; ofd.DefaultExt = "path"; ofd.Filter = "SharpImage Path files (*.path)|*.path"; ofd.Multiselect = false; ofd.RestoreDirectory = true; ofd.ShowHelp = false; ofd.Title = "Please select the Path file to open..."; if (ofd.ShowDialog(this) == DialogResult.OK) this.ReadPathsFromFile(ofd.FileName); } private void ReadPathsFromFile(String file) { try { if (!File.Exists(file)) throw new FileNotFoundException("Path file does not exist", file); StreamReader reader = File.OpenText(file); while (!reader.EndOfStream) { String line = reader.ReadLine(); siPathSelection path = siPathSelection.ParseFromString(line); TreeNode node = this.AddPath(path); this.AddPointsToNode(node); } reader.Close(); this.ParentApplication.ScriptConsole.WriteLine("Open Paths: " + file); } catch (Exception ex) { string message = "An error occured while reading the Path file.\r\n\r\n" + ex.ToString(); string caption = "Unable to open Path file"; MessageBox.Show(this, message, caption, MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void cmdSave_Click(object sender, EventArgs e) { // Show the save file dialog SaveFileDialog sfd = new SaveFileDialog(); sfd.CheckFileExists = false; sfd.CheckPathExists = true; sfd.DefaultExt = "path"; sfd.Filter = "SharpImage Path files (*.path)|*.path"; sfd.RestoreDirectory = true; sfd.ShowHelp = false; sfd.Title = "Please select the Path file to open..."; if (sfd.ShowDialog(this) == DialogResult.OK) this.WritePathsToFile(sfd.FileName); } private void WritePathsToFile(String file) { try { StreamWriter writer = File.CreateText(file); foreach (siPathSelection path in this.Paths) writer.WriteLine(path.ToString()); writer.Flush(); writer.Close(); this.ParentApplication.ScriptConsole.WriteLine("Saved Paths: " + file); } catch (Exception ex) { string message = "An error occured while writing the Path file.\r\n\r\n" + ex.ToString(); string caption = "Unable to write Path file"; MessageBox.Show(this, message, caption, MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void menuChangeColor_Click(object sender, EventArgs e) { ColorDialog cd = new ColorDialog(); cd.AllowFullOpen = true; cd.AnyColor = true; cd.FullOpen = true; DialogResult result = cd.ShowDialog(this); if (result == DialogResult.OK) { siPathSelection path = this.FindSelectedPath(); if (path != null) path.Color = cd.Color; } } protected override void OnContinue(object sender, ContinueEventArgs e) { if (this.Paths.Count > 0) base.OnContinue(sender, e); else { string caption = "No paths were created"; string text = "No paths were created. At least one path must be created."; this.ParentApplication.ShowMessageBox(text, caption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); e.SuppressContinue = true; return; } } } }