Changeset 10561 for trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionTreeChart.cs
- Timestamp:
- 03/06/14 15:07:05 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionTreeChart.cs
r10531 r10561 58 58 } 59 59 60 private ILayoutEngine<ISymbolicExpressionTreeNode> TreeLayoutEngine { 61 get { return layoutEngine; } 62 set { 63 layoutEngine = value; 64 InitializeLayout(); 65 Repaint(); 66 } 67 } 68 60 69 public SymbolicExpressionTreeChart(ISymbolicExpressionTree tree) 61 70 : this() { … … 128 137 if (this.Width <= 1 || this.Height <= 1) 129 138 this.image = new Bitmap(1, 1); 130 else 139 else { 131 140 this.image = new Bitmap(Width, Height); 141 } 132 142 this.Repaint(); 143 } 144 145 public event EventHandler Repainted;//expose this event to notify the parent control that the tree was repainted 146 protected virtual void OnRepaint(object sender, EventArgs e) { 147 var repainted = Repainted; 148 if (repainted != null) { 149 repainted(sender, e); 150 } 133 151 } 134 152 … … 137 155 this.GenerateImage(); 138 156 this.Refresh(); 157 OnRepaint(this, EventArgs.Empty); 139 158 } 140 159 } … … 147 166 foreach (var visualNode in visualTreeNodes.Values) { 148 167 DrawTreeNode(graphics, visualNode); 168 if (visualNode.Content.SubtreeCount > 0) { 169 foreach (var visualSubtree in visualNode.Content.Subtrees.Select(s => visualTreeNodes[s])) { 170 DrawLine(graphics, visualNode, visualSubtree); 171 } 172 } 149 173 } 150 174 } … … 170 194 graphics.Clear(backgroundColor); 171 195 if (tree != null) { 172 DrawFunctionTree( tree,graphics, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance);196 DrawFunctionTree(graphics, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance); 173 197 } 174 198 } … … 236 260 237 261 private void SymbolicExpressionTreeChart_MouseMove(object sender, MouseEventArgs e) { 238 239 262 VisualTreeNode<ISymbolicExpressionTreeNode> visualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y); 240 263 if (draggedSymbolicExpressionTree != null && … … 262 285 #endregion 263 286 287 #region initialize the layout 264 288 private void InitializeLayout() { 265 var actualRoot = tree.Root.SubtreeCount == 1 ? tree.Root.GetSubtree(0) : tree.Root; 289 var actualRoot = tree.Root; 290 if (actualRoot.Symbol is ProgramRootSymbol && actualRoot.SubtreeCount == 1) { 291 actualRoot = tree.Root.GetSubtree(0); 292 } 266 293 layoutEngine.Initialize(actualRoot, n => n.Subtrees, n => n.GetLength(), n => n.GetDepth()); 267 294 layoutEngine.CalculateLayout(this.Width, this.Height); 295 UpdateDictionaries(); 296 } 297 298 private void UpdateDictionaries() { 268 299 var visualNodes = layoutEngine.GetVisualNodes().ToList(); 269 300 //populate the visual nodes and visual connections dictionaries … … 276 307 } 277 308 } 278 309 #endregion 279 310 #region methods for painting the symbolic expression tree 280 private void DrawFunctionTree( ISymbolicExpressionTree symbExprTree,Graphics graphics, int preferredWidth, int preferredHeight, int minHDistance, int minVDistance) {311 private void DrawFunctionTree(Graphics graphics, int preferredWidth, int preferredHeight, int minHDistance, int minVDistance) { 281 312 //we assume here that the layout has already been initialized when the symbolic expression tree was changed 282 313 //recalculate layout according to new node widths and spacing … … 286 317 layoutEngine.VerticalSpacing = minVDistance; 287 318 layoutEngine.CalculateLayout(Width, Height); 319 UpdateDictionaries();//this will reset the visual nodes, therefore colors will be reset to default values 288 320 var visualNodes = visualTreeNodes.Values; 289 321 //draw nodes and connections … … 333 365 } 334 366 } 367 368 protected void DrawLine(Graphics graphics, VisualTreeNode<ISymbolicExpressionTreeNode> startNode, VisualTreeNode<ISymbolicExpressionTreeNode> endNode) { 369 var origin = new Point(startNode.X + startNode.Width / 2, startNode.Y + startNode.Height); 370 var target = new Point(endNode.X + endNode.Width / 2, endNode.Y); 371 graphics.Clip = new Region(new Rectangle(Math.Min(origin.X, target.X), origin.Y, Math.Max(origin.X, target.X), target.Y)); 372 var visualLine = GetVisualSymbolicExpressionTreeNodeConnection(startNode.Content, endNode.Content); 373 using (var linePen = new Pen(visualLine.LineColor)) { 374 linePen.DashStyle = visualLine.DashStyle; 375 graphics.DrawLine(linePen, origin, target); 376 } 377 } 335 378 #endregion 336 379 #region save image … … 348 391 Image image = new Bitmap(Width, Height); 349 392 using (Graphics g = Graphics.FromImage(image)) { 350 DrawFunctionTree( tree,g, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance);393 DrawFunctionTree(g, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance); 351 394 } 352 395 image.Save(filename); … … 358 401 using (Metafile file = new Metafile(filename, g.GetHdc())) { 359 402 using (Graphics emfFile = Graphics.FromImage(file)) { 360 DrawFunctionTree( tree,emfFile, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance);403 DrawFunctionTree(emfFile, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance); 361 404 } 362 405 } … … 377 420 378 421 private void reingoldTilfordToolStripMenuItem_Click(object sender, EventArgs e) { 379 layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> {422 TreeLayoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> { 380 423 NodeWidth = preferredNodeWidth, 381 424 NodeHeight = preferredNodeHeight, … … 383 426 VerticalSpacing = minVerticalDistance 384 427 }; 385 InitializeLayout();386 Repaint();387 428 } 388 429 389 430 private void boxesToolStripMenuItem_Click(object sender, EventArgs e) { 390 layoutEngine = new BoxesLayoutEngine<ISymbolicExpressionTreeNode> {431 TreeLayoutEngine = new BoxesLayoutEngine<ISymbolicExpressionTreeNode> { 391 432 NodeWidth = preferredNodeWidth, 392 433 NodeHeight = preferredNodeHeight, … … 394 435 VerticalSpacing = minVerticalDistance 395 436 }; 396 InitializeLayout();397 Repaint();398 437 } 399 438 }
Note: See TracChangeset
for help on using the changeset viewer.