Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionTreeChart.cs @ 10616

Last change on this file since 10616 was 10565, checked in by mkommend, 11 years ago

#2076: Simplified the API of the tree layout engines.

File size: 18.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Drawing.Imaging;
26using System.IO;
27using System.Linq;
28using System.Windows.Forms;
29
30
31namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views {
32  public partial class SymbolicExpressionTreeChart : UserControl {
33    private Image image;
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 const int minHorizontalDistance = 30;
42    private const int minVerticalDistance = 30;
43
44    public SymbolicExpressionTreeChart() {
45      InitializeComponent();
46      this.image = new Bitmap(Width, Height);
47      this.stringFormat = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center };
48      this.spacing = 5;
49      this.lineColor = Color.Black;
50      this.backgroundColor = Color.White;
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;
63    }
64
65    //private ILayoutEngine<ISymbolicExpressionTreeNode> TreeLayoutEngine {
66    //  get { return layoutEngine; }
67    //  set {
68    //    layoutEngine = value;
69    //    InitializeLayout();
70    //    Repaint();
71    //  }
72    //}
73
74    public SymbolicExpressionTreeChart(ISymbolicExpressionTree tree)
75      : this() {
76      this.Tree = tree;
77    }
78
79    #region Public properties
80    private int spacing;
81    public int Spacing {
82      get { return this.spacing; }
83      set {
84        this.spacing = value;
85        this.Repaint();
86      }
87    }
88
89    private Color lineColor;
90    public Color LineColor {
91      get { return this.lineColor; }
92      set {
93        this.lineColor = value;
94        this.Repaint();
95      }
96    }
97
98    private Color backgroundColor;
99    public Color BackgroundColor {
100      get { return this.backgroundColor; }
101      set {
102        this.backgroundColor = value;
103        this.Repaint();
104      }
105    }
106
107    private Font textFont;
108    public Font TextFont {
109      get { return this.textFont; }
110      set {
111        this.textFont = value;
112        this.Repaint();
113      }
114    }
115
116    private ISymbolicExpressionTree tree;
117    public ISymbolicExpressionTree Tree {
118      get { return this.tree; }
119      set {
120        tree = value;
121        Repaint();
122      }
123    }
124
125    private bool suspendRepaint;
126    public bool SuspendRepaint {
127      get { return suspendRepaint; }
128      set { suspendRepaint = value; }
129    }
130    #endregion
131
132    protected override void OnPaint(PaintEventArgs e) {
133      e.Graphics.DrawImage(image, 0, 0);
134      base.OnPaint(e);
135    }
136    protected override void OnResize(EventArgs e) {
137      base.OnResize(e);
138      if (this.Width <= 1 || this.Height <= 1)
139        this.image = new Bitmap(1, 1);
140      else {
141        this.image = new Bitmap(Width, Height);
142      }
143      this.Repaint();
144    }
145
146    public event EventHandler Repainted;//expose this event to notify the parent control that the tree was repainted
147    protected virtual void OnRepaint(object sender, EventArgs e) {
148      var repainted = Repainted;
149      if (repainted != null) {
150        repainted(sender, e);
151      }
152    }
153
154    public void Repaint() {
155      if (!suspendRepaint) {
156        this.GenerateImage();
157        this.Refresh();
158        OnRepaint(this, EventArgs.Empty);
159      }
160    }
161
162    public void RepaintNodes() {
163      if (!suspendRepaint) {
164        using (var graphics = Graphics.FromImage(image)) {
165          graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
166          graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
167          foreach (var visualNode in visualTreeNodes.Values) {
168            DrawTreeNode(graphics, visualNode);
169            if (visualNode.Content.SubtreeCount > 0) {
170              foreach (var visualSubtree in visualNode.Content.Subtrees.Select(s => visualTreeNodes[s])) {
171                DrawLine(graphics, visualNode, visualSubtree);
172              }
173            }
174          }
175        }
176        this.Refresh();
177      }
178    }
179
180    public void RepaintNode(VisualTreeNode<ISymbolicExpressionTreeNode> visualNode) {
181      if (!suspendRepaint) {
182        using (var graphics = Graphics.FromImage(image)) {
183          graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
184          graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
185          DrawTreeNode(graphics, visualNode);
186        }
187        this.Refresh();
188      }
189    }
190
191    private void GenerateImage() {
192      using (Graphics graphics = Graphics.FromImage(image)) {
193        graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
194        graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
195        graphics.Clear(backgroundColor);
196        if (tree != null) {
197          DrawFunctionTree(graphics, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance);
198        }
199      }
200    }
201
202    public VisualTreeNode<ISymbolicExpressionTreeNode> GetVisualSymbolicExpressionTreeNode(ISymbolicExpressionTreeNode symbolicExpressionTreeNode) {
203      if (visualTreeNodes.ContainsKey(symbolicExpressionTreeNode))
204        return visualTreeNodes[symbolicExpressionTreeNode];
205      return null;
206    }
207
208    public VisualTreeNodeConnection GetVisualSymbolicExpressionTreeNodeConnection(ISymbolicExpressionTreeNode parent, ISymbolicExpressionTreeNode child) {
209      if (child.Parent != parent) throw new ArgumentException();
210      var key = Tuple.Create(parent, child);
211      VisualTreeNodeConnection connection = null;
212      visualLines.TryGetValue(key, out connection);
213      return connection;
214    }
215
216    #region events
217    public event MouseEventHandler SymbolicExpressionTreeNodeClicked;
218    protected virtual void OnSymbolicExpressionTreeNodeClicked(object sender, MouseEventArgs e) {
219      var clicked = SymbolicExpressionTreeNodeClicked;
220      if (clicked != null)
221        clicked(sender, e);
222    }
223
224    protected virtual void SymbolicExpressionTreeChart_MouseClick(object sender, MouseEventArgs e) {
225      var visualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);
226      if (visualTreeNode != null) {
227        OnSymbolicExpressionTreeNodeClicked(visualTreeNode, e);
228      }
229    }
230
231    public event MouseEventHandler SymbolicExpressionTreeNodeDoubleClicked;
232    protected virtual void OnSymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) {
233      var doubleClicked = SymbolicExpressionTreeNodeDoubleClicked;
234      if (doubleClicked != null)
235        doubleClicked(sender, e);
236    }
237
238    protected virtual void SymbolicExpressionTreeChart_MouseDoubleClick(object sender, MouseEventArgs e) {
239      VisualTreeNode<ISymbolicExpressionTreeNode> visualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);
240      if (visualTreeNode != null)
241        OnSymbolicExpressionTreeNodeDoubleClicked(visualTreeNode, e);
242    }
243
244    public event ItemDragEventHandler SymbolicExpressionTreeNodeDrag;
245    protected virtual void OnSymbolicExpressionTreeNodeDragDrag(object sender, ItemDragEventArgs e) {
246      var dragged = SymbolicExpressionTreeNodeDrag;
247      if (dragged != null)
248        dragged(sender, e);
249    }
250
251    private VisualTreeNode<ISymbolicExpressionTreeNode> draggedSymbolicExpressionTree;
252    private MouseButtons dragButtons;
253    private void SymbolicExpressionTreeChart_MouseDown(object sender, MouseEventArgs e) {
254      this.dragButtons = e.Button;
255      this.draggedSymbolicExpressionTree = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);
256    }
257    private void SymbolicExpressionTreeChart_MouseUp(object sender, MouseEventArgs e) {
258      this.draggedSymbolicExpressionTree = null;
259      this.dragButtons = MouseButtons.None;
260    }
261
262    private void SymbolicExpressionTreeChart_MouseMove(object sender, MouseEventArgs e) {
263      VisualTreeNode<ISymbolicExpressionTreeNode> visualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);
264      if (draggedSymbolicExpressionTree != null &&
265        draggedSymbolicExpressionTree != visualTreeNode) {
266        OnSymbolicExpressionTreeNodeDragDrag(draggedSymbolicExpressionTree, new ItemDragEventArgs(dragButtons, draggedSymbolicExpressionTree));
267        draggedSymbolicExpressionTree = null;
268      } else if (draggedSymbolicExpressionTree == null &&
269        visualTreeNode != null) {
270        string tooltipText = visualTreeNode.ToolTip;
271        if (this.toolTip.GetToolTip(this) != tooltipText)
272          this.toolTip.SetToolTip(this, tooltipText);
273
274      } else if (visualTreeNode == null)
275        this.toolTip.SetToolTip(this, "");
276    }
277
278    public VisualTreeNode<ISymbolicExpressionTreeNode> FindVisualSymbolicExpressionTreeNodeAt(int x, int y) {
279      foreach (var visualTreeNode in visualTreeNodes.Values) {
280        if (x >= visualTreeNode.X && x <= visualTreeNode.X + visualTreeNode.Width &&
281            y >= visualTreeNode.Y && y <= visualTreeNode.Y + visualTreeNode.Height)
282          return visualTreeNode;
283      }
284      return null;
285    }
286    #endregion
287
288    private void CalculateLayout(int preferredWidth, int preferredHeight, int minHDistance, int minVDistance) {
289      layoutEngine.NodeWidth = preferredWidth;
290      layoutEngine.NodeHeight = preferredHeight;
291      layoutEngine.HorizontalSpacing = minHDistance;
292      layoutEngine.VerticalSpacing = minVDistance;
293
294      var actualRoot = tree.Root;
295      if (actualRoot.Symbol is ProgramRootSymbol && actualRoot.SubtreeCount == 1) {
296        actualRoot = tree.Root.GetSubtree(0);
297      }
298
299      var visualNodes = layoutEngine.CalculateLayout(actualRoot, Width, Height).ToList();
300      visualTreeNodes = visualNodes.ToDictionary(x => x.Content, x => x);
301      visualLines = new Dictionary<Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>, VisualTreeNodeConnection>();
302      foreach (var node in visualNodes.Select(n => n.Content)) {
303        foreach (var subtree in node.Subtrees) {
304          visualLines.Add(new Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>(node, subtree), new VisualTreeNodeConnection());
305        }
306      }
307    }
308
309    #region methods for painting the symbolic expression tree
310    private void DrawFunctionTree(Graphics graphics, int preferredWidth, int preferredHeight, int minHDistance, int minVDistance) {
311      CalculateLayout(preferredWidth, preferredHeight, minHDistance, minVDistance);
312      var visualNodes = visualTreeNodes.Values;
313      //draw nodes and connections
314      foreach (var visualNode in visualNodes) {
315        DrawTreeNode(visualNode);
316        var node = visualNode.Content;
317        foreach (var subtree in node.Subtrees) {
318          var visualLine = GetVisualSymbolicExpressionTreeNodeConnection(node, subtree);
319          var visualSubtree = visualTreeNodes[subtree];
320          var origin = new Point(visualNode.X + visualNode.Width / 2, visualNode.Y + visualNode.Height);
321          var target = new Point(visualSubtree.X + visualSubtree.Width / 2, visualSubtree.Y);
322          graphics.Clip = new Region(new Rectangle(Math.Min(origin.X, target.X), origin.Y, Math.Max(origin.X, target.X), target.Y));
323          using (var linePen = new Pen(visualLine.LineColor)) {
324            linePen.DashStyle = visualLine.DashStyle;
325            graphics.DrawLine(linePen, origin, target);
326          }
327        }
328      }
329    }
330
331    protected void DrawTreeNode(VisualTreeNode<ISymbolicExpressionTreeNode> visualTreeNode) {
332      using (var graphics = Graphics.FromImage(image)) {
333        graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
334        graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
335        DrawTreeNode(graphics, visualTreeNode);
336      }
337    }
338
339    protected void DrawTreeNode(Graphics graphics, VisualTreeNode<ISymbolicExpressionTreeNode> visualTreeNode) {
340      graphics.Clip = new Region(new Rectangle(visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width + 1, visualTreeNode.Height + 1));
341      graphics.Clear(backgroundColor);
342      var node = visualTreeNode.Content;
343      using (var textBrush = new SolidBrush(visualTreeNode.TextColor))
344      using (var nodeLinePen = new Pen(visualTreeNode.LineColor))
345      using (var nodeFillBrush = new SolidBrush(visualTreeNode.FillColor)) {
346        //draw terminal node
347        if (node.SubtreeCount == 0) {
348          graphics.FillRectangle(nodeFillBrush, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height);
349          graphics.DrawRectangle(nodeLinePen, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height);
350        } else {
351          graphics.FillEllipse(nodeFillBrush, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height);
352          graphics.DrawEllipse(nodeLinePen, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height);
353        }
354        //draw name of symbol
355        var text = node.ToString();
356        graphics.DrawString(text, textFont, textBrush, new RectangleF(visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height), stringFormat);
357      }
358    }
359
360    protected void DrawLine(Graphics graphics, VisualTreeNode<ISymbolicExpressionTreeNode> startNode, VisualTreeNode<ISymbolicExpressionTreeNode> endNode) {
361      var origin = new Point(startNode.X + startNode.Width / 2, startNode.Y + startNode.Height);
362      var target = new Point(endNode.X + endNode.Width / 2, endNode.Y);
363      graphics.Clip = new Region(new Rectangle(Math.Min(origin.X, target.X), origin.Y, Math.Max(origin.X, target.X), target.Y));
364      var visualLine = GetVisualSymbolicExpressionTreeNodeConnection(startNode.Content, endNode.Content);
365      using (var linePen = new Pen(visualLine.LineColor)) {
366        linePen.DashStyle = visualLine.DashStyle;
367        graphics.DrawLine(linePen, origin, target);
368      }
369    }
370    #endregion
371    #region save image
372    private void saveImageToolStripMenuItem_Click(object sender, EventArgs e) {
373      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
374        string filename = saveFileDialog.FileName.ToLower();
375        if (filename.EndsWith("bmp")) SaveImageAsBitmap(filename);
376        else if (filename.EndsWith("emf")) SaveImageAsEmf(filename);
377        else SaveImageAsBitmap(filename);
378      }
379    }
380
381    public void SaveImageAsBitmap(string filename) {
382      if (tree == null) return;
383      Image image = new Bitmap(Width, Height);
384      using (Graphics g = Graphics.FromImage(image)) {
385        DrawFunctionTree(g, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance);
386      }
387      image.Save(filename);
388    }
389
390    public void SaveImageAsEmf(string filename) {
391      if (tree == null) return;
392      using (Graphics g = CreateGraphics()) {
393        using (Metafile file = new Metafile(filename, g.GetHdc())) {
394          using (Graphics emfFile = Graphics.FromImage(file)) {
395            DrawFunctionTree(emfFile, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance);
396          }
397        }
398        g.ReleaseHdc();
399      }
400    }
401    #endregion
402    #region export pgf/tikz
403    private void exportLatexToolStripMenuItem_Click(object sender, EventArgs e) {
404      using (var dialog = new SaveFileDialog { Filter = "Tex (*.tex)|*.tex" }) {
405        if (dialog.ShowDialog() != DialogResult.OK) return;
406        string filename = dialog.FileName.ToLower();
407        var formatter = new SymbolicExpressionTreeLatexFormatter();
408        File.WriteAllText(filename, formatter.Format(Tree));
409      }
410    }
411    #endregion
412
413    private void reingoldTilfordToolStripMenuItem_Click(object sender, EventArgs e) {
414      layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>(n => n.Subtrees) {
415        NodeWidth = preferredNodeWidth,
416        NodeHeight = preferredNodeHeight,
417        HorizontalSpacing = minHorizontalDistance,
418        VerticalSpacing = minVerticalDistance
419      };
420      reingoldTilfordToolStripMenuItem.Checked = true;
421      boxesToolStripMenuItem.Checked = false;
422      Repaint();
423    }
424
425    private void boxesToolStripMenuItem_Click(object sender, EventArgs e) {
426      layoutEngine = new BoxesLayoutEngine<ISymbolicExpressionTreeNode>(n => n.Subtrees, n => n.GetLength(), n => n.GetDepth()) {
427        NodeWidth = preferredNodeWidth,
428        NodeHeight = preferredNodeHeight,
429        HorizontalSpacing = minHorizontalDistance,
430        VerticalSpacing = minVerticalDistance
431      };
432      reingoldTilfordToolStripMenuItem.Checked = false;
433      boxesToolStripMenuItem.Checked = true;
434      Repaint();
435    }
436  }
437}
Note: See TracBrowser for help on using the repository browser.