Mark Heath avatar Mark Heath committed fe3630c

mercurial and git use the new commit node control

Comments (0)

Files changed (4)

GraphPad/Logic/GraphRenderer.cs

     class GraphRenderer
     {
         private Canvas canvas;
-        private readonly Func<Node, UserControl> controlBuilder;
-        private readonly double nodeWidth;
-        private readonly double nodeHeight;
+        private readonly Func<Node, Size, UserControl> controlBuilder;
+        private readonly Size nodeSize;
         private const double nodePadding = 10.0;
 
-        public GraphRenderer(Canvas canvas, Func<Node,UserControl> controlBuilder, double nodeWidth, double nodeHeight)
+        public GraphRenderer(Canvas canvas, Func<Node, Size, UserControl> controlBuilder, Size nodeSize)
         {
             this.canvas = canvas;
             this.controlBuilder = controlBuilder;
-            this.nodeWidth = nodeWidth;
-            this.nodeHeight = nodeHeight;
+            this.nodeSize = nodeSize;
         }
 
         public void Render(Graph graph)
             // create nodes
             foreach (var node in graph.Nodes)
             {
-                var left = node.GetColumn() * (nodePadding + nodeWidth);
-                var top = node.GetRow() * (nodePadding + nodeHeight);
-                var nodeControl = controlBuilder(node);
+                var left = node.GetColumn() * (nodePadding + nodeSize.Width);
+                var top = node.GetRow() * (nodePadding + nodeSize.Height);
+                var nodeControl = controlBuilder(node, nodeSize);
                 nodeControl.SetValue(Canvas.LeftProperty, left);
                 nodeControl.SetValue(Canvas.TopProperty, top);
                 canvas.Children.Add(nodeControl);
                 node.MetaData["Control"] = nodeControl;
             }
+            canvas.UpdateLayout();
             CreateConnections(graph);
 
-            canvas.Width = canvas.Children.OfType<UserControl>().Max(x => (double)x.GetValue(Canvas.LeftProperty) + x.Width + nodePadding);
-            canvas.Height = canvas.Children.OfType<UserControl>().Max(x => (double)x.GetValue(Canvas.TopProperty) + x.Height + nodePadding);
+            canvas.Width = canvas.Children.OfType<UserControl>().Max(x => (double)x.GetValue(Canvas.LeftProperty) + x.ActualWidth + nodePadding);
+            canvas.Height = canvas.Children.OfType<UserControl>().Max(x => (double)x.GetValue(Canvas.TopProperty) + x.ActualHeight + nodePadding);
         }
 
         private void CreateConnections(Graph graph)

GraphPad/MainWindow.xaml.cs

             {
                 var builder = new MercurialGraphBuilder();
                 var graph = builder.LoadGraph(dialog.SelectedPath);
-                RedrawGraph(graph);
+                RedrawGraph(graph, CreateCommitNodeControl, new Size(200, 80));
             }
         }
 
             var result = dialog.ShowDialog();
             if (result == System.Windows.Forms.DialogResult.OK)
             {
-                GitGraphBuilder gitBuilder = new GitGraphBuilder();
+                var gitBuilder = new GitGraphBuilder();
                 var graph = gitBuilder.LoadGraph(dialog.SelectedPath);
-                RedrawGraph(graph);
+                RedrawGraph(graph, CreateCommitNodeControl, new Size(200, 80));
             }
         }
 
 
         private void RecreateGraph()
         {
-            GraphBuilder builder = new GraphBuilder();
-            Graph graph = builder.GenerateGraph(graphText.Text);
-            RedrawGraph(graph);
+            var builder = new GraphBuilder();
+            var graph = builder.GenerateGraph(graphText.Text);
+            RedrawGraph(graph, CreateCircleNodeControl, new Size(40, 40));
         }
 
-        private void RedrawGraph(Graph graph)
+        private void RedrawGraph(Graph graph, Func<Node, Size, UserControl> controlBuilder, Size nodeSize)
         {
-            GraphRenderer renderer = new GraphRenderer(graphCanvas, CreateCircleNodeControl, 40, 40);
-            GraphLayoutEngine layout = new GraphLayoutEngine();
+            var renderer = new GraphRenderer(graphCanvas, controlBuilder, nodeSize);
+            var layout = new GraphLayoutEngine();
             layout.Layout(graph);
             renderer.Render(graph);
         }
 
-        private static UserControl CreateCircleNodeControl(Node node)
+        private static UserControl CreateCircleNodeControl(Node node, Size size)
         {
             var nodeControl = new CircularNodeControl();
             nodeControl.NodeName = node.Name;
             return nodeControl;
         }
+
+        private static UserControl CreateCommitNodeControl(Node node, Size size)
+        {
+            var nodeControl = new CommitNodeControl();
+            nodeControl.CommitId = node.Name;
+            nodeControl.UserName = (string)node.MetaData["Author"];
+            nodeControl.MaxWidth = size.Width;
+            nodeControl.MaxHeight = size.Height;
+            return nodeControl;
+        }
     }
 }

graphpad/CommitNodeControl.xaml

             </LinearGradientBrush>
         </Border.Background>
         <StackPanel>
-            <TextBlock FontWeight="Bold" Text="{Binding Path=UserName, ElementName=root}" FontSize="20"  />
+            <TextBlock FontWeight="Bold" Text="{Binding Path=CommitId, ElementName=root}" FontSize="20"  />
             <TextBlock Text="{Binding Path=UserName, ElementName=root}" FontSize="20"  />
         </StackPanel>
     </Border>

graphpad/CommitNodeControl.xaml.cs

     /// <summary>
     /// Interaction logic for CommitNodeControl.xaml
     /// </summary>
-    public partial class CommitNodeControl : UserControl
+    public partial class CommitNodeControl : UserControl, INodeControl
     {
         public CommitNodeControl()
         {
             InitializeComponent();
         }
+
         public string CommitId
         {
             get { return (string)GetValue(CommitIdProperty); }
             DependencyProperty.Register("UserName", typeof(string), typeof(CommitNodeControl), new UIPropertyMetadata("Mark Heath"));
 
 
+        public Point GetConnectionPoint(ConnectionDirection connectionDirection)
+        {
+            var fromPosition = new Point((double)this.GetValue(Canvas.LeftProperty), (double)this.GetValue(Canvas.TopProperty));
+            var width = this.ActualWidth;
+            var height = this.ActualHeight;
+            var corner = width/4;
 
+            switch (connectionDirection)
+            {
+                case ConnectionDirection.West:
+                    return new Point(fromPosition.X, fromPosition.Y + height/2);
+                case ConnectionDirection.East:
+                    return new Point(fromPosition.X + width, fromPosition.Y + height/2);
+                case ConnectionDirection.North:
+                    return new Point(fromPosition.X + width/2, fromPosition.Y);
+                case ConnectionDirection.South:
+                    return new Point(fromPosition.X + width/2, fromPosition.Y + height);
+                case ConnectionDirection.NorthWest:
+                    return new Point(fromPosition.X + corner, fromPosition.Y);
+                case ConnectionDirection.SouthWest:
+                    return new Point(fromPosition.X + corner, fromPosition.Y + height);
+                case ConnectionDirection.NorthEast:
+                    return new Point(fromPosition.X + width - corner, fromPosition.Y);
+                case ConnectionDirection.SouthEast:
+                    return new Point(fromPosition.X + width - corner, fromPosition.Y + height);
+                default:
+                    throw new ArgumentException("Invalid connection direction");
+            }
+        }
     }
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.