Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2929_PrioritizedGrammarEnumeration/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionTreeChart.cs @ 16824

Last change on this file since 16824 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 18.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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    public SymbolicExpressionTreeChart() {
40      InitializeComponent();
41      this.image = new Bitmap(Width, Height);
42      this.stringFormat = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center };
43      this.lineColor = Color.Black;
44      this.backgroundColor = Color.White;
45      this.textFont = new Font(FontFamily.GenericSansSerif, 12);
46
47      visualTreeNodes = new Dictionary<ISymbolicExpressionTreeNode, VisualTreeNode<ISymbolicExpressionTreeNode>>();
48      visualLines = new Dictionary<Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>, VisualTreeNodeConnection>();
49
50      layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>(n => n.Subtrees) {
51        NodeWidth = PreferredNodeWidth,
52        NodeHeight = PreferredNodeHeight,
53        HorizontalSpacing = MinimumHorizontalDistance,
54        VerticalSpacing = MinimumVerticalDistance
55      };
56      reingoldTilfordToolStripMenuItem.Checked = true;
57    }
58
59    public SymbolicExpressionTreeChart(ISymbolicExpressionTree tree)
60      : this() {
61      this.Tree = tree;
62    }
63
64    #region Public properties
65    private int preferredNodeWidth = 70;
66    public int PreferredNodeWidth {
67      get { return preferredNodeWidth; }
68      set {
69        preferredNodeWidth = value;
70        Repaint();
71      }
72    }
73
74    private int preferredNodeHeight = 46;
75    public int PreferredNodeHeight {
76      get { return preferredNodeHeight; }
77      set {
78        preferredNodeHeight = value;
79        Repaint();
80      }
81    }
82
83    private int minHorizontalDistance = 30;
84    public int MinimumHorizontalDistance {
85      get { return minHorizontalDistance; }
86      set {
87        minHorizontalDistance = value;
88        Repaint();
89      }
90    }
91
92    private int minVerticalDistance = 30;
93    public int MinimumVerticalDistance {
94      get { return minVerticalDistance; }
95      set {
96        minVerticalDistance = value;
97        Repaint();
98      }
99    }
100
101    private int minHorizontalPadding = 20;
102    public int MinimumHorizontalPadding {
103      get { return minHorizontalPadding; }
104      set {
105        minHorizontalPadding = value;
106        Repaint();
107      }
108    }
109
110    private int minVerticalPadding = 20;
111    public int MinimumVerticalPadding {
112      get { return minVerticalPadding; }
113      set {
114        minVerticalPadding = value;
115        Repaint();
116      }
117    }
118
119    private Color lineColor;
120    public Color LineColor {
121      get { return this.lineColor; }
122      set {
123        this.lineColor = value;
124        this.Repaint();
125      }
126    }
127
128    private Color backgroundColor;
129    public Color BackgroundColor {
130      get { return this.backgroundColor; }
131      set {
132        this.backgroundColor = value;
133        this.Repaint();
134      }
135    }
136
137    private Font textFont;
138    public Font TextFont {
139      get { return this.textFont; }
140      set {
141        this.textFont = value;
142        this.Repaint();
143      }
144    }
145
146    private ISymbolicExpressionTree tree;
147    public ISymbolicExpressionTree Tree {
148      get { return this.tree; }
149      set {
150        tree = value;
151        Repaint();
152      }
153    }
154
155    private bool suspendRepaint;
156    public bool SuspendRepaint {
157      get { return suspendRepaint; }
158      set { suspendRepaint = value; }
159    }
160    #endregion
161
162    protected override void OnPaint(PaintEventArgs e) {
163      e.Graphics.DrawImage(image, 0, 0);
164      base.OnPaint(e);
165    }
166    protected override void OnResize(EventArgs e) {
167      base.OnResize(e);
168      if (this.Width <= 1 || this.Height <= 1)
169        this.image = new Bitmap(1, 1);
170      else {
171        this.image = new Bitmap(Width, Height);
172      }
173      this.Repaint();
174    }
175
176    public event EventHandler Repainted;//expose this event to notify the parent control that the tree was repainted
177    protected virtual void OnRepaint(object sender, EventArgs e) {
178      var repainted = Repainted;
179      if (repainted != null) {
180        repainted(sender, e);
181      }
182    }
183
184    public void Repaint() {
185      if (!suspendRepaint) {
186        this.GenerateImage();
187        this.Refresh();
188        OnRepaint(this, EventArgs.Empty);
189      }
190    }
191
192    public void RepaintNodes() {
193      if (!suspendRepaint) {
194        using (var graphics = Graphics.FromImage(image)) {
195          graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
196          graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
197          foreach (var visualNode in visualTreeNodes.Values) {
198            DrawTreeNode(graphics, visualNode);
199            if (visualNode.Content.SubtreeCount > 0) {
200              foreach (var visualSubtree in visualNode.Content.Subtrees.Select(s => visualTreeNodes[s])) {
201                DrawLine(graphics, visualNode, visualSubtree);
202              }
203            }
204          }
205        }
206        this.Refresh();
207      }
208    }
209
210    public void RepaintNode(VisualTreeNode<ISymbolicExpressionTreeNode> visualNode) {
211      if (!suspendRepaint) {
212        using (var graphics = Graphics.FromImage(image)) {
213          graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
214          graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
215          DrawTreeNode(graphics, visualNode);
216        }
217        this.Refresh();
218      }
219    }
220
221    private void GenerateImage() {
222      using (Graphics graphics = Graphics.FromImage(image)) {
223        graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
224        graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
225        graphics.Clear(backgroundColor);
226        if (tree != null) {
227          DrawFunctionTree(graphics, PreferredNodeWidth, PreferredNodeHeight, MinimumHorizontalDistance, MinimumVerticalDistance);
228        }
229      }
230    }
231
232    public VisualTreeNode<ISymbolicExpressionTreeNode> GetVisualSymbolicExpressionTreeNode(ISymbolicExpressionTreeNode symbolicExpressionTreeNode) {
233      if (visualTreeNodes.ContainsKey(symbolicExpressionTreeNode))
234        return visualTreeNodes[symbolicExpressionTreeNode];
235      return null;
236    }
237
238    public VisualTreeNodeConnection GetVisualSymbolicExpressionTreeNodeConnection(ISymbolicExpressionTreeNode parent, ISymbolicExpressionTreeNode child) {
239      if (child.Parent != parent) throw new ArgumentException();
240      var key = Tuple.Create(parent, child);
241      VisualTreeNodeConnection connection = null;
242      visualLines.TryGetValue(key, out connection);
243      return connection;
244    }
245
246    #region events
247    public event MouseEventHandler SymbolicExpressionTreeNodeClicked;
248    protected virtual void OnSymbolicExpressionTreeNodeClicked(object sender, MouseEventArgs e) {
249      var clicked = SymbolicExpressionTreeNodeClicked;
250      if (clicked != null)
251        clicked(sender, e);
252    }
253
254    protected virtual void SymbolicExpressionTreeChart_MouseClick(object sender, MouseEventArgs e) {
255      var visualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);
256      if (visualTreeNode != null) {
257        OnSymbolicExpressionTreeNodeClicked(visualTreeNode, e);
258      }
259    }
260
261    public event MouseEventHandler SymbolicExpressionTreeNodeDoubleClicked;
262    protected virtual void OnSymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) {
263      var doubleClicked = SymbolicExpressionTreeNodeDoubleClicked;
264      if (doubleClicked != null)
265        doubleClicked(sender, e);
266    }
267
268    protected virtual void SymbolicExpressionTreeChart_MouseDoubleClick(object sender, MouseEventArgs e) {
269      VisualTreeNode<ISymbolicExpressionTreeNode> visualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);
270      if (visualTreeNode != null)
271        OnSymbolicExpressionTreeNodeDoubleClicked(visualTreeNode, e);
272    }
273
274    public event ItemDragEventHandler SymbolicExpressionTreeNodeDrag;
275    protected virtual void OnSymbolicExpressionTreeNodeDragDrag(object sender, ItemDragEventArgs e) {
276      var dragged = SymbolicExpressionTreeNodeDrag;
277      if (dragged != null)
278        dragged(sender, e);
279    }
280
281    private VisualTreeNode<ISymbolicExpressionTreeNode> draggedSymbolicExpressionTree;
282    private MouseButtons dragButtons;
283    private void SymbolicExpressionTreeChart_MouseDown(object sender, MouseEventArgs e) {
284      this.dragButtons = e.Button;
285      this.draggedSymbolicExpressionTree = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);
286    }
287    private void SymbolicExpressionTreeChart_MouseUp(object sender, MouseEventArgs e) {
288      this.draggedSymbolicExpressionTree = null;
289      this.dragButtons = MouseButtons.None;
290    }
291
292    private void SymbolicExpressionTreeChart_MouseMove(object sender, MouseEventArgs e) {
293      VisualTreeNode<ISymbolicExpressionTreeNode> visualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);
294      if (draggedSymbolicExpressionTree != null &&
295        draggedSymbolicExpressionTree != visualTreeNode) {
296        OnSymbolicExpressionTreeNodeDragDrag(draggedSymbolicExpressionTree, new ItemDragEventArgs(dragButtons, draggedSymbolicExpressionTree));
297        draggedSymbolicExpressionTree = null;
298      } else if (draggedSymbolicExpressionTree == null &&
299        visualTreeNode != null) {
300        string tooltipText = visualTreeNode.ToolTip;
301        if (this.toolTip.GetToolTip(this) != tooltipText)
302          this.toolTip.SetToolTip(this, tooltipText);
303
304      } else if (visualTreeNode == null)
305        this.toolTip.SetToolTip(this, "");
306    }
307
308    public VisualTreeNode<ISymbolicExpressionTreeNode> FindVisualSymbolicExpressionTreeNodeAt(int x, int y) {
309      foreach (var visualTreeNode in visualTreeNodes.Values) {
310        if (x >= visualTreeNode.X && x <= visualTreeNode.X + visualTreeNode.Width &&
311            y >= visualTreeNode.Y && y <= visualTreeNode.Y + visualTreeNode.Height)
312          return visualTreeNode;
313      }
314      return null;
315    }
316    #endregion
317
318    private void CalculateLayout(int preferredWidth, int preferredHeight, int minHDistance, int minVDistance) {
319      layoutEngine.NodeWidth = preferredWidth;
320      layoutEngine.NodeHeight = preferredHeight;
321      layoutEngine.HorizontalSpacing = minHDistance;
322      layoutEngine.VerticalSpacing = minVDistance;
323
324      var actualRoot = tree.Root;
325      if (actualRoot.Symbol is ProgramRootSymbol && actualRoot.SubtreeCount == 1) {
326        actualRoot = tree.Root.GetSubtree(0);
327      }
328      var visualNodes = layoutEngine.CalculateLayout(actualRoot, Width - MinimumHorizontalPadding, Height - MinimumVerticalPadding).ToList();
329      // add the padding
330      foreach (var vn in visualNodes) {
331        vn.X += MinimumHorizontalPadding / 2;
332        vn.Y += MinimumVerticalPadding / 2;
333      }
334
335      visualTreeNodes = visualNodes.ToDictionary(x => x.Content, x => x);
336      visualLines = new Dictionary<Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>, VisualTreeNodeConnection>();
337      foreach (var node in visualNodes.Select(n => n.Content)) {
338        foreach (var subtree in node.Subtrees) {
339          visualLines.Add(new Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>(node, subtree), new VisualTreeNodeConnection());
340        }
341      }
342    }
343
344    #region methods for painting the symbolic expression tree
345    private void DrawFunctionTree(Graphics graphics, int preferredWidth, int preferredHeight, int minHDistance, int minVDistance, bool recalculateLayout = true) {
346      if (recalculateLayout)
347        CalculateLayout(preferredWidth, preferredHeight, minHDistance, minVDistance);
348      var visualNodes = visualTreeNodes.Values;
349      //draw nodes and connections
350      foreach (var visualNode in visualNodes) {
351        DrawTreeNode(graphics, visualNode);
352        var node = visualNode.Content;
353        foreach (var subtree in node.Subtrees) {
354          var visualLine = GetVisualSymbolicExpressionTreeNodeConnection(node, subtree);
355          var visualSubtree = visualTreeNodes[subtree];
356          var origin = new Point(visualNode.X + visualNode.Width / 2, visualNode.Y + visualNode.Height);
357          var target = new Point(visualSubtree.X + visualSubtree.Width / 2, visualSubtree.Y);
358          graphics.Clip = new Region(new Rectangle(Math.Min(origin.X, target.X), origin.Y, Math.Max(origin.X, target.X), target.Y));
359          using (var linePen = new Pen(visualLine.LineColor)) {
360            linePen.DashStyle = visualLine.DashStyle;
361            graphics.DrawLine(linePen, origin, target);
362          }
363        }
364      }
365    }
366
367    private Action<Brush, int, int, int, int> fill;
368    private Action<Pen, int, int, int, int> draw;
369    protected void DrawTreeNode(Graphics graphics, VisualTreeNode<ISymbolicExpressionTreeNode> visualTreeNode) {
370      graphics.Clip = new Region(new Rectangle(visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width + 1, visualTreeNode.Height + 1));
371      graphics.Clear(backgroundColor);
372      var node = visualTreeNode.Content;
373      using (var textBrush = new SolidBrush(visualTreeNode.TextColor))
374      using (var nodeLinePen = new Pen(visualTreeNode.LineColor))
375      using (var nodeFillBrush = new SolidBrush(visualTreeNode.FillColor)) {
376        var x = visualTreeNode.X;
377        var y = visualTreeNode.Y;
378        var w = visualTreeNode.Width - 1;  // allow 1px for the drawing of the line
379        var h = visualTreeNode.Height - 1; // allow 1px for the drawing of the line
380        // draw leaf nodes as rectangles and internal nodes as ellipses
381        if (node.SubtreeCount > 0) {
382          fill = graphics.FillEllipse; draw = graphics.DrawEllipse;
383        } else {
384          fill = graphics.FillRectangle; draw = graphics.DrawRectangle;
385        }
386        fill(nodeFillBrush, x, y, w, h);
387        draw(nodeLinePen, x, y, w, h);
388        //draw name of symbol
389        graphics.DrawString(node.ToString(), textFont, textBrush, new RectangleF(x, y, w, h), stringFormat);
390      }
391    }
392
393    protected void DrawLine(Graphics graphics, VisualTreeNode<ISymbolicExpressionTreeNode> startNode, VisualTreeNode<ISymbolicExpressionTreeNode> endNode) {
394      var origin = new Point(startNode.X + startNode.Width / 2, startNode.Y + startNode.Height);
395      var target = new Point(endNode.X + endNode.Width / 2, endNode.Y);
396      graphics.Clip = new Region(new Rectangle(Math.Min(origin.X, target.X), origin.Y, Math.Max(origin.X, target.X), target.Y));
397      var visualLine = GetVisualSymbolicExpressionTreeNodeConnection(startNode.Content, endNode.Content);
398      using (var linePen = new Pen(visualLine.LineColor)) {
399        linePen.DashStyle = visualLine.DashStyle;
400        graphics.DrawLine(linePen, origin, target);
401      }
402    }
403    #endregion
404    #region save image
405    private void saveImageToolStripMenuItem_Click(object sender, EventArgs e) {
406      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
407        string filename = saveFileDialog.FileName.ToLower();
408        if (filename.EndsWith("bmp")) SaveImageAsBitmap(filename);
409        else if (filename.EndsWith("emf")) SaveImageAsEmf(filename);
410        else SaveImageAsBitmap(filename);
411      }
412    }
413
414    public void SaveImageAsBitmap(string filename) {
415      if (tree == null) return;
416      Image image = new Bitmap(Width, Height);
417      using (Graphics g = Graphics.FromImage(image)) {
418        DrawFunctionTree(g, PreferredNodeWidth, PreferredNodeHeight, MinimumHorizontalDistance, MinimumVerticalDistance, false);
419      }
420      image.Save(filename);
421    }
422
423    public void SaveImageAsEmf(string filename) {
424      if (tree == null) return;
425      using (Graphics g = CreateGraphics()) {
426        using (Metafile file = new Metafile(filename, g.GetHdc())) {
427          using (Graphics emfFile = Graphics.FromImage(file)) {
428            DrawFunctionTree(emfFile, PreferredNodeWidth, PreferredNodeHeight, MinimumHorizontalDistance, MinimumVerticalDistance, false);
429          }
430        }
431        g.ReleaseHdc();
432      }
433    }
434    #endregion
435    #region export pgf/tikz
436    private void exportLatexToolStripMenuItem_Click(object sender, EventArgs e) {
437      var t = Tree;
438      if (t == null) return;
439      using (var dialog = new SaveFileDialog { Filter = "Tex (*.tex)|*.tex" }) {
440        if (dialog.ShowDialog() != DialogResult.OK) return;
441        string filename = dialog.FileName.ToLower();
442        var formatter = new SymbolicExpressionTreeLatexFormatter();
443        File.WriteAllText(filename, formatter.Format(t));
444      }
445    }
446    #endregion
447
448    private void reingoldTilfordToolStripMenuItem_Click(object sender, EventArgs e) {
449      layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>(n => n.Subtrees) {
450        NodeWidth = PreferredNodeWidth,
451        NodeHeight = PreferredNodeHeight,
452        HorizontalSpacing = MinimumHorizontalDistance,
453        VerticalSpacing = MinimumVerticalDistance
454      };
455      reingoldTilfordToolStripMenuItem.Checked = true;
456      boxesToolStripMenuItem.Checked = false;
457      Repaint();
458    }
459
460    private void boxesToolStripMenuItem_Click(object sender, EventArgs e) {
461      layoutEngine = new BoxesLayoutEngine<ISymbolicExpressionTreeNode>(n => n.Subtrees, n => n.GetLength(), n => n.GetDepth()) {
462        NodeWidth = PreferredNodeWidth,
463        NodeHeight = PreferredNodeHeight,
464        HorizontalSpacing = MinimumHorizontalDistance,
465        VerticalSpacing = MinimumVerticalDistance
466      };
467      reingoldTilfordToolStripMenuItem.Checked = false;
468      boxesToolStripMenuItem.Checked = true;
469      Repaint();
470    }
471  }
472}
Note: See TracBrowser for help on using the repository browser.