Changeset 10501 for branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4
- Timestamp:
- 02/21/14 18:20:47 (11 years ago)
- Location:
- branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/GraphicalSymbolicExpressionTreeView.Designer.cs
r9456 r10501 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 -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/GraphicalSymbolicExpressionTreeView.cs
r10347 r10501 20 20 #endregion 21 21 22 using System.Drawing;23 22 using System.Windows.Forms; 24 23 using HeuristicLab.Core.Views; … … 53 52 symbolicExpressionTreeChart.Enabled = Content != null; 54 53 } 55 56 public void HighlightNode(ISymbolicExpressionTreeNode node, Color color) {57 symbolicExpressionTreeChart.HighlightNode(node, color);58 }59 60 public void HighlightSubtree(ISymbolicExpressionTreeNode subtree, Color color) {61 symbolicExpressionTreeChart.HighlightSubtree(subtree, color);62 }63 54 } 64 55 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionTreeChart.Designer.cs
r10302 r10501 50 50 this.saveImageToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); 51 51 this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); 52 this.export LatexToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();52 this.exportPgfLaTeXToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); 53 53 this.contextMenuStrip.SuspendLayout(); 54 54 this.SuspendLayout(); … … 58 58 this.contextMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { 59 59 this.saveImageToolStripMenuItem, 60 this.export LatexToolStripMenuItem});60 this.exportPgfLaTeXToolStripMenuItem}); 61 61 this.contextMenuStrip.Name = "contextMenuStrip"; 62 this.contextMenuStrip.Size = new System.Drawing.Size(16 1, 70);62 this.contextMenuStrip.Size = new System.Drawing.Size(166, 70); 63 63 // 64 64 // saveImageToolStripMenuItem 65 65 // 66 66 this.saveImageToolStripMenuItem.Name = "saveImageToolStripMenuItem"; 67 this.saveImageToolStripMenuItem.Size = new System.Drawing.Size(1 60, 22);67 this.saveImageToolStripMenuItem.Size = new System.Drawing.Size(152, 22); 68 68 this.saveImageToolStripMenuItem.Text = "Save Image"; 69 69 this.saveImageToolStripMenuItem.Click += new System.EventHandler(this.saveImageToolStripMenuItem_Click); … … 73 73 this.saveFileDialog.Filter = "Bitmap (*.bmp)|*.bmp|EMF (*.emf)|*.emf"; 74 74 // 75 // export LatexToolStripMenuItem75 // exportPgfLaTeXToolStripMenuItem 76 76 // 77 this.export LatexToolStripMenuItem.Name = "exportLatexToolStripMenuItem";78 this.export LatexToolStripMenuItem.Size = new System.Drawing.Size(160, 22);79 this.export LatexToolStripMenuItem.Text = "Export Pgf/Latex";80 this.export LatexToolStripMenuItem.Click += new System.EventHandler(this.exportLatexToolStripMenuItem_Click);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 81 // 82 82 // SymbolicExpressionTreeChart … … 102 102 protected System.Windows.Forms.ToolStripMenuItem saveImageToolStripMenuItem; 103 103 protected System.Windows.Forms.SaveFileDialog saveFileDialog; 104 private System.Windows.Forms.ToolStripMenuItem export LatexToolStripMenuItem;104 private System.Windows.Forms.ToolStripMenuItem exportPgfLaTeXToolStripMenuItem; 105 105 } 106 106 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionTreeChart.cs
r10456 r10501 32 32 public partial class SymbolicExpressionTreeChart : UserControl { 33 33 private Image image; 34 private StringFormat stringFormat;34 private readonly StringFormat stringFormat; 35 35 private Dictionary<ISymbolicExpressionTreeNode, VisualSymbolicExpressionTreeNode> visualTreeNodes; 36 36 private Dictionary<Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>, VisualSymbolicExpressionTreeNodeConnection> visualLines; … … 51 51 this.lineColor = Color.Black; 52 52 this.backgroundColor = Color.White; 53 this.textFont = new Font(FontFamily.GenericS erif, 14, GraphicsUnit.Pixel);53 this.textFont = new Font(FontFamily.GenericSansSerif, 12); 54 54 layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>(); 55 55 layoutAdapter = new SymbolicExpressionTreeLayoutAdapter(); … … 108 108 visualLines = new Dictionary<Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>, VisualSymbolicExpressionTreeNodeConnection>(); 109 109 if (tree != null) { 110 foreach (ISymbolicExpressionTreeNode node in tree.IterateNodesPrefix()) { 110 IEnumerable<ISymbolicExpressionTreeNode> nodes; 111 if (tree.Root.SubtreeCount == 1) nodes = tree.Root.GetSubtree(0).IterateNodesPrefix(); 112 else nodes = tree.Root.IterateNodesPrefix(); 113 foreach (ISymbolicExpressionTreeNode node in nodes) { 111 114 visualTreeNodes[node] = new VisualSymbolicExpressionTreeNode(node); 112 115 if (node.Parent != null) visualLines[Tuple.Create(node.Parent, node)] = new VisualSymbolicExpressionTreeNodeConnection(); … … 166 169 this.Refresh(); 167 170 } 168 }169 170 public void HighlightNode(ISymbolicExpressionTreeNode node, Color color) {171 var visualNode = GetVisualSymbolicExpressionTreeNode(node);172 if (visualNode == null) return;173 visualNode.LineColor = color;174 RepaintNode(visualNode);175 }176 177 public void HighlightSubtree(ISymbolicExpressionTreeNode subtree, Color color) {178 foreach (var node in subtree.IterateNodesBreadth())179 HighlightNode(node, color);180 171 } 181 172 … … 280 271 private void DrawFunctionTree(ISymbolicExpressionTree symbolicExpressionTree, Graphics graphics, int preferredWidth, int preferredHeight, int minHDistance, int minVDistance) { 281 272 var layoutNodes = layoutAdapter.Convert(symbolicExpressionTree).ToList(); 273 if (symbolicExpressionTree.Root.SubtreeCount == 1) layoutNodes.RemoveAt(0); 282 274 layoutEngine.Reset(); 283 275 layoutEngine.Root = layoutNodes[0]; 284 foreach (var ln in layoutNodes) 285 layoutEngine.AddNode(ln.Content, ln); 286 layoutEngine.MinHorizontalSpacing = (preferredNodeWidth + minHDistance); 287 layoutEngine.MinVerticalSpacing = (preferredNodeHeight + minVDistance); 276 layoutEngine.AddNodes(layoutNodes); 277 layoutEngine.MinHorizontalSpacing = (preferredWidth + minHDistance); 278 layoutEngine.MinVerticalSpacing = (preferredHeight + minVDistance); 288 279 layoutEngine.CalculateLayout(); 289 280 var bounds = layoutEngine.Bounds(); 290 double sx = this.Width / (bounds.Width + preferredWidth); 291 if (sx > 1) sx = 1; 292 double sy = this.Height / bounds.Height; 293 if (sy > 1) sy = 1; 294 double dx = (this.Width - bounds.Width) / 2; 295 if (dx < 0) dx = 0; 296 double dy = (this.Height - bounds.Height) / 2; 297 if (dy < 0) dy = 0; 298 299 var levels = layoutNodes.GroupBy(n => n.Level, n => n); 300 301 foreach (var level in levels) { 302 var nodes = level.ToList(); 303 double min = 0; 304 for (int i = 0; i < nodes.Count - 1; ++i) { 305 var w = (nodes[i + 1].X - nodes[i].X) * sx - preferredWidth; 306 if (w < min) min = w; 307 } 308 if (min > 0) min = 0; 309 310 foreach (var layoutNode in level) { 281 282 double verticalScalingFactor = 1.0; 283 double layoutHeight = (bounds.Height + preferredHeight); 284 if (this.Height < layoutHeight) 285 verticalScalingFactor = this.Height / layoutHeight; 286 287 double horizontalScalingFactor = 1.0; 288 double layoutWidth = (bounds.Width + preferredWidth); 289 if (this.Width < layoutWidth) 290 horizontalScalingFactor = this.Width / layoutWidth; 291 292 double horizontalOffset; 293 if (this.Width > layoutWidth) 294 horizontalOffset = (this.Width - layoutWidth) / 2.0; 295 else 296 horizontalOffset = preferredWidth / 2.0; 297 298 var levels = layoutNodes.GroupBy(n => n.Level, n => n).ToList(); 299 for (int i = levels.Count - 1; i >= 0; --i) { 300 var nodes = levels[i].ToList(); 301 302 double minSpacing = double.MaxValue; 303 if (nodes.Count > 1) { 304 for (int j = 1; j < nodes.Count() - 1; ++j) { 305 var distance = nodes[j].X - nodes[j - 1].X; // guaranteed to be > 0 306 if (minSpacing > distance) minSpacing = distance; 307 } 308 } 309 minSpacing *= horizontalScalingFactor; 310 311 int minWidth = (int)Math.Round(preferredWidth * horizontalScalingFactor); 312 int width = (int)Math.Min(minSpacing, preferredWidth) - 2; // leave some pixels so that node margins don't overlap 313 314 foreach (var layoutNode in nodes) { 311 315 var visualNode = visualTreeNodes[layoutNode.Content]; 312 visualNode.Width = (int)Math.Round(preferredWidth + min) - 2; // -2 to allow enough padding (1px on each side) for the node contour to be drawn313 visualNode.Height = (int)Math.Round(preferredHeight * sy);314 visualNode.X = (int) (Math.Round(layoutNode.X * sx + dx));315 visualNode.Y = (int) (Math.Round(layoutNode.Y * sy + dy));316 visualNode.Width = width; 317 visualNode.Height = (int)Math.Round(preferredHeight * verticalScalingFactor); 318 visualNode.X = (int)Math.Round((layoutNode.X + horizontalOffset) * horizontalScalingFactor + (minWidth - width) / 2.0); 319 visualNode.Y = (int)Math.Round(layoutNode.Y * verticalScalingFactor); 316 320 DrawTreeNode(graphics, visualNode); 317 321 } 318 322 } 319 320 graphics.ResetClip(); // reset clip region321 323 // draw node connections 322 324 foreach (var visualNode in visualTreeNodes.Values) { … … 327 329 var origin = new Point(visualNode.X + visualNode.Width / 2, visualNode.Y + visualNode.Height); 328 330 var target = new Point(visualSubtree.X + visualSubtree.Width / 2, visualSubtree.Y); 331 graphics.Clip = new Region(new Rectangle(Math.Min(origin.X, target.X), origin.Y, Math.Max(origin.X, target.X), target.Y)); 329 332 using (var linePen = new Pen(visualLine.LineColor)) { 330 333 linePen.DashStyle = visualLine.DashStyle; … … 378 381 Image image = new Bitmap(Width, Height); 379 382 using (Graphics g = Graphics.FromImage(image)) { 380 int height = this.Height / tree.Depth; 381 DrawFunctionTree(tree, g, 0, 0, Width, height); 383 DrawFunctionTree(tree, g, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance); 382 384 } 383 385 image.Save(filename); … … 386 388 public void SaveImageAsEmf(string filename) { 387 389 if (tree == null) return; 388 using (var graphics = Graphics.FromImage(image)) { 389 var rectangle = new Rectangle(0, 0, image.Width, image.Height); 390 using (var metafile = new Metafile(filename, graphics.GetHdc(), rectangle, MetafileFrameUnit.Pixel, EmfType.EmfPlusDual)) { 391 graphics.ReleaseHdc(); 392 using (var g = Graphics.FromImage(metafile)) { 393 DrawFunctionTree(tree, g, 0, 0, image.Width, image.Height); 390 using (Graphics g = CreateGraphics()) { 391 using (Metafile file = new Metafile(filename, g.GetHdc())) { 392 using (Graphics emfFile = Graphics.FromImage(file)) { 393 DrawFunctionTree(tree, emfFile, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance); 394 394 } 395 395 } 396 g.ReleaseHdc(); 396 397 } 397 398 }
Note: See TracChangeset
for help on using the changeset viewer.