using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; using TD; using TD.SandDock; using MindFusion.Diagramming; namespace ProfileEventViewer { public partial class TaskGraph : UserTabbedDocument { ProfileEventData.ProfileContext.Event m_SelectedEvent; ProfileEventDoc m_Doc; public TaskGraph(ProfileEventDoc doc) { InitializeComponent(); // m_Doc = doc; doc.SelectedProfileEventChanged += new EventDelegate(OnSelectedEventChanged); Text = doc.Name += " - Task Graph"; this.MouseWheel += new MouseEventHandler(OnMouseWheel); } bool m_NeedGraphUpdate = false; private void OnSelectedEventChanged(ProfileEventDoc doc) { if (m_SelectedEvent != doc.SelectedProfileEvent) { m_SelectedEvent = m_Doc.SelectedProfileEvent; if (Visible) { RebuildGraph(); } else { m_NeedGraphUpdate = true; } } } Dictionary m_TaskInstanceNodes = new Dictionary(); private ShapeNode GetNodeForInstance(ProfileEventData.TaskInstance instance) { ShapeNode item; if (!m_TaskInstanceNodes.ContainsKey(instance)) { ShapeNode node = new ShapeNode(taskGraphDiagram); node.Shape = Shape.FromId("Rectangle"); node.Text = instance.Desc.Name; node.ToolTip = instance.GetFullDescription(); RectangleF shapeBounds = node.Bounds; StringFormat format = node.TextFormat; format.Trimming = StringTrimming.None; shapeBounds.Height = 5; shapeBounds.Width = 40; node.Bounds = shapeBounds; node.PolygonalTextLayout = true; node.ResizeToFitText(FitSize.KeepHeight); shapeBounds = node.Bounds; shapeBounds.Width += 1; node.Bounds = shapeBounds; taskGraphDiagram.Items.Add(node); m_TaskInstanceNodes.Add(instance, node); item = node; } else { item = m_TaskInstanceNodes[instance]; } return item; } private void RebuildGraph() { m_NeedGraphUpdate = false; // clear graph taskGraphDiagram.ClearAll(); m_TaskInstanceNodes.Clear(); if (m_SelectedEvent != null && m_SelectedEvent.Task != null) { Dictionary processedTasks = new Dictionary(); Stack taskInstancesToProcess = new Stack(); taskInstancesToProcess.Push(m_SelectedEvent.Task); processedTasks.Add(m_SelectedEvent.Task, true); ShapeNode someItem = null; // create all the graph nodes while (taskInstancesToProcess.Count != 0) { ProfileEventData.TaskInstance instance = taskInstancesToProcess.Pop(); ShapeNode item = GetNodeForInstance(instance); someItem = item; // link to next tasks foreach (ProfileEventData.TaskInstance task in instance.PostTasks) { ShapeNode postNode = GetNodeForInstance(task); if (!processedTasks.ContainsKey(task)) { processedTasks.Add(task, true); taskInstancesToProcess.Push(task); } DiagramLink link = new DiagramLink(taskGraphDiagram, item, postNode); link.HeadShape = ArrowHead.Triangle; link.HeadShapeSize = 3; link.ToolTip = String.Format("{0} to {1}", instance.Desc.Name, task.Desc.Name); taskGraphDiagram.Items.Add(link); } } // layout the graph property graphLayeredLayout.Arrange(taskGraphDiagram); taskGraphDiagram.ResizeToFitItems(10); taskGraphView.ZoomToFit(); } } private void TaskGraph_VisibleChanged(object sender, EventArgs e) { if (m_NeedGraphUpdate) { RebuildGraph(); } } void OnMouseWheel(object sender, MouseEventArgs e) { if (e.Delta > 0) taskGraphView.ZoomIn(); else taskGraphView.ZoomOut(); } /*bool m_DragMode = false; int m_LastX; int m_LastY;*/ private void taskGraphView_MouseDown(object sender, MouseEventArgs e) { /*if (e.Button == MouseButtons.Middle) { Capture = true; m_DragMode = true; }*/ } private void taskGraphView_MouseUp(object sender, MouseEventArgs e) { /*if (e.Button == MouseButtons.Middle && m_DragMode) { Capture = false; m_DragMode = false; }*/ } private void taskGraphView_MouseMove(object sender, MouseEventArgs e) { /*if (m_DragMode) { int dx = m_LastX - e.X; int dy = m_LastY - e.Y; taskGraphView.ScrollX += dx; taskGraphView.ScrollY += dy; } m_LastX = e.X; m_LastY = e.Y;*/ } } }