Changeset 11120
- Timestamp:
- 07/07/14 16:55:50 (10 years ago)
- Location:
- stable
- Files:
-
- 2 deleted
- 20 edited
- 5 copied
Legend:
- Unmodified
- Added
- Removed
-
stable
- Property svn:mergeinfo changed
/trunk/sources merged: 10496,10499,10520-10523,10531,10561,10564-10565,10799-10800,10862,10885,10953,10956,11065
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
-
stable/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/Formatters/SymbolicExpressionTreeLatexFormatter.cs
r10520 r11120 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Drawing; 24 25 using System.Globalization; 25 26 using System.Linq; … … 36 37 {"StartSymbol","RPB"} 37 38 }; 38 private readonly ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>(); 39 40 private readonly ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> layoutEngine; 39 41 40 42 public SymbolicExpressionTreeLatexFormatter() 41 43 : base("LaTeX/PDF Formatter", "Formatter for symbolic expression trees for use with latex package tikz.") { 42 layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> {44 layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>(n => n.Subtrees) { 43 45 HorizontalSpacing = 2, 44 46 VerticalSpacing = 2, … … 57 59 58 60 public string Format(ISymbolicExpressionTree symbolicExpressionTree) { 59 layoutEngine.Reset();60 61 var root = symbolicExpressionTree.Root; 61 62 var actualRoot = root.SubtreeCount == 0 ? root.GetSubtree(0) : root; 62 layoutEngine.Initialize(actualRoot, x => x.Subtrees); 63 layoutEngine.CalculateLayout(); 64 var nodeCoordinates = layoutEngine.GetCoordinates(); 63 var nodeCoordinates = layoutEngine.CalculateLayout(actualRoot).ToDictionary(n => n.Content, n => new PointF(n.X, n.Y)); 65 64 var sb = new StringBuilder(); 66 65 var nl = Environment.NewLine; -
stable/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/GraphicalSymbolicExpressionTreeView.Designer.cs
r9456 r11120 19 19 */ 20 20 #endregion 21 22 using System.Drawing; 21 23 22 24 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views { … … 59 61 this.symbolicExpressionTreeChart.Spacing = 5; 60 62 this.symbolicExpressionTreeChart.TabIndex = 0; 61 this.symbolicExpressionTreeChart.TextFont = new System.Drawing.Font( "Times New Roman", 6F);63 this.symbolicExpressionTreeChart.TextFont = new System.Drawing.Font(FontFamily.GenericSerif, 8F); 62 64 // 63 65 // FunctionTreeView -
stable/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views-3.4.csproj
r8600 r11120 110 110 </ItemGroup> 111 111 <ItemGroup> 112 <Compile Include="Formatters\SymbolicExpressionTreeLatexFormatter.cs" /> 113 <Compile Include="LayoutEngines\BoxesLayoutEngine.cs" /> 114 <Compile Include="LayoutEngines\ILayoutEngine.cs" /> 115 <Compile Include="LayoutEngines\LayoutNode.cs" /> 116 <Compile Include="LayoutEngines\ReingoldTilfordLayoutEngine.cs" /> 112 117 <Compile Include="Plugin.cs" /> 113 118 <Compile Include="SymbolicExpressionGrammarAllowedChildSymbolsControl.cs"> … … 160 165 <DependentUpon>SymbolicExpressionView.cs</DependentUpon> 161 166 </Compile> 162 <Compile Include="Visual SymbolicExpressionTreeNode.cs" />163 <Compile Include="Visual SymbolicExpressionTreeNodeConnection.cs" />167 <Compile Include="VisualTreeNode.cs" /> 168 <Compile Include="VisualTreeNodeConnection.cs" /> 164 169 </ItemGroup> 165 170 <ItemGroup> … … 276 281 --> 277 282 <PropertyGroup> 278 <PreBuildEvent Condition=" '$(OS)' == 'Windows_NT' ">set Path=%25Path%25;$(ProjectDir);$(SolutionDir)283 <PreBuildEvent Condition=" '$(OS)' == 'Windows_NT' ">set Path=%25Path%25;$(ProjectDir);$(SolutionDir) 279 284 set ProjectDir=$(ProjectDir) 280 285 set SolutionDir=$(SolutionDir) … … 283 288 call PreBuildEvent.cmd 284 289 </PreBuildEvent> 285 <PreBuildEvent Condition=" '$(OS)' != 'Windows_NT' ">290 <PreBuildEvent Condition=" '$(OS)' != 'Windows_NT' "> 286 291 export ProjectDir=$(ProjectDir) 287 292 export SolutionDir=$(SolutionDir) -
stable/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/LayoutEngines/BoxesLayoutEngine.cs
r10520 r11120 1 1 #region License Information 2 3 /* HeuristicLab 4 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 5 * 6 * This file is part of HeuristicLab. 7 * 8 * HeuristicLab is free software: you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation, either version 3 of the License, or 11 * (at your option) any later version. 12 * 13 * HeuristicLab is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>. 20 */ 21 22 #endregion 23 2 24 using System; 3 25 using System.Collections.Generic; 4 using System.Drawing;5 26 using System.Linq; 6 27 7 28 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views { 8 29 public class BoxesLayoutEngine<T> : ILayoutEngine<T> where T : class { 9 private readonly Dictionary<T, VisualTreeNode<T>> nodeMap;10 11 30 public int NodeWidth { get; set; } 12 31 public int NodeHeight { get; set; } 13 32 public int HorizontalSpacing { get; set; } 14 33 public int VerticalSpacing { get; set; } 15 private VisualTreeNode<T> layoutRoot;16 34 17 public int Width { get; private set; } 18 public int Height { get; private set; } 35 private readonly Func<T, IEnumerable<T>> GetChildren; 36 private readonly Func<T, int> GetLength; 37 private readonly Func<T, int> GetDepth; 19 38 20 public Func<T, IEnumerable<T>> GetChildren { get; set; } 21 public Func<T, int> GetLength { get; set; } 22 public Func<T, int> GetDepth { get; set; } 39 public BoxesLayoutEngine(Func<T, IEnumerable<T>> GetChildren, Func<T, int> GetLength, Func<T, int> GetDepth) { 40 if (GetChildren == null) throw new ArgumentNullException("GetChildren"); 41 if (GetLength == null) throw new ArgumentNullException("GetLength"); 42 if (GetDepth == null) throw new ArgumentNullException("GetDepth"); 23 43 24 public BoxesLayoutEngine() { 25 nodeMap = new Dictionary<T, VisualTreeNode<T>>(); 44 this.GetChildren = GetChildren; 45 this.GetLength = GetLength; 46 this.GetDepth = GetDepth; 26 47 } 27 48 28 public void CalculateLayout() { 29 throw new Exception("The BoxesLayoutEngine does not support arbitrary bounds. Please use method CalculateLayout(Width, Height)"); 49 50 public IEnumerable<VisualTreeNode<T>> CalculateLayout(T root, float width, float height) { 51 var nodeMap = new Dictionary<T, VisualTreeNode<T>>(); 52 CreateVisualNodes(root, nodeMap); 53 RecursiveLayout(nodeMap, nodeMap[root], 0, 0, (int)Math.Round(width), (int)Math.Round(height) / GetDepth(root)); 54 return nodeMap.Values; 30 55 } 31 56 32 public void CalculateLayout(float width, float height) { 33 Width = (int)Math.Round(width); 34 Height = (int)Math.Round(height); 35 Reset(); 36 RecursiveLayout(layoutRoot, 0, 0, Width, Height / GetDepth(layoutRoot.Content)); 37 } 38 39 public void Initialize(T root, Func<T, IEnumerable<T>> getChildren, Func<T, int> getLength, Func<T, int> getDepth) { 40 if (getChildren == null || getLength == null || getDepth == null) 41 throw new ArgumentNullException("The BoxesLayoutEngine requires all of the lambdas: (getChildren, getLength and getDepth) to be defined."); 42 GetChildren = getChildren; 43 GetLength = getLength; 44 GetDepth = getDepth; 45 Clear(); 46 Expand(root); // produce the nodeMap 47 layoutRoot = nodeMap[root]; 48 } 49 50 private void Expand(T root) { 57 private void CreateVisualNodes(T root, Dictionary<T, VisualTreeNode<T>> map) { 51 58 var node = new VisualTreeNode<T>(root) { 52 59 PreferredWidth = NodeWidth, 53 60 PreferredHeight = NodeHeight 54 61 }; 55 nodeMap.Add(root, node); 62 63 map.Add(root, node); 56 64 var children = GetChildren(root).ToList(); 57 65 if (children.Any()) { 58 66 foreach (var child in children) { 59 Expand(child);67 CreateVisualNodes(child, map); 60 68 } 61 69 } 62 70 } 63 71 64 public void Center(float width, float height) { 65 // does nothing because the BoxesLayout centers the tree by default 66 } 67 68 public void Clear() { 69 nodeMap.Clear(); 70 layoutRoot = null; 71 } 72 73 public void Reset() { 74 foreach (var node in nodeMap.Values) { 75 node.X = 0; 76 node.Y = 0; 77 } 78 } 79 80 public Dictionary<T, PointF> GetCoordinates() { 81 return nodeMap.ToDictionary(x => x.Key, x => new PointF(x.Value.X, x.Value.Y)); 82 } 83 84 public void FitToBounds(float width, float height) { 85 // does nothing because the BoxesLayout is by default stretched on the whole drawing area 86 } 87 88 private void RecursiveLayout(VisualTreeNode<T> visualTreeNode, int x, int y, int width, int height) { 72 private void RecursiveLayout(Dictionary<T, VisualTreeNode<T>> nodeMap, VisualTreeNode<T> visualTreeNode, int x, int y, int width, int height) { 89 73 float center_x = x + width / 2; 90 74 float center_y = y + height / 2; 91 int actualWidth = width - VerticalSpacing;75 int actualWidth = width - HorizontalSpacing; 92 76 int actualHeight = height - VerticalSpacing; 93 77 … … 120 104 visualTreeNode.Y = y; 121 105 } 122 //calculate areas for the subtrees according to their tree size and call drawFunctionTree106 //calculate areas for the subtrees according to their tree size 123 107 var node = visualTreeNode.Content; 124 108 var children = GetChildren(node).ToList(); … … 127 111 for (int i = 0; i < children.Count; i++) { 128 112 xBoundaries[i + 1] = (int)(xBoundaries[i] + (width * (double)GetLength(children[i])) / (GetLength(node) - 1)); 129 RecursiveLayout(nodeMap [children[i]], xBoundaries[i], y + height, xBoundaries[i + 1] - xBoundaries[i], height);113 RecursiveLayout(nodeMap, nodeMap[children[i]], xBoundaries[i], y + height, xBoundaries[i + 1] - xBoundaries[i], height); 130 114 } 131 }132 133 public IEnumerable<VisualTreeNode<T>> GetVisualNodes() {134 return nodeMap.Values;135 115 } 136 116 } -
stable/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/LayoutEngines/ILayoutEngine.cs
r10520 r11120 1 2 using System; 1 #region License Information 2 3 /* HeuristicLab 4 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 5 * 6 * This file is part of HeuristicLab. 7 * 8 * HeuristicLab is free software: you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation, either version 3 of the License, or 11 * (at your option) any later version. 12 * 13 * HeuristicLab is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>. 20 */ 21 22 #endregion 23 3 24 using System.Collections.Generic; 4 using System.Drawing;5 25 6 26 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views { … … 11 31 int VerticalSpacing { get; set; } 12 32 13 void CalculateLayout(); 14 void CalculateLayout(float width, float height); 15 void Initialize(T root, Func<T, IEnumerable<T>> getChildren, Func<T, int> getLength = null, Func<T, int> getDepth = null); 16 void Clear(); 17 void Reset(); 18 19 // function members necessary to navigate the tree structure 20 Func<T, IEnumerable<T>> GetChildren { get; set; } 21 Func<T, int> GetLength { get; set; } 22 Func<T, int> GetDepth { get; set; } 23 24 IEnumerable<VisualTreeNode<T>> GetVisualNodes(); 25 Dictionary<T, PointF> GetCoordinates(); 33 IEnumerable<VisualTreeNode<T>> CalculateLayout(T root, float width, float height); 26 34 } 27 35 } -
stable/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/LayoutEngines/LayoutNode.cs
r10520 r11120 25 25 26 26 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views { 27 publicclass LayoutNode<T> : object where T : class {27 internal class LayoutNode<T> : object where T : class { 28 28 public float Width { get; set; } 29 29 public float Height { get; set; } -
stable/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/LayoutEngines/ReingoldTilfordLayoutEngine.cs
r10520 r11120 7 7 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views { 8 8 public class ReingoldTilfordLayoutEngine<T> : ILayoutEngine<T> where T : class { 9 private readonly Dictionary<T, LayoutNode<T>> nodeMap; // provides a reverse mapping T => LayoutNode10 9 public int NodeWidth { get; set; } 11 10 public int NodeHeight { get; set; } … … 22 21 } 23 22 24 public Func<T, IEnumerable<T>> GetChildren { get; set; } 25 public Func<T, int> GetLength { get; set; } 26 public Func<T, int> GetDepth { get; set; } 27 private LayoutNode<T> layoutRoot; 28 29 public ReingoldTilfordLayoutEngine() { 30 nodeMap = new Dictionary<T, LayoutNode<T>>(); 31 } 32 33 public ReingoldTilfordLayoutEngine(T root, Func<T, IEnumerable<T>> childrenFunc) 34 : this() { 35 Initialize(root, childrenFunc); 36 } 37 38 public void Initialize(T root, Func<T, IEnumerable<T>> getChildren, Func<T, int> getLength = null, Func<T, int> getDepth = null) { 39 GetChildren = getChildren; 40 Clear(); 41 var node = new LayoutNode<T> { Content = root, Width = NodeWidth, Height = NodeHeight }; 42 node.Ancestor = node; 43 layoutRoot = node; 44 Expand(node); 45 } 46 47 private void Expand(LayoutNode<T> lRoot) { 48 nodeMap.Add(lRoot.Content, lRoot); 23 private readonly Func<T, IEnumerable<T>> GetChildren; 24 25 public ReingoldTilfordLayoutEngine(Func<T, IEnumerable<T>> GetChildren) { 26 this.GetChildren = GetChildren; 27 } 28 29 public IEnumerable<VisualTreeNode<T>> CalculateLayout(T root) { 30 return CalculateLayout(root, 0, 0); 31 } 32 33 public IEnumerable<VisualTreeNode<T>> CalculateLayout(T root, float width, float height) { 34 Dictionary<T, LayoutNode<T>> layoutNodeMap = new Dictionary<T, LayoutNode<T>>(); 35 var layoutRoot = new LayoutNode<T> { Content = root, Width = NodeWidth, Height = NodeHeight, }; 36 layoutRoot.Ancestor = layoutRoot; 37 Expand(layoutRoot, layoutNodeMap); 38 39 FirstWalk(layoutRoot); 40 SecondWalk(layoutRoot, -layoutRoot.Prelim); 41 NormalizeCoordinates(layoutNodeMap.Values); 42 if (height != 0 && width != 0) { 43 FitToBounds(width, height, layoutNodeMap.Values); 44 Center(width, height, layoutNodeMap.Values); 45 } 46 47 return layoutNodeMap.Values.Select(x => new VisualTreeNode<T>(x.Content) { 48 Width = (int)Math.Round(x.Width), 49 Height = (int)Math.Round(x.Height), 50 X = (int)Math.Round(x.X), 51 Y = (int)Math.Round(x.Y) 52 }); 53 } 54 55 private void Expand(LayoutNode<T> lRoot, Dictionary<T, LayoutNode<T>> map) { 56 map.Add(lRoot.Content, lRoot); 49 57 var children = GetChildren(lRoot.Content).ToList(); 50 58 if (!children.Any()) return; … … 61 69 node.Ancestor = node; 62 70 lRoot.Children.Add(node); 63 Expand(node); 64 } 65 } 66 67 public IEnumerable<VisualTreeNode<T>> GetVisualNodes() { 68 return nodeMap.Values.Select(x => new VisualTreeNode<T>(x.Content) { 69 Width = (int)Math.Round(x.Width), 70 Height = (int)Math.Round(x.Height), 71 X = (int)Math.Round(x.X), 72 Y = (int)Math.Round(x.Y) 73 }); 74 } 75 76 public void AddNode(T content) { 77 if (nodeMap.ContainsKey(content)) { throw new ArgumentException("Content already present in the dictionary."); } 78 var node = new LayoutNode<T> { Content = content }; 79 nodeMap.Add(content, node); 80 } 81 82 public void AddNode(LayoutNode<T> node) { 83 var content = node.Content; 84 if (nodeMap.ContainsKey(content)) { throw new ArgumentException("Content already present in the dictionary."); } 85 nodeMap.Add(content, node); 86 } 87 88 public void AddNodes(IEnumerable<LayoutNode<T>> nodes) { 89 foreach (var node in nodes) 90 nodeMap.Add(node.Content, node); 91 } 92 93 public LayoutNode<T> GetNode(T content) { 94 LayoutNode<T> layoutNode; 95 nodeMap.TryGetValue(content, out layoutNode); 96 return layoutNode; 97 } 98 99 public void ResetCoordinates() { 100 foreach (var node in nodeMap.Values) { 101 node.ResetCoordinates(); 102 } 103 } 104 105 public Dictionary<T, PointF> GetCoordinates() { 106 return nodeMap.ToDictionary(x => x.Key, x => new PointF(x.Value.X, x.Value.Y)); 107 } 71 Expand(node, map); 72 } 73 } 74 108 75 109 76 /// <summary> 110 77 /// Transform LayoutNode coordinates so that all coordinates are positive and start from (0,0) 111 78 /// </summary> 112 private void NormalizeCoordinates() { 113 var nodes = nodeMap.Values.ToList(); 79 private static void NormalizeCoordinates(IEnumerable<LayoutNode<T>> nodes) { 114 80 float xmin = 0, ymin = 0; 115 81 foreach (var node in nodes) { … … 123 89 } 124 90 125 p ublic void Center(float width, float height) {91 private void Center(float width, float height, IEnumerable<LayoutNode<T>> nodes) { 126 92 // center layout on screen 127 var bounds = Bounds( );93 var bounds = Bounds(nodes); 128 94 float dx = 0, dy = 0; 129 95 if (width > bounds.Width) { dx = (width - bounds.Width) / 2f; } 130 96 if (height > bounds.Height) { dy = (height - bounds.Height) / 2f; } 131 foreach (var node in node Map.Values) { node.Translate(dx, dy); }132 } 133 134 p ublic void FitToBounds(float width, float height) {135 var bounds = Bounds( );97 foreach (var node in nodes) { node.Translate(dx, dy); } 98 } 99 100 private void FitToBounds(float width, float height, IEnumerable<LayoutNode<T>> nodes) { 101 var bounds = Bounds(nodes); 136 102 var myWidth = bounds.Width; 137 103 var myHeight = bounds.Height; … … 139 105 if (myWidth <= width && myHeight <= height) return; // no need to fit since we are within bounds 140 106 141 var layers = node Map.Values.GroupBy(node => node.Level, node => node).ToList();107 var layers = nodes.GroupBy(node => node.Level, node => node).ToList(); 142 108 143 109 if (myWidth > width) { … … 150 116 float spacing = minHorizontalSpacing * x; 151 117 foreach (var layer in layers) { 152 var nodes = layer.ToList();118 var nodesLayer = layer.ToList(); 153 119 float minWidth = float.MaxValue; 154 for (int i = 0; i < nodes .Count - 1; ++i) { minWidth = Math.Min(minWidth, nodes[i + 1].X - nodes[i].X); }120 for (int i = 0; i < nodesLayer.Count - 1; ++i) { minWidth = Math.Min(minWidth, nodesLayer[i + 1].X - nodesLayer[i].X); } 155 121 float w = Math.Min(NodeWidth, minWidth - spacing); 156 foreach (var node in nodes ) {122 foreach (var node in nodesLayer) { 157 123 node.X += (node.Width - w) / 2f; 158 124 node.Width = w; … … 178 144 } 179 145 180 public void Clear() {181 layoutRoot = null;182 nodeMap.Clear();183 }184 185 public void Reset() {186 foreach (var layoutNode in nodeMap.Values) {187 // reset layout-related parameters188 layoutNode.Reset();189 }190 }191 192 public void CalculateLayout() {193 if (layoutRoot == null) throw new Exception("Layout layoutRoot cannot be null.");194 Reset(); // reset node parameters like Mod, Shift etc. and set coordinates to 0195 FirstWalk(layoutRoot);196 SecondWalk(layoutRoot, -layoutRoot.Prelim);197 NormalizeCoordinates();198 }199 200 public void CalculateLayout(float width, float height) {201 CalculateLayout();202 FitToBounds(width, height);203 Center(width, height);204 }205 146 206 147 /// <summary> … … 208 149 /// </summary> 209 150 /// <returns></returns> 210 p ublic RectangleF Bounds() {151 private RectangleF Bounds(IEnumerable<LayoutNode<T>> nodes) { 211 152 float xmin = 0, xmax = 0, ymin = 0, ymax = 0; 212 var list = nodeMap.Values.ToList(); 213 foreach (LayoutNode<T> node in list) { 153 foreach (LayoutNode<T> node in nodes) { 214 154 float x = node.X, y = node.Y; 215 155 if (xmin > x) xmin = x; -
stable/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionGrammarAllowedChildSymbolsControl.cs
r9456 r11120 29 29 using HeuristicLab.PluginInfrastructure; 30 30 31 using VisualSymbolicExpressionTreeNode = HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views.VisualTreeNode<HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ISymbolicExpressionTreeNode>; 32 31 33 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views { 32 34 public sealed partial class SymbolicExpressionGrammarAllowedChildSymbolsControl : UserControl { … … 103 105 104 106 var tree = new SymbolicExpressionTree(new SymbolicExpressionTreeNode(Symbol)); 105 symbolicExpressionTreeChart.SuspendRepaint = true;106 107 if (Grammar.GetMaximumSubtreeCount(Symbol) > 0) { 107 108 for (int i = 0; i < Grammar.GetMaximumSubtreeCount(Symbol); i++) { … … 116 117 } 117 118 symbolicExpressionTreeChart.Tree = tree; 118 119 symbolicExpressionTreeChart.SuspendRepaint = true; 119 120 foreach (var subtreeNode in tree.Root.Subtrees) { 120 121 foreach (var allowedChildNode in subtreeNode.Subtrees) { … … 142 143 } 143 144 } 144 145 145 symbolicExpressionTreeChart.SuspendRepaint = false; 146 146 UpdateSelectedSymbolicExpressionTreeNodes(); … … 153 153 else visualNode.FillColor = Color.LightSteelBlue; 154 154 } 155 symbolicExpressionTreeChart.Repaint ();155 symbolicExpressionTreeChart.RepaintNodes(); 156 156 } 157 157 … … 162 162 163 163 VisualSymbolicExpressionTreeNode clickedNode = (VisualSymbolicExpressionTreeNode)sender; 164 var selectedNode = clickedNode. SymbolicExpressionTreeNode;164 var selectedNode = clickedNode.Content; 165 165 if (selectedNode.SubtreeCount == 0) { 166 166 if (!selectedSymbolicExpressionTreeNodes.Contains(selectedNode)) … … 207 207 var visualNode = symbolicExpressionTreeChart.FindVisualSymbolicExpressionTreeNodeAt(coordinates.X, coordinates.Y); 208 208 if (visualNode != null) { 209 var node = visualNode. SymbolicExpressionTreeNode;209 var node = visualNode.Content; 210 210 var root = symbolicExpressionTreeChart.Tree.Root; 211 211 if (node == root || node.Parent == root) e.Effect = DragDropEffects.Copy; … … 223 223 var symbols = data as IEnumerable<ISymbol>; 224 224 225 if (node. SymbolicExpressionTreeNode== root) {225 if (node.Content == root) { 226 226 if (symbol != null) 227 227 Grammar.AddAllowedChildSymbol(root.Symbol, symbol); … … 229 229 foreach (var s in symbols) Grammar.AddAllowedChildSymbol(root.Symbol, s); 230 230 } else { 231 int argumentIndex = root.IndexOfSubtree(node. SymbolicExpressionTreeNode);231 int argumentIndex = root.IndexOfSubtree(node.Content); 232 232 if (symbol != null) 233 233 Grammar.AddAllowedChildSymbol(root.Symbol, symbol, argumentIndex); -
stable/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionTreeChart.Designer.cs
r9456 r11120 49 49 this.contextMenuStrip = new System.Windows.Forms.ContextMenuStrip(this.components); 50 50 this.saveImageToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); 51 this.exportPgfLaTeXToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); 52 this.layoutEngineToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); 53 this.reingoldTilfordToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); 54 this.boxesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); 51 55 this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); 52 56 this.contextMenuStrip.SuspendLayout(); … … 56 60 // 57 61 this.contextMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { 58 this.saveImageToolStripMenuItem}); 62 this.saveImageToolStripMenuItem, 63 this.exportPgfLaTeXToolStripMenuItem, 64 this.layoutEngineToolStripMenuItem}); 59 65 this.contextMenuStrip.Name = "contextMenuStrip"; 60 this.contextMenuStrip.Size = new System.Drawing.Size(1 35, 26);66 this.contextMenuStrip.Size = new System.Drawing.Size(166, 70); 61 67 // 62 68 // saveImageToolStripMenuItem 63 69 // 64 70 this.saveImageToolStripMenuItem.Name = "saveImageToolStripMenuItem"; 65 this.saveImageToolStripMenuItem.Size = new System.Drawing.Size(1 34, 22);71 this.saveImageToolStripMenuItem.Size = new System.Drawing.Size(165, 22); 66 72 this.saveImageToolStripMenuItem.Text = "Save Image"; 67 73 this.saveImageToolStripMenuItem.Click += new System.EventHandler(this.saveImageToolStripMenuItem_Click); 74 // 75 // exportPgfLaTeXToolStripMenuItem 76 // 77 this.exportPgfLaTeXToolStripMenuItem.Name = "exportPgfLaTeXToolStripMenuItem"; 78 this.exportPgfLaTeXToolStripMenuItem.Size = new System.Drawing.Size(165, 22); 79 this.exportPgfLaTeXToolStripMenuItem.Text = "Export Pgf/LaTeX"; 80 this.exportPgfLaTeXToolStripMenuItem.Click += new System.EventHandler(this.exportLatexToolStripMenuItem_Click); 81 // 82 // layoutEngineToolStripMenuItem 83 // 84 this.layoutEngineToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { 85 this.reingoldTilfordToolStripMenuItem, 86 this.boxesToolStripMenuItem}); 87 this.layoutEngineToolStripMenuItem.Name = "layoutEngineToolStripMenuItem"; 88 this.layoutEngineToolStripMenuItem.Size = new System.Drawing.Size(165, 22); 89 this.layoutEngineToolStripMenuItem.Text = "Layout Engine:"; 90 // 91 // reingoldTilfordToolStripMenuItem 92 // 93 this.reingoldTilfordToolStripMenuItem.Name = "reingoldTilfordToolStripMenuItem"; 94 this.reingoldTilfordToolStripMenuItem.Size = new System.Drawing.Size(161, 22); 95 this.reingoldTilfordToolStripMenuItem.Text = "Reingold-Tilford"; 96 this.reingoldTilfordToolStripMenuItem.Click += new System.EventHandler(this.reingoldTilfordToolStripMenuItem_Click); 97 // 98 // boxesToolStripMenuItem 99 // 100 this.boxesToolStripMenuItem.Name = "boxesToolStripMenuItem"; 101 this.boxesToolStripMenuItem.Size = new System.Drawing.Size(161, 22); 102 this.boxesToolStripMenuItem.Text = "Boxes"; 103 this.boxesToolStripMenuItem.Click += new System.EventHandler(this.boxesToolStripMenuItem_Click); 68 104 // 69 105 // saveFileDialog 70 106 // 71 107 this.saveFileDialog.Filter = "Bitmap (*.bmp)|*.bmp|EMF (*.emf)|*.emf"; 72 this.saveFileDialog.FilterIndex = 1;108 // 73 109 // SymbolicExpressionTreeChart 74 110 // 75 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);76 111 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit; 77 112 this.ContextMenuStrip = this.contextMenuStrip; 113 this.DoubleBuffered = true; 78 114 this.Name = "SymbolicExpressionTreeChart"; 79 115 this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.SymbolicExpressionTreeChart_MouseClick); … … 93 129 protected System.Windows.Forms.ToolStripMenuItem saveImageToolStripMenuItem; 94 130 protected System.Windows.Forms.SaveFileDialog saveFileDialog; 131 private System.Windows.Forms.ToolStripMenuItem exportPgfLaTeXToolStripMenuItem; 132 private System.Windows.Forms.ToolStripMenuItem layoutEngineToolStripMenuItem; 133 private System.Windows.Forms.ToolStripMenuItem reingoldTilfordToolStripMenuItem; 134 private System.Windows.Forms.ToolStripMenuItem boxesToolStripMenuItem; 95 135 } 96 136 } -
stable/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionTreeChart.cs
r9931 r11120 24 24 using System.Drawing; 25 25 using System.Drawing.Imaging; 26 using System.IO; 27 using System.Linq; 26 28 using System.Windows.Forms; 29 27 30 28 31 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views { 29 32 public partial class SymbolicExpressionTreeChart : UserControl { 30 33 private Image image; 31 private StringFormat stringFormat; 32 private Dictionary<ISymbolicExpressionTreeNode, VisualSymbolicExpressionTreeNode> visualTreeNodes; 33 private Dictionary<Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>, VisualSymbolicExpressionTreeNodeConnection> visualLines; 34 private readonly StringFormat stringFormat; 35 private Dictionary<ISymbolicExpressionTreeNode, VisualTreeNode<ISymbolicExpressionTreeNode>> visualTreeNodes; 36 private Dictionary<Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>, VisualTreeNodeConnection> visualLines; 37 private ILayoutEngine<ISymbolicExpressionTreeNode> layoutEngine; 38 39 private const int preferredNodeWidth = 70; 40 private const int preferredNodeHeight = 46; 41 private int minHorizontalDistance = 30; 42 private int minVerticalDistance = 30; 34 43 35 44 public SymbolicExpressionTreeChart() { … … 40 49 this.lineColor = Color.Black; 41 50 this.backgroundColor = Color.White; 42 this.textFont = new Font("Times New Roman", 8); 51 this.textFont = new Font(FontFamily.GenericSansSerif, 12); 52 53 visualTreeNodes = new Dictionary<ISymbolicExpressionTreeNode, VisualTreeNode<ISymbolicExpressionTreeNode>>(); 54 visualLines = new Dictionary<Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>, VisualTreeNodeConnection>(); 55 56 layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>(n => n.Subtrees) { 57 NodeWidth = preferredNodeWidth, 58 NodeHeight = preferredNodeHeight, 59 HorizontalSpacing = minHorizontalDistance, 60 VerticalSpacing = minVerticalDistance 61 }; 62 reingoldTilfordToolStripMenuItem.Checked = true; 43 63 } 44 64 … … 48 68 } 49 69 70 #region Public properties 50 71 private int spacing; 51 72 public int Spacing { … … 89 110 set { 90 111 tree = value; 91 visualTreeNodes = new Dictionary<ISymbolicExpressionTreeNode, VisualSymbolicExpressionTreeNode>();92 visualLines = new Dictionary<Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>, VisualSymbolicExpressionTreeNodeConnection>();93 if (tree != null) {94 foreach (ISymbolicExpressionTreeNode node in tree.IterateNodesPrefix()) {95 visualTreeNodes[node] = new VisualSymbolicExpressionTreeNode(node);96 if (node.Parent != null) visualLines[Tuple.Create(node.Parent, node)] = new VisualSymbolicExpressionTreeNodeConnection();97 }98 }99 112 Repaint(); 100 113 } … … 106 119 set { suspendRepaint = value; } 107 120 } 121 #endregion 108 122 109 123 protected override void OnPaint(PaintEventArgs e) { … … 115 129 if (this.Width <= 1 || this.Height <= 1) 116 130 this.image = new Bitmap(1, 1); 117 else 131 else { 118 132 this.image = new Bitmap(Width, Height); 133 } 119 134 this.Repaint(); 135 } 136 137 public event EventHandler Repainted;//expose this event to notify the parent control that the tree was repainted 138 protected virtual void OnRepaint(object sender, EventArgs e) { 139 var repainted = Repainted; 140 if (repainted != null) { 141 repainted(sender, e); 142 } 120 143 } 121 144 … … 124 147 this.GenerateImage(); 125 148 this.Refresh(); 149 OnRepaint(this, EventArgs.Empty); 126 150 } 127 151 } … … 134 158 foreach (var visualNode in visualTreeNodes.Values) { 135 159 DrawTreeNode(graphics, visualNode); 160 if (visualNode.Content.SubtreeCount > 0) { 161 foreach (var visualSubtree in visualNode.Content.Subtrees.Select(s => visualTreeNodes[s])) { 162 DrawLine(graphics, visualNode, visualSubtree); 163 } 164 } 136 165 } 137 166 } … … 140 169 } 141 170 142 public void RepaintNode(Visual SymbolicExpressionTreeNodevisualNode) {171 public void RepaintNode(VisualTreeNode<ISymbolicExpressionTreeNode> visualNode) { 143 172 if (!suspendRepaint) { 144 173 using (var graphics = Graphics.FromImage(image)) { … … 157 186 graphics.Clear(backgroundColor); 158 187 if (tree != null) { 159 int height = this.Height / tree.Depth; 160 DrawFunctionTree(tree, graphics, 0, 0, this.Width, height); 161 } 162 } 163 } 164 165 public VisualSymbolicExpressionTreeNode GetVisualSymbolicExpressionTreeNode(ISymbolicExpressionTreeNode symbolicExpressionTreeNode) { 188 DrawFunctionTree(graphics, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance); 189 } 190 } 191 } 192 193 public VisualTreeNode<ISymbolicExpressionTreeNode> GetVisualSymbolicExpressionTreeNode(ISymbolicExpressionTreeNode symbolicExpressionTreeNode) { 166 194 if (visualTreeNodes.ContainsKey(symbolicExpressionTreeNode)) 167 195 return visualTreeNodes[symbolicExpressionTreeNode]; … … 169 197 } 170 198 171 public Visual SymbolicExpressionTreeNodeConnection GetVisualSymbolicExpressionTreeNodeConnection(ISymbolicExpressionTreeNode parent, ISymbolicExpressionTreeNode child) {199 public VisualTreeNodeConnection GetVisualSymbolicExpressionTreeNodeConnection(ISymbolicExpressionTreeNode parent, ISymbolicExpressionTreeNode child) { 172 200 if (child.Parent != parent) throw new ArgumentException(); 173 201 var key = Tuple.Create(parent, child); 174 Visual SymbolicExpressionTreeNodeConnection connection = null;202 VisualTreeNodeConnection connection = null; 175 203 visualLines.TryGetValue(key, out connection); 176 204 return connection; … … 186 214 187 215 protected virtual void SymbolicExpressionTreeChart_MouseClick(object sender, MouseEventArgs e) { 188 VisualSymbolicExpressionTreeNodevisualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);216 var visualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y); 189 217 if (visualTreeNode != null) { 190 218 OnSymbolicExpressionTreeNodeClicked(visualTreeNode, e); … … 200 228 201 229 protected virtual void SymbolicExpressionTreeChart_MouseDoubleClick(object sender, MouseEventArgs e) { 202 Visual SymbolicExpressionTreeNodevisualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);230 VisualTreeNode<ISymbolicExpressionTreeNode> visualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y); 203 231 if (visualTreeNode != null) 204 232 OnSymbolicExpressionTreeNodeDoubleClicked(visualTreeNode, e); … … 212 240 } 213 241 214 private Visual SymbolicExpressionTreeNodedraggedSymbolicExpressionTree;242 private VisualTreeNode<ISymbolicExpressionTreeNode> draggedSymbolicExpressionTree; 215 243 private MouseButtons dragButtons; 216 244 private void SymbolicExpressionTreeChart_MouseDown(object sender, MouseEventArgs e) { … … 224 252 225 253 private void SymbolicExpressionTreeChart_MouseMove(object sender, MouseEventArgs e) { 226 Visual SymbolicExpressionTreeNodevisualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);254 VisualTreeNode<ISymbolicExpressionTreeNode> visualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y); 227 255 if (draggedSymbolicExpressionTree != null && 228 256 draggedSymbolicExpressionTree != visualTreeNode) { … … 239 267 } 240 268 241 public Visual SymbolicExpressionTreeNodeFindVisualSymbolicExpressionTreeNodeAt(int x, int y) {269 public VisualTreeNode<ISymbolicExpressionTreeNode> FindVisualSymbolicExpressionTreeNodeAt(int x, int y) { 242 270 foreach (var visualTreeNode in visualTreeNodes.Values) { 243 271 if (x >= visualTreeNode.X && x <= visualTreeNode.X + visualTreeNode.Width && … … 249 277 #endregion 250 278 279 private void CalculateLayout(int preferredWidth, int preferredHeight, int minHDistance, int minVDistance) { 280 layoutEngine.NodeWidth = preferredWidth; 281 layoutEngine.NodeHeight = preferredHeight; 282 layoutEngine.HorizontalSpacing = minHDistance; 283 layoutEngine.VerticalSpacing = minVDistance; 284 285 var actualRoot = tree.Root; 286 if (actualRoot.Symbol is ProgramRootSymbol && actualRoot.SubtreeCount == 1) { 287 actualRoot = tree.Root.GetSubtree(0); 288 } 289 290 var visualNodes = layoutEngine.CalculateLayout(actualRoot, Width, Height).ToList(); 291 visualTreeNodes = visualNodes.ToDictionary(x => x.Content, x => x); 292 visualLines = new Dictionary<Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>, VisualTreeNodeConnection>(); 293 foreach (var node in visualNodes.Select(n => n.Content)) { 294 foreach (var subtree in node.Subtrees) { 295 visualLines.Add(new Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>(node, subtree), new VisualTreeNodeConnection()); 296 } 297 } 298 } 299 251 300 #region methods for painting the symbolic expression tree 252 private void DrawFunctionTree(ISymbolicExpressionTree tree, Graphics graphics, int x, int y, int width, int height) { 253 DrawFunctionTree(tree.Root, graphics, x, y, width, height, Point.Empty); 254 } 255 256 /// <summary> 257 /// 258 /// </summary> 259 /// <param name="functionTree"> function tree to draw</param> 260 /// <param name="graphics">graphics object to draw on</param> 261 /// <param name="x">x coordinate of drawing area</param> 262 /// <param name="y">y coordinate of drawing area</param> 263 /// <param name="width">width of drawing area</param> 264 /// <param name="height">height of drawing area</param> 265 private void DrawFunctionTree(ISymbolicExpressionTreeNode node, Graphics graphics, int x, int y, int width, int height, Point connectionPoint) { 266 VisualSymbolicExpressionTreeNode visualTreeNode = visualTreeNodes[node]; 267 float center_x = x + width / 2; 268 float center_y = y + height / 2; 269 int actualWidth = width - spacing; 270 int actualHeight = height - spacing; 271 272 using (var textBrush = new SolidBrush(visualTreeNode.TextColor)) 273 using (var nodeLinePen = new Pen(visualTreeNode.LineColor)) 274 using (var nodeFillBrush = new SolidBrush(visualTreeNode.FillColor)) { 275 276 //calculate size of node 277 if (actualWidth >= visualTreeNode.PreferredWidth && actualHeight >= visualTreeNode.PreferredHeight) { 278 visualTreeNode.Width = visualTreeNode.PreferredWidth; 279 visualTreeNode.Height = visualTreeNode.PreferredHeight; 280 visualTreeNode.X = (int)center_x - visualTreeNode.Width / 2; 281 visualTreeNode.Y = (int)center_y - visualTreeNode.Height / 2; 282 } 283 //width too small to draw in desired sized 284 else if (actualWidth < visualTreeNode.PreferredWidth && actualHeight >= visualTreeNode.PreferredHeight) { 285 visualTreeNode.Width = actualWidth; 286 visualTreeNode.Height = visualTreeNode.PreferredHeight; 287 visualTreeNode.X = x; 288 visualTreeNode.Y = (int)center_y - visualTreeNode.Height / 2; 289 } 290 //height too small to draw in desired sized 291 else if (actualWidth >= visualTreeNode.PreferredWidth && actualHeight < visualTreeNode.PreferredHeight) { 292 visualTreeNode.Width = visualTreeNode.PreferredWidth; 293 visualTreeNode.Height = actualHeight; 294 visualTreeNode.X = (int)center_x - visualTreeNode.Width / 2; 295 visualTreeNode.Y = y; 296 } 297 //width and height too small to draw in desired size 298 else { 299 visualTreeNode.Width = actualWidth; 300 visualTreeNode.Height = actualHeight; 301 visualTreeNode.X = x; 302 visualTreeNode.Y = y; 303 } 304 305 //draw terminal node 306 if (node.SubtreeCount == 0) { 307 graphics.FillRectangle(nodeFillBrush, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height); 308 graphics.DrawRectangle(nodeLinePen, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height); 309 } else { 310 graphics.FillEllipse(nodeFillBrush, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height); 311 graphics.DrawEllipse(nodeLinePen, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height); 312 } 313 314 //draw name of symbol 315 var text = node.ToString(); 316 graphics.DrawString(text, textFont, textBrush, new RectangleF(visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height), stringFormat); 317 318 //draw connection line to parent node 319 if (!connectionPoint.IsEmpty && node.Parent != null) { 320 var visualLine = GetVisualSymbolicExpressionTreeNodeConnection(node.Parent, node); 321 using (Pen linePen = new Pen(visualLine.LineColor)) { 301 private void DrawFunctionTree(Graphics graphics, int preferredWidth, int preferredHeight, int minHDistance, int minVDistance) { 302 CalculateLayout(preferredWidth, preferredHeight, minHDistance, minVDistance); 303 var visualNodes = visualTreeNodes.Values; 304 //draw nodes and connections 305 foreach (var visualNode in visualNodes) { 306 DrawTreeNode(graphics, visualNode); 307 var node = visualNode.Content; 308 foreach (var subtree in node.Subtrees) { 309 var visualLine = GetVisualSymbolicExpressionTreeNodeConnection(node, subtree); 310 var visualSubtree = visualTreeNodes[subtree]; 311 var origin = new Point(visualNode.X + visualNode.Width / 2, visualNode.Y + visualNode.Height); 312 var target = new Point(visualSubtree.X + visualSubtree.Width / 2, visualSubtree.Y); 313 graphics.Clip = new Region(new Rectangle(Math.Min(origin.X, target.X), origin.Y, Math.Max(origin.X, target.X), target.Y)); 314 using (var linePen = new Pen(visualLine.LineColor)) { 322 315 linePen.DashStyle = visualLine.DashStyle; 323 graphics.DrawLine(linePen, connectionPoint, new Point(visualTreeNode.X + visualTreeNode.Width / 2, visualTreeNode.Y));316 graphics.DrawLine(linePen, origin, target); 324 317 } 325 318 } 326 327 //calculate areas for the subtrees according to their tree size and call drawFunctionTree 328 Point connectFrom = new Point(visualTreeNode.X + visualTreeNode.Width / 2, visualTreeNode.Y + visualTreeNode.Height); 329 int[] xBoundaries = new int[node.SubtreeCount + 1]; 330 xBoundaries[0] = x; 331 for (int i = 0; i < node.SubtreeCount; i++) { 332 xBoundaries[i + 1] = (int)(xBoundaries[i] + (width * (double)node.GetSubtree(i).GetLength()) / (node.GetLength() - 1)); 333 DrawFunctionTree(node.GetSubtree(i), graphics, xBoundaries[i], y + height, xBoundaries[i + 1] - xBoundaries[i], height, connectFrom); 334 } 335 } 336 } 337 338 protected void DrawTreeNode(VisualSymbolicExpressionTreeNode visualTreeNode) { 339 using (var graphics = Graphics.FromImage(image)) { 340 graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 341 graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 342 DrawTreeNode(graphics, visualTreeNode); 343 } 344 } 345 346 protected void DrawTreeNode(Graphics graphics, VisualSymbolicExpressionTreeNode visualTreeNode) { 319 } 320 } 321 322 protected void DrawTreeNode(Graphics graphics, VisualTreeNode<ISymbolicExpressionTreeNode> visualTreeNode) { 347 323 graphics.Clip = new Region(new Rectangle(visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width + 1, visualTreeNode.Height + 1)); 348 324 graphics.Clear(backgroundColor); 349 var node = visualTreeNode. SymbolicExpressionTreeNode;325 var node = visualTreeNode.Content; 350 326 using (var textBrush = new SolidBrush(visualTreeNode.TextColor)) 351 327 using (var nodeLinePen = new Pen(visualTreeNode.LineColor)) … … 364 340 } 365 341 } 342 343 protected void DrawLine(Graphics graphics, VisualTreeNode<ISymbolicExpressionTreeNode> startNode, VisualTreeNode<ISymbolicExpressionTreeNode> endNode) { 344 var origin = new Point(startNode.X + startNode.Width / 2, startNode.Y + startNode.Height); 345 var target = new Point(endNode.X + endNode.Width / 2, endNode.Y); 346 graphics.Clip = new Region(new Rectangle(Math.Min(origin.X, target.X), origin.Y, Math.Max(origin.X, target.X), target.Y)); 347 var visualLine = GetVisualSymbolicExpressionTreeNodeConnection(startNode.Content, endNode.Content); 348 using (var linePen = new Pen(visualLine.LineColor)) { 349 linePen.DashStyle = visualLine.DashStyle; 350 graphics.DrawLine(linePen, origin, target); 351 } 352 } 366 353 #endregion 367 368 354 #region save image 369 355 private void saveImageToolStripMenuItem_Click(object sender, EventArgs e) { … … 380 366 Image image = new Bitmap(Width, Height); 381 367 using (Graphics g = Graphics.FromImage(image)) { 382 int height = this.Height / tree.Depth; 383 DrawFunctionTree(tree, g, 0, 0, Width, height); 368 DrawFunctionTree(g, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance); 384 369 } 385 370 image.Save(filename); … … 391 376 using (Metafile file = new Metafile(filename, g.GetHdc())) { 392 377 using (Graphics emfFile = Graphics.FromImage(file)) { 393 int height = this.Height / tree.Depth; 394 DrawFunctionTree(tree, emfFile, 0, 0, Width, height); 378 DrawFunctionTree(emfFile, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance); 395 379 } 396 380 } … … 399 383 } 400 384 #endregion 385 #region export pgf/tikz 386 private void exportLatexToolStripMenuItem_Click(object sender, EventArgs e) { 387 var t = Tree; 388 if (t == null) return; 389 using (var dialog = new SaveFileDialog { Filter = "Tex (*.tex)|*.tex" }) { 390 if (dialog.ShowDialog() != DialogResult.OK) return; 391 string filename = dialog.FileName.ToLower(); 392 var formatter = new SymbolicExpressionTreeLatexFormatter(); 393 File.WriteAllText(filename, formatter.Format(t)); 394 } 395 } 396 #endregion 397 398 private void reingoldTilfordToolStripMenuItem_Click(object sender, EventArgs e) { 399 minHorizontalDistance = 30; 400 minVerticalDistance = 30; 401 layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>(n => n.Subtrees) { 402 NodeWidth = preferredNodeWidth, 403 NodeHeight = preferredNodeHeight, 404 HorizontalSpacing = minHorizontalDistance, 405 VerticalSpacing = minVerticalDistance 406 }; 407 reingoldTilfordToolStripMenuItem.Checked = true; 408 boxesToolStripMenuItem.Checked = false; 409 Repaint(); 410 } 411 412 private void boxesToolStripMenuItem_Click(object sender, EventArgs e) { 413 minHorizontalDistance = 5; 414 minVerticalDistance = 5; 415 layoutEngine = new BoxesLayoutEngine<ISymbolicExpressionTreeNode>(n => n.Subtrees, n => n.GetLength(), n => n.GetDepth()) { 416 NodeWidth = preferredNodeWidth, 417 NodeHeight = preferredNodeHeight, 418 HorizontalSpacing = minHorizontalDistance, 419 VerticalSpacing = minVerticalDistance 420 }; 421 reingoldTilfordToolStripMenuItem.Checked = false; 422 boxesToolStripMenuItem.Checked = true; 423 Repaint(); 424 } 401 425 } 402 426 } -
stable/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionView.Designer.cs
r11118 r11120 52 52 // textBox 53 53 // 54 this.textBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 55 | System.Windows.Forms.AnchorStyles.Left)56 54 this.textBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 55 | System.Windows.Forms.AnchorStyles.Left) 56 | System.Windows.Forms.AnchorStyles.Right))); 57 57 this.textBox.BackColor = System.Drawing.Color.White; 58 this.textBox.Font = new System.Drawing.Font("Consolas", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 58 59 this.textBox.Location = new System.Drawing.Point(3, 30); 59 60 this.textBox.Multiline = true; … … 66 67 // formattersComboBox 67 68 // 68 this.formattersComboBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 69 69 this.formattersComboBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 70 | System.Windows.Forms.AnchorStyles.Right))); 70 71 this.formattersComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; 71 72 this.formattersComboBox.FormattingEnabled = true; -
stable/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionView.cs
r11118 r11120 78 78 UpdateTextbox(); 79 79 } 80 80 81 } 81 82 } -
stable/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/VisualTreeNodeConnection.cs
r10521 r11120 24 24 25 25 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views { 26 public class VisualTreeNodeConnection : object{26 public class VisualTreeNodeConnection { 27 27 private static readonly Color defaultLineColor = Color.Black; 28 28 private static readonly DashStyle defaultDashStyle = DashStyle.Solid; -
stable/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.csproj
r11118 r11120 135 135 <Compile Include="Creators\RampedHalfAndHalfTreeCreator.cs" /> 136 136 <Compile Include="Formatters\SymbolicExpressionTreeGraphvizFormatter.cs" /> 137 <Compile Include="Formatters\SymbolicExpressionTreeHierarchicalFormatter.cs" /> 137 138 <Compile Include="Interfaces\IReadOnlySymbol.cs" /> 138 139 <Compile Include="Interfaces\ISymbolicExpressionGrammar.cs" /> -
stable/HeuristicLab.Problems.DataAnalysis.Symbolic.Views
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Views merged: 10521-10522,10561,10564,10799,10956
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.cs
r9478 r11120 52 52 Content.ModelChanged += Content_Changed; 53 53 Content.ProblemDataChanged += Content_Changed; 54 treeChart.Repainted += treeChart_Repainted; 54 55 } 55 56 protected override void DeregisterContentEvents() { … … 57 58 Content.ModelChanged -= Content_Changed; 58 59 Content.ProblemDataChanged -= Content_Changed; 60 treeChart.Repainted -= treeChart_Repainted; 59 61 } 60 62 … … 68 70 UpdateView(); 69 71 viewHost.Content = this.Content; 72 } 73 74 private void treeChart_Repainted(object sender, EventArgs e) { 75 if (nodeImpacts != null && nodeImpacts.Count > 0) 76 PaintNodeImpacts(); 70 77 } 71 78 … … 96 103 97 104 private void treeChart_SymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) { 98 var visualNode = (Visual SymbolicExpressionTreeNode)sender;99 var symbExprTreeNode = (SymbolicExpressionTreeNode)visualNode.SymbolicExpressionTreeNode;100 if (symbExprTreeNode == null) return;101 var tree = Content.Model.SymbolicExpressionTree;105 var visualNode = (VisualTreeNode<ISymbolicExpressionTreeNode>)sender; 106 if (visualNode.Content == null) { throw new Exception("Visual node content cannot be null."); } 107 var symbExprTreeNode = (SymbolicExpressionTreeNode)visualNode.Content; 108 if (!foldedNodes.ContainsKey(symbExprTreeNode)) return; // constant nodes cannot be folded 102 109 var parent = symbExprTreeNode.Parent; 103 110 int indexOfSubtree = parent.IndexOfSubtree(symbExprTreeNode); 104 if (foldedNodes.ContainsKey(symbExprTreeNode)) { 105 // undo node folding 106 SwitchNodeWithReplacementNode(parent, indexOfSubtree); 107 } 108 UpdateModel(tree); 111 SwitchNodeWithReplacementNode(parent, indexOfSubtree); 112 UpdateModel(Content.Model.SymbolicExpressionTree); 109 113 } 110 114 111 115 private void SwitchNodeWithReplacementNode(ISymbolicExpressionTreeNode parent, int subTreeIndex) { 112 116 ISymbolicExpressionTreeNode subTree = parent.GetSubtree(subTreeIndex); 113 parent.RemoveSubtree(subTreeIndex);114 117 if (foldedNodes.ContainsKey(subTree)) { 118 parent.RemoveSubtree(subTreeIndex); 115 119 var replacementNode = foldedNodes[subTree]; 116 120 parent.InsertSubtree(subTreeIndex, replacementNode); … … 125 129 double max = impacts.Max(); 126 130 double min = impacts.Min(); 127 foreach ( ISymbolicExpressionTreeNodetreeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) {128 Visual SymbolicExpressionTreeNodevisualTree = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode);131 foreach (var treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) { 132 VisualTreeNode<ISymbolicExpressionTreeNode> visualTree = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode); 129 133 130 134 if (!(treeNode is ConstantTreeNode) && nodeImpacts.ContainsKey(treeNode)) { 135 visualTree.ToolTip = visualTree.Content.ToString(); // to avoid duplicate tooltips 131 136 double impact = nodeImpacts[treeNode]; 132 137 -
stable/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicExpressionTreeChart.cs
r9456 r11120 33 33 internal sealed partial class InteractiveSymbolicExpressionTreeChart : SymbolicExpressionTreeChart { 34 34 private ISymbolicExpressionTreeNode tempNode; // node in clipboard (to be cut/copy/pasted etc) 35 private Visual SymbolicExpressionTreeNodecurrSelected; // currently selected node35 private VisualTreeNode<ISymbolicExpressionTreeNode> currSelected; // currently selected node 36 36 private enum EditOp { NoOp, CopySubtree, CutSubtree } 37 37 private EditOp lastOp = EditOp.NoOp; … … 65 65 pasteToolStripMenuItem.Visible = false; 66 66 } else { 67 var node = currSelected. SymbolicExpressionTreeNode;67 var node = currSelected.Content; 68 68 insertNodeToolStripMenuItem.Visible = true; 69 69 changeNodeToolStripMenuItem.Visible = true; … … 77 77 pasteToolStripMenuItem.Enabled = tempNode != null && insertNodeToolStripMenuItem.Enabled 78 78 && !(lastOp == EditOp.CutSubtree 79 && tempNode.IterateNodesBreadth().Contains(currSelected. SymbolicExpressionTreeNode));79 && tempNode.IterateNodesBreadth().Contains(currSelected.Content)); 80 80 } 81 81 } 82 82 83 83 protected override void OnSymbolicExpressionTreeNodeClicked(object sender, MouseEventArgs e) { 84 currSelected = (Visual SymbolicExpressionTreeNode)sender; ;84 currSelected = (VisualTreeNode<ISymbolicExpressionTreeNode>)sender; ; 85 85 if (currSelected != null) { 86 86 currSelected.LineColor = Color.FromArgb(130, currSelected.LineColor); … … 104 104 105 105 private void insertNodeToolStripMenuItem_Click(object sender, EventArgs e) { 106 if (currSelected == null || currSelected. SymbolicExpressionTreeNodeis SymbolicExpressionTreeTerminalNode) return;107 var parent = currSelected. SymbolicExpressionTreeNode;106 if (currSelected == null || currSelected.Content is SymbolicExpressionTreeTerminalNode) return; 107 var parent = currSelected.Content; 108 108 109 109 using (var dialog = new InsertNodeDialog()) { … … 138 138 if (currSelected == null) return; 139 139 140 var node = (ISymbolicExpressionTreeNode)currSelected. SymbolicExpressionTreeNode.Clone();141 var originalNode = currSelected. SymbolicExpressionTreeNode;140 var node = (ISymbolicExpressionTreeNode)currSelected.Content.Clone(); 141 var originalNode = currSelected.Content; 142 142 143 143 ISymbolicExpressionTreeNode newNode = null; … … 180 180 } 181 181 } 182 tempNode = currSelected. SymbolicExpressionTreeNode;182 tempNode = currSelected.Content; 183 183 foreach (var node in tempNode.IterateNodesPostfix()) { 184 184 var visualNode = GetVisualSymbolicExpressionTreeNode(node); … … 194 194 } 195 195 private void removeNodeToolStripMenuItem_Click(object sender, EventArgs e) { 196 var node = currSelected. SymbolicExpressionTreeNode;196 var node = currSelected.Content; 197 197 if (node == tempNode) tempNode = null; 198 198 ModifyTree(Tree, node.Parent, node, null, removeSubtree: false); … … 200 200 } 201 201 private void removeSubtreeToolStripMenuItem_Click(object sender, EventArgs e) { 202 var node = currSelected. SymbolicExpressionTreeNode;202 var node = currSelected.Content; 203 203 if (node.IterateNodesPostfix().Contains(tempNode)) tempNode = null; 204 204 ModifyTree(Tree, node.Parent, node, null, removeSubtree: true); … … 209 209 if (!(lastOp == EditOp.CopySubtree || lastOp == EditOp.CutSubtree)) return; 210 210 // check if the copied/cut node (stored in the tempNode) can be inserted as a child of the current selected node 211 var node = currSelected. SymbolicExpressionTreeNode;211 var node = currSelected.Content; 212 212 if (node is ConstantTreeNode || node is VariableTreeNode) return; 213 213 // check if the currently selected node can accept the copied node as a child … … 218 218 if (tempNode.IterateNodesBreadth().Contains(node)) 219 219 throw new ArgumentException();// cannot cut/paste a node into itself 220 ModifyTree(Tree, tempNode.Parent, tempNode, null); //remove node from its original parent 220 ModifyTree(Tree, tempNode.Parent, tempNode, null); //remove node from its original parent 221 221 ModifyTree(Tree, node, null, tempNode); //insert it as a child to the new parent 222 222 break; -
stable/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/TextualSymbolicDataAnalysisModelView.designer.cs
r11118 r11120 48 48 this.SuspendLayout(); 49 49 // 50 // expressionTreeView50 // symbolicExpressionTreeView 51 51 // 52 52 this.symbolicExpressionTreeView.AllowDrop = true; … … 55 55 this.symbolicExpressionTreeView.Dock = System.Windows.Forms.DockStyle.Fill; 56 56 this.symbolicExpressionTreeView.Location = new System.Drawing.Point(0, 0); 57 this.symbolicExpressionTreeView.Name = " expressionTreeView";57 this.symbolicExpressionTreeView.Name = "symbolicExpressionTreeView"; 58 58 this.symbolicExpressionTreeView.ReadOnly = false; 59 59 this.symbolicExpressionTreeView.Size = new System.Drawing.Size(352, 413); 60 60 this.symbolicExpressionTreeView.TabIndex = 0; 61 61 // 62 // SymbolicExpressionModelView62 // TextualSymbolicDataAnalysisModelView 63 63 // 64 64 this.AllowDrop = true; 65 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);66 65 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit; 67 66 this.Controls.Add(this.symbolicExpressionTreeView); 68 this.Name = " SymbolicExpressionModelView";67 this.Name = "TextualSymbolicDataAnalysisModelView"; 69 68 this.Size = new System.Drawing.Size(352, 413); 70 69 this.ResumeLayout(false);
Note: See TracChangeset
for help on using the changeset viewer.